KY-009 3-color LED SMD module Raspberry Pi example

In this example we take a look at the KY-009 3-color full-color LED SMD module and connect this to a Raspberry Pi.

RGB LEDs consist of one red, one green, and one blue LED. By independently adjusting each of the three, RGB LEDs are capable of producing a wide color gamut. Unlike dedicated-color LEDs, however, these obviously do not produce pure wavelengths. Moreover, such modules as commercially available are often not optimized for smooth color mixing.

Parts List

Namelink
Raspberry Pi3Raspberry Pi 3 Model B With WiFi & Bluetooth
37 sensor kit37 IN 1 SENSOR KITS FOR ARDUINO HIGH-QUALITY FREE SHIPPING (Works with Official for Arduino Boards)
connecting wireFree shipping Dupont line 120pcs 20cm male to male + male to female and female to female jumper wire

 

Layout

The KY-009 RGB Full Color LED SMD Module consists of a 5050 SMD LED, you should use with limiting resistors to prevent burnout.

Operating Voltage5V max
Red 1.8V ~2.4V
Green 2.8V ~ 3.6V
Blue 2.8V ~ 3.6V
Forward Current20mA ~ 30mA
  • 180 Ohm resistor –> Pin ‘R’ of KY-009 module
  • 100 Ohm resistor –> Pin ‘G’ of KY-009 module
  • 100 Ohm resistor –> Pin ‘B’ of KY-009 module

 

 

 

 

 

Code

Save this example as rgdled.py

import RPi.GPIO as GPIO
import time
colors = [0xFF0000, 0x00FF00, 0x0000FF, 0xFFFF00, 0xFF00FF, 0x00FFFF]
pins = {'pin_R':11, 'pin_G':12, 'pin_B':13}  # pins is a dict
GPIO.setmode(GPIO.BOARD)       # Numbers GPIOs by physical location
for i in pins:
	GPIO.setup(pins[i], GPIO.OUT)   # Set pins' mode is output
	GPIO.output(pins[i], GPIO.HIGH) # Set pins to high(+3.3V) to off led
p_R = GPIO.PWM(pins['pin_R'], 2000)  # set Frequece to 2KHz
p_G = GPIO.PWM(pins['pin_G'], 2000)
p_B = GPIO.PWM(pins['pin_B'], 5000)
p_R.start(0)      # Initial duty Cycle = 0(leds off)
p_G.start(0)
p_B.start(0)
def map(x, in_min, in_max, out_min, out_max):
	return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min
def setColor(col):   # For example : col = 0x112233
	R_val = (col & 0xFF0000) >> 16
	G_val = (col & 0x00FF00) >> 8
	B_val = (col & 0x0000FF) >> 0
	R_val = map(R_val, 0, 255, 0, 100)
	G_val = map(G_val, 0, 255, 0, 100)
	B_val = map(B_val, 0, 255, 0, 100)
	p_R.ChangeDutyCycle(R_val)     # Change duty cycle
	p_G.ChangeDutyCycle(G_val)
	p_B.ChangeDutyCycle(B_val)
try:
	while True:
		for col in colors:
			setColor(col)
			time.sleep(0.5)
except KeyboardInterrupt:
	p_R.stop()
	p_G.stop()
	p_B.stop()
	for i in pins:
		GPIO.output(pins[i], GPIO.HIGH)    # Turn off all leds
	GPIO.cleanup()

Copy this to your raspberry pi and open up a terminal and type in

sudo python rgdled.py

Download

https://github.com/getelectronics/PIBits/blob/master/python/rgbled.py