Hey! This article is really old! So old, in fact, that I clearly thought that saying (ahem) â€œw00t w00tâ€ was a good idea. Information here may be misleading and possibly wrong. You probably want to be using a newer client library and you definitely want to use an Arduino IDE â‰¥ 1.6 and not the ancient one that comes with Raspbian.
pyFirmata‘s documentation is, to be charitable, sparse. After writing Raspberry Pi, Python & Arduino *and* a GUI (which should be making an appearance in The MagPi soon,
w00t w00t yeet!), I looked at pyFirmata again to see what it could do. That pretty much meant digging through the source.
Firmata can drive hobby servos, and if you’re not driving too many, you can run them straight from the Arduino with no additional power. I used a standard cheapo-but-decent Futaba S3003, which gives you about 180Â° of motion. The particular one I tried started to make little growly noises past 175Â°, so in the example below, that’s hardcoded as the limit.
#!/usr/bin/python # -*- coding: utf-8 -*- # move a servo from a Tk slider - scruss 2012-10-28 import pyfirmata from Tkinter import * # don't forget to change the serial port to suit board = pyfirmata.Arduino('/dev/tty.usbmodem26271') # start an iterator thread so # serial buffer doesn't overflow iter8 = pyfirmata.util.Iterator(board) iter8.start() # set up pin D9 as Servo Output pin9 = board.get_pin('d:9:s') def move_servo(a): pin9.write(a) # set up GUI root = Tk() # draw a nice big slider for servo position scale = Scale(root, command = move_servo, to = 175, orient = HORIZONTAL, length = 400, label = 'Angle') scale.pack(anchor = CENTER) # run Tk event loop root.mainloop()
The code above makes a slider (oh, okay, a Tkinter Scale widget) that moves the servo connected to Arduino pin D9 through its whole range. To set the servo position, you just need to write the angle value to the pin.
I haven’t tried this with the Raspberry Pi yet. It wouldn’t surprise me if it needed external power to drive the Arduino and the servo. This might be a good excuse to use my Omega-328U board â€” it’s Arduino code compatible, runs from an external power supply, and has Signal-Voltage-Ground (SVG) connectors that the servo cable would just plug straight into.