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.

1 Like

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! :)