Introduction
This blog post explains how to use MicroPython and the Machine module to light up a ‘full colour LED’ on a RaspberryPi Pico2.
Full-Colour LEDs are LEDs that can display multiple colours from a single LED.
They are affordable and easy to use. They have a wide range of applications, such as making crafts and products glow like a gaming PC, glowing toys and illumination.
Even beginners can use them easily, so please refer to the article and try them out.
The Pico2 is used in this blog post, but the exact same wiring and programme can be used with the Pico and Pico W.
If you don’t have a Pico2 yet, please try it out with your Pico series.
~ この記事の内容 / Contents ~
Environment
The environment used in this blog post is as follows
environment | Version etc. | Comments |
Developing PC | Windows11 | Windows 10’s fine. |
Language | MicroPython Ver.1.24.0 | |
Development Environment | Thonny Ver.4.1.4 | |
board | RaspberryPi Pico2 | Can also use Pico, PicoW |
Full Color LED(SH5RGB)
Full-colour LEDs are LEDs that can produce a variety of colours by combining red, green and blue.
Three of the four legs growing from the LED correspond to the colours ‘red (R)’, ‘green (G)’ and ‘blue (B)’, and by changing the voltage of each leg it is possible to specify the colour.
The remaining leg is a common ground and is called ‘cathode common’ because the ground is common.
Conversely, the positive side is common and the ground side is divided into three parts, which is called ‘anode common’.
The Pico series does not have the ability to change the output voltage of a pin. Therefore the voltage is changed using PWM;
MicroPython also has a library called NeoPixel which handles libraries for full-colour LEDs, but this article ventures to explain how to use the basic Machine library.
Terminals (legs) and specifications
The order of terminals (legs) and specifications are as follows.
型番 | SH5RGB | |
Forward voltage(VF) | RED | 1.8 ~ 2.2V |
GREEN | 3.2 〜 3.4V | |
BLUE | 3.0 〜 3.4V | |
Forward current | 20mA |
Connecting Pico2 and full colour LED
Connect the Pico2 and the full-colour LED as follows.
Pin No | Name | Wiring connections | note |
17 | GP13 | RED | |
18 | GND | Common GND | |
19 | GP14 | GREEN | |
20 | GP15 | BLUE |
Pico Pin-Out
Cited from pico Official site
Parts in use
RaspberryPi Pico 2
If you’re getting a Pico now, we recommend the Pico2 – it’s 1.5 times faster than the Pico and almost the same price.
Ready-to-use kits are also available.
Ready to use kits with soldered headers, USB cable and pin layout chart are also available.
Full Color LED
These are the full-colour LEDs used in this blog post, and are available in just the right quantity of 20, although they are often sold in units of 100.
I actually bought mine from the link below. The packaging is also in good condition, including a sealed envelope, so I can recommend this as a place to buy.
jumper wire
This kit contains a set of hard jumper wires. It can make a cleaner circuit than soft wiring.
Jumper wire (for self-production)
The kit doesn’t have the right length or colour. You can make your own with the following wiring. The wires are almost as stiff as jumper wire, so you can make an ideal jumper wire just by cutting and bending.
Carbon resistance
You can use what you have. If you don’t have one, you can save postage and time by having a set.
Breadboard
This breadboard is made by San Hayato in Japan. It is a bit stiff to point at, but unlike the Chinese ones, all the pinhole numbers are printed on the board and are of high quality, so it is recommended.
Programme overview.
The Programme overview is the following.
- PWM settings.
- Switch the lights on in the following order:
- red → green → blue → yellow → white → off.
The results of running the programme
The following is the result of executing the code described below.
The brightness is so low that it cannot be seen directly. In the image, the red color looks dark, but in reality it is just bright enough and does not feel dark.
Entire code
The full text of the Code is as follows.
The details are explained in the section “Key Points of the Code” below.
import machine
import time
from machine import PWM
# Set each pin to PWM.
Red = PWM(machine.Pin(13, machine.Pin.OUT))
Green = PWM(machine.Pin(14, machine.Pin.OUT))
Blue = PWM(machine.Pin(15, machine.Pin.OUT))
# Sets the PWM period (Hz).
Red.freq(1000)
Green.freq(1000)
Blue.freq(1000)
# Maximum value of duty ratio.
max = 65535
for i in range(100):
# Red
Red.duty_u16(max)
Green.duty_u16(0)
Blue.duty_u16(0)
time.sleep(2)
# Green
Red.duty_u16(0)
Green.duty_u16(max)
Blue.duty_u16(0)
time.sleep(2)
# Blue
Red.duty_u16(0)
Green.duty_u16(0)
Blue.duty_u16(max)
time.sleep(2)
# Yellow
Red.duty_u16(max)
Green.duty_u16(max)
Blue.duty_u16(0)
time.sleep(2)
# White
Red.duty_u16(max)
Green.duty_u16(max)
Blue.duty_u16(max)
time.sleep(2)
# Switching off the light
Red.duty_u16(0)
Green.duty_u16(0)
Blue.duty_u16(0)
time.sleep(2)
Key Points of the Code
PWM
Configure the PWM settings on the Machine module, specifying the pin number (GPxx) you want to use for PWM.
Use the “freq” function to set the frequency. The default setting is 1000Hz (1kHz).
# Set each pin to PWM.
Red = PWM(machine.Pin(13, machine.Pin.OUT))
Green = PWM(machine.Pin(14, machine.Pin.OUT))
Blue = PWM(machine.Pin(15, machine.Pin.OUT))
# Sets the PWM period (Hz).
Red.freq(1000)
Green.freq(1000)
Blue.freq(1000)
Single-color LEDs appear to flash at lower frequencies, such as 10 Hz, but full-color LEDs do not appear to flash.
Specify a color to light the LEDs.
Specify the color by setting the RGB balance.
In the Pico series, the voltage can be varied by changing the duty ratio of the PWM.
(Duty ratio: how long the signal is turned on during a cycle)
The “duty_u16” function for setting the duty ratio requires the ratio to be specified in unsigned 16-bit (0~65535) values.
The three colors can be mixed, and like paint, specifying all three colors will result in white, and specifying red and yellow will result in “yellow”.
Specifying them all as 0 will result in “off” lighting.
# Red
Red.duty_u16(max)
Green.duty_u16(0)
Blue.duty_u16(0)
time.sleep(2)
# Green
Red.duty_u16(0)
Green.duty_u16(max)
Blue.duty_u16(0)
time.sleep(2)
# Blue
Red.duty_u16(0)
Green.duty_u16(0)
Blue.duty_u16(max)
time.sleep(2)
# Yellow
Red.duty_u16(max)
Green.duty_u16(max)
Blue.duty_u16(0)
time.sleep(2)
# White
Red.duty_u16(max)
Green.duty_u16(max)
Blue.duty_u16(max)
time.sleep(2)
# Switching off the light
Red.duty_u16(0)
Green.duty_u16(0)
Blue.duty_u16(0)
time.sleep(2)
Finally
I explained how to use MicroPython and Machine module to light “full color LEDs” on RaspberryPi Pico2.
It is cheap and easy to use. It is fun to make because the results of the program, such as colors, lighting and blinking, are reflected in an easy to understand way. It also expands the range of expression of your ideas and productions, so please give it a try.
I would be happy to help.
質問・要望 大歓迎です
「こんな解説記事作って」「こんなことがしたいけど、〇〇で困ってる」など、コメント欄で教えてください。 質問・要望に、中の人ができる限り対応します。
使えたよ・設定できたよの一言コメントも大歓迎。気軽に足跡を残してみてください。記事を紹介したい方はブログ、SNSにバシバシ貼ってもらってOKです。