Tiny 2040 RGB LED control - Tutorial

The RGB LED built into the Tiny 2040 may give you a surprise if you have only used common cathode RGB LEDs in the past. Here low numbers make it brighter and high numbers make it dimmer.
Here is a routine to control it like the one built into the Pico Display.

# Tiny 2040 RBG LED control
# Tony Goodhew 11th March 2021

import utime
from machine import Pin, PWM

#Setup RGB LED
# Construct PWM objects with RGB LED
rpwm = PWM(Pin(18)) # RED
gpwm = PWM(Pin(19)) # GREEN
bpwm = PWM(Pin(20)) # BLUE
# Set the PWM frequency.
rpwm.freq(1000)
gpwm.freq(1000)
bpwm.freq(1000)
# Turn off
rduty = 65535
gduty = 65535
bduty = 65535
rpwm.duty_u16(rduty)
gpwm.duty_u16(gduty)
bpwm.duty_u16(bduty)

def LED(r,g,b):
    rduty = int(65535 -(65535 * r/255))
    gduty = int(65535 -(65535 * g/255))
    bduty = int(65535 -(65535 * b/255))
#    print(rduty)
#    print(gduty)
#    print(bduty)
    rpwm.duty_u16(rduty)
    gpwm.duty_u16(gduty)
    bpwm.duty_u16(bduty)
    
LED(255,255,255)
utime.sleep(0.3)
LED(255,0,0)
utime.sleep(0.3)
# Blink
for i in range(4):
    LED(0,0,255)
    utime.sleep(0.3)
    LED(0,0,0)
    utime.sleep(0.3)
# Fade UP
for i in range(255):
    LED(i,i,0)
    utime.sleep(0.01)
# Fade DOWN
for ii in range(255,-1,-1):
    LED(ii,ii,0)
    utime.sleep(0.01)

I hope it helps if you are stuck.

4 Likes

Problem I have is getting Tiny2040 to be seen by mu-editor or even circuitpython. Could easily load uf2 files and can see files on devices. Under linux Tiny2040 is read only. Have read that I have to edit a file to add usb references. As yet no real luck.

Pico was easier as it is recognised by editors.

Could do with either pointing to a guide or a little help? Not found much online as device is quite new. Very keen to get that led working ;-)

My code above is in MicroPython. Give it a try.

Yes I may have figured it out…First I did not have micropython on Tiny2040 to start with. With Thonny editor set too a MicroPython generic it picked up Tiny2040 ;-)

Under Linux I had to add my user to allow ttyACM0 to work, so shell is running too.

Easy when you know how…

Thanks for your help

Glad you have it working. Just use the pins you have on the Tiny. MP is great as long as you do not need I2C and SPI drivers! (SSD1306 works very well if you need a display for your Tiny project.)

Mu doesn’t recognize your Tiny2040 while using CircuitPython because the board is recent, combined with a bug in Mu 1.1.0.beta, more details in CircuitPython mode: support for multiple serial ports per board; use serial port to identify by dhalbert · Pull Request #1371 · mu-editor/mu · GitHub

If you want to give it a try, this is what you need to do: Locate circuitpython.py as part of your Mu install (in a Mac it’s located under /Applications/Mu\ Editor.app/Contents/Resources/Python/lib/python3.8/site-packages/mu/modes/) and edit it (make a backup first). You need to add (0x16d0, None, None, "Tiny 2040") to the valid_boards list.

This is a hack, but will get you going until PR #1371 (or another fix) is merged in the Mu codebase.

Alternatively you can use Thonny, another Python IDE, that has no issues recognizing CircuitPython on the Tiny 2040.

I hope this helps.

Cool - got my Tiny today, and both it and your code worked straight out of the box (or rather the Pirate-brand Static Shielding Bag :-).
Next tasks will be to figure out:

  1. What exactly how does your program work?
  2. How can I tweak it?
  3. What exactly am I going to use this cutie for? (not to be confused with the QT Py RP2040, which I going to get next! :)

This example was extremely useful for getting bootstrapped with the RGB LED on the Tiny. I primarily use CircuitPython (not sure why, just used to it I guess), so I ported it over to use CircuitPython libs. Figured I’d post it here in case anyone else was in the same boat.

# Tiny 2040 RBG LED control
# Original Micro Python code by Tony Goodhew 11th March 2021
# Ported to Circuit Python

import time
import board
import pwmio

#Setup RGB LED
# Construct PWM objects with RGB LED
rpwm = pwmio.PWMOut(board.LED_R, frequency=1000) # RED
gpwm = pwmio.PWMOut(board.LED_G, frequency=1000) # GREEN
bpwm = pwmio.PWMOut(board.LED_B, frequency=1000) # BLUE

# Turn off
rduty = 65535
gduty = 65535
bduty = 65535
rpwm.duty_cycle = rduty
gpwm.duty_cycle = gduty
bpwm.duty_cycle = bduty

def LED(r,g,b):
    rduty = int(65535 -(65535 * r/255))
    gduty = int(65535 -(65535 * g/255))
    bduty = int(65535 -(65535 * b/255))
#    print(rduty)
#    print(gduty)
#    print(bduty)
    rpwm.duty_cycle = rduty
    gpwm.duty_cycle = gduty
    bpwm.duty_cycle = bduty

LED(255,255,255)
time.sleep(0.3)
LED(255,0,0)
time.sleep(0.3)

# Blink
for i in range(4):
    LED(0,0,255)
    time.sleep(0.3)
    LED(0,0,0)
    time.sleep(0.3)
# Fade UP
for i in range(255):
    LED(i,i,0)
    time.sleep(0.01)
# Fade DOWN
for ii in range(255,-1,-1):
    LED(ii,ii,0)
    time.sleep(0.01)

2 Likes

We’ve started off a new Github repo as a place to put CircuitPython code examples for Pico/RP2040 boards, if anyone fancies making any contributions :) GitHub - pimoroni/pico-circuitpython-examples: CircuitPython examples for Pimoroni RP2040 boards.

Great example. Thank you! Happy to see this working on the Tiny2040.

Perfect bit of code to inject life into a box fresh Tiny’, thank you.
I added a while true so it runs forever…

Good evening Tony… I have recently received a tiny 2040 and have downloaded micropython for it and loaded it on. I am now trying to use your script but nothing happens. Im uploading it with Thonny and I am a New User for Thonny this is my first time. I believed I have flashed the “Correct” uf2 file for micropython which i found here: Raspberry Pi Documentation - MicroPython the file is named: rp2-pico-20230511-unstable-v1.20.0-68-g3229791b6.uf2
I am use to Adafruit but when i seen Thonny i gravitated toward it but cannot seem to get my tiny2040 to do anything after running your script. I also am using the RaspberryPi Pico Interpreter. Ive tried other scripts with issues loading “import board” and it errors. To what “board” are they speaking of in packages? I really want to get accustomed to Thonnu but im beginning to see the information i find is in regards to loading Thonny and setting up Micropython Iterpreter then all info sort of falls off and nothing can be found. Can you assist at all? I would appreciate any information you can guide me with…
Thank You

-Brian-

You need the custom Pimoroni uf2 file for the Tiny
Release Version 1.20.1 · pimoroni/pimoroni-pico (github.com)

https://github.com/pimoroni/pimoroni-pico/releases/download/v1.20.1/pimoroni-tiny2040-v1.20.1-micropython.uf2

board is a CircuitPython module - examples that use it won’t work with MicroPython. There’s a version of this example that will work with MicroPython in the first post of this thread.

1 Like

I just copied the first posts script and am trying it and although NO errors are being printed nothing is happening. Again Im using Thonny with the RaspberryPi pico Interpreter.

I just re-flashed the tiny with this custom uf2 and Now Thonny wont even recognize the tiny while selecting RaspberryPi Pico Micropython. Its like it doesnt see it at all

I wonder if thats the right image? Is yours a 2MB or 8MB? I seem to remember there being two tiny uf2’s, 1 for 2MB and 1 for 8MB?
Both of my Tiny 8MB are showing USB device not recognised when I plug them in? I can’t even flash them. Will have to go hunt up a different USB cable and try again.

One of mine is seen if not in boot mode, and has an earlier version of the Pimoroni uf2 on it.

MicroPython v1.19 on 2022-06-17; Pimoroni Tiny 2040 with RP2040

Type "help()" for more information.

And Tony’s code from the first post works on it. I ran it from Thonny. I’m on my Windows 10 PC. I can’t get either of my Tiny’s to show up in boot mode on this PC, so no way to flash to the latest v1.20 uf2?

OK, solved my error and I can flash my Tiny now.
There is something wrong with the 120.1 uf2, same deal for me, not seen at all by Thonny.
1.19.18 works though, and Tony’s file works in Thonny in Micro Python.

EDIT: The new 120.2 worked for me on my 8mb Tiny

Alpha thanks for responding. I loaded the 1.19 on and its now recognized however it doesnt look like you shown above. Mine shows:
MicroPython 38e7b84 on 2023-03-27; Pimoroni Tiny 2040 with RP2040
Type “help()” for more information.

and when i run Tony’s first script above it still doesnt do anything. I checked what chip i have and its the 2MB from Waveshare.