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.
import pyfirmata # Definition of the analog pin PINS = (0, 1, 2, 3) # Creates a new board board = pyfirmata.Arduino(/dev/ttyACM0) print "Setting up the connection to the board ..." it = pyfirmata.util.Iterator(board) it.start() # Start reporting for defined pins for pin in PINS: board.analog[pin].enable_reporting() # Loop for reading the input. Duration approx. 10 s for i in range(1, 11): print "\nValues after %i second(s)" % i for pin in PINS: print "Pin %i : %s" % (pin, board.analog[pin].read()) board.pass_time(1) board.exit()
Checkout the complete script at this location.
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.
import pyfirmata # Time (approx. seconds) to get to the maximum/minimum DURATION = 5 # Numbers of steps to get to the maximum/minimum STEPS = 10 # Creates a new board and define the pin board = pyfirmata.Arduino(/dev/ttyACM0) digital_0 = board.get_pin('d:11:p') # Waiting time between the wait_time = DURATION/float(STEPS) # Up for i in range(1, STEPS + 1): value = i/float(STEPS) digital_0.write(value) board.pass_time(wait_time) # Down increment = 1/float(STEPS) while STEPS > 0: value = increment * STEPS digital_0.write(value) board.pass_time(wait_time) STEPS = STEPS - 1 board.exit()
Download the commented and extended script from here.
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.
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.
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.
import pyfirmata PIN = 12 DELAY = 2 PORT = '/dev/ttyACM0' board = pyfirmata.Arduino(PORT) while True: board.digital[PIN].write(1) board.pass_time(DELAY) board.digital[PIN].write(0) board.pass_time(DELAY)
Download it from here.

