LED

From Sketching with Hardware at LMU Wiki
Jump to navigation Jump to search

Description[edit]

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 sketching we can just use the following values (see #Background for more details):

  • 100 Ohm for a controller with 3.3V (our ESP32/ESP8266)
  • 150 Ohm, 200 Ohm or 330 Ohm for a controller with 5V

How to connect it electrically[edit]

This is how to connect an LED to a power supply. It does not matter where the resistor goes. But if the direction of the LED is wrong, it will not light up.

Led00.PNG


LED will be on if Pin26 is 3.3V (or ON or High)

Led02.PNG


LED will be on if Pin26 is 0V (or OFF or Low)

Led03.PNG

How to control it in MicroPython[edit]

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[edit]

 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)

The setup to try the program:

Exampleled.PNG Exampleled1.JPG

Related Tutorial Videos[edit]

Background[edit]

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.

Led04.JPG