# MicroPython Basics: Blink a LED

## Overview

Danger: 

Warning: 

![](https://cdn-learn.adafruit.com/assets/assets/000/034/889/medium800/microcontrollers_IMG_5128.jpg?1471572831)

https://youtu.be/Vkkhy51cdjQ

When you learn programming you usually start with a simple **Hello World** program that prints a message to the screen. &nbsp;Hello World helps you get started by learning the basics of typing in and running code that does something interesting like print to the screen. &nbsp;For hardware projects instead of printing to a screen the simplest Hello World is blinking a LED on and off.&nbsp; If you can blink a LED then you can control the pins on a board, and once you have control of the board there's almost no limit to what you can do!

This guide walks through how to use MicroPython to blink a LED. &nbsp;You'll learn how to connect a LED to a board and what MicroPython code to run to make it blink. &nbsp;As you walk through this guide you'll learn the basics of running MicroPython code and controlling hardware.

Before you get started you'll want to review these guides that explain MicroPython and how to load it on a board:

- [MicroPython Basics: What is MicroPython?](../../../../micropython-basics-what-is-micropython/overview)
- [MicroPython Basics: How to Load MicroPython on a Board](../../../../micropython-basics-how-to-load-micropython-on-a-board/)

# MicroPython Basics: Blink a LED

## Hardware

Danger: 

# Parts

You'll need the following parts to follow this guide:

- **A board running MicroPython.** &nbsp;Check this [how to load MicroPython on a board guide](../../../../micropython-basics-how-to-load-micropython-on-a-board) for more information on getting MicroPython on a board. &nbsp;If you're new to MicroPython and don't have a board yet, consider the [Feather HUZZAH ESP8266](https://www.adafruit.com/product/2821) as a great beginner option. &nbsp;If the board you're using has a user-controllable LED on it, like the Feather HUZZAH ESP8266, you actually don't need any more parts and can skip everything else below.
- **LED.** &nbsp;Almost any [basic LED](https://www.adafruit.com/categories/90) will do, but be sure it's **not** something fancy like a RGB multicolor or NeoPixel style digital addressable LED. &nbsp;You want a basic LED that has two leads, one short one and one long one--these [5mm diffused red LEDs](https://www.adafruit.com/products/299) are perfect.
- **~560-3k ohm resistor.** &nbsp;You need to use a resistor to limit the current that can flow through the LED (otherwise you might send too much current through the LED and damage it or the MicroPython board!). &nbsp;The exact value of the resistor usually doesn't matter here, anything in the 560-3,000 ohm range should work great (higher resistance will be lower current and usually dimmer LED). &nbsp;If unsure grab a [2200 ohm resistor pack](https://www.adafruit.com/products/2782).
- **[Breadboard](https://www.adafruit.com/products/64) & [jumper wires](https://www.adafruit.com/products/153).** &nbsp;You'll use these parts to hold the LED and&nbsp;MicroPython board and connect wires between them. &nbsp;If you've never used a breadboard before be sure to [check out this guide on what they are and how to use them](../../../../lesson-0-getting-started/breadboard).

If you're totally new to hardware and starting from scratch you might want to pick up the **[Adafruit Parts Pal pack](https://www.adafruit.com/products/2975)**. &nbsp;This pack includes a small breadboard, wires, LEDs, resistors, and more. &nbsp;The parts pal&nbsp;plus a board running MicroPython are&nbsp;all you need to follow this guide!

# Wiring

If your MicroPython board doesn't have a built-in LED you can control, or if you're just curious to experiment you can wire a LED to your MicroPython board as follows.

First identify a 'digital GPIO' or general purpose input/output pin on the board. Usually these are assigned numbers, like 0, 1, 2, 3, etc. &nbsp;Check the [guide or documentation for your board](../../../../micropython-basics-what-is-micropython#faq-10) to see more details on the pinout and which pins are digital GPIO.

Note the BBC micro:bit doesn't easily plug in&nbsp;to a breadboard like other boards. &nbsp;However you can use [alligator clip wires](https://www.adafruit.com/products/1592) to connect from the board's pins to wires that plug into a breadboard.

Next wire your LED to the digital GPIO pin as follows (this is an example of the Feather HUZZAH ESP8266 with a LED wired to pin 15):

![](https://cdn-learn.adafruit.com/assets/assets/000/034/887/medium800/microcontrollers_esp8266_blink_led_bb.png?1471571428)

- **Digital GPIO 15** is connected to the **anode, or longer leg, of the LED**. &nbsp;It's very important to use the correct leg of the LED otherwise it won't light up as expected!
- **The cathode, or shorter leg, of the LED** is connected to **one side of the resistor&nbsp;** (unlike the LED it doesn't matter which way you orient the resistor).
- **The other side of the resistor** is connected to the board's **ground or GND pin**.

![](https://cdn-learn.adafruit.com/assets/assets/000/034/888/medium800/microcontrollers_IMG_5124.jpg?1471572781)

# MicroPython Basics: Blink a LED

## Blink LED

Danger: 

To blink the LED we'll connect to the MicroPython board's REPL (read-eval-print loop, like a Python 'command prompt') and run commands that control the digital GPIO connected to the LED. &nbsp;This way we can interactively turn the LED on and off right from the board without uploading any code, sketches, etc.

If your board has built-in LEDs that you can control you'll want to note the pin names or numbers for the LEDs:

- **pyboard**

  - The pyboard has 4 built-in&nbsp;LEDs that you can control. &nbsp;See the [pinout diagram here](https://docs.micropython.org/en/latest/pyboard/pyboard/quickref.html) and its note that pins P2-P5 control the LEDs. &nbsp;One important clarification though, when you reference those pins in code you want to use the 'CPU name' in the white box on the diagram and not the pin name in the yellow box. &nbsp;So pin P2 is actually called B4, P3 is A15, etc. &nbsp;All of the LED pin CPU names are:

    - B4 = blue LED
    - A15 = yellow LED
    - A14 = green LED
    - A13 = red LED

  - Note too on the pyboard pin names are strings like 'B4' for the blue LED. &nbsp;Instead of trying to send a number like 4 to pin functions you'll instead send a string 'B4'.

- **Feather HUZZAH&nbsp;ESP8266** and&nbsp; **HUZZAH&nbsp;ESP8266 Breakout**

  - Both the HUZZAH&nbsp;ESP8266&nbsp;boards have a red LED you can control connected to pin number 0.

If your board doesn't have built-in LEDs make sure you've wired up a LED to one of the digital GPIO pins as shown in the previous page.

# Connect to Serial REPL

To get started you'll need to connect to the board's serial REPL so you can send commands to control the LED. &nbsp;Check out the [Serial REPL page from this guide](../../../../micropython-basics-how-to-load-micropython-on-a-board/serial-terminal) for information on tools to connect to the board's REPL. &nbsp;Make sure you can connect to the REPL and see the **\>\>\>** prompt before continuing!

# Control the LED

Now for some excitement, let's turn the LED on and off from MicroPython! &nbsp;The first step is to run a line of code that imports a special MicroPython module called 'machine'. &nbsp;Run the following command at the serial REPL:

```auto
import machine
```

![](https://cdn-learn.adafruit.com/assets/assets/000/034/879/medium800/microcontrollers_Screen_Shot_2016-08-18_at_5.08.37_PM.png?1471565406)

After pressing enter you should see no output and the **\>\>\>** prompt return. &nbsp;This just means the command succeeded and the machine module is now imported. &nbsp;Once a module is imported you can access classes, objects, etc. from inside it (just like if you were writing Python code to run on your computer).

If you're curious you can even run a help command to see what objects and things are available in the machine module that was imported:

```auto
help(machine)
```

![](https://cdn-learn.adafruit.com/assets/assets/000/034/880/medium800/microcontrollers_Screen_Shot_2016-08-18_at_5.11.22_PM.png?1471565516)

We'll use the Pin class to create an object that represents a digital GPIO pin on the board. &nbsp;Once you have an instance of this Pin object you can call functions on it to set it to high and low levels that turn on and off the LED.

First create the Pin instance with a command like the following (note change the pin number, 15 in this example,&nbsp;to the pin number or name for your LED):

```auto
led = machine.Pin(15, machine.Pin.OUT)
```

![](https://cdn-learn.adafruit.com/assets/assets/000/034/881/medium800/microcontrollers_Screen_Shot_2016-08-18_at_5.27.54_PM.png?1471566522)

Again you shouldn't see any output after running the command, and the REPL prompt should appear again.

Notice how when the Pin object is created it takes two parameters, first the pin number or name and then the function of the pin. &nbsp;A pin can be set as an output which can set its level or voltage to a high or low value (like 'on' or 'off'), or it can be set as an input which can read if it's at a high or low value. &nbsp;To blink the LED we just need to use the pin as an output since we're telling the LED to turn on or off using the level/voltage from the pin.

If you have multiple LEDs connected to the board you can create different Pin objects to control them individually! &nbsp;Use the same syntax as above but create a new object like **led2** and set the pin number or name appropriately.

Now for some fun, let's turn the LED on by setting the Pin to a high level with this command:

```auto
led.high()
```

![](https://cdn-learn.adafruit.com/assets/assets/000/034/882/medium800/microcontrollers_Screen_Shot_2016-08-18_at_5.34.25_PM.png?1471566886)

You should see the LED instantly turn on! &nbsp;There won't be any output from this command either.

Now turn the LED off by calling the low() function on the Pin object:

```auto
led.low()
```

![](https://cdn-learn.adafruit.com/assets/assets/000/034/883/medium800/microcontrollers_Screen_Shot_2016-08-18_at_5.35.39_PM.png?1471566959)

You should see the LED turn off after running the command. &nbsp;Woo hoo you're blinking a LED with MicroPython!

If for some reason your LED doesn't turn on or off with the above commands go back and carefully check all the wiring between the board and LED. &nbsp;Make sure the LED is oriented the right way with the long leg (anode) connected to the board pin and the short leg (cathode) connected through a resistor to the board ground. &nbsp;Also double and triple check you're using the right pin number or name--boards usually have a lot of pins and it's easy to use the wrong one on accident.

Try toggling the LED on and off by running the high and low commands again. &nbsp;In the serial REPL you can actually press the **up arrow** or **down arrow** keys to quickly go back to previously run commands.

# Add a Loop

Blinking the LED yourself with commands you run manually will get a little tiring after a while. &nbsp;However since MicroPython is really just like desktop Python you can use a Python loop to blink the LED a number of times or even forever.

First you'll want to import the time module so you can use its sleep function to delay for a short time (otherwise the loop would run so fast you couldn't tell the LED is blinking!). &nbsp;Just like importing the machine module you can import time with:

```auto
import time
```

Now let's use a for loop to blink the LED 10 times on and off. &nbsp;Enter the following code in the REPL, and notice how the REPL changes and starts to indent code inside the loop for you:

```auto
for i in range(10):
    led.high()
    time.sleep(0.5)
    led.low()
    time.sleep(0.5)
```

![](https://cdn-learn.adafruit.com/assets/assets/000/034/884/medium800/microcontrollers_Screen_Shot_2016-08-18_at_5.46.02_PM.png?1471567591)

After entering the last line, time.sleep(0.5), and pressing enter you'll need to end the indented block by pressing delete and then enter again. &nbsp;At this point the loop will start running and the LED should blink on and off!

Notice as the LED blinks and the loop runs you can't see the **\>\>\>** REPL prompt or enter any code. &nbsp;This is because MicroPython can only do one thing at a time and running code in a loop like that requires all of the board's attention. &nbsp;Only once the code stops running can MicroPython give you the prompt to enter more code.

Try entering the code again and changing the value of the **time.sleep** function calls. &nbsp;This function takes in a number that controls how long in seconds the board will delay. &nbsp;You can use decimal values like 0.5 to sleep for half a second. &nbsp;If you increase the value the LED will stay on and off for longer periods and slow down the blinking. &nbsp;Likewise if you decrease it then the LED will blink faster.

Also try changing the number in the **range** function call that starts the loop. &nbsp;The value 10 means the loop should repeat 10 times, however you can increase or decrease this to control how many times the LED blinks. &nbsp;Every time the loop runs it will run each instruction inside of it in order, so you can see how it starts by turning the LED on, then delaying a bit, turning the LED off, and delaying again. &nbsp;When all the code inside the loop has been run it will go back to the top and run the code again, unless the loop has been run for as long as the range function specified.

For one final example let's use an infinite loop that will blink the LED forever. &nbsp;Enter the following code:

```auto
while True:
    led.high()
    time.sleep(0.5)
    led.low()
    time.sleep(0.5)
```

![](https://cdn-learn.adafruit.com/assets/assets/000/034/890/medium800/microcontrollers_Screen_Shot_2016-08-18_at_5.53.53_PM.png?1471573450)

Again remember you have to press delete after the last line to end the indented block. &nbsp;Once you run this code the LED should start blinking on and off forever!

You might be worried to see the **\>\>\>** REPL prompt never appears again and you can't enter code. &nbsp;Remember this is because MicroPython can only do one thing at a time and the infinite loop means the board is busy blinking the LED.

Luckily you can tell the board to stop whatever it's doing and return back to the REPL prompt. &nbsp;Press the **Ctrl-c** key on your keyboard and you should see the code stop with a KeyboardInterrupt error like this:

![](https://cdn-learn.adafruit.com/assets/assets/000/034/886/medium800/microcontrollers_Screen_Shot_2016-08-18_at_5.56.04_PM.png?1471568185)

This is a handy tip to remember, if you ever need to stop a program that's running on the board just connect to the REPL and press **Ctrl-c**. &nbsp;Sometimes you might need to press **Ctrl-c** a few times to stop everything.

That's all there is to blinking a LED with MicroPython! &nbsp;With a few simple commands you can control a LED connected to a board pin. &nbsp;Then using standard Python loop and delay&nbsp;functions you can make MicroPython blink the LED however you&nbsp;desire.

One important note though, you might wonder what happens if you start the LED blinking with a while loop like above and then reset or unplug/plug back in the board. &nbsp;Try it to see what happens!

You might think the board will start up and go back to running the code you entered. &nbsp;However that's not how MicroPython works--any code you enter in the REPL is just run for that moment in time. &nbsp;As soon as you power down the board it will 'forget' what it was doing and start over fresh. &nbsp;You'll see in a later guide how to make the board run a script when it starts, and that way you can make the board run your code like blinking a LED as soon as it powers up.

If you're curious to go further be sure to [explore your board's documentation](../../../../micropython-basics-what-is-micropython/overview?view=all#faq-10) to see what else the machine module and Pin class expose. &nbsp;For example here's the [documentation for MicroPython ESP8266 machine module](http://docs.micropython.org/en/v1.8.2/esp8266/library/machine.html).


## Featured Products

### Adafruit Parts Pal

[Adafruit Parts Pal](https://www.adafruit.com/product/2975)
[LEDs](https://www.adafruit.com/categories/90) and [passives](https://www.adafruit.com/categories/156) and [transistors](https://www.adafruit.com/categories/153) and [switches](https://www.adafruit.com/categories/155) and op-amps oh my!&nbsp;The...

Out of Stock
[Buy Now](https://www.adafruit.com/product/2975)
[Related Guides to the Product](https://learn.adafruit.com/products/2975/guides)
### Diffused Red 5mm LED (25 pack)

[Diffused Red 5mm LED (25 pack)](https://www.adafruit.com/product/299)
Need some indicators? We are big fans of these diffused red LEDs, in fact we use them exclusively in our kits. They are fairly bright so they can be seen in daytime, and from any angle. They go easily into a breadboard and will add that extra zing to your project.

- Pack of 25...

In Stock
[Buy Now](https://www.adafruit.com/product/299)
[Related Guides to the Product](https://learn.adafruit.com/products/299/guides)
### Through-Hole Resistors - 2.2K ohm 5% 1/4W - Pack of 25

[Through-Hole Resistors - 2.2K ohm 5% 1/4W - Pack of 25](https://www.adafruit.com/product/2782)
ΩMG! You're not going to be able to resist these handy resistor packs!&nbsp;Well, axially, they&nbsp;do all of the resisting for you!

This is a **25 Pack of 2.2K Ω Resistors.** More specifically, they are **carbon film** , through-hole...

In Stock
[Buy Now](https://www.adafruit.com/product/2782)
[Related Guides to the Product](https://learn.adafruit.com/products/2782/guides)
### MicroPython pyboard

[MicroPython pyboard](https://www.adafruit.com/product/2390)
The **pyboard** is a compact and powerful electronics development board that runs MicroPython. It connects to your PC over USB, giving you a USB flash drive to save your Python scripts, and a serial Python prompt (a REPL) for instant programming. Requires a micro USB cable, and...

Out of Stock
[Buy Now](https://www.adafruit.com/product/2390)
[Related Guides to the Product](https://learn.adafruit.com/products/2390/guides)
### Adafruit Feather HUZZAH with ESP8266 - Loose Headers

[Adafruit Feather HUZZAH with ESP8266 - Loose Headers](https://www.adafruit.com/product/2821)
Feather is the new development board from Adafruit, and like its namesake, it is thin, light, and lets you fly! We designed Feather to be a new standard for portable microcontroller cores.

This is the&nbsp; **Adafruit Feather HUZZAH ESP8266** &nbsp;- our take on an...

In Stock
[Buy Now](https://www.adafruit.com/product/2821)
[Related Guides to the Product](https://learn.adafruit.com/products/2821/guides)
### Adafruit HUZZAH ESP8266 Breakout

[Adafruit HUZZAH ESP8266 Breakout](https://www.adafruit.com/product/2471)
Add Internet to your next project with an adorable, bite-sized WiFi microcontroller, at a price you like! The ESP8266 processor from Espressif is an 80 MHz microcontroller with a full WiFi front-end (both as client and access point) and TCP/IP stack with DNS support as well. While this chip...

In Stock
[Buy Now](https://www.adafruit.com/product/2471)
[Related Guides to the Product](https://learn.adafruit.com/products/2471/guides)
### ESP8266 WiFi Module

[ESP8266 WiFi Module](https://www.adafruit.com/product/2282)
This interesting module is a lot of fun for hobbyists and students who are interested in experimenting with the ESP8266 WiFi chipset. We bought a bunch of these modules, updated the firmware to the much-easier-to-use v0.924 and wrote some Arduino code to grab a webpage.

We do not...

No Longer Stocked
[Buy Now](https://www.adafruit.com/product/2282)
[Related Guides to the Product](https://learn.adafruit.com/products/2282/guides)
### WiPy 1.0 - IoT Development Platform

[WiPy 1.0 - IoT Development Platform](https://www.adafruit.com/product/3184)
The **WiPy** is an enterprise grade IOT development platform &nbsp;that runs Python in real time with the perfect blend of power, speed, friendliness, and flexibility. Within a few minutes you can wirelessly connect to it and get a Python REPL command line.

In addition to...

No Longer Stocked
[Buy Now](https://www.adafruit.com/product/3184)
[Related Guides to the Product](https://learn.adafruit.com/products/3184/guides)

## Related Guides

- [Adafruit Feather HUZZAH ESP8266](https://learn.adafruit.com/adafruit-feather-huzzah-esp8266.md)
- [3D Printed IoT On Air Sign for Twitch](https://learn.adafruit.com/3d-printed-iot-on-air-sign-for-twitch.md)
- [Chilled Drinkibot](https://learn.adafruit.com/chilled-drinkibot.md)
- [Adafruit IO Basics: Temperature & Humidity](https://learn.adafruit.com/adafruit-io-basics-temperature-and-humidity.md)
- [CircuitPython Hardware: MPR121 Capacitive Touch Breakout](https://learn.adafruit.com/circuitpython-hardware-mpr121-capacitive-touch-breakout.md)
- [Remote Control with the Huzzah + Adafruit.io](https://learn.adafruit.com/remote-control-with-the-huzzah-plus-adafruit-io.md)
- [WiFi OLED Display Badge](https://learn.adafruit.com/digital-display-badge.md)
- [MicroPython Hardware: I2C Devices](https://learn.adafruit.com/micropython-hardware-i2c-devices.md)
- [MicroPython Basics: Load Files & Run Code](https://learn.adafruit.com/micropython-basics-load-files-and-run-code.md)
- [Mystery Box: Crypto Countdown Case](https://learn.adafruit.com/mystery-box-crypto-countdown-case.md)
- [MAC Address Finder](https://learn.adafruit.com/mac-address-finder.md)
- [Adding a WiFi Co-Processor to CircuitPython](https://learn.adafruit.com/adding-a-wifi-co-processor-to-circuitpython-esp8266-esp32.md)
- [Adafruit IO Environmental Monitor for Feather or Raspberry Pi](https://learn.adafruit.com/adafruit-io-air-quality-monitor.md)
- [Smart Toilet Light](https://learn.adafruit.com/smart-toilet-light.md)
- [Building CircuitPython](https://learn.adafruit.com/building-circuitpython.md)
- [All the Internet of Things - Episode Two: Protocols](https://learn.adafruit.com/alltheiot-protocols.md)
