You want to control the brightness of an LED by using PWM through an Arduino.
Use PyFirmata to send commands to an Arduino to generate a PWM signal on one of its outputs.
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-11. This is the same arrangement as in Recipe 14.4.
If you haven’t already done so, follow Recipe 14.3 to set up PyFirmata.
The following Python script (ardu_pwm.py) will prompt you to enter a value for the PWM power and then set the LED brightness accordingly. It is very similar to the program in Recipe 9.2.
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_pwm.py.
import pyfirmata
board = pyfirmata.Arduino(‘/dev/ttyACM0’) |
With the value entered as 100, the LED should be at full brightness. The brightness decreases as the number decreases.
$ sudo python ardu_pwm.py Enter Brightness (0 to 100):100 Enter Brightness (0 to 100):50 Enter Brightness (0 to 100):10 Enter Brightness (0 to 100):5 Enter Brightness (0 to 100): |
Discussion
The sketch for this is actually very straightforward. You define the output as PWM output using the command:
led_pin = board.get_pin(‘d:10:p’) |
The p is for PWM. But remember, this only works on Arduino pins marked with a ~ symbol.
We can also modify the slider control (Figure 14-12) in Recipe 9.8 so that it will operate through PiFirmata. You can download this sketch as ardu_gui_slider.
See Also
Although an Arduino can deliver 40 mA to an output, roughly 10 times the current available on a Raspberry Pi GPIO pin, it’s still not enough to directly drive a motor or high-power LED module. For these, you would need to use the circuit described in Recipe 9.4, modified to use an Arduino output pin rather than a Raspberry Pi GPIO pin.
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 |