Simple Newbie LED Question...

The official pyboard running MicroPython.
This is the reference design and main target board for MicroPython.
You can buy one at the store.
Target audience: Users with a pyboard.
Post Reply
Posts: 2
Joined: Wed Dec 26, 2018 11:31 pm

Simple Newbie LED Question...

Post by abisdad » Thu Dec 27, 2018 4:34 am

I'm almost embarrassed :oops: to ask this, but having trawled through the forum, looked at YouTube and the WWW in general, I have not come across a simple tutorial to just turn on an external LED on a pyboard using micropython! Yes, I can turn on the onboard LEDs, but I want to attach a LED to say X1, and control it. Can somebody tell me how to do this, or point me to a clear tutorial if there is one... :? Thanks, Rob.

Posts: 104
Joined: Wed Jun 25, 2014 10:07 am

Re: Simple Newbie LED Question...

Post by chrismas9 » Thu Dec 27, 2018 9:03 am

There is a good example in the signal class of the machine library: ... ignal.html

This refers to active high and active low LEDs. Active high LEDs have the Anode connected to the MCU pin directly or through a resistor. Active low LEDs have the cathode connected to the pin. There must be a resistor in series with the LED. It doesn't matter which side. To calculate the resistor you need to know the supply voltage (Vs) and the LED forward voltage drop (Vf) and the current you want.

R = (Vs - Vf) / I. Eg Vs = 3.3V, Vf = 2.1V, I = 5mA.

R = (3.3 - 2.1) / 0.005 = 240 ohm. Use the nearest 5% value, eg 220 ohm. To be really accurate you should also subtract the saturation voltage of the pin at the chosen current, typically 100 to 500 mV.

Blue, white and some other LEDs, mainly some green, have a forward drop of between about 3 to 4 volts. Many of these are marginal at 3.3V. The small blue LEDs on MCU boards are picked for between 2.8V and 3V but most bigger LEDs are around 4V. To use these with a 3.3V MCU connect the anode end to 5V and use active low mode. When the MCU pin is set high the LED will see 5 - 3.3 = 1.7V. this is not enough to turn it on. When the pin is set low it will see nearly 5V less the current limiting drop across the resistor and it will turn on brightly. Don't do this with low drop (1 to 2V) LEDs, eg normal red.

Posts: 2
Joined: Wed Dec 26, 2018 11:31 pm

Re: Simple Newbie LED Question...

Post by abisdad » Thu Dec 27, 2018 3:46 pm

:D Thank you chrismas9, that example was just what I needed!

I appreciated your LED current limiting calculations too, but that was not my issue.

The problem was I could not find how to reference the pyboard pins, as I had an error when I typed:
[code]from machine import Pin, Signal
led1_pin = Pin(0, Pin.OUT)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>[/code]

But have not worked it out. I've now sorted it:
[code]from machine import Pin, Signal
led1_pin = Pin('X1', Pin.OUT)

I've just also worked out that this does the same:
[code]from pyb import Pin
p_out = Pin('X1', Pin.OUT_PP)

So I guess I need to find out the difference between pyb & machine...
Just found my answer to that here: viewtopic.php?t=2056

Thank you for your help!


Post Reply