LED
Contents
Description
LEDs are most common as simple visual output. LED stands for light-emitting diode (https://en.wikipedia.org/wiki/Light-emitting_diode).
LEDs have 2 connectors: anode (+, long leg) and cathode (-, short leg)
The direction in which the LED is put into the circuit is important. For the forward direction and this is what we want, as this is when the LED is lighting up, the cathode (-, short leg) has to go towards 0V/GND and the anode (+, long leg) towards 3.3V/+/VSS.
A LED typically requires a resistor that is in series with the LED to restrict the current that is flowing through the LED.
For an LED we are interested in the following parameters:
- the forward voltage (typically about 2V)
- the forward current (typically about 20mA = 0,02A)
We use the equations R=U/I and U=U1+U2 to calculate the resistor required.
For sketching a shortcut we can also just use the following values:
- 100 Ohm for a controller with 3.3V (our ESP32/ESP8266)
- 330 Ohm for a controller with 5V
How to connect it electrically
LED will be on if Pin is 3.3V (or ON or High)
LED will be on if Pin is 0V (or OFF or Low)
How to control it in MicroPython
1 from machine import Pin
2 # assume our LED is connected to Pin 26, we use it as output
3 myLED = Pin(26, Pin.OUT)
4 # this switches the LED on
5 myLED.on()
6 # this switches the LED off
7 myLED.off()
A small Program in MicroPython
1 from machine import Pin
2 from time import sleep
3
4 # assume our LED is connected to Pin 26, we use it as output
5 myLED = Pin(26, Pin.OUT)
6
7 while True:
8 # this switches the LED on for 1 second
9 myLED.on()
10 sleep(1)
11 # this switches the LED off for 500 ms
12 myLED.off()
13 sleep(0.5)
Related Tutorial Videos