# Using microPython on Thonny IDE to blink a built-in LED



In this tutorial, we will be setting and using microPython firmware on the Thonny IDE.

### Things Needed&#x20;

1. Windows computer&#x20;
2. Thonny IDE installed&#x20;
3. microPython Firmware installed&#x20;
4. NodeMCU dev kit and a USB cable&#x20;

### Getting Started

To start programming on your NodeMCU (esp8266) using Thonny IDE, you will need to open the Thonny IDE and then click on **Tools > Options** and select the **Interpreter tab.**

On the options, choose the **Micropython (esp8266)**  and then choose the port name (number ) to which your nodeMCU dev board is connected. In this case we chose COM11.

![](https://2671334201-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-Ly3OADOGtKzS60vUfrw%2F-MBFAS71IOLtyxOUusXS%2F-MBFB2iyumxsX3I0Sxbw%2FmicroPython\(3\).PNG?alt=media\&token=d0aa51c2-7658-46cc-a97e-f20333b46b91)

Thonny should now be connected to your NodeMCU board and you should the following message on the shell window.

![](https://2671334201-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-Ly3OADOGtKzS60vUfrw%2F-MBFEoVCaZNql75DVCKr%2F-MBFFIp1DLrb15hwL9jk%2FmicroPython\(5\).PNG?alt=media\&token=d57ba602-4fa7-4a35-8890-c75e2fe14611)

To test the configuration, type the command help() and see what you get in response. when everything is in order you should see the following.

![](https://2671334201-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-Ly3OADOGtKzS60vUfrw%2F-MBFFz1IYEUPBtlLGUmZ%2F-MBFGTdcHK-0aAFXcv6A%2FmicroPython\(6\).PNG?alt=media\&token=26e6d098-1a95-4150-a294-cc84697b2b0f)

### &#x20;Testing MiicroPython&#x20;

Write the following commands on the Thonny IDE and execute them to light up the on-board LED&#x20;

```
>>> from machine import Pin 
>>> Pin(2, Pin.OUT).value(0)
```

![](https://2671334201-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-Ly3OADOGtKzS60vUfrw%2F-MBJD68uPlD2lX5kIsUa%2F-MBJEONmNuhGLToqaYZR%2Fcarbon%20\(9\).png?alt=media\&token=173f55b4-ca28-4924-b788-293d9995e963)

When using the esp8266 board, the logic works in the opposite. That is to say, the value () argument will be 0 instead of 1 which is usually reserved for "ON".

### Blinking the built-in LED&#x20;

In this section, we will be programming the nodeMCU board to blink it's built-in LED. The following script will be run on the Thonny IDE's editor.

![](https://2671334201-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-Ly3OADOGtKzS60vUfrw%2F-MBJjOhHoZqljedl3rwx%2F-MBJks1Xe0WDQjFxkA1e%2Fcarbon%20\(10\).png?alt=media\&token=2081eb62-be6c-494b-9215-738fa761eb49)

N.B. when uploading the code as ledblink.py it will be saved as main.py on the board regardless of what you saved it as on your computer.&#x20;

Once you are done uploading the code press the Reset (RST) button on the board.

### Explanation of the code:

```
from machine import Pin 
```

Here we are importing a module called machine and from it we are accessing the class.

```
from time import sleep
```

From the module time we get the class sleep.&#x20;

```
led = Pin(2, Pin.OUT)
```

Here we create a Pin object called led. We are using the pin 2 that is where the built-in LED is connected. The pin is also declared as an output device.&#x20;



&#x20;

