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

  1. Windows computer

  2. Thonny IDE installed

  3. microPython Firmware installed

  4. NodeMCU dev kit and a USB cable

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.

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

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.

Testing MiicroPython

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

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

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

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.

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.

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.

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.

Last updated