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


Topic attachments
I Attachment HistorySorted ascending Action Size Date Who Comment
PNGpng 2018-01-30-182614_520x384_scrot.png r1 manage 143.8 K 2018-01-30 - 17:27 UliRaich  
Texttxt fullColors.py.txt r1 manage 0.7 K 2018-01-30 - 19:24 UliRaich  
PNGpng ky-009.png r1 manage 145.5 K 2018-01-30 - 17:15 UliRaich  
PNGpng lilyPadTriColorLED.png r1 manage 152.5 K 2018-01-30 - 17:15 UliRaich  
Texttxt rgbColorIntensities.py.txt r1 manage 1.0 K 2018-01-30 - 19:24 UliRaich  
PNGpng rgbLED-1.png r1 manage 128.6 K 2018-01-30 - 17:15 UliRaich  
Texttxt rgbLED.py.txt r1 manage 0.6 K 2018-01-30 - 19:24 UliRaich  
Unknown file formatgz rgbLED.tar.gz r1 manage 0.7 K 2018-01-30 - 19:26 UliRaich  
Edit | Attach | Watch | Print version | History: r5 < r4 < r3 < r2 < r1 | Backlinks | Raw View | Raw edit | More topic actions...
Topic revision: r3 - 2018-02-08 - UliRaich
 
  • Edit
  • Attach
This site is powered by the TWiki collaboration platform Powered by PerlCopyright © 2008-2024 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback