0

CircuitPy Code for RGB LED connected to pins
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

CircuitPy Code for RGB LED connected to pins

by molliekelleher on Wed May 15, 2019 11:11 pm

Hello,
Is it possible to use CircuitPython to program an RGB LED (the type you'd use on a breadboard) from the 0, 1, and 2 pins on a Trinket M0? I've looked for days, but can't find anything that will program an via the pinouts similar to way Arduino does. Thank you!

molliekelleher
 
Posts: 10
Joined: Fri Apr 07, 2017 12:44 pm

Re: CircuitPy Code for RGB LED connected to pins

by adafruit_support_carter on Thu May 16, 2019 12:22 pm

You can. The general idea would be to attach each of the color channels R, G, and B to the pins 0, 1, and 2. Then you would PWM the pins to control the brightness of each channel.

The trick is that you generally should not drive LEDs directly from the GPIO pins. This is done in a lot of demos for a single LED, but it's not an approach that scales. So even though it's shown here:
https://learn.adafruit.com/adafruit-ard ... s/overview
it's better to do something like this:
https://learn.adafruit.com/rgb-led-strips/usage

See also the approach taken here:
https://learn.adafruit.com/pulse-room/led-driver

adafruit_support_carter
 
Posts: 12976
Joined: Tue Nov 29, 2016 2:45 pm

Re: CircuitPy Code for RGB LED connected to pins

by kevinjwalters on Thu May 16, 2019 12:58 pm

Another forum frequenter is doing a similar thing (using a CPX board) so you may be able to swap ideas, see Adafruit Forums: Adafruit CircuitPython and MicroPython: ValueError: All timers for this pin are in use.

The Trinket M0 page states it only has two PWM outputs and not three. I'm wondering if that'll be a problem?

kevinjwalters
 
Posts: 432
Joined: Sun Oct 01, 2017 3:15 pm

Re: CircuitPy Code for RGB LED connected to pins

by adafruit_support_carter on Thu May 16, 2019 1:53 pm

Good point about the pins. PWM is generally available on most of the pins. But pin 1 is not one of them. Pins 0, 2, 3, and 4 are OK. But being able to actually create 3 PWMOut object on the Trinket M0 might be a little tight on timer resources.

Code: Select all | TOGGLE FULL SIZE
Adafruit CircuitPython 4.0.0-rc.3 on 2019-05-14; Adafruit Trinket M0 with samd21e18
>>> import board, pulseio
>>> r = pulseio.PWMOut(board.D0)
>>> g = pulseio.PWMOut(board.D2)
>>> b = pulseio.PWMOut(board.D3)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
RuntimeError: All timers in use
>>>


Code: Select all | TOGGLE FULL SIZE
Adafruit CircuitPython 4.0.0-rc.3 on 2019-05-14; Adafruit Trinket M0 with samd21e18
>>> import board, pulseio
>>> r = pulseio.PWMOut(board.D4)
>>> g = pulseio.PWMOut(board.D3)
>>> b = pulseio.PWMOut(board.D0)
>>>

adafruit_support_carter
 
Posts: 12976
Joined: Tue Nov 29, 2016 2:45 pm

Re: CircuitPy Code for RGB LED connected to pins

by molliekelleher on Thu May 16, 2019 6:01 pm

Thank you both for your replies. I'm new to electronics and my experience is limited to Arduino. So let me see if I understand you correctly:

Are you recommending using another board? Sadly, I've already invested in the Trinkets. Is there a way to make them work?

Are you recommending a different type of RBG LED (the strips) or just adding a power resistor? Is this different from tiny transistors used on an Arduino breadboard?

You included two sets of code for the pinouts. The first had a runtime error, but the second one didn't. Does that mean the second one works?

The goal for our project (7th & 8th Graders) is a build a magic wand with a programmable RBG LED at the tip. Will the Trinkets work for this task?

Thank you again!

molliekelleher
 
Posts: 10
Joined: Fri Apr 07, 2017 12:44 pm

Re: CircuitPy Code for RGB LED connected to pins

by adafruit_support_carter on Thu May 16, 2019 6:25 pm

I bet we can make this work somehow with the Trinket.

Have you bought the actual LED item yet?

adafruit_support_carter
 
Posts: 12976
Joined: Tue Nov 29, 2016 2:45 pm

Re: CircuitPy Code for RGB LED connected to pins

by kevinjwalters on Fri May 17, 2019 12:17 pm

I'm also curious on your plan for connecting things up, are you planning to do any soldering?

kevinjwalters
 
Posts: 432
Joined: Sun Oct 01, 2017 3:15 pm

Please be positive and constructive with your questions and comments.