Writing Digital Outputs on an Arduino from a Raspberry Pi
You want to control Arduino digital outputs from Python on a Raspberry Pi.
In Recipe 14.3, you flashed the built-in LED (labeled with an L) on the Arduino board. Here, you build on this to attach an external LED and write a short Python program to make it blink.
To make this recipe, you need:
• Arduino Uno (see “Modules” on page 381)
• Breadboard and jumper wires (see “Prototyping Equipment” on page 380)
• 270Ω resistor (see “Resistors and Capacitors” on page 380)
• LED (see “Opto-Electronics” on page 381)
Connect the breadboard, holding the components to the Arduino as shown in Figure 14-5.
If you haven’t already done so, follow Recipe 14.3 to set up PyFirmata.
The following Python script makes the LED blink at a rate of about 1 Hz. 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_flash.py.
import pyfirmata import time board = pyfirmata.Arduino(‘/dev/ttyACM0’) led_pin = board.get_pin(‘d:10:o’) while True: led_pin.write(1) time.sleep(0.5) led_pin.write(0) time.sleep(0.5) |
Discussion
This is very similar to connecting an LED to a Raspberry Pi (Recipe 9.1). However, note that since Arduino outputs can supply a lot more current than Raspberry Pi outputs, you can use a smaller value resistor and make the LED a bit brighter. Arduino outputs are also 5V rather than 3.3V, so the LED will draw about four times the current that it did in Recipe 9.1.
If you want the user interface to control the LED, like you had in Recipe 9.7 (and shown in Figure 14-6), it’s pretty straightforward to modify the code. You can find the modified program called ardu_gui_switch.py. Remember, this will not work from the SSH command line. You need to have access to the Raspberry Pi’s graphical environment so that you can see the user interface.
See Also
See Recipe 9.1 for controlling an LED directly from a Raspberry Pi.
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-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-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 |