You want to control the position of a servo motor by using an Arduino.
Use PyFirmata to send commands to an Arduino to generate the pulses necessary to control the position of a servo motor.
To make this recipe, you need:
Arduino Uno (see “Modules” on page 381)
• Breadboard and jumper wires (see “Prototyping Equipment” on page 380)
• 1kΩ resistor (see “Resistors and Capacitors” on page 380)
• LED (see “Opto-Electronics” on page 381)
Connect the breadboard as shown in Figure 14-13.
If you have not already done so, follow Recipe 14.3 to set up PyFirmata.
The following Python script (ardu_servo.py) will prompt you to enter a value for the servo’s angle and then set the arm of the servo motor accordingly.
Open an editor (nano or IDLE) and paste in the following code. As with all the program examples in this book, you can also download the program from the Code section of http://www.raspberrypicookbook.com, where it is called ardu_servo.py.
import pyfirmata board = pyfirmata.Arduino(‘/dev/ttyACM0’) servo_pin = board.get_pin(‘d:11:s’) while True: angle_s = raw_input(“Enter Angle (0 to 180):”) angle = int(angle_s) servo_pin.write(angle) |
With the value entered as 0, the servo should be at one end of its travel. Changing this to 180 sends it to the other end, and 90 puts it somewhere in the middle.
$ sudo python ardu_servo.py Enter Angle (0 to 180):0 Enter Angle (0 to 180):180 Enter Angle (0 to 180):90 |
Discussion
The sketch for this is actually very straightforward. You define the output as a servo output using the command:
led_pin = board.get_pin(‘d:11:s’) |
The s is for servo. This can be used on any of the Arduino digital pins.
If you’ve built Recipe 10.1, you will notice that, by comparison, there is no jitter of the servo when used with an Arduino in this way.
See Also
The straight Raspberry Pi–only solution to using a servo motor is described in Recipe 10.1.
An alternative to using an Arduino for a large number of servo motors is to use a servo module as described in Recipe 10.2.
Table A-8. Modules | |
Raspberry Pi camera module | Adafruit: 1367, MCM: 28-17733, CPC: SC13023 |
Arduino Uno | SparkFun: DEV-11021, Adafruit: 50, CPC: A000066 |
Level converter, four-way | SparkFun: BOB-11978, Adafruit: 757 |
Level converter eight-way | Adafruit: 395 |
LiPo boost converter/charger | SparkFun: PRT-11231 |
PowerSwitch tail | Adafruit: 268 |
16-channel servo controller | Adafruit: 815 |
Motor driver 1A dual | SparkFun: ROB-09457 |
RaspiRobot board | Sparkfun: KIT-11561, raspirobot.com |
PiFace digital interface board | MCM: 83-14472, CPC: SC12827 |
Humble Pi | MCM: 83-14637, CPC: SC12871 |
Pi Plate | Adafruit: 801 |
Gertboard | MCM: 83-14460, CPC: SC12828 |
Breakout board with paddle terminals | MCM: 83-14876, CPC: SC12885 |
PIR motion detector | Adafruit: 189 |
Venus GPS module | SparkFun: GPS-11058 |
Methane sensor | SparkFun: SEN-09404 |
Gas sensor breakout board | SparkFun: BOB-08891 |
ADXL335 triple-axis accelerometer | Adafruit: 163 |
4×7-segment LED with I2C backpack | Adafruit: 878 |
Bicolor LED square-pixel matrix with I2C backpack | Adafruit: 902 |
PiLite interface board | Ciseco, CPC: SC13018 |
aLaMode interface board | Makershed: MKWY1, Seeedstudio: ARD10251P |
Freetronics Arduino LCD shield | www.freetronics.com |
RTC module | Adafruit: 264 |
16 x 2 HD44780 compatible LCD module | SparkFun: LCD-00255, Adafruit: 181 |
Table A-3. Prototyping equipment | |
Description | Suppliers |
M-M jumper wires | SparkFun: PRT-08431, Adafruit: 759 |
M-F jumper wires | SparkFun: PRT-09140, Adafruit: 825 |
F-F jumper wires | SparkFun: PRT-08430, Adafruit: 794 |
Half-sized breadboard | SparkFun: PRT-09567 Adafruit: 64 |
Pi Cobbler | Adafruit: 1105 |
Table A-4. Resistors and capacitors | |
270Ω 0.25W resistor | Mouser: 293-270-RC |
470Ω 0.25W resistor | Mouser: 293-470-RC |
1kΩ 0.25W resistor | Mouser: 293-1k-RC |
3.3kΩ 0.25W resistor | Mouser: 293-3.3k-RC |
4.7kΩ 0.25W resistor | Mouser: 293-4.7k-RC |
10 kΩ trimpot | Adafruit: 356, SparkFun: COM-09806, Mouser: 652-3362F-1-103LF |
Photoresistor | Adafruit: 161, SparkFun: SEN-09088 |
220nF capacitor | MCM: 31-0610, Mouser: 80-C322C224M5U5HA |
Table A-7. Opto-electronics | |
5mm red LED | SparkFun: COM-09590, Adafruit: 299 |
RGB common cathode LED | SparkFun: COM-11120 |
TSOP38238 IR sensor | SparkFun: SEN-10266, Adafruit: 157 |