Home Code Connect a Raspberry Pi to a KY-029 Dual Color LED

Connect a Raspberry Pi to a KY-029 Dual Color LED

by shedboy71

In this article, we connect an KY-029 Dual Color LED to a Raspberry Pi 4

The KY-029 Dual Color LED module can emit red and green light. You can adjust the intensity of each color using a Python pwm pin or simply switch the LED on/off using standard GPIO.

We will use Python for these examples

The sensor looks like this

Operating Voltage 2.3-2.6V for green, 1.9-2.2V for red
Working Current 20mA
Color Red + Green
Wavelength 571nm + 625nm
Luminous Intensity 20~40mcd,  60~80mcd

Series resistors are recommended, about 120 ohms should be just fine.

Series resistor (3.3 V) [Red] 120 Ω
Series resistor (3,3 V) [Green] 120 Ω

Parts Required

You can connect to the module using dupont style jumper wire. There is no reason this device will not work with any Raspberry Pi hardware that you have – in this example a Raspberry Pi 4 but you can use a Zero if you want a lower cost solution

Name   Link
Raspberry Pi 4
37 in one sensor kit
Connecting cables

 

Schematic/Connection

Pico SENSOR
Pin 18 LED Red
Pin 16 LED Green
GND GND

I didn’t have 120 ohms resistors in fritzing, so they are 150 ohms. It still worked. I also saw no side effects with no resistors fitted but I would recommend fitting a couple for peace of mind.

Code Examples

Basic example in  thonny, running on the Raspberry Pi

import RPi.GPIO as GPIO
import time
   
GPIO.setmode(GPIO.BCM)
   
# Here the output pin is declared, to which the LEDs are connected.
RED_LED = 24
GREEN_LED = 23
GPIO.setup(RED_LED, GPIO.OUT, initial= GPIO.LOW)
GPIO.setup(GREEN_LED, GPIO.OUT, initial= GPIO.LOW)
   
print ("LED test [press CTRL+C to end test]")
  
# main program loop
try:
    while True:
        print("LED RED 2 seconds on")
        GPIO.output(RED_LED,GPIO.HIGH) #LED is turned on
        GPIO.output(GREEN_LED,GPIO.LOW) #LED is switched off
        time.sleep(2) # wait for 2 seconds
        print("LED GREEN 2 seconds on") 
        GPIO.output(RED_LED,GPIO.LOW) #LED is switched off
        GPIO.output(GREEN_LED,GPIO.HIGH) #LED is switched on
        time.sleep(2) #wait for two seconds
   
# clean up after the program has been terminated
except KeyboardInterrupt:
    GPIO.cleanup()

pwm example in  thonny, running on the Raspberry Pi

import random, time 
import RPi.GPIO as GPIO
  
GPIO.setmode(GPIO.BCM) 
 
# Here we declare the output pin to which the LEDs are connected.
RED_LED = 24
GREEN_LED = 23
  
# Set pins to output mode
GPIO.setup(RED_LED, GPIO.OUT) 
GPIO.setup(GREEN_LED, GPIO.OUT)
  
Freq = 100 #Hz
  
# The respective colors are initialized.
RED = GPIO.PWM(RED_LED, Freq) 
GREEN = GPIO.PWM(GREEN_LED, Freq)
RED.start(0)  
GREEN.start(0)
  
def LED_Color(Red, Green, pause):
    RED.ChangeDutyCycle(Red)
    GREEN.ChangeDutyCycle(Green)
    time.sleep(pause)
 
    RED.ChangeDutyCycle(0)
    GREEN.ChangeDutyCycle(0)
   
print ("LED test [press CTRL+C to end test]")
  
# Main program loop:
try:
    while True:
        for x in range(0,2):
            for y in range(0,2):
                print (x,y)
                for i in range(0,101):
                    LED_Color((x*i),(y*i),.02)
  

except KeyboardInterrupt:
    GPIO.cleanup()

 

REPL Output

N/A

Links

https://github.com/getelectronics/PIBits/tree/master/Sensor%20Kit/KY-029_2_Color

 

 

You may also like

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More