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.
Thank you for your helpful and insightful posts on Raspberry Pi programming with Python and Pyfirmata. They have been a great resource as I have been working on creating a command-and-control GUI for a Pi/Adruino-based remote-control robot for my university senior design project. Following your post on how to command a servo with Python really saved me! I had been at a loss as how to control a servo since the ‘servo pulse’ pin parameter seems to not be explicitly documented. Now that I know I can assign a pin for ‘servo pulse’ I can now control IR-scanning servos and my robot’s motor controllers (Sabertooth, configured to accept RC pulses). Plus, these posts have also been a nice intro into GUI programming with Tkinter!
FYI, pyFirmata has moved: pyFirmata.
Thank you very much for sharing.
How do I control a stepper motor with python and pyFirmata?
I did it with the servos and it works very well. But with the stepper motor has not been possible.
I don’t think the modular version of Firmata – the one that supports steppers – is supported by pyFirmata
I really appreciate your code publication, helped me a lot