Analog input example for pyFirmata

Using pyFirmata to read from analog inputs is an easy task. pyFirmate provides a helper function (aka iterator) to simplify the reading. Below is a simple script.

  1. import pyfirmata
  2.  
  3. # Definition of the analog pin
  4. PINS = (0, 1, 2, 3)
  5.  
  6. # Creates a new board
  7. board = pyfirmata.Arduino(/dev/ttyACM0)
  8. print "Setting up the connection to the board ..."
  9. it = pyfirmata.util.Iterator(board)
  10. it.start()
  11.  
  12. # Start reporting for defined pins
  13. for pin in PINS:
  14. board.analog[pin].enable_reporting()
  15.  
  16. # Loop for reading the input. Duration approx. 10 s
  17. for i in range(1, 11):
  18. print "\nValues after %i second(s)" % i
  19. for pin in PINS:
  20. print "Pin %i : %s" % (pin, board.analog[pin].read())
  21. board.pass_time(1)
  22.  
  23. board.exit()

Checkout the complete script at this location.

Permalink 02/20/12 10:51:00 pm, by fab Email , 99 words, Categories: General, Arduino , Leave a comment »

Dimming a LED with pyFirmata

There are a lot of tutorials available for dimming a LED with sketches. My approach uses pyFirmata to do it.

  1. import pyfirmata
  2.  
  3. # Time (approx. seconds) to get to the maximum/minimum
  4. DURATION = 5
  5. # Numbers of steps to get to the maximum/minimum
  6. STEPS = 10
  7.  
  8. # Creates a new board and define the pin
  9. board = pyfirmata.Arduino(/dev/ttyACM0)
  10. digital_0 = board.get_pin('d:11:p')
  11.  
  12. # Waiting time between the
  13. wait_time = DURATION/float(STEPS)
  14.  
  15. # Up
  16. for i in range(1, STEPS + 1):
  17. value = i/float(STEPS)
  18. digital_0.write(value)
  19. board.pass_time(wait_time)
  20.  
  21. # Down
  22. increment = 1/float(STEPS)
  23. while STEPS > 0:
  24. value = increment * STEPS
  25. digital_0.write(value)
  26. board.pass_time(wait_time)
  27. STEPS = STEPS - 1
  28.  
  29. board.exit()

Download the commented and extended script from here.

Permalink 02/20/12 07:35:00 pm, by fab Email , 89 words, Categories: General, Arduino , Leave a comment »

pyFirmata GUI

Using Glade and GTK to make a simple GUI is not that hard. If you want to get rid of dealing with the serial connection, using pyFirmata is a good choice.

As shown in a previous blog post working with pyFirmata is similar to writing sketches in the Arduino software. It's Python but the concepts are the same.

There is not much to say about the code because it's contains almost the same elements as the previous example.

pyFirmata GUI

Download the two files from here. I hope that upstream will proceed the pull request after that this will be available in the official pyFirmata package.

Permalink 02/20/12 06:37:00 pm, by fab Email , 104 words, Categories: General, Arduino , Leave a comment »

Sonntag und schlechtes Wetter = selbergemachte Linzertorte

Für den ersten Versuch ist der Duft himmlisch, aber leider kann das Auge nicht unbedingt mitessen...

Linzertorte

Permalink 02/19/12 06:20:00 pm, by fab Email , 16 words, Categories: General , Leave a comment »

Firmata firmware and the Arduino

If your Arduino is running with the latest Firmata firmware (aka sketch), you will notice that there are plenty of issues with binding implementations. This is because the Firmata protocol implements some new features of the past years and some bindings are not updated to work with Firmata > 2.0.

I started with python-firmata. Not really useful, it's outdated. Then I found pyfirmata which is working with newer versions of Firmata.

  1. import pyfirmata
  2.  
  3. PIN = 12
  4. DELAY = 2
  5. PORT = '/dev/ttyACM0'
  6.  
  7. board = pyfirmata.Arduino(PORT)
  8.  
  9. while True:
  10. board.digital[PIN].write(1)
  11. board.pass_time(DELAY)
  12. board.digital[PIN].write(0)
  13. board.pass_time(DELAY)

Download it from here.

Permalink 02/19/12 01:15:00 pm, by fab Email , 87 words, Categories: General, Arduino , Leave a comment »

1 2 3 4 5 6 7 8 9 10 11 ... 209 >>