How to Configure a...

  • 2022-09-23 11:58:40

How to Configure and Control an ESP8266 Board Using MicroPython

Step 1: What is MicroPython?

MicorPython is one of the many programming languages we can use to program the ESP8266 module. It is a stripped down and express version of the Python 3 programming language and has several advantages over traditional programming languages such as C and C++.
MicroPython is designed to be as compatible as possible with normal Python. It has a full Python compiler and runtime, and provides an interactive prompt called REPL (read-evaluate-print loop).

MicorPython is designed to support several different types of microcontrollers. But for this tutorial, I will use only one model: an ESP8266 based board (NodeMCU). Note that you can buy several different boards with the same chip.

Step 2: Requirements

How to Configure and Control an ESP8266 Board Using MicroPython

To be able to follow this tutorial, you only need basic coding experience in Python. You do not need to have any previous knowledge of microcontrollers. , electronics, and even MicroPython.

You'll also need a Windows, Mac, or Linux computer with a free USB port, since you'll connect the microcontroller to the computer for programming.

Parts required:

1 x NodeMCU (or other ESP8266 based board)

1 x Red 5mm LED

1x 220 Ω1/4W resistor

1x10KΩ Rotary Potentiometer

1 x Breadboard

1 x USB to MicroUSB cable

jumper.

Step 3: Why ESP8266 Baseboard?

One way you can get the most out of the ESP8266 is by using MicroPython. Also, the ESP8266 module is one of the best platforms to learn how to use MicroPython. This is because the ESP8266 provides simple GPIO pin control functionality as well as wireless functionality, allowing you to test all aspects of the MicroPython programming language.

The ESP8266 chip is very popular in the open source development industry. There are many development boards from different manufacturers that use the ESP8266 chip. MicroPython is designed to provide a general purpose port that will run on most of these boards with as few limitations as possible. This port is based on the Adafruit Feather HUZZAH board When using other ESP8266 boards, be sure to check their schematics and datasheets to determine the differences between them and the Adafruit Feather HUZZAH board. This way, you can accommodate differences in your code.

Reading and resources:

ESP8266

Adafruit Feather HUZZAH

Step 4: Set up the computer

Before programming the ESP8266 board with MicroPython, you need to set up a few things.

We will complete the setup process in this step. In this way, you will learn how to configure the ESP8266 board for use with MicroPython.

get ready

All you need from this step to step 6 is your ESP8266 and a USB cable. Connect the ESP8266 board to the computer.

How to do it. ... ..

Step 1: Install Device Drivers

If you have a Linux computer, then you don't need to install any device drivers for the driver to recognize the microcontroller. But you have a Mac or Windows machine and need a driver to allow the computer to recognize the microcontroller as a serial device.

"Step 2: Install Python

The tools you'll use to communicate with the ESP8266 are written in Python, so you'll need Python installed on your computer.

If your operating system does not provide prepackaged Python, you can visit https://python.org to download the official version for any supported operating system.

STEP3: Install esptool and rshell

Install two packages to help you manage your board with pip. To open a terminal and run

pip install esptool rshell

STEP4: Download MicroPython

As I write this, the current version is 1.11 and the firmware file is named esp8266-20190529-v1.11.bin

When you do, you may find an updated version.

Step 5: Flash MicroPython with Esptool.py

Before flashing new firmware onto the board, it's a good idea to erase all previous data. This is what you should always do so that new firmware runs from a clean state.

Go to the location where you put the .bin file. Erase the flash with esptool.py.

For Linux:

esptool.py --port /dev/ttyUSB0 erase_flash

For Windows:

esptool.py --port COM3 erase_flash

You may need to change the serial port in the command to the serial port the ESP8266 board is connected to. If you don't know the serial number of the ESP8266, you can check out the Arduino IDE. Just open the IDE and click Tools | Ports. You should see the serial port of the ESP8266 board listed there. Replace the serial port in the command (/dev/ttyUSB0) with the serial port of the board.

Now that the board is completely wiped, you can flash the MicroPython build you just downloaded. This is also done using the esptool.py command:

esptool.py --port /dev/ttyUSB0 --baud 460800 write_flash 0 esp8266-20190529-v1.11.bin

This command will write the contents of the MicroPython .bin file to the board at address 0.

Make sure you change the name of the firmware .bin file in the command (esp82688-2019-080529-v1.11.bin) to the name of the firmware you downloaded.

After successfully installing the firmware on the ESP8266 you can access the REPL on the board via a wired connection (UART serial) or IDE WiFi.

Step 6: Using the MicroPython REPL with Rshell

You can now start MicroPython on the ESP8266 board.

I'll show you how to connect to the Python prompt to run on your board. This is called REPL, which stands for "Read-Eval-Print-Loop". This is the standard Python prompt you might see when using a regular Python interpreter, but this time it will be running on your board, and to interact with it, you will use a serial connection to your computer. Ready?

To connect to your board and open a REPL session, enter the following commands:

rshell --port

This command will take you to the rshell prompt. See the photo above.

If you're following this tutorial on Windows, be aware of the history of issues with rshell running on Windows.

So to fix that type:

rshell -a --port COM3

From this prompt, you can perform administrative tasks related to your microcontroller board and also launch a Python REPL that you can use to interact with the board in real-time. So just type the following command:

repl

To make sure everything works, type a simple Python sentence:

print("Hello World")

Step 7: Control Pins Using MicroPython

In this step, we will learn how to control the ESP8266 pins using MicroPython. To do this, we will set up a setting where we will toggle the state of the LEDs connected to the GPIO pins of the ESP8266 board. This will help you understand how to control digital output with MicoPython.

get ready

You will need the following to complete this step:

1 x NodeMCU

1 x Red 5mm LED

1 x 220Ω resistor

1 x Breadboard

jumper

Construct

First mount the LEDs to the breadboard. Connect one end of the 220Ω resistor to the positive terminal of the LED (the positive terminal of the LED is usually the higher of the two legs). Connect the other end of the resistor to pin D1 of the ESP8266 board. Then connect the negative terminal of the LED to the GND pin of the ESP8266 board. The connections are as shown above.

Once set up, connect the ESP8266 board to the computer via a USB cable.

How to do it. . .

Type the following code in the REPL:

# blink LED every 1 second

def blink(pin=5, time=1) # blink function by default pin=5, time=1s

import machine # the machine module holds the pin configurations and modes

from time import sleep # import sleep for some delay

LED = machine.Pin(led_pin, machine.PIN.OUT) # configure LED as OUTPUT

while True: # run forever

LED.value(1) # set LED to HIGH

sleep(time) # wait 1 second by default

LED.value(0) # set LED to LOW

sleep(time) # wait 1 second by default

Type blink() in a RPEL session to test this code. This will blink LED every 1 second connected to GPIO5.

You can change the pin and/or time by calling:

blink(pin=, time=)

Press ctrl+c to quit running the code.

You can use MicroPython to read the input connected to the ESP8266. Continue to the next step to learn how.

Check if the video is stuck.

Step 8: Fade the LEDs

In this step, we will learn how to adjust the brightness of the LED using the rotary potentiometer. We'll use a technique called Pulse Width Modulation (PWM), which allows us to dim the LEDs using up to 256 settings.

Note: All pins of ESP8266 can be used as PWM pins (D0) except GPIO16.

be ready:

You need to complete the following steps:

1 x NodeMCU

1 x Red 5mm LED

1 x 50KΩ rotary potentiometer.

1 x Breadboard

jumper

Construct

The connection is as shown in the picture above: After the setup is complete, connect the ESP8266 board to the computer via a USB cable.

How to do it. ..

Type following the code in the REPL:

# Fading LED every 0.5 by reading data from the Potentiometer

import machine

from time import sleep

led_pin = 5 # led pin

POT = machine.ADC(0) # ADC0 pin

LED = machine.Pin(led_pin) # create LED object

LED_pwm = machine.PWM(LED, freq= 500 ) # create LED_pwm object and set frequency to 500Hz

while True:

LED_pwm.duty(POT.read()) # get the value from the Pot and set it to the duty cycle

sleep(0.5) # wait 0.5

This will change the brightness of the LED connected to GPIO 5 by changing the value of the potentiometer.

Press ctrl+c to exit the running code.

Check if the video gets stuck.

Step 9: Where to start?

So far we have seen how to configure and run MicroPython on an ESP8266 based board. We learned how to control the pins to blink the LED, then we added a potentiometer in order to control the brightness of the LED using pulse width modulation techniques.

Now we can read the data from the sensor and send it to the cloud, we can also create an HTTP server where you can print our data etc in a simple web page. ..