MicroPython Programming Tutorial: Getting Started with the ESP8266

Overview

In this tutorial, you’ll get to know the micropython programming and its platforms. At the end of this tutorial, you have learned about micropython, its applications, and its programming environment, and you can write a simple program using micropython’s practical libraries and functions.

What You Will Learn:

  • Introduction of MicroPython
  • Introduction of MicroPython Programming Environment
  • Programming ESP8266 with MicroPython

What is MicroPython?

Python is a very popular and practical high-level programming language. But in the case of programming the microcontrollers, other programming languages ​​are more common than Python due to their high speed and proper processing power. MicroPython is an open source implementation of Python 3 that includes a small subset of Python standard libraries and is optimized for execution on microcontrollers with limited ROM and RAM. This version of the Python language is designed to have high speed and be used to communicate with some microcontrollers such as pyboard, Wipy, ESP32, ESP8266, and MicroBit.

Required Materials

Hardware Components

ESP8266 board×1

Software Apps

MicroPython firmware for ESP8266
ESP8266Flasher
putty

Why ESP8266?

We preferred to use ESP8266 for MicroPython programming rather than other microcontrollers. The advantages of using the ESP8266 module are having  Wi-Fi connectivity for Internet and network connection, including a powerful controller with high RAM and its affordable price.

Running MicroPython on ESP8266

As the first step to run the Python script on the ESP8266 module, you should download the MicroPython firmware for ESP8266 and upload it on the board.

You can download the ESP8266, or other microcontrollers firmware, from the following link:

http://micropython.org/download

After downloading the firmware, you should upload it on the board. In order to program the ESP8266 module you need the ESP8266Flasher, which you can download  from the following link:

https://github.com/nodemcu/nodemcu-flasher

To enter the MicroPython programming environment or REPL(Read Evaluate Print Loop), you should use a serial terminal. We have used the Putty software to communicate with the ESP8266 on Windows. You can download the Putty software from the following link as well:

https://www.putty.org/

Now that you have downloaded the required software, you can program the ESP8266 as it is shown below:

  1. Choose the port which is connected to the ESP8266 module from the Operation menu.

2. Now add the path of the firmware of the module that you have just downloaded, in the Config menu.

3. Now click on Flash to install the firmware on your ESP8266 module.

After installing the firmware, you should set the following configuration for the Putty software to enter the REPL.

  1. Choose the serial connection from the Putty connection type. Then choose the right COM and set the baud rate to 115200.

2. Now click on the Open to get a REPL command prompt.

Cautiouns
If the text does not appear on the screen after entering the REPL programming environment, or you can not write any command in the command prompt, enter the Terminal section from the Putty settings and enable the Force – on option

Running MicroPython on ESP8266

MicroPython Programming through WebREPL

Follow the steps below to enter the WebREPL MicroPython programming environment:

  1. Open the http://micropython.org/webrepl/ in your browser. Then return to the command prompt and enter the following command.


2. After running this command, you will be asked if you want to enable or disable the WebREPL. Enter E to enable the WebREPL.

MicroPython Programming through WebREPL

3. Then you will be asked to set a password. You can enter your desired password after entering Y.

4. When you have done these steps, connect to the WiFi of the ESP module. Look for a WiFi network named micropython. The password is micropythoN by default.

5. After connecting to the WiFi, return to the WebREPL page in your browser. Click on the Connect and enter the password you chose before.

Success
Welcome to the WebREPL for MicroPython programming!

MicroPython Programming through WebREPL

Project 1: Controlling an LED with MicroPython (Digital Control)

Turning the LED On and Off

Now we want to run a simple code to control an LED with the MicroPhyton. The ESP8266 has two LEDs on its board, connected to pins 2 and 16. To turn these LEDs on and off with MicroPayton, you need to control the input and output pins (GPIOs) of the module. You can use the machine library to do this.


Now you can use the functions of this library. The following command defines the pin2 of the module as an output pin:


You can turn the LED that you have defined above, on and off using the following commands:


Or:


 

Blinking LED

Now we want to make a blinking LED by adding a key to the circuit. We need also the utime library as well as the machine library for this part.

Note

When you program a code on the ESP8266, it will continue running forever! In order to upload a new code on the module, you need to use the sys library to end the previous code that is still running.


By connecting a key to the pin 0, you will be able to exit the previous code by pressing the key.


The exit() function ends the previous program.


The sleep_ms() function acts like delay function and generate a delay in milliseconds.

Tip

For more information about MicroPython programming and its libraries and functions you can visit the following link:

https://docs.micropython.org/en/latest/library/index.html

Project 2: Controlling an LED with MicroPython (Analog Control)

Now we want to control the LED light with a potentiometer. To control the LED light, we use PWM. Connect the potentiometer to the ESP8266 module ADC pin. This pin reads the analog value of the potentiometer and converts it to a digital value between 0 and 1024. Then this digital value is sent as a PWM duty cycle to control the intensity of the LED light.

What's Next?

  • Try to learn more function and do more complicated projects by visiting the MicroPython site

Share this post

Comment (1)

  • dps Reply

    I think that you have to do from machine import Pin now, to be able to use the Pin functions. At least thats what I have had to do, other wise it throws an error.

    December 10, 2019 at 5:43 am

Leave a Reply

Your email address will not be published. Required fields are marked *