RGB LEDs
Introduction
When construction the
virtual world demo we will use two different types of sensors
- an RGB color sensor giving us the color of a paper sheet
- an ultrasonic distance sensor determining the distance between the paper sheet and the sensor.
Before using this information however we will have to understand the sensors and their readout and as a preliminary experiment we will read the color of the paper sheet and light an rgb color LED with the same color. Switching to a paper sheet with a different color will also light the LED in the same different color.
There are quite a few different types of breakout boards implementing RGB LEDs on the market which have to connected to the controller in different ways. Most of them just consist of 3 LEDs of the base colors implemented in a single package. Some of the breakout boards however have the necessary current limiting resistors already on board while for others they must be provided externally.
There is also a type of "intelligent" LED where the RGB values are provided in a serial data stream.
KY-009 SMD LED needing external resistors |
Lily Pad Tri-Color LED |
Standard 3 color LED with resistors on board |
WS2812 "intelligent" LED |
For you information, here is the
WS2812 data sheet
Programming the LEDs
It is an excellent introductory exercise to try programming the LEDs and we will go about it in steps. First the device must be connected to the Raspberry Pi controller which is done through the cobbler and the bread board. Depending on the breakout board used we will need additional external 330 Ohm resistors on each of the color components (KY-009). Then we connect as follows:
- - -> Gnd
- red -> gpio 18
- green -> gpio 17
- blue -> gpio 22
This cabling is of course only true for the first 3 devices (not the WS2812). To check the device we can temporarily connect these pins to Vcc (make sure the current limiting resistor is there, either on the breakout board or externally added by you) in which case we should see the corresponding color come up on the LED. Once connected to the gpio pins we can start programming.
Making a LED light up is one of the most simple programming exercises possible and therefore ideally suited to get us going with Python programming.
The following programs work for common cathode LEDs (KY-009 and Standard 3 color LED) only for which the GND pin is common. For Common Anode LEDs, which have a common Vcc like the the Lily Pad one you must pull the individual red, green, blue pins low to make the LED light up.
Three programs will be developed with increasing difficulty:
- rgbLED.py uses 3 bit binary numbers, where each bit corresponds to one of the three LEDs. A total number of 7 (+ all LEDs dark) can be displayed this way. First the pigpio library is included and then the connections to the gpio pins defined and each of these pins set to OUTPUT. Then we switch the LEDs on, where the binary number 001 means that only green is on, 010 only red is on 100 only blue is on. A combination of bits like 011 or 110 mean a combination of green and red or red and blue respectivly.
- fullColors.py is a program that changes the light intensity from total dark to full color intensity for each of the 3 LEDs using Pulse Width Modulation (PWM).
- rgbColorIntensities.py finally combines both methods and allows the user to define an arbitrary color by entering rgb values through the command line.
You may download the file
rgbLED.tar.gz containing all three programs.
--
Uli Raich - 2018-01-30
Comments