Introduction: MicroPython Basics Using NodeMCU (ESP8266)

MicroPython is a project that allows you to run a miniature version of python 3 on micro controllers and embedded boards. It has a growing support of microcontroller boards and rather than installing a full Linux distro onto the board it only gives a minimized version of python with respect to the board, with a python shell and you can upload small python files on to the board and run it.

In this instructable I will be showing you how to use MicoPython on a NodeMCU, the NodeMCU is a development board based on esp8266-12.

Step 1: Tools and Components

For this project you will need a,

  • NodeMCU
  • LEDs
  • Servo
  • Breadboard
  • MicroUSB Cable

Step 2: Installing MicroPython

To install micropython on the esp8266, I'm using the esp8266-12 version board. To install micropython you will need esptool you will need to download and install python and pip, to install the esptool.

Run the below command on a terminal or cmd to install esptool

pip install esptool

Next you can visit micropython website and download the latest firmware for the esp8266, after downloading it open up a terminal in the same directory as the firmware file and then run the below command --port /dev/ttyUSB0 erase_flash --port /dev/ttyUSB0 --baud 460800 write_flash --flash_size=detect 0 esp8266-xxxxx-vxxxx.bin

You will need to change the port based on you PC.

After this you should have successfully installed micropython.

Step 3: Testing Blink

Now that you have successfully installed micopython it is time to try out a few test programs to do this we need to open up the python shell using a serial monitor, I use putty on a windows machine to open up a serial monitor on the com port the esp8266 is assigned to.

This python shell is similar to that of the python 3 shell, run the below script to blink an led connected to the esp8266

import esp

pin = machine.Pin(0)

pin = machine.Pin(0, machine.Pin.OUT)

Then running the below line of python script will turn on the led and the second line would turn it off.


alternatively you could also run these lines to do the same

Step 4: Using WebREPL

Now lets enable the micropython WebREPL which allows us to upload scripts to the esp8266 over WiFi hence eliminating the need of wires.

First we need to enable WebREPL, open up a serial terminal and execute the below line, this setups the webrepl and asks you to set up a password to improve security.

import webrepl_setup

Next you should see a WiFi access point named MicroPython-xxxxxx, connect to it and open up a internet browser and visit the webREPL webpage. Now you should get a web page, hit connect and enter the password you created. Now you can execute scripts on the esp8266 wireless.

Step 5: Going Further

Now that you got micropython up and running you can execute scripts on it, similar to what you do a raspberry pi. There are a lot of modules available for micropython to work with and you can get a good documentation of it in the micro python official website under esp8266 category.