« Bcfg2 1.2.1 for FedoraDimming a LED with pyFirmata »

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 ,

No feedback yet