ESP32

ESP32 MicroPython Projects for Beginners

ESP32 MicroPython Projects:

ESP32 MicroPython Projects for Beginners- 4 simple ESP32 MicroPython projects that you should try as a beginner before attempting any intermediate or complex projects using ESP32 and MicroPython.

  1. You should know how to control any GPIO pin on the ESP32 board. I have already explained this in my getting started article ‘MicroPython on ESP32’. In that article, I explained how to install MicroPython firmware on the ESP32, how to set up Thonny IDE, and how to control the onboard LED. If you can control the onboard LED, you can also turn any GPIO pin ON or OFF.
  2. You should know how to read an analog sensor and print its value. In this example, I will use a Potentiometer. If you learned how to read a potentiometer then you can read any analog sensor.
  3. You should know how to read a digital signal on any GPIO pin and then control an output device connected to another Pin on the ESP32. For example, you can use a pushbutton to control the Onboard LED. But to make it more interesting, I am going to use a PIR Sensor and control a Buzzer. You might already know about PIR Sensor; PIR stands for “Passive Infrared” It’s basically a motion detector. So, we will make a small security system: whenever the PIR sensor is going to detect a human, the Buzzer will turn on. If you learned how to control the buzzer, then you can control anything. For instance, if you want to control a light, you can connect a relay module instead of the buzzer and then connect the bulb to the relay.
  4. You should know how to display text and sensor values on the SSD1306 OLED display module. There is also another variant of the OLED display module that uses different set of instructions, so make sure you get yourself the SSD1306 version of the Oled display module. For this example, you can also use a Potentiometer to read and display its value on the OLED display. But, to make it more interesting, I will use a DHT21 Temperature and Humidity sensor. We will read the temperature and humidity values from this sensor and display them on the SSD1306 OLED display module.

You should know how to display text on an I2C supported 16×2 LCD. Nowadays, this type of LCD is not used as much, and I myself mostly use OLED displays. But maybe you might need it in some of your upcoming projects, so that’s why I will also cover the I2C supported 16×2 LCD.

ESP32 MicroPython Projects

allpcb circuit

I am sure you now have an idea of which projects we are going to work on and which components we will need for those projects. So, without any further delay, let’s get started.




Amazon Links:

ESP32 WiFi + Bluetooth Module (Recommended)

DHT21 Temperature and Humidity Sensor

SSD1306 Oled Display Module

I2C supported 16×2 LCD

PIR Sensor

5V Buzzer

Potentiometer

*Disclosure: These are affiliate links. As an Amazon Associate I earn from qualifying purchases.

Let me remind on more time, for the MicroPython firmware installation on the ESP32, how to install and use Thonny IDE, and how to control the Onboard LED watch my getting started tutorial “MicroPython On ESP32”.  So, controlling the onboard LED was the first project.



ESP32 MicroPython, Analog Sensor:

In this 2nd project, we are going to Read this Potentiometer and print its value to the console.

ESP32 MicroPython Projects

For this, simply connect the middle leg of the Potentiometer to the GPIO34, and the other two legs of the Potentiometer to the 3.3V and GND pins on the ESP32. For the connections you can follow this circuit diagram.

ESP32 MicroPython Projects

Now, let’s go ahead and take a look at the programming.



ESP32 MicroPython, Analog Sensor Code:

Code Explanation:

We have a potentiometer connected to our ESP32 board. First, we import the necessary modules ADC (analog to digital converter) and Pin for handling GPIO pins, and time for managing time related functions.

We specify that our potentiometer is connected to pin 34 on the ESP32.

Then, we initialize the ADC with specific settings. We set the attenuation to 11dB, which adjusts the input range for the ADC.

In MicroPython, for ESP32’s ADC module, you can choose from these different attenuation values:

ADC.ATTN_0DB: Sets the attenuation to 0dB, providing the full input voltage range (0-3.3V).

ADC.ATTN_2_5DB: Sets the attenuation to 2.5dB.

ADC.ATTN_6DB: Sets the attenuation to 6dB.

ADC.ATTN_11DB: Sets the attenuation to 11dB.

Setting the ADC attenuation to 11dB (adc.atten(ADC.ATTN_11DB)) provides a higher attenuation value compared to lower values like 0dB, 2.5dB, and 6dB. This means that the input voltage range that the ADC can accurately measure is wider.

The benefit of using a higher attenuation value like 11dB is that it allows the ADC to measure a wider range of voltages accurately. This can be useful in situations where you expect the input voltage to vary over a wide range, or when you need to measure relatively high voltages accurately.

And we set the ADC width to 12 bits for higher resolution.

Choosing a higher resolution, such as 12 bits, means that the ADC can represent the analog signal with more precision because it can distinguish between a greater number of voltage levels. Specifically, a 12-bit ADC can represent the analog signal using 212 (or 4096) different levels.

The good thing about using a higher resolution ADC is that it can give more precise measurements. This is handy when the analog signal has tiny changes, or when you need very accurate measurements.

We define a function called read_potentiometer() to read the value of the potentiometer using the ADC.

In the main loop, we continuously read the potentiometer value using the read_potentiometer() function.

We print the potentiometer value to the console.

Finally, we add a short delay of 0.1 seconds before reading the potentiometer value again.



Analog Sensor Practical Demonstration:

Previously, I have explained how to save and run the program. But let me do it one more time for you.

Go to the Run menu and click on the Configure interpreter.

On the interpreter Tab make sure you set the “Which kind of interpreter should Thonny use for running your Code?” to the MicroPython (ESP32).

ESP32 MicroPython Projects

And make sure you select the correct communication port. After doing this.

Next, you can save the program. Make sure you select the MicroPython device. Write the file and save it with .py extension. And that’s it.

Now, you can click on the Run button.

ESP32 MicroPython Projects

You can see it’s printing values to the console; when I rotate the knob of the potentiometer, the values change.




ESP32 MicroPython PIR Sensor:

Next, we are going to make a very basic Security system using a PIR Sensor and a 5V buzzer.

ESP32 MicroPython Projects

For this connect the Voltage and GND pins of the PIR Sensor to the ESP32 3.3V and GND pins. Connect the PIR Sensor Output pin to the ESP32 GPIO12.

Connect the +Ve leg of the Buzzer to the ESP32 5V pin. GND of the Buzzer module to the GND of ESP32. And the buzzer input wire to the GPIO18. For the connections you can follow this circuit diagram.

ESP32 MicroPython Projects



ESP32 MicroPython PIR Sensor Code:

Code Explanation:

First, we import the necessary modules.

Then, we define which GPIO pins are connected to the PIR sensor and the buzzer.

We set up the PIR sensor pin as an input and the buzzer pin as an output.

The funciton trigger_buzzer() is used to turn ON the buzzer, waits for a short time, and then turns it off. This function is used to make the buzzer sound when motion is detected.

In the main loop, we continuously check if motion is detected by reading the PIR sensor. If motion is detected (the value is 1), we call the trigger_buzzer() function to sound the buzzer.

PIR Sensor and Buzzer Practical Demonstration:

If you want to power up your project using an external power supply, and you want your program to automatically run. Then you will have to save the code with the name main.py.

ESP32 MicroPython Projects

By saving your code as main.py, you ensure that it runs automatically whenever the board is powered on or reset, without needing to manually execute the script. This is particularly convenient for standalone projects or applications where you want the code to start running as soon as the board starts up, without any user intervention.

After running the project, the PIR Sensor successfully detected my hand movement. For the practical demonstration, you can watch my video tutorial available at the end of this article.



ESP32 MicroPython DHT21 and Oled Display:

Next, we are going to start with the DHT21 Temperature and Humidity Sensor and the SSD1306 Oled display Module.

ESP32 MicroPython Projects

For this, connect the VCC and GND wires of the DHT21 Temperature and Humidity Sensor to the ESP32 3.3V and GND pins. Connect the Data pin to the ESP32 GPIO19.

Connect the VCC and GND pins of the SSD1306 Oled display module to the ESP32 3.3V and GND pins. Connect the SDA and SCL pins to the ESP32 GPIO’S 21 and 22 respectively. For the connections you can follow this circuit diagram.

ESP32 MicroPython ProjectsNow, let’s go ahead and take a look at the programming.

ESP32 MicroPython DHT21 and Oled Display Code:



Code Explanation:

First, we import the necessary modules.

Then, we initialize the I2C communication for the SSD1306 OLED display and set its width and height.

Next, we initialize the DHT sensor on pin 19.

In the main loop, we continuously read temperature and humidity data from the DHT sensor.

We print the temperature and humidity values to the console for debugging purposes.

We clear the OLED display, then display the temperature and humidity values on it.

DHT21 and Oled Display Practical Demonstration:

Next, save this script on the MicroPython device. We are going to save it with the name main.py.

Now, if I go and click on the Run button. It will generate an error “no module named ‘ssd1306’.

ESP32 MicroPython Projects

To fix this, go to the Tools menu and click on Manage packages.

Search for the SSD1306.

ESP32 MicroPython Projects

From the search results, click on the ssd1306 and install it.

Now, we are going to do it for the DHT sensor.

ESP32 MicroPython Projects

After installing the required libraries, now we can click on the Run button.

It’s printing the Temperature and Humidity values to the console. And I can also see the same values on the Oled display module. Let me show it to you.

ESP32 MicroPython Projects




ESP32 MicroPython I2C 16×2 LCD:

Next, we are going to start with the I2C supported 16×2 LCD.

ESP32 MicroPython Projects

Connect the VCC and GND pins of the I2C supported 16×2 LCD to the 5V and GND. Connect the SDA and SCL pins to the GPIO pins 21 and 22 respectively. For the connections you can follow this circuit diagram.

ESP32 MicroPython Projects

Now, let’s go ahead and take a look at the programming.

ESP32 MicroPython I2C 16×2 LCD Code:

Main.py:

Code explanation:

The code is pretty simple; we are importing the necessary modules.

We have defined the I2C address, the number of rows, and columns.

21 and 22 are the i2c pins on the ESP32 our i2c supported 16×2 LCD is connected to.

Next, we initialize the LCD.

And then we print ESP32 and Tutorial on the LCD.

If we go to the top where we are importing the modules. You might have noticed these two lines.

from lcd_api import LcdApi

from i2c_lcd import I2cLcd

You can see we are importing LcdApi from the lcd_api and I2cLcd from the i2c_lcd. These are actually two different files which are going to be stored on the ESP32 along with the main.py file.

We need to save the following code with the name lcd_api.py.

lcd_api.py:

And we need to save the following code with the name i2c_lcd.py.

i2c_lcd.py:

Now, we can save the main code, which is going to run repeatedly, with the name main.py. and that’s it. Finally, we can click on the Run button.

ESP32 MicroPython Projects



Watch Video Tutorial:

Engr Fahad

My name is Shahzada Fahad and I am an Electrical Engineer. I have been doing Job in UAE as a site engineer in an Electrical Construction Company. Currently, I am running my own YouTube channel "Electronic Clinic", and managing this Website. My Hobbies are * Watching Movies * Music * Martial Arts * Photography * Travelling * Make Sketches and so on...

Related Articles

Leave a Reply

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

Back to top button