Home CodeMicropython A Raspberry Pi Pico and KY-029 Dual Color LED

A Raspberry Pi Pico and KY-029 Dual Color LED

by rp2040guy71

In this article, we connect an KY-029 Dual Color LED to a Raspberry Pi Pico, any Rp2040 type board will be suitable. We actually tested this with a Cytron Maker Pi Pico

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

We will use Micropython for these examples but of course you can use the Arduino IDE as well if you have Raspberry Pi Pico support enabled

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.

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

 

Schematic/Connection

Pico SENSOR
GPIO0 LED Red
GPIO1 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

from machine import Pin, PWM
from time import sleep

# Initialization of GPIO0 and GPIO1 as output
Green = Pin(1, Pin.OUT)
Red = Pin(0, Pin.OUT)

while True:
    Green.value(1)
    Red.value(0)
    sleep(1)
    Green.value(0)
    Red.value(1)
    sleep(1)
    Green.value(0)
    Red.value(0)

pwm example

import machine
import math

# Initialization of GPIO0 and GPIO1 as PWM pins
Red = machine.PWM(machine.Pin(0))
Red.freq(1000)
Green = machine.PWM(machine.Pin(1))
Green.freq(1000)

RG = [0,0]

def sinColour(number):
    a = (math.sin(math.radians(number))+1)*32768
    c = (math.sin(math.radians(number+90))+1)*32768
    RG = (int(a),int(c))
    return RG

# Infinite loop
a = 0
while True:
    RG = sinColour(a)
    a = a + 0.01
    if a == 360:
        a = 0
    Red.duty_u16(RG[0])
    Green.duty_u16(RG[1])

 

REPL Output

N/A

Links

https://github.com/getelectronics/rp2040learning/tree/main/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