Skip to content

MicroPython: LED

Leo Vidarte edited this page Mar 15, 2017 · 9 revisions

A light-emitting diode (LED) is a semiconductor device that emits visible light when an electric current passes through it. The light is not particularly bright, but in most LEDs it is monochromatic, occurring at a single wavelength. The output from an LED can range from red (at a wavelength of approximately 700 nanometers) to blue-violet (about 400 nanometers). Some LEDs emit infrared (IR) energy (830 nanometers or longer); such a device is known as an infrared-emitting diode (IRED).

Schematic

Code

from machine import Pin
from time import sleep

# GPIO16 (D0) is the internal LED for NodeMCU
led = Pin(16, Pin.OUT)

# The internal LED turn on when the pin is LOW
while True:
    led.high()
    sleep(1)
    led.low()
    sleep(1)

Here is the reason why the built in led turn on when Pin 16 is Low :)

Toggle a LED

def led_toggle():
    led.value(not led.value())

Now we can use this function to blink the LED

while True:
    led_toggle()
    sleep(1)