Skip to content

Mixly Tutorial

Resources Download

Note: Please download the code and library files for all Mixly courses here first.

Click to download: Mixly Resources

Project 01 Hello World

1. Project Introduction:

For ESP32 beginners, we will start with something simple. In this project, you only need an ESP32 board, a USB cable, and a computer to complete the "Hello World!" project. It is not only a communication test between the ESP32 board and the computer, but also a beginner-level project for the ESP32.

2. Project Components:

Image not found Image not found
ESP32*1 USB cable*1

3. Project Wiring:

In this project, we connect the ESP32 to the computer via a USB cable.

Image not found

4. Code Description:

Img

Set the serial port baud rate, generally set to 9600.

Img

Output data with automatic line wrapping. Output data from the serial port, followed by a carriage return and a line feed.

Img

Output data from the serial port without line wrapping.

Img

Pause the program execution for a period of time, i.e., delay. The unit is milliseconds.

5. Project Code:

The code can be found in the "Resources Download" section above. It is recommended to use the downloaded code directly.

You can also write the code yourself, as follows:

  1. Drag out ""Img"" from ""Img"".

  2. Drag out ""Img"" from ""Img"" and place it into ""Img"".

  3. Drag out ""Img"" from ""Img"".

  4. Drag out ""Img"" from ""Img"" and place it into ""Img"", change hello to Hello World.

  5. Drag out ""Img"" from ""Img"".

Complete code:

Img

Before uploading the project code to the ESP32, you need to manually select the Arduino ESP32 board type "ESP32 Dev Module" and the serial port (COM6) (Note: different computers have different serial ports). (Note: You can only see the corresponding port after connecting the ESP32 board to the computer via USB cable.) (The same steps apply for uploading project code later, i.e., same as below.)

Img

Img

Click Img to upload the project code to the ESP32 board. (Same as below)

Img

Note (Same as below) If the code upload is not successful, you can click Img again and hold down the Boot button on the ESP32 board Image not found with your hand. Release the Boot button after the upload progress percentage appears, as shown in the figure below:

Img

Project code uploaded successfully!

Img

6. Project Result:

After the project code is uploaded successfully, power it on via the USB cable, click the icon Img to enter the serial monitor, set the baud rate to 9600, and the serial monitor will print "Hello World!".

Img

Img

Img

Project 02 Light Up LED

1. Project Introduction:

In this project, we will show you how to light up an LED. We use a digital pin of the ESP32 to turn on the LED, causing it to light up.

2. Project Components:

Image not found Image not found Image not found
ESP32*1 Breadboard*1 Red LED*1
Image not found Image not found Image not found
220Ω Resistor*1 Jumper Wires*2 USB cable*1

3. Component Knowledge:

(1)LED

Image not found

An LED is a semiconductor called a "Light Emitting Diode," an electronic device made of semiconductor materials (silicon, selenium, germanium, etc.). It has a positive and a negative terminal. The short leg is the negative terminal, connected to GND; the long leg is the positive terminal, connected to 3.3V or 5V.

Image not found

(2)Five-Color Band Resistor

A resistor is an electronic component that limits or regulates the flow of current in a circuit. On the left is the appearance of a resistor, and on the right is the symbol representing a resistor in a circuit. The unit of resistance (R) is the ohm (Ω), 1 MΩ = 1000 kΩ, 1 kΩ = 1000 Ω.

We can use resistors to protect sensitive components like LEDs. The resistance value (in Ω) is marked on the body of the resistor with small color bands. Each color represents a number, which you can look up using a resistor color code chart.

Image not found

In this kit, we provide 3 five-color band resistors with different resistance values. Here are 3 examples:

220Ω Resistor ×10

Image not found

10KΩ Resistor ×10

Image not found

1KΩ Resistor ×10

Image not found

At the same voltage, there will be a smaller current and a larger resistance. The relationship between current, voltage, and resistance can be expressed by the formula: I = U/R. In the figure below, the current through R1 is: I = U / R = 3 V / 10 KΩ = 0.0003A = 0.3mA.

Image not found

Do not directly connect a resistor with a very low resistance across the power supply terminals, as this will cause excessive current and damage electronic components. Resistors do not have positive or negative terminals.

(3)Breadboard

A breadboard is an important tool used for prototyping circuits in the laboratory. It has many holes for inserting integrated circuits, resistors, and other circuit components. Mastering the use of a breadboard is one of the important foundations for improving experiment efficiency and reducing the probability of experiment failures. Below is a brief introduction to the structure and usage of a breadboard. A typical breadboard is shown below:

Image not found

The appearance and internal structure of a breadboard are shown above. A common minimum-unit breadboard is divided into three parts: top, middle, and bottom. The top and bottom parts are usually narrow strips consisting of one or two rows of holes, while the middle part is a strip consisting of a central isolation groove and 5 rows of holes above and below.

Image not found

The two narrow strips of the breadboard each have two rows of holes. The two rows are not connected to each other and are generally used as power supply rails. The first row at the top, marked with "+", has 10 groups of holes (all internally connected) for the positive terminal; the second row, marked with "-", has 10 groups of holes (all internally connected) for ground. The first and second rows at the bottom of the breadboard have the same structure. If you need to use the entire breadboard, you usually connect "+" to "+" and "-" to "-" with wires.

The middle wide strip consists of a central isolation groove and 5 rows of holes above and below. The 5 holes in the same column are connected to each other, while columns and the areas above and below the groove are not connected. The appearance and structure are shown below:

Image not found

The middle wide strip's connection holes are divided into upper and lower parts, which is the main working area of the breadboard, used for inserting components and jumper wires. The 5 holes in the same column (i.e., a-b-c-d-e, f-g-h-i-j) are connected to each other; columns and the areas above and below the groove are not connected. When doing experiments, a small unit consisting of two narrow strips and one wide strip is usually used. The main part of the circuit is built on the wide strip, one row of the upper narrow strip is used for power, and one row of the lower narrow strip is used for ground. The middle wide strip is used to connect the circuit. Since the areas above and below the groove are not connected, integrated circuits are generally inserted across the groove.

(4) Power Supply

The ESP32 requires a 3.3V-5V power supply. In this project, we connect the ESP32 to the computer using a USB cable.

Image not found

4. Project Wiring Diagram:

First, disconnect all power from the ESP32. Then build the circuit according to the wiring diagram. After the circuit is built and verified to be correct, connect the ESP32 to the computer using a USB cable.

Note: Avoid any possible short circuits (especially between 3.3V and GND)!

Warning: Short circuits may cause large currents in the circuit, resulting in overheating of components and permanent damage to the hardware.

Image not found

Note:

How to connect the LED

Image not found

How to identify a five-color band 220Ω resistor

Image not found

5. Code Description:

Img

Set a specified pin to "HIGH" or "LOW". Selecting "HIGH" sets the pin to a high level, with voltage and current, and the LED lights up; selecting "LOW" sets the pin to a low level, with no voltage and no current, and the LED does not light up.

6. Project Code:

The code can also be found in the "Resources Download" section above. It is recommended to use the downloaded code directly.

You can also write the code yourself, as follows:

  1. Drag out ""Img"" from ""Img"".

  2. Drag out " Img " from ""Img"" and place it into ""Img"", set pin to 15, set to "LOW".

  3. Drag out " Img " from ""Img"", set pin to 15, set to "HIGH".

Complete code:

Img

7. Project Phenomenon:

After the project code is uploaded successfully, power it on via the USB cable, and the LED will light up.

Image not found

Project 03 LED Blinking

1. Project Introduction:

In this project, we will show you the LED blinking effect. We use a digital pin of the ESP32 to turn on the LED and make it blink.

2. Project Components:

Image not found Image not found Image not found
ESP32*1 Breadboard*1 Red LED*1
Image not found Image not found Image not found
220Ω Resistor*1 Jumper Wires*2 USB cable*1

3. Project Wiring Diagram:

First, disconnect all power from the ESP32. Then build the circuit according to the circuit diagram and wiring diagram. After the circuit is built and verified to be correct, connect the ESP32 to the computer using a USB cable.

Note: Avoid any possible short circuits (especially between 3.3V and GND)!

Warning: Short circuits may cause large currents in the circuit, resulting in overheating of components and permanent damage to the hardware.

Image not found

Note:

How to connect the LED

Image not found

How to identify a five-color band 220Ω resistor

Image not found

4. Project Code:

The code can also be found in the "Resources Download" section above. It is recommended to use the downloaded code directly. (Note: From this course onwards, this reminder will no longer be provided in subsequent courses.)

You can also write the code yourself, as follows:

  1. Drag out ""Img"" from ""Img"".

  2. Drag out " Img " from ""Img"" and place it into ""Img"", set pin to 15, set to "LOW".

  3. Drag out " Img " from ""Img"", set pin to 15, set to "HIGH".

  4. Drag out ""Img"" from ""Img"", set delay to 500 milliseconds.

  5. Copy the code block ""Img"" 1 time, change "HIGH" to "LOW".

Complete code:

Img

5. Project Phenomenon:

After the project code is uploaded successfully, power it on via the USB cable. The LED in the circuit will blink repeatedly.

Image not found

Project 04 Breathing Light

1. Project Introduction:

In our previous studies, we learned that an LED has ON/OFF states. So how do we achieve an intermediate state? How do we output an intermediate state to make the LED "half bright"? This is what we will learn. A breathing light is when the LED goes from off to on, and then from on to off, like "breathing". So how do we control the brightness of the LED? We will use the PWM of the ESP32 to achieve this goal.

2. Project Components:

Image not found Image not found Image not found
ESP32*1 Breadboard*1 Red LED*1
Image not found Image not found Image not found
220Ω Resistor*1 Jumper Wires*2 USB cable*1

3. Component Knowledge:

Image not found

Analog Signal & Digital Signal

An analog signal is a signal that is continuous in both time and value. In contrast, a digital signal or discrete-time signal is a time series composed of a sequence of numbers. Most signals in life are analog signals. A familiar example of an analog signal is: the temperature throughout the day changes continuously, rather than suddenly jumping from 0 to 10. However, the value of a digital signal can change instantaneously. This change is represented by the numbers 1 and 0 (the basis of binary code). As shown in the figure below, we can more easily see the difference between them.

Image not found

In practical applications, we often use binary as digital signals, i.e., a series of 0s and 1s. Since binary signals have only two values (0 or 1), they have great stability and reliability. Finally, analog signals and digital signals can be converted to each other.

PWM:

Pulse Width Modulation (PWM) is an effective method of using digital signals to control analog circuits. Ordinary processors cannot directly output analog signals. PWM technology makes this conversion (converting digital signals to analog signals) very convenient. PWM technology uses digital pins to send square waves of a certain frequency, i.e., outputs of high and low levels, alternating for a period of time. The total time of each set of high and low levels is generally fixed and is called the period (note: the reciprocal of the period is the frequency). The time of the high-level output is usually called the pulse width, and the duty cycle is the percentage ratio of the pulse width (PW) to the total period (T) of the waveform. The longer the high-level output duration, the longer the duty cycle, and the higher the corresponding voltage in the analog signal. The figure below shows the corresponding analog signal voltage changes between 0V-3.3V (high level is 3.3V) for pulse widths of 0%-100%.

Image not found

The longer the PWM duty cycle, the higher the output power. Now that we understand this relationship, we can use PWM to control the brightness of an LED or the speed of a DC motor, etc. As can be seen from the above, PWM is not a real analog signal; the effective voltage value is equal to the corresponding analog signal. Therefore, we can control the output power of LEDs and other output modules to achieve different effects.

ESP32 and PWM

On the ESP32, the LEDC (PWM) controller has 16 independent channels, each of which can independently control frequency, duty cycle, and even precision. Unlike traditional PWM pins, the PWM output pins of the ESP32 are configurable, and each channel has one or more PWM output pins. The relationship between maximum frequency and bit precision is shown in the following formula:

Image not found

Where the maximum value of the bit is 31. For example: generating PWM with 8-bit precision (2^8 = 256. Value range is 0 ~ 255), the maximum frequency is 80,000,000/255 = 312,500Hz.)

4. Project Wiring Diagram:

Image not found

5. Code Description:

Img

Set the channel for a specified pin; the value can be 0 ~ 255.

Img

Set the channel of digital pin 15 to CH0 (LT0), assign value 0, and the LED is off.

Img

Set the channel of pin 15 to CH0 (LT0), assign value i.

Img

Set a variable i, i gradually increases from 0 to 255, adding 5 each time, for a total of 51 additions. Each increment of 5 is at a frequency of 10 milliseconds, and the LED gradually brightens.

Img

Set a variable i, i gradually decreases from 255 to 0, subtracting 5 each time, for a total of 51 subtractions. Each decrement of 5 is at a frequency of 10 milliseconds, and the LED gradually dims.

6. Project Code:

This project is designed to make GPIO15 output PWM, with the pulse width gradually increasing from 0% to 100%, and then gradually decreasing from 100% to 0%.

You can also write the code yourself, as follows:

  1. Drag out ""Img"" from ""Img"".

  2. Drag out " Img " from ""Img"" and place it into ""Img"", set pin to 15, channel to CH0 (LT0), assign value 0.

  3. Drag out " Img " from ""Img"", change from 1 to 10 step 1 to from 0 to 255 step 5.

  4. Drag out " Img " from ""Img"" and place it into " Img ", set pin to 15, channel to CH0 (LT0); then drag out ""Img"" from " Img" and place it where the assigned value is 0.

  5. Drag out ""Img"" from ""Img"" and place it into " Img ", set delay to 10 milliseconds.

  6. Copy the code block " Img" 1 time, change from 0 to 255 step 5 to from 255 to 0 step -5.

Complete code:

Img

7. Project Phenomenon:

After the project code is uploaded successfully, power it on via the USB cable. The LED in the circuit will gradually brighten from dark, and then gradually dim from bright, just like breathing.

Image not found

Project 05 Traffic Light

1. Project Introduction:

Traffic lights are very common in our daily life. According to certain time patterns, traffic lights consist of three colors: red, yellow, and green. Everyone should follow traffic rules, which can prevent many traffic accidents. In this project, we will use the ESP32 and some LEDs (red, yellow, green) to simulate a traffic light.

2. Project Components:

Image not found Image not found Image not found Image not found
ESP32*1 Breadboard*1 Red LED*1 Yellow LED*1
Image not found Image not found Image not found Image not found
Green LED*1 220Ω Resistor*3 Jumper Wires (several) USB cable*1

3. Project Wiring Diagram:

Image not found

4. Project Code:

You can also write the code yourself, as follows:

  1. Drag out ""Img"" from ""Img"".

  2. Drag out " Img ", " Img ", and " Img " from ""Img"" and place them into ""Img"". The red LED pin is 0, the yellow LED pin is 2, and the green LED pin is 15, all set to "LOW".

Img

  1. Copy the code block " Img " 1 time, set the red LED to "HIGH"; then drag out ""Img"" from ""Img"", set delay to 5000 milliseconds; then copy the code block " Img " 1 more time, with a delay of 500 milliseconds.

Img

  1. Drag out " Img " from ""Img"", change from 1 to 10 step 1 to from 1 to 3 step 1.

  2. Copy the code block " Img " 1 time and place it into " Img ", change the yellow LED from "LOW" to "HIGH"; then copy the code block " Img " 1 time and place it into " Img ",

Img

  1. Copy the code block " Img " 1 time, change the red LED from "HIGH" to "LOW", and change the green LED from "LOW" to "HIGH".

Img

  1. Copy the code block " Img " 1 time.

Complete code:

Img

5. Project Phenomenon:

After the project code is uploaded successfully, power it on via the USB cable. What you will see is: 1. First, the red light will be on for 5 seconds, then turn off; 2. Next, the yellow light will blink 3 times, then turn off; 3. Then, the green light will be on for 5 seconds, then turn off; 4. Continue repeating steps 1-3.

Image not found

Project 06 RGB LED

Image not found

1. Project Introduction:

An RGB LED is composed of three colors (red, green, blue), and by mixing these three basic colors, it can emit different colors. In this project, we will introduce the RGB LED to you and show you how to use the ESP32 to control the RGB LED to emit different colors of light. Even though the RGB LED is very basic, it is also a great way to introduce yourself or others to the fundamentals of electronics and coding.

2. Project Components:

Image not found Image not found Image not found
ESP32*1 Breadboard*1 RGB LED*1
Image not found Image not found Image not found
220Ω Resistor*3 Jumper Wires (several) USB cable*1

3. Component Knowledge:

Most displays follow the RGB color standard, and all colors on a computer screen are produced by mixing red, green, and blue in different proportions.

Image not found

This RGB LED has 4 pins, one for each color (red, green, blue) and a common cathode. To change the brightness of the RGB LED, we can use the PWM pins of the ESP. The PWM pins will give the RGB LED signals with different duty cycles to obtain different colors.

If we use 3 10-bit PWMs to control the RGB LED, theoretically we can create 2^10 × 2^10 × 2^10 = 1,073,741,824 (1 billion) colors through different combinations.

4. Project Wiring Diagram:

Image not found

5. Code Description:

Img

Set a common cathode RGB LED to light up different colors; R, G, B can be set to any number between 0 ~ 255.

6. Project Code:

You can open the code we provided, or you can write the code yourself, as follows:

  1. Drag out ""Img"" from ""Img"".

  2. Drag out " Img " from ""Img"" and place it into ""Img"". The R pin is 0, the G pin is 2, the B pin is 15, and the values of R, G, and B are all set to 0.

Img

  1. Drag out " Img " from ""Img"", change from 1 to 10 step 1 to from 0 to 255 step 1.

  2. Copy the code block " Img " 1 time and place it into " Img ", then drag out ""Img"" from " Img" and place it after R (0 ~ 255) where the value is 0; then drag out " Img from " Img " and place it after G (0 ~ 255) where the value is 0, change "+" to "-", change the 1 before "-" to 128, and replace the 1 after "-" with the variable i; then copy the code block " Img " and place it after G (0~255) where the value is 0, change 128 to 255; then delay 10 milliseconds.

Img

  1. Copy the code block " Img " 1 time, change from 0 to 255 step 1 to from 255 to 0 step -1.

Img

Complete code:

Img

7. Project Phenomenon:

After the code is uploaded successfully and powered on via the USB cable, what you will see is: the RGB LED starts displaying random colors.

Image not found

Project 07 Flowing Light

1. Project Introduction:

In daily life, we can see many billboards composed of LEDs of different colors. They constantly change the lights (like flowing water) to attract customers' attention. In this project, we will use the ESP32 to control 10 LEDs to achieve a flowing water effect.

2. Project Components:

Image not found Image not found Image not found
ESP32*1 Breadboard*1 Red LED*10
Image not found Image not found Image not found
220Ω Resistor*10 Jumper Wires (several) USB cable*1

3. Project Wiring Diagram:

Img

4. Project Code:

This project involves designing and making a flowing light. Here are the steps: First, turn on LED 1, then turn it off. Then turn on LED 2, then turn it off... and repeat the same operation for all 10 LEDs until the last LED is turned off. This process repeats continuously to achieve the "flowing water" effect.

You can open the code we provided, or you can write the code yourself, as follows:

  1. Drag out ""Img"" from ""Img"".

  2. First, drag out ""Img"" from " Img" and place it into ""Img""; then drag out ""Img"" from ""Img"" and place it into ""Img"", change "item" to " i ".

Img

  1. Drag out " Img " from ""Img"" and place it into ""Img"", change from 1 to 10 step 1 to from 12 to 22 step 1.

Img

  1. First, drag out " Img " from ""Img"" and place it into " Img"; then drag out " Img " from " Img" and place it where it says "Pin 0", change "HIGH" to "LOW".

Img

  1. First, drag out " Img " from ""Img"", change from 1 to 10 step 1 to from 12 to 22 step 1; then drag out " Img " from ""Img"" and place it into " Img"; then drag out " Img " from " Img" and place it where it says "Pin 0"; add a delay of 200 milliseconds.

Img

  1. Copy the code block " Img " 1 time, change from 12 to 22 step 1 to from 22 to 12 step -1, change "HIGH" to "LOW".

Img

Complete code:

Img

5. Project Phenomenon:

After the code is uploaded successfully and powered on via the USB cable, what you will see is: 10 LEDs will light up from right to left, and then return from left to right.

Img

Img

Project 08 Single-Digit Seven-Segment Display

1. Project Introduction:

A seven-segment display is an electronic display device for showing decimal numbers. It is widely used in digital clocks, electronic instruments, basic calculators, and other electronic devices that display numerical information. Even the bombs we see in movies have seven-segment displays. Perhaps seven-segment displays don't look modern enough, but they are a good alternative to more complex dot-matrix displays and are easy to use in low-light conditions and strong sunlight. In this project, we will use the ESP32 to control a single-digit seven-segment display to show numbers.

2. Project Components:

Image not found Image not found Image not found
ESP32*1 Breadboard*1 Single-digit seven-segment display*1
Image not found Image not found Image not found
220Ω Resistor*8 Jumper Wires (several) USB cable*1

3. Component Knowledge:

Image not found

Single-digit seven-segment display principle: A seven-segment display is a semiconductor light-emitting device. Its basic unit is a light-emitting diode (LED). According to the number of segments, seven-segment displays can be divided into 7-segment and 8-segment displays. An 8-segment display has one more LED unit than a 7-segment display (used for decimal point display). Each segment of a seven-segment LED display is a separate LED. According to the wiring of the LED units, seven-segment displays can be divided into common anode and common cathode displays.

In a common cathode 7-segment display, all the cathodes (or negative terminals) of the segment LEDs are connected together. You should connect the common cathode to GND. To light up a segment LED, you can set its associated pin to HIGH.

In a common anode 7-segment display, all the anodes (positive terminals) of the segment LEDs are connected together. You should connect the common anode to +5V. To light up a segment LED, you can set its associated pin to LOW.

Image not found

Each segment of the display consists of an LED. So when you use it, you also need to use a current-limiting resistor. Otherwise, the LED will be burned out. In this experiment, we use a common common cathode single-digit seven-segment display. As we mentioned above, you should connect the common cathode to GND. To light up a segment LED, you can set its associated pin to HIGH.

4. Project Wiring Diagram:

Note: The orientation of the seven-segment display inserted into the breadboard should match the wiring diagram, with an extra dot at the bottom right.

Image not found

Image not found

5. Code Description:

Img

Initialize the pins of the single-digit seven-segment display, 2D arrays, etc.

Img

This is the single-digit seven-segment display instruction block for showing numbers; it can display 1 digit: 0 ~ 9.

6. Project Code:

Numbers are displayed in 7 segments, and the decimal point is displayed in 1 segment. When displaying certain numbers, the corresponding segments will be lit. For example, when displaying the number 1, segments b and c will be turned on.

You can open the code we provided, or you can write the code yourself, as follows:

  1. Drag out ""Img"" from ""Img"".

  2. Drag out " Img " from ""Img"" and place it into ""Img"".

  3. Drag out " Img " from " Img ", change number 0 to 9.

  4. Drag out ""Img"" from ""Img"", set delay to 1000 milliseconds.

  5. Copy the code block ""Img"" 9 times, change number 9 to 8, 7, 6, 5, 4, 3, 2, 1, 0 respectively.

Complete code:

Img

7. Project Phenomenon:

After the code is uploaded successfully and powered on via the USB cable, what you will see is: the single-digit seven-segment display will show numbers from 9 to 0.

Image not found

Project 09 Four-Digit Seven-Segment Display

1. Project Introduction:

A four-digit seven-segment display is a very practical display device. It is needed for electronic clock displays, scoreboards on sports fields, and people counters in parks. Due to its low price and ease of use, more and more projects will use 4-digit seven-segment displays. In this project, we use the ESP32 to control a four-digit seven-segment display to show four-digit numbers.

2. Project Components:

Image not found Image not found Image not found
ESP32*1 Breadboard*1 Four-digit seven-segment display*1
Image not found Image not found
Female-to-male Dupont wires*4 USB cable*1

3. Component Knowledge:

TM1650 Four-Digit Seven-Segment Display: It is a 12-pin four-digit common cathode display module with a clock point (0.36 inches). The driver chip is TM1650, and only 2 signal wires are needed for a microcontroller to control the four-digit display. The control interface level can be 5V or 3.3V.

G: Power negative terminal

V: Power positive terminal, +5V

DIO: Data IO module, can be connected to any digital pin

CLK: Clock pin, can be connected to any digital pin

4-digit seven-segment display module specifications:

Operating voltage: DC 3.3V-5V

Operating current: ≤100MA

Maximum power: 0.5W

Display color: Red

4-digit seven-segment display module schematic:

Image not found

4. Project Wiring Diagram:

Image not found

5. Code Description:

Img

Initialize the pins of the TM1650 four-digit seven-segment display module.

Img

Turn on/off or clear the four-digit seven-segment display module.

Img

The four-digit seven-segment display module shows a 4-digit string (numbers or letters, etc.).

6. Project Code:

You can open the code we provided, or you can write the code yourself, as follows:

  1. Drag out ""Img"" from ""Img"".

  2. Drag out ""Img"" and ""Img"" from ""Img"" and place them into ""Img"". The CLK pin is 22 and the DIO pin is 21.

Img

  1. First, drag out ""Img"" from ""Img"", change string 1234 to 0000; then drag out ""Img"" from ""Img"", set delay to 1000 milliseconds.

Img

  1. Copy the code block ""Img"" 9 times, change string 0000 to 1111, 2222, 3333, 4444, 5555, 6666, 7777, 8888, 9999 respectively.

Complete code:

Img

7. Project Phenomenon:

After the code is uploaded successfully and powered on via the USB cable, what you will see is: the four-digit seven-segment display shows four-digit numbers 0000-9999, repeating these actions in an infinite loop.

Image not found

Project 10 8×8 Dot Matrix Display

1. Project Introduction:

A dot matrix display is an electronic digital display device that can show information on machines, clocks, public transport departure indicators, and many other devices. In this project, we will use the ESP32 to control an 8x8 LED dot matrix to display patterns.

2. Project Components:

Image not found Image not found Image not found
ESP32*1 Breadboard*1 8×8 dot matrix display*1
Image not found Image not found
Dupont wires (several) USB cable*1

3. Component Knowledge:

8×8 dot matrix display module: The 8×8 dot matrix consists of 64 LEDs, each placed at the intersection of a row and a column. When using a microcontroller to drive an 8×8 dot matrix, we need a total of 16 digital ports, which greatly wastes microcontroller resources. For this reason, we specially designed this module, using the HT16K33 chip to drive one 8×8 dot matrix. It only needs the microcontroller's I2C communication port to control the dot matrix, greatly saving microcontroller resources.

8×8 dot matrix display module specifications:

Operating voltage: DC 5V

Operating current: ≤200MA

Maximum power: 1W

8×8 dot matrix display module principle:

As shown in the schematic, if you want to light up the LED in the first row and first column, you only need to set C1 to high level and R1 to low level, and it will light up. If we want to light up all the LEDs in the first row, we set R1 to low level and C1~C8 all to high level. The principle is very simple. But this way we need a total of 16 IO ports, which greatly wastes microcontroller resources. For this reason, we specially designed this module, using the HT16K33 chip to drive one 8*8 dot matrix. It only needs the microcontroller's I2C communication port to control the dot matrix, greatly saving microcontroller resources.

Image not found

There are two rows of pins on the back of the module: one side is IN, and the other side is OUT. The main board connects to the IN side, and control signals are input through it; the OUT row of pins is used for cascading multiple modules, i.e., connecting to the IN of the second module.

Image not found

4. Project Wiring Diagram:

Image not found

No. Dot Matrix Module Main Board Description
1 VCC 3.3V Power positive terminal 3.3V or 5V
2 GND GND Power negative terminal
3 DIN 23 Data input (MOSI)
4 CS 5 Chip select signal (active low)
5 CLK 18 Clock signal (SCK)

5. Code Description:

Img

Initialize the address and pins of the HT16K33 8×8 dot matrix display module.

Img

Clear the HT16K33 8×8 dot matrix display module.

Img

You can set patterns for the HT16K33 8×8 dot matrix display. Each gray dot corresponds to an LED on the module. Check a gray dot to light up the corresponding LED on the module.

6. Project Code:

You can open the code we provided, or you can write the code yourself, as follows:

  1. Drag out ""Img"" from ""Img"".

  2. Drag out ""Img"" and ""Img"" from ""Img"" and place them into ""Img"". The SDA pin is 21, the SCL pin is 22, and the default address is 0x70.

Img

  1. Drag out ""Img"" from ""Img"", click the gray dots (√) to set the display pattern.

Img

Complete code:

Img

7. Project Phenomenon:

After the code is uploaded successfully and powered on via the USB cable, what you will see is: the 8*8 dot matrix display shows a "heart" pattern.

Image not found

Project 11 Shift Register Controlling 8 LEDs

1. Project Introduction:

In the previous project, we have learned how to light up an LED.

Usually, the number of IO ports on a development board is limited. If you need to control a large number of devices, you may run out of IO ports. For example, in Project 2, each light requires one IO port to control. If you need to control multiple lights, you will run out of IO ports. To deal with this situation, we introduce a shift register to expand the IO ports. In this project, we will use the ESP32, 74HC595 chip, and LEDs to make a flowing light to understand the function of the 74HC595 chip.

2. Project Components:

Image not found Image not found Image not found Image not found
ESP32*1 Breadboard*1 74HC595N chip*1 Red LED*8
Image not found Image not found Image not found
220Ω Resistor*8 Jumper Wires (several) USB cable*1

3. Component Knowledge:

Image not found

74HC595N chip: This chip has an 8-bit shift register and a memory, as well as tri-state output capability. Simply put, its main function is to "use a small number of I/O ports to expand multiple output ports." Under normal circumstances, controlling 8 LEDs requires 8 GPIO pins of the ESP32. But through the 74HC595N, you only need to occupy 3 GPIO pins to easily control 8 outputs, and you can also expand infinitely through cascading.

If you want to learn more, you can check the relevant resources Click to jump

Img

Pin Description:

Pin: Pin Description:
Pin 13 OE Is an output enable pin, used to ensure whether the latch data is input to the Q0-Q7 pins. At low level, high level is not output. In this experiment, we directly connect it to GND, keeping it at low level to output data.
Pin 14 SI/DS This is the pin where 74HC595 receives data, i.e., the serial data input. Only one bit can be input at a time, so 8 consecutive inputs make up one byte.
Pin 10 SCLR/MR A pin to initialize the storage register pin. At low level, it initializes the internal storage register. In this experiment, we connect it to VCC to keep it at high level.
Pin 11 SCK/SH_CP The clock pin of the shift register. On the rising edge, the data in the shift register shifts backward as a whole and receives new data input.
Pin 12 RCK/ST_CP The clock input pin of the storage register. On the rising edge, data is transferred from the shift register to the storage register. At this point, the data is output in parallel from the Q0~Q7 ports.
Pin 9 SQH This pin is a serial output pin, specifically used for chip cascading, connected to the SI pin of the next 74HC595.
Q0--Q7 (Pin 15, Pins 1-7) 8-bit parallel output terminals, which can directly control the 8 segments of a digital display.

4. Project Wiring Diagram:

Note: Pay attention to the orientation of the 74HC595N chip when inserting it.

Image not found

Image not found

5. Project Code:

You can open the code we provided, or you can write the code yourself, as follows:

  1. Drag out ""Img"" from ""Img"".

  2. First, drag out ""Img"" 3 times from " Img " and place them into ""Img""; then drag out ""Img"" 3 times from ""Img"" and place them into ""Img"", change item to dataPin, latchPin, clockPin respectively, and change the corresponding assigned values from 0 to 14, 12, 13.

Img

  1. Drag out ""Img"" from ""Img"", change from 1 to 10 step 1 to from 0 to 255 step 1.

  2. Drag out ""Img"", ""Img"", ""Img"" from ""Img"" and place them into " Img ", then drag out ""Img"", ""Img"", ""Img"", ""Img"", ""Img"" from ""Img"" and place them in order; change the first pin latchPin from "HIGH" to "LOW", and change "MSB first" to "LSB first".

Img

  1. Drag out ""Img"" from ""Img"" and place it into ""Img"", set delay to 100 milliseconds.

Complete code:

Img

6. Project Phenomenon:

After the code is uploaded successfully and powered on via the USB cable, what you will see is: 8 LEDs start flashing in a flowing pattern.

Image not found

Project 12 Active Buzzer

1. Project Introduction:

An active buzzer is a sound-producing component. It is widely used as a sound-producing element in computers, printers, alarms, electronic toys, telephones, timers, etc. It has an internal vibration source and only needs to be connected to a 5V power supply to continuously emit a buzzing sound. In this project, we will use the ESP32 to control the active buzzer to emit a "beep beep" sound.

2. Project Components:

Image not found Image not found Image not found Image not found
ESP32*1 Breadboard*1 Active buzzer*1 NPN transistor (S8050)*1
Image not found Image not found Image not found
1KΩ Resistor*1 Jumper Wires (several) USB cable*1

3. Component Knowledge:

Image not found

Active buzzer: It has a simple internal oscillator circuit that converts constant DC power into a pulse signal of a specific frequency. Once the active buzzer receives a high level, it will produce sound. A passive buzzer, on the other hand, is an integrated electronic buzzer without an internal vibration source. It must be driven by a 2K-5K square wave, not a DC signal. The appearance of these two buzzers is very similar, but a buzzer with a green circuit board is a passive buzzer, while the one with black tape is an active buzzer. A passive buzzer cannot distinguish between positive and negative polarity, while an active buzzer can. As shown below:

Image not found

Image not found

Transistor: Since the buzzer requires a large current, the GPIO output capability of the ESP32 cannot meet the requirement. An NPN transistor is needed here to amplify the current. A transistor, full name: semiconductor transistor, is a semiconductor device that controls current. A transistor can be used to amplify weak signals or as a switch. It has three electrodes (pins): base (b), collector (c), and emitter (e). When current flows between "be", "ce" will allow several times the current to pass through (transistor amplification), and at this time, the transistor operates in the amplification region. When the current between "be" exceeds a certain value, "ce" will no longer allow the current to increase, and at this time, the transistor operates in the saturation region. There are two types of transistors as shown below: PNP and NPN.

Image not found

In our kit, the PNP transistor is marked 8550, and the NPN transistor is marked 8050.

Based on the characteristics of transistors, they are often used as switches in digital circuits. Since the microcontroller's current output capability is weak, we will use a transistor to amplify the current and drive high-current components. When using an NPN transistor to drive a buzzer, the following method is usually adopted: if the GPIO outputs a high level, current will flow through R1, the transistor will conduct, and the buzzer will sound. If the GPIO outputs a low level, no current flows through R1, the transistor will not conduct, and the buzzer will not sound. When using a PNP transistor to drive a buzzer, the following method is usually adopted: if the GPIO outputs a low level, current will flow through R1, the transistor will conduct, and the buzzer will sound. If the GPIO outputs a high level, no current flows through R1, the transistor will not conduct, and the buzzer will not sound.

Image not found

4. Project Wiring Diagram:

Image not found

Note: The buzzer in this circuit is powered by 5V. At 3.3V, the buzzer can work but with reduced volume.

5. Code Description:

Img

Set a specified pin to "HIGH" or "LOW". Selecting "HIGH" sets the pin to a high level, with voltage and current, and the active buzzer sounds; selecting "LOW" sets the pin to a low level, with no voltage and no current, and the active buzzer does not sound.

6. Project Code:

You can open the code we provided, or you can write the code yourself, as follows:

  1. Drag out ""Img"" from ""Img"".

  2. Drag out " Img " from ""Img"" and place it into ""Img"", set pin to 15, set to "LOW".

  3. Drag out " Img " from ""Img"", set pin to 15, set to "HIGH".

  4. Drag out ""Img"" from ""Img"", set delay to 500 milliseconds.

  5. Copy the code block " Img " 1 time, change "HIGH" to "LOW".

Complete code:

Img

7. Project Phenomenon:

After the code is uploaded successfully and powered on via the USB cable, what you will see is: the active buzzer emits a "beep beep" sound.

Image not found

Project 13 Passive Buzzer

1. Project Introduction:

In the previous project, we studied the active buzzer, which can only produce one sound and may feel monotonous. This project will learn about another type of buzzer, the passive buzzer. Unlike the active buzzer, the passive buzzer can produce sounds of different frequencies. In this project, you will use the ESP32 to control the passive buzzer.

2. Project Components:

Image not found Image not found Image not found Image not found
ESP32*1 Breadboard*1 Passive buzzer*1 NPN transistor (S8050)*1
Image not found Image not found Image not found
1KΩ Resistor*1 Jumper Wires (several) USB cable*1

3. Component Knowledge:

Image not found

Passive buzzer: It is an integrated electronic buzzer without an internal vibration source. It must be driven by a 2K-5K square wave, not a DC signal. The appearance is very similar to an active buzzer, but a buzzer with a green circuit board is a passive buzzer, while the one with black tape is an active buzzer. A passive buzzer cannot distinguish between positive and negative polarity, while an active buzzer can.

Image not found

Transistor: Please refer to Project 12.

4. Project Wiring Diagram:

Image not found

5. Code Description:

Img

Turn off the passive buzzer on a specified pin, so the buzzer does not sound.

Img

Set the tone and beat of the passive buzzer on a specified pin.

Img

Set the passive buzzer on a specified pin to play specific music.

6. Project Code:

You can open the code we provided, or you can write the code yourself, as follows:

  1. Drag out ""Img"" from ""Img"".

  2. Drag out " Img " from ""Img"" and place it into ""Img"", set pin to 15.

  3. First, drag out " Img " from ""Img"", set pin to 15, select "Christmas Song"; then drag out " Img ", set pin to 15.

  4. Drag out ""Img"" from ""Img"", set delay to 2000 milliseconds.

  5. Copy the code block " Img " 1 time, select "Happy Birthday Song".

Complete code:

Img

7. Project Phenomenon:

After the code is uploaded successfully and powered on via the USB cable, what you will see is: the passive buzzer plays music.

Image not found

Project 14 Small Desk Lamp

1. Project Introduction:

Did you know that the ESP32 can light up an LED when you press an external button? In this project, we will use the ESP32, a button switch, and an LED to make a mini desk lamp.

2. Project Components:

Image not found Image not found Image not found
ESP32*1 Breadboard*1 Button*1
Image not found Image not found Image not found
10KΩ Resistor*1 Red LED*1 220Ω Resistor*1
Image not found Image not found Image not found
Button cap*1 Jumper Wires (several) USB cable*1

3. Component Knowledge:

Image not found

Button: A button can control the on/off state of a circuit. When connected in a circuit, the circuit is open when the button is not pressed. Once the button is pressed, the circuit is closed, but it opens again when released. But why does it only conduct electricity when pressed? This has to do with the internal structure of the button. Before pressing, current cannot pass from one end of the button to the other; when pressed, the metal piece inside the button connects both sides to let current pass through.

The internal structure of the button is shown in the figure: Image not found. Before pressing, 1 and 2 are connected, 3 and 4 are also connected, but 1-3, 1-4, 2-3, and 2-4 are disconnected; only when the button is pressed, 1-3, 1-4, 2-3, and 2-4 become connected.

In circuit design, button switches are one of the most commonly used components.

Button schematic:

Image not found

What is button debouncing?

The switch circuit we imagine is "press the button - conducts immediately" and "press again - disconnects immediately," but this is not the case in reality.

Buttons usually use mechanical elastic switches. At the moment the mechanical contacts open and close (usually around 10ms), a series of bounces are produced due to the elastic action, causing the button switch to not immediately conduct the circuit stably when closing, nor to disconnect instantly when opening.

Image not found

How to eliminate button bounce?

There are two common debouncing methods: software method and hardware method. Here we focus on the convenient and simple software method.

We already know that the bounce time caused by elastic inertia is about 10ms. Using a delay command to postpone the execution time of the command can achieve the debouncing effect.

So we added a 0.02-second delay in the code to achieve button debouncing.

Image not found

4. Project Wiring Diagram:

Image not found

Note:

How to connect the LED

Image not found

How to identify a five-color band 220Ω resistor and a five-color band 10KΩ resistor

Image not found

Image not found

5. Code Description:

Img

Read the digital signal (HIGH/LOW) of a button switch from a specified digital pin.

Img

A boolean (bool) variable has only two values: true and false. In C++, if the value is non-zero it is True, and if it is zero it is False. Here we can see that the initial value of ledState is 0.

Img

Invert the current value of ledState and assign it back to ledState itself.

Img

The purpose of the delay here is software debouncing. When the mechanical contacts of the button open and close, due to the elastic action of the contacts, the button switch does not immediately conduct or disconnect stably. Both the closing and opening moments are accompanied by a series of bounces. The measure taken to prevent this phenomenon is button debouncing. After the code detects that the button is closed, it executes a delay program with a 10ms delay, allowing the leading edge bounce to disappear before detecting the button state again. If it remains at the closed state level, it is confirmed that the button is truly pressed.

6. Project Code:

You can open the code we provided, or you can write the code yourself, as follows:

  1. Drag out ""Img"" from ""Img"".

  2. Drag out " Img " from ""Img"" and place it into ""Img"", set pin to 4, set to "LOW".

Img

  1. First, drag out ""Img"" from " Img" and place it into ""Img"", change "Integer" to "Boolean" and "item" to "ledState"; then drag out ""Img"" from ""Img"" and place it into ""Img"", select "False".

Img

  1. First, drag out ""Img"" from ""Img""; then drag out ""Img"" from ""Img"" and place it into ""Img""; then drag out " Img " from ""Img"" and place it on the left side of "=", set pin to 15; finally, drag out ""Img"" from ""Img"" and place it on the right side of "=".

Img

  1. Drag out ""Img"" from ""Img"", set delay to 10 milliseconds.

Img

  1. First, copy the code block " Img " 1 time, change the delay from 10 milliseconds to 200 milliseconds; then copy the code block " Img " 1 time, change the number 0 to 1.

Img

  1. Copy the code block ""Img"" 1 time, remove the 10ms delay, then drag out ""Img"" from " Img", then drag out ""Img"" from " Img ", and finally drag out " Img " from " Img".

Img

  1. Drag out " Img " from ""Img"", set pin to 4, then drag out " Img " from " Img" and place it at the "HIGH" position.

Img

Complete code:

Img

7. Project Phenomenon:

After the code is uploaded successfully and powered on via the USB cable, what you will see is: when you press the button, the LED lights up; when you release the button, the LED stays on. Press the button again, the LED turns off; when you release the button, the LED stays off. Isn't it like a small desk lamp?

Image not found

Project 15 Simulated Hourglass

1. Project Introduction:

In ancient times, people did not have electronic clocks, so they invented hourglasses to measure time. An hourglass has a large capacity on both sides, with fine sand on one side and a very small channel in the middle. When the hourglass is stood upright with the sand side on top, the fine sand flows down through the channel to the other side due to gravity. When all the sand has flowed to the bottom, it is turned over, and by recording the number of repetitions in a day, the next day you can know the approximate time of the day by the number of times the sand has flowed back and forth. In this lesson, we will use the ESP32 to control a tilt switch and LED lights to simulate an hourglass, making an electronic hourglass.

2. Project Components:

Image not found Image not found Image not found Image not found
ESP32*1 Breadboard*1 Tilt switch*1 10KΩ Resistor*1
Image not found Image not found Image not found Image not found
Red LED*4 220Ω Resistor*1 Jumper Wires (several) USB cable*1

3. Component Knowledge:

Image not found

A tilt switch is also called a digital switch or ball switch, with a metal ball inside. It is used to detect small angles of tilt.

The principle is simple: when the switch is tilted at a certain angle, the ball inside rolls down and touches the two contacts connected to the external pins, thereby triggering the circuit. Otherwise, the ball stays away from the contacts, disconnecting the circuit.

Here we use the internal structure of the tilt switch to illustrate how it works, as shown in the figure below:

Image not found

4. Project Wiring Diagram:

Image not found

Note:

How to connect the LED

Image not found

How to identify a five-color band 220Ω resistor and a five-color band 10KΩ resistor

Image not found

Image not found

5. Code Description:

Img

Read the digital signal (HIGH/LOW) of the tilt switch from a specified digital pin.

6. Project Code:

You can open the code we provided, or you can write the code yourself, as follows:

  1. Drag out ""Img"" from ""Img"".

  2. Drag out ""Img"" from ""Img"" and place it into ""Img"".

  3. First, drag out ""Img"" from " Img" and place it into ""Img""; then drag out ""Img"" from ""Img"" and place it into ""Img"", change "Integer" to "Byte" and "item" to "switch_state".

Img

  1. First, drag out ""Img"" from ""Img"", then drag out " Img " from ""Img"", set pin to 15.

Img

  1. First, drag out " Img " from ""Img"", then drag out ""Img"" from " Img".

Img

  1. First, drag out ""Img"" from ""Img""; then drag out ""Img"" from ""Img"" and place it into ""Img""; then drag out ""Img"" from " Img" and place it on the left side of "="; finally, drag out ""Img"" from ""Img"" and place it on the right side of "=".

Img

  1. First, drag out " Img " from ""Img"" and place it into " Img ", change from 1 to 10 step 1 to from 16 to 19 step 1; then drag out " Img " from ""Img"" and place it into " Img "; then drag out " Img " from " Img" and place it where it says "Pin 0"; add a delay of 500 milliseconds.

Img

  1. Copy the code block " Img " 1 time, change the number 0 on the right side of "=" to 1, change from 16 to 19 step 1 to from 19 to 16 step -1, and change "HIGH" to "LOW".

Img

Complete code:

Img

7. Project Phenomenon:

After the code is uploaded successfully and powered on via the USB cable, what you will see is: when the breadboard is tilted to a certain angle, the LEDs will light up one by one. When returned to the previous angle, the LEDs will turn off one by one. Just like an hourglass, as time passes, the sand leaks out.

Image not found

Project 16 Anti-Theft Alarm

1. Project Introduction:

A PIR (Passive Infrared) sensor measures the heat infrared (IR) rays emitted by moving objects. This sensor can detect the movement of people, animals, and cars, triggering security alarms and lighting. They are used to detect movement and are ideal for security applications such as burglar alarms and security lighting systems. In this project, we will use the ESP32 to control a PIR sensor, buzzer, and LED to simulate a burglar alarm.

2. Project Components:

Image not found Image not found Image not found Image not found
ESP32*1 Breadboard*1 PIR sensor*1 NPN transistor (S8050)*1
Image not found Image not found Image not found Image not found
Active buzzer*1 Red LED*1 220Ω Resistor*1 1KΩ Resistor*1
Image not found Image not found Image not found
Dupont wires (several) Jumper wires (several) USB cable*1

3. Component Knowledge:

PIR sensor: It is a PIR sensor based on the pyroelectric effect, capable of detecting infrared rays emitted by the human body or animals. Combined with a Fresnel lens, it can detect over a longer and wider range. It mainly uses the RE200B-P sensor element. When a person or animal moves nearby, the module outputs a high level 1; otherwise, it outputs a low level 0. Note: this sensor can detect moving people, animals, and cars, but cannot detect stationary people, animals, and cars. The maximum detection distance is about 7 meters.

Note: The PIR sensor should be kept away from direct sunlight, car headlights, and incandescent lamps, and should not face heat sources (such as radiators, heaters) or air conditioners, to avoid false alarms caused by large changes in ambient temperature. It is also susceptible to RF radiation interference.

Sensor technical parameters:

Maximum input voltage: DC 5-15V

Maximum operating current: 50MA

Maximum power: 0.3W

Static current: <50uA

Operating temperature: -20 ~ 85℃

Control signal: Digital signal (1/0)

Delay time: Approximately 2.3 to 3 seconds

Sensing angle: Less than 100 degree cone angle

Maximum detection distance: About 7 meters

Sensor schematic:

Image not found

4. Project Wiring Diagram:

Image not found

  1. Code Description:

Img

Read the digital signal (HIGH/LOW) of the PIR sensor from a specified digital pin, used to detect whether a person or animal is moving nearby. If movement is detected, it outputs a high level 1; otherwise, it outputs a low level 0.

6. Project Code:

You can open the code we provided, or you can write the code yourself, as follows:

  1. Drag out ""Img"" from ""Img"".

  2. Drag out ""Img"" from ""Img"" and place it into ""Img"".

  3. First, drag out ""Img"" from " Img" and place it into ""Img"", change "Integer" to "Byte" and "item" to "pirStat"; then drag out ""Img"" from ""Img"" and place it into ""Img"".

Img

  1. First, drag out ""Img"" from ""Img"", then drag out " Img " from ""Img"", set pin to 15.

Img

  1. First, drag out " Img " from ""Img"", then drag out " Img " from " Img".

Img

  1. First, drag out ""Img"" from ""Img""; then drag out ""Img"" from ""Img"" and place it into ""Img""; then drag out " Img " from " Img" and place it on the left side of "="; finally, drag out ""Img"" from ""Img"" and place it on the right side of "=", change the number 0 to 1.

Img

  1. Drag out " Img " and " Img " from ""Img"" and place them into " Img ". The red LED pin is 0 and the active buzzer pin is 2; then drag out ""Img"" from ""Img"", set delay to 500 milliseconds.

Img

  1. Copy the code block " Img " 1 time and place it into " Img ", change both "HIGH" to "LOW".

Img

  1. Copy the code block " Img " 1 time, change the number 1 on the right side of "=" to 0, delete the code block " Img " and the two 500ms delay blocks.

Img

Complete code:

Img

7. Project Phenomenon:

After the code is uploaded successfully and powered on via the USB cable, what you will see is: if the PIR sensor detects movement of a person nearby, the buzzer will continuously sound the alarm and the LED will flash continuously.

Image not found

Project 17 OLED Display

1. Project Introduction:

In life, we can use display modules for various experiments. You can also DIY various small objects. For example, use a temperature sensor and a display to make a temperature tester, or use an ultrasonic module and a display to make a distance tester. Below, we will use the LCD_128X32_DOT module as a display, connecting it to the ESP32 control board. The ESP32 board will control the LCD_128X32_DOT display to show various English text, common symbols, and numbers.

Note: We have upgraded the LCD screen to a more commonly used OLED screen. The documentation is being updated. If you find that the code does not run correctly, please contact customer service for repair.

2. Project Components:

Image not found Image not found Image not found
ESP32*1 Breadboard*1 OLED screen*1
Image not found Image not found
Female-to-male Dupont wires*4 USB cable*1

3. Component Knowledge:

Image not found

LCD_128X32_DOT: A 128*32 pixel LCD module with the driver chip ST7567A. The module uses IIC communication. It can display not only English letters and symbols, but also Chinese characters and patterns. When in use, you can also set it in the code to display English letters and symbols in different sizes.

LCD_128X32_DOT schematic:

Image not found

LCD_128X32_DOT technical parameters:

Display pixels: 128*32 characters

Operating voltage: DC 5V

Operating current: 100mA (5V)

Module optimal operating voltage: 5V

Brightness and contrast can be controlled via program commands

4. Project Wiring Diagram:

Image not found

6. Project Code:

You can open the code we provided, or you can write the code yourself, as follows:

  1. The display control position is as follows, drag it out and use it directly:

Img

  1. The complete code is as follows:

Img

7. Project Phenomenon:

After the code is uploaded successfully and powered on via the USB cable, what you will see is: the 128X32 LCD module display shows "Hello World!" on the first line, "0123456789" on the second line, and "Using SoftI2C" on the third line.

Image not found

Project 18 Small Fan

1. Project Introduction:

In the hot summer, we need electric fans to cool us down. In this project, we will use the ESP32 to control a 130 motor module and small fan blades to make a small electric fan.

2. Project Components:

Image not found Image not found Image not found
ESP32*1 Breadboard*1 130 motor module*1
Image not found Image not found Image not found
Breadboard power module*1 6-cell AA battery box*1 Fan blade*1
Image not found Image not found Image not found
Dupont wires (several) AA batteries (self-provided)*6 USB cable*1

3. Component Knowledge:

Image not found

130 motor module: This motor control module uses the HR1124S motor control chip. HR1124S is a single-channel H-bridge driver chip for DC motor solutions. The H-bridge driver section of HR1124S uses low on-resistance PMOS and NMOS power transistors. The low on-resistance ensures low power loss, allowing the chip to operate safely for a longer time. In addition, HR1124S has low standby current and low static operating current, making the 130 motor module suitable for toy solutions.

130 motor module parameters:

Operating voltage: 5V

Operating current: ≤200MA

Operating power: 2W

Operating temperature: -10℃~+50℃

130 motor module principle:

The HR1124S chip is used to drive the motor. Since the motor requires a large current, it cannot be driven by a transistor or directly by an IO port. In future project development, motor driver modules are almost always used to drive motors, rather than developing a motor driver from scratch. There are two control methods: one is high/low level control (controlling rotation and stop), and the other is PWM control (controlling speed).

Image not found

Breadboard power module:

Image not found

Description:

This module conveniently provides 3.3V and 5V power to the breadboard. It has a DC2.1 input (DC7-12V) and also features a USB Type-C power input.

Specifications:

Input voltage: DC jack: 7-12V; Type C USB: 5V

Current: 3.3V: max 500mA; 5V: max 500mA

Maximum power: 2.5W

Dimensions: 53mm x 26.3mm

Environmental compliance: ROHS

Interface description:

Image not found

Schematic:

Image not found

4. Project Wiring Diagram:

Image not found

You can also power directly from the development board by plugging in USB, using the simplified wiring as follows (note: the fan will start spinning after the program is uploaded, so it's best to hold it in your hand):

Image not found

(Note: Connect the wires first, then install a small fan blade on the DC motor.)

5. Code Description:

Img

Img

These 2 blocks can both be used to set the DC motor (small fan) to stop.

Img

Set the DC motor (small fan) to rotate counterclockwise.

Img

Set the DC motor (small fan) to rotate clockwise.

6. Project Code:

You can open the code we provided, or you can write the code yourself, as follows:

  1. Drag out ""Img"" from ""Img"".

  2. Drag out ""Img"" from ""Img"" and place it into ""Img"". The INA pin is 15, the INB pin is 2, change both "HIGH" to "LOW".

Img

  1. First, drag out ""Img"" from ""Img"", the INA pin is 15, the INB pin is 2, change the "HIGH" after INB to "LOW", then drag out ""Img"" from ""Img"", set delay to 5000 milliseconds.

Img

  1. Copy the code block " Img " 1 time, change the "HIGH" after INA to "LOW", change the 5000ms delay to 2000ms.

Img

  1. Copy the code block " Img " 1 time, change INA "HIGH" INB "LOW" to INA "LOW" INB "HIGH", keep everything else unchanged.

Img

Complete code:

Img

7. Project Phenomenon:

After the code is uploaded successfully and powered on via external power, what you will see is: the small fan first rotates counterclockwise for 5 seconds, stops for 2 seconds, then rotates clockwise for 5 seconds, stops for 2 seconds, repeating this pattern.

Image not found

Project 19 Servo

1. Project Introduction:

A servo is a motor that can rotate very precisely. It is currently widely used in toy cars, remote control helicopters, airplanes, robots, and other fields. In this project, we will use the ESP32 to control the rotation of a servo.

2. Project Components:

Image not found Image not found Image not found
ESP32*1 Breadboard*1 Servo*1
Image not found Image not found
Jumper wires (several) USB cable*1

3. Component Knowledge:

Image not found

Servo: A servo is a position servo driver, mainly composed of a housing, circuit board, coreless motor, gears, and a position detector. Its working principle is that a receiver or microcontroller sends a signal to the servo. Inside, there is a reference circuit that generates a 20ms period, 1.5ms width reference signal. The obtained DC bias voltage is compared with the voltage of a potentiometer to obtain a voltage differential output. The IC on the circuit board determines the direction of rotation and drives the coreless motor to start rotating. Power is transmitted to the swing arm through reduction gears, while the position detector sends back a signal to determine if the position has been reached. It is suitable for control systems where the angle needs to change continuously and be maintained. When the motor speed is constant, the reduction gear drives the potentiometer to rotate, making the voltage differential 0 and the motor stops. Generally, the rotation angle range of a servo is 0 to 180 degrees.

The pulse period for controlling a servo is 20ms, with a pulse width of 0.5ms ~ 2.5ms, corresponding to positions of -90° ~ +90°. Below is an example of a 180° servo:

Image not found

Servos come in various specifications, but they all have three connecting wires: brown, red, and orange (different brands may have different colors). Brown is GND, red is the power positive terminal, and orange is the signal wire.

Image not found

4. Project Wiring Diagram:

When powering the servo, please note that the power supply voltage should be 3.3V-5V. Please ensure there are no errors when connecting the servo to the power supply. Image not found

5. Code Description:

Img

Set the rotation angle and delay of the servo on a specified pin.

6. Project Code:

You can open the code we provided, or you can write the code yourself, as follows:

  1. Drag out ""Img"" from ""Img"".

  2. Drag out " Img " from " Img " and place it into ""Img"", set pin to 15, angle to 0, delay 200 milliseconds.

Img

  1. First, drag out " Img " from ""Img"" and change from 1 to 10 step 1 to from 0 to 180 step 1; then drag out " Img " from " Img " and place it into " Img ", set pin to 15; then drag out " Img " from " Img" and place it where it says "Angle 0"; delay 15 milliseconds.

Img

  1. Copy the code block " Img " 1 time, change from 0 to 180 step 1 to from 180 to 0 step -1.

Img

Complete code:

Img

7. Project Phenomenon:

After the code is uploaded successfully and powered on via the USB cable, what you will see is: the servo will rotate from 0° to 180°, then reverse direction to rotate from 180° to 0°, repeating these actions in an infinite loop.

Image not found

Project 20 Stepper Motor

1. Project Introduction:

Stepper motors have accurate positioning and are the most important components in industrial robots, 3D printers, large lathes, and other mechanical equipment. In this project, we will use the ESP32 to control a ULN2003 stepper motor driver board to drive a stepper motor.

2. Project Components:

Image not found Image not found Image not found
ESP32*1 Breadboard*1 ULN2003 stepper motor driver board*1
Image not found Image not found Image not found
Breadboard power module*1 6-cell AA battery box*1 Stepper motor*1
Image not found Image not found Image not found
Female-to-male Dupont wires (several) AA batteries (self-provided)*6 USB cable*1

3. Project Knowledge:

Image not found

Stepper motor: It is a motor controlled by a series of electromagnetic coils. It can rotate by precise degrees (or steps) as needed, allowing you to move it to a precise position and hold that position. It achieves this by energizing the coils inside the motor for very short periods of time, but you must keep the motor powered to hold it in the desired position. There are two basic types of stepper motors: unipolar and bipolar. In this project, we use a unipolar stepper motor 28-BYJ48.

Image not found

28BYJ-48 stepper motor working principle:

A stepper motor is mainly composed of a stator and a rotor. The stator is the stationary part, as shown in the figure below, the part with A, B, C, D coil groups wound around it. When the coil groups are energized, they produce a magnetic field. The rotor is the rotating part, the part in the middle of the stator, with two poles being permanent magnets.

Single-step 4-beat rotation principle: Initially, coil group A is energized, and the rotor poles face coil group A directly; then coil group A is de-energized, coil group B is energized, and the rotor rotates clockwise to coil group B, completing one step; B is de-energized, C is energized, the rotor rotates to C; C is de-energized, D is energized, the rotor rotates to D; D is de-energized, A is energized, the rotor rotates to coil group A. This way the rotor has turned half a circle, 180 degrees. Then repeat once more, B-C-D-A, the rotor returns to coil group A, completing one full rotation with a total of 8 steps. As shown in the figure below, this is the single-beat rotation principle of a stepper motor: A - B - C - D - A ....

If you want the stepper motor to rotate counterclockwise, just reverse the beat sequence: D - C - B - A - D .....

Image not found

Half-step 8-beat rotation principle: 8 beats, using a single-double beat pattern: A - AB - B - BC - C - CD - D - DA - A ...... This way, for each beat, the rotor only rotates half a step. For example, coil group A is energized, the rotor turns to face coil group A directly; then A and B are energized together, so the strongest magnetic field is in the middle of AB coil groups, and the rotor poles turn to the middle of AB coil groups, i.e., rotating half a step clockwise.

Stepper motor parameters:

The stepper motor we provide requires 32 steps for the rotor to complete one rotation, and it also goes through a 1:64 reduction gear set to drive the output shaft. So the output shaft requires 32 * 64 = 2048 steps per rotation.

Voltage 5V, 4-phase stepper motor, 4-beat mode step angle is 11.25, 8-beat mode step angle is 5.625, reduction ratio is 1:64.

ULN2003 stepper motor driver board: ULN2003 stepper motor driver, which converts weak signals into stronger control signals to drive the stepper motor.

The schematic below shows how to connect a unipolar stepper motor to the ESP32 pins using the ULN2003 stepper motor driver board interface, and how to use four TIP120 interfaces.

Image not found

4. Project Wiring Diagram:

Set the power module jumper to 5V, connect the stepper motor driver board VCC to breadboard 5V, GND to breadboard GND;

Connect the development board pin 5V to breadboard 5V, GND to breadboard GND, or power directly via USB.

Development Board Pin Motor Driver Board Pin
15 IN1
16 IN2
17 IN3
18 IN4

Image not found

5. Code Description:

Img

Initialize the stepper motor pins, step count, and speed settings.

Img

Set the number of steps for the stepper motor to move; positive and negative values relate to the direction of rotation.

6. Project Code:

You can open the code we provided, or you can write the code yourself, as follows:

  1. Drag out ""Img"" from ""Img"".

  2. Drag out " Img " from " Img " and place it into ""Img"". Pin 1# is 15, pin 2# is 16, pin 3# is 17, pin 4# is 18, set steps to 100, set speed to 200.

Img

  1. First, drag out " Img " from " Img ", set the number of steps to -2048; then drag out ""Img"" from ""Img"", set delay to 1000 milliseconds.

Img

  1. Copy the code block " Img " 1 time, change -2048 to 2048.

Img

Complete code:

Img

7. Project Phenomenon:

After the code is uploaded successfully and powered on via external power, what you will see is: the four LEDs D1, D2, D3, D4 on the ULN2003 driver module light up, the stepper motor rotates, and this state loops continuously.

Image not found

Project 21 Relay

1. Project Introduction:

In daily life, we generally use AC power to drive electrical equipment, and sometimes we use switches to control appliances. If the switch is directly connected to the AC circuit, it can be dangerous if leakage occurs. For safety reasons, we have specially designed this relay module with NO (Normally Open) and NC (Normally Closed) terminals. In this lesson, we will learn about a special and useful switch, the relay module.

2. Project Components:

Image not found Image not found Image not found Image not found
ESP32*1 Breadboard*1 Relay module*1 Red LED*1
Image not found Image not found Image not found Image not found
Dupont wires (several) USB cable*1 220Ω Resistor*1 Jumper wires (several)

3. Component Knowledge:

Relay: It mainly uses the HK4100F-DC 5V-SHC relay component. The relay has 3 terminals for external circuits: NO, COM, and NC (silk-screened on the back).

Normally Open (NO): This pin is normally open unless a signal is provided to the relay module's signal pin. Therefore, the common contact pin disconnects its connection through the NC pin and establishes a connection through the NO pin.

Common Contact (COM): This pin is used to connect other modules/components, such as an LED.

LED: Image not found

Normally Closed (NC): This NC pin is connected through the COM pin, forming a closed circuit. You can control the relay module via a development board like the ESP32 to switch between closed and open.

When the relay has no control signal, the COM and NC terminals are connected, and the COM and NO terminals are disconnected.

For control, connect G to the control board's GND, V to the control board's 5V, and S to the control board's digital port. When the S signal terminal is set to high level, the relay is activated, the COM and NC terminals disconnect, and the COM and NO terminals connect; when the S signal terminal is set to low level, the relay is deactivated, the COM and NC terminals connect, and the COM and NO terminals disconnect.

  • Operating voltage: 5V (DC)
  • Operating current: ≤50MA
  • Maximum power: 0.25W
  • Input signal: Digital signal
  • Contact current: <3A

Relay schematic:

Image not found

4. Project Wiring Diagram:

Image not found

5. Code Description:

Img

Set a specified pin to "HIGH" or "LOW". Selecting "HIGH" sets the pin to a high level, with voltage and current, and the relay engages; selecting "LOW" sets the pin to a low level, with no voltage and no current, and the relay disengages.

6. Project Code:

You can open the code we provided, or you can write the code yourself, as follows:

  1. Drag out ""Img"" from ""Img"".

  2. Drag out " Img " from ""Img"" and place it into ""Img"", set pin to 15, set to "LOW".

  3. Drag out " Img " from ""Img"", set pin to 15, set to "HIGH".

  4. Drag out ""Img"" from ""Img"", set delay to 500 milliseconds.

  5. Copy the code block " Img " 1 time, change "HIGH" to "LOW".

Complete code:

Img

7. Project Phenomenon:

After the code is uploaded successfully and powered on via the USB cable, what you will see is: the relay will cycle on and off, with the LED on for 1 second and off for 1 second. You can also hear the relay clicking and see the indicator light on the relay showing the state changes.

Image not found

Project 22 Dimmable Light

1. Project Introduction:

A potentiometer is a three-terminal resistor with a sliding or rotating contact that forms an adjustable voltage divider. Its working principle is to change the position of the sliding contact on a uniform resistance. In a potentiometer, the entire input voltage is applied across the entire length of the resistor, and the output voltage is the voltage between the fixed contact and the sliding contact. In this project, we will learn to use the ESP32 to read the value of a potentiometer and combine it with an LED to make a dimmable light.

2. Project Components:

Image not found Image not found Image not found Image not found
ESP32*1 Breadboard*1 Potentiometer*1 Red LED*1
Image not found Image not found Image not found
220Ω Resistor*1 USB cable*1 Jumper wires (several)

3. Component Knowledge:

Image not found

Potentiometer: A potentiometer is a type of resistor and analog electronic component with two states, 0 and 1 (high level and low level). Different analog values correspond to data states such as 1 ~ 1024 in a linear manner.

ADC: ADC is an electronic integrated circuit used to convert analog signals (such as voltage) into digital or binary form consisting of 1s and 0s. The ADC range on our ESP32 is 12 bits, meaning the resolution is 2^12 = 4096, which means a range (3.3V) will be evenly divided into 4096 parts. The analog value range corresponds to the ADC value. Therefore, the more bits the ADC has, the denser the analog divisions, and the higher the final conversion accuracy.

Image not found

Segment 1: Analog values in the range 0V—(3.3/4095)V correspond to digital 0;

Segment 2: Analog values in the range (3.3/4095)V—2*(3.3/4095)V correspond to digital 1;

......

The following analog values will be divided accordingly. The conversion formula is as follows:

Image not found

DAC: The reverse of this process requires a DAC, a digital-to-analog converter. Digital I/O ports can output high and low levels (0 or 1), but cannot output intermediate voltage values, which is where DACs are useful. The ESP32 has two 8-bit DAC output pins, GPIO25 and GPIO26, which can divide VCC (here 3.3V) into 2^8 = 256 parts. For example, when the digital value is 1, the output voltage is 3.3/256×1V; when the digital value is 128, the output voltage is 3.3/256 × 128 = 1.65V. The higher the DAC precision, the higher the output voltage accuracy.

The conversion formula is as follows:

Image not found

ADC on the ESP32 board:

The ESP32 has 16 pins that can be used to measure analog signals. The GPIO pin numbers and analog pin definitions are shown in the table below:

Image not found

DAC on the ESP32 board:

Image not found

4. Reading the Analog Value of the Potentiometer:

We connect the potentiometer to the analog IO port of the ESP32 to read its analog value. Please refer to the following wiring diagram:

Image not found

Code Description:

Img

Read the analog signal of the potentiometer from a specified analog pin; the analog signal range is: 0 ~ 4095.

For more details, please refer to the link: https://vimsky.com/examples/usage/arduino-language-functions-analog-io-analogread-ar.html.

Img

Map the analog signal of the potentiometer (0 ~ 4095) to the brightness of the LED (0 ~ 255).

You can open the code we provided, or you can write the code yourself, as follows:

  1. Drag out ""Img"" from ""Img"".

  2. Drag out ""Img"" from ""Img"" and place it into ""Img"", set baud rate to 115200.

  3. First, drag out ""Img"" from ""Img""; then drag out " Img " from " Img ", set pin to 36.

  4. Drag out ""Img"" from ""Img"", set delay to 100 milliseconds.

Complete code:

Img

Compile and upload the code to the ESP32. After the code is uploaded successfully, power it on via the USB cable, click the icon Img to enter the serial monitor, set the baud rate to 115200. What you will see is: the serial monitor window will print the analog value of the potentiometer, and the analog value changes when you turn the potentiometer knob.

Img

5. Dimmable Light Wiring Diagram:

In the previous step, we read the analog value of the potentiometer. Now we need to convert the potentiometer's analog value to LED brightness to make an adjustable brightness light. See the wiring diagram below:

Image not found

6. Project Code:

Img

7. Project Phenomenon:

Compile and upload the code to the ESP32. After the code is uploaded successfully, power it on via the USB cable. What you will see is: when you turn the potentiometer knob, the brightness of the LED changes accordingly.

Image not found

Project 23 Fire Alarm

1. Project Introduction:

Fire is a terrible disaster, and fire alarm systems are very useful in homes, commercial buildings, and factories. In this project, we will use the ESP32 to control a flame sensor, buzzer, and LED to simulate a fire alarm device. This is a meaningful maker activity.

2. Project Components:

Image not found Image not found Image not found Image not found
ESP32*1 Breadboard*1 Flame sensor*1 Red LED*1
Image not found Image not found Image not found Image not found
Active buzzer*1 220Ω Resistor*1 10KΩ Resistor*1 Jumper wires (several)
Image not found Image not found Image not found
NPN transistor (S8050)*1 1kΩ Resistor*1 USB cable*1

3. Component Knowledge:

Image not found

Flame sensor (Infrared receiving transistor): Fire emits a certain degree of IR light, which is invisible to the human eye, but our infrared receiving transistor can detect it and alert the microcontroller (such as ESP32) that a fire has been detected. The infrared receiving transistor can detect flames and convert the flame brightness into a fluctuating level signal. The short pin of the infrared receiving transistor is the negative terminal, and the other long pin is the positive terminal. We should connect the short pin (negative) to 5V, and the long pin (positive) to an analog pin, a resistor, and GND. As shown in the figure below:

Image not found

Note: The flame sensor should be kept away from direct sunlight, car headlights, and incandescent lamps, and should not face heat sources (such as radiators, heaters) or air conditioners, to avoid false alarms caused by large changes in ambient temperature. It is also susceptible to RF radiation interference.

4. Reading the Analog Value of the Flame Sensor:

We first use a simple code to read the analog value of the flame sensor and print it out. Please refer to the following wiring diagram:

Image not found

Code Description:

Img

Read the analog signal of the flame sensor from a specified analog pin; the analog signal range is: 0 ~ 4095.

You can open the code we provided, or you can write the code yourself, as follows:

  1. Drag out ""Img"" from ""Img"".

  2. Drag out ""Img"" from ""Img"" and place it into ""Img"", set baud rate to 115200.

  3. First, drag out ""Img"" from ""Img""; then drag out " Img ", set pin to 36.

  4. Drag out ""Img"" from ""Img"", set delay to 100 milliseconds.

Complete code:

Img

Compile and upload the code to the ESP32. After the code is uploaded successfully, power it on via the USB cable, click the icon Img to enter the serial monitor, set the baud rate to 115200. What you will see is: the serial monitor window will print the analog value read by the flame sensor. When a flame approaches the flame sensor, the analog value increases; conversely, the analog value decreases.

Img

5. Fire Alarm Wiring Diagram:

Next, we will use a flame sensor, buzzer, and LED to make an interesting project -- a fire alarm device. When the flame sensor detects a flame, the LED flashes and the buzzer alarms.

Image not found

6. Project Code:

(Note: The threshold value 500 in the code can be reset according to actual conditions)

Img

7. Project Phenomenon:

Compile and upload the code to the ESP32. After the code is uploaded successfully, power it on via the USB cable. What you will see is: when the flame sensor detects a flame, the LED flashes and the buzzer alarms; otherwise, the LED does not light up and the buzzer does not sound.

Image not found

Project 24 Small Night Light

1. Project Introduction:

Sensors or components are everywhere in our daily life. For example, some public street lights automatically turn on at night and turn off during the day. Why? In fact, these all utilize a photosensitive component that can sense the intensity of external ambient light. At night, when the outdoor brightness decreases, the street lights automatically turn on; during the day, the street lights automatically turn off. The principle behind this is very simple. In this experiment, we use the ESP32 to control an LED to achieve this street light effect.

2. Project Components:

Image not found Image not found Image not found Image not found
ESP32*1 Breadboard*1 Photoresistor*1 Red LED*1
Image not found Image not found Image not found Image not found
220Ω Resistor*1 10KΩ Resistor*1 Jumper wires (several) USB cable*1

3. Component Knowledge:

Image not found

Photoresistor: It is a light-sensitive resistor. Its principle is that the resistance decreases when the brightness (light) received on the surface of the photoresistor increases. The resistance of the photoresistor changes with the intensity of the detected ambient light. With this characteristic, we can use a photoresistor to detect light intensity. The photoresistor and its electronic symbol are shown below:

Image not found

The circuit below is used to detect changes in the resistance of the photoresistor:

Image not found

In the above circuit, when the resistance of the photoresistor changes due to variations in light intensity, the voltage between the photoresistor and resistor R2 also changes. Therefore, by measuring this voltage, the light intensity can be obtained. This project uses the circuit on the left side of the above figure for wiring.

4. Reading the Analog Value of the Photoresistor:

We first use a simple code to read the analog value of the photoresistor and print it out. Please refer to the following wiring diagram:

Image not found

Code Description:

Img

Read the analog signal of the photoresistor (light intensity) from a specified analog pin; the analog signal range is: 0 ~ 4095.

You can open the code we provided, or you can write the code yourself, as follows:

  1. Drag out ""Img"" from ""Img"".

  2. Drag out ""Img"" from ""Img"" and place it into ""Img"", set baud rate to 115200.

  3. First, drag out ""Img"" from ""Img""; then drag out " Img ", set pin to 36.

  4. Drag out ""Img"" from ""Img"", set delay to 100 milliseconds.

Complete code:

Img

Compile and upload the code to the ESP32. After the code is uploaded successfully, power it on via the USB cable, click the icon Img to enter the serial monitor, set the baud rate to 115200. What you will see is: the serial monitor window will print the analog value read by the photoresistor. When the light intensity in the photoresistor's environment is gradually reduced, the analog value gradually increases; conversely, the analog value gradually decreases.

Img

5. Light-Controlled Lamp Wiring Diagram:

We previously made a small dimmable light, and now we will make a light-controlled lamp. The principle is the same, i.e., the ESP32 obtains the analog value of the sensor and then adjusts the brightness of the LED.

Image not found

6. Project Code:

Img

7. Project Phenomenon:

Compile and upload the code to the ESP32. After the code is uploaded successfully, power it on via the USB cable. What you will see is: when the light intensity in the photoresistor's environment is reduced, the LED brightens; conversely, the LED dims.

Image not found

Project 25 Human Body Sensor Light

1. Project Introduction:

Human body sensor lights are generally used in dark corridor areas. With the development of technology, the use of human body sensor lights is very common in our real life. They are used in residential corridors, bedrooms, underground garages, bathrooms, and other areas. Real-life human body sensor lights are generally composed of PIR sensors, lights, photoresistor sensors, etc.

In this project, we will learn how to use a PIR sensor, LED, and photoresistor to make a human body sensor light.

2. Project Components:

Image not found Image not found Image not found Image not found
ESP32*1 Breadboard*1 Photoresistor*1 Red LED*1
Image not found Image not found Image not found Image not found
220Ω Resistor*1 10KΩ Resistor*1 Jumper wires (several) USB cable*1
Image not found Image not found
PIR sensor*1 Dupont wires (several)*1

3. Project Wiring Diagram:

Image not found

4. Project Code:

Img

5. Project Phenomenon:

Compile and upload the code to the ESP32. After the code is uploaded successfully, power it on via the USB cable. What you will see is: when you cover the photosensitive part of the photoresistor with your hand to simulate darkness, and then wave your other hand in front of the PIR sensor, the LED lights up for 5 seconds; if the photosensitive part of the photoresistor is not covered, waving your hand in front of the PIR sensor will not light up the LED.

Image not found

Project 26 Sound-Controlled Fan

1. Project Introduction:

A sound sensor has a built-in capacitive electret microphone and power amplifier. It can be used to detect the sound intensity of the environment. In this project, we use the ESP32 to control a sound sensor and motor module to simulate a sound-controlled fan.

2. Project Components:

Image not found Image not found Image not found Image not found
ESP32*1 Breadboard*1 130 motor module*1 Sound sensor*1
Image not found Image not found Image not found Image not found
Breadboard power module*1 6-cell AA battery box*1 Fan blade*1 Dupont wires (several)
Image not found Image not found Image not found
Jumper wires (several) AA batteries (self-provided)*6 USB cable*1

3. Component Knowledge:

Image not found

Sound sensor: Usually used to detect the sound loudness in the surrounding environment. A microcontroller can collect its output signal through an analog input interface. The S pin of the sensor is the analog output, which is the real-time output of the microphone voltage signal. The sensor comes with a potentiometer so you can adjust the signal strength. You can use it to make some interactive works, such as voice-operated switches.

Sound sensor parameters:

  • Operating voltage: 3.3V-5V (DC)
  • Operating current: ≤100mA
  • Maximum power: 0.5W
  • Output signal: Analog signal

Sound sensor schematic:

It mainly uses a high-sensitivity microphone element and an LM386 chip. The high-sensitivity microphone element is used to detect external sounds. Using the LM386 chip to build an appropriate circuit, we amplify the sound detected by the high-sensitivity microphone, with a maximum amplification of 200 times. When in use, you can adjust the amplification by rotating the potentiometer on the sensor. When adjusting, rotate the potentiometer clockwise to the end for maximum amplification.

Image not found

4. Reading the Analog Value of the Sound Sensor:

We first use a simple code to read the analog value of the sound sensor and print it out. Please refer to the following wiring diagram:

Image not found

Code Description:

Img

Read the external sound intensity (analog value).

You can open the code we provided, or you can write the code yourself, as follows:

  1. Drag out ""Img"" from ""Img"".

  2. Drag out ""Img"" from ""Img"" and place it into ""Img"", set baud rate to 115200.

  3. First, drag out ""Img"" from ""Img""; then drag out " Img " from " Img ", set pin to 36.

  4. Drag out ""Img"" from ""Img"", set delay to 100 milliseconds.

Complete code:

Img

Compile and upload the code to the ESP32. After the code is uploaded successfully, power it on via the USB cable, open the serial monitor, set the baud rate to 115200. What you will see is: the serial monitor window will print the analog value read by the sound sensor. When you clap your hands at the sensor, the analog value of the sound sensor changes significantly.

Img

5. Smart Fan Wiring Diagram:

Next, we officially enter this project. We use a sound sensor, 130 motor module, and fan blades to simulate a sound-controlled fan. The wiring diagram is as follows:

Image not found

(Note: Connect the wires first, then install a small fan blade on the DC motor.)

6. Project Code:

(Note: The threshold value 600 in the code can be reset according to actual conditions)

Img

7. Project Phenomenon:

Compile and upload the code to the ESP32. After the code is uploaded successfully, power it on via external power. What you will see is: when you clap your hands at the sound sensor, if the sound intensity exceeds the threshold of 600, the small fan rotates; conversely, the small fan does not rotate.

Image not found

Project 27 Temperature Measurement

1. Project Introduction:

LM35 is a commonly used and easy-to-use temperature sensor. It does not require additional hardware; you only need one analog port. The difficulty lies in compiling the code to convert the analog value it reads into Celsius temperature. In this project, we use a temperature sensor and 3 LEDs to make a temperature tester. When the temperature sensor touches objects of different temperatures, the LEDs display different colors.

2. Project Components:

Image not found Image not found Image not found Image not found
ESP32*1 Breadboard*1 Red LED*1 Yellow LED*1
Image not found Image not found Image not found Image not found
Green LED*1 220Ω Resistor*3 LM35 temperature sensor*1 Dupont wires (several)
Image not found Image not found
Jumper wires (several) USB cable*1

3. Component Knowledge:

Image not found

LM35 temperature sensor: It is a commonly used LM35 temperature sensor, mainly using the LM35DZ sensor element. The output voltage of this element has a linear relationship with the Celsius temperature scale. At room temperature, it can achieve an accuracy of 1/4℃ without additional calibration. The LM35 temperature sensor produces different voltages at different temperatures. When the temperature is 0℃, it outputs 0V; for every 1℃ increase, the output voltage increases by 10mV. The conversion formula is as follows:

Image not found

4. Reading the Temperature Value of LM35:

We first use a simple code to read the value of the temperature sensor and print it out. The wiring diagram is as follows:

Due to different batches, the interfaces may vary: OUT connects to GPIO36, + connects to 3.3V, - connects to GND.

Image not found

Code Description:

Img

Read the temperature value detected by the LM35 sensor.

Here, the LM35 outputs to the ESP32's analog pin GPIO36. This analog voltage is converted to digital form and processed to obtain the temperature reading.

You can open the code we provided, or you can write the code yourself, as follows:

  1. Drag out ""Img"" from ""Img"".

  2. Drag out ""Img"" from ""Img"" and place it into ""Img"", set baud rate to 115200.

  3. First, drag out ""Img"" from ""Img"", change "Auto line wrap" to "No line wrap"; then drag out " Img " from " Img ", change " hello " to "temperature: ".

  4. First, drag out ""Img"" from ""Img"", change "Auto line wrap" to "No line wrap"; then drag out " Img " from " Img ", set pin to 36.

  5. Copy the code block ""Img"" 1 time, change "No line wrap" to "Auto line wrap", change "temperature: " to " ℃ ".

  6. Drag out ""Img"" from ""Img"", set delay to 100 milliseconds.

Complete code:

Img

Compile and upload the code to the ESP32. After the code is uploaded successfully, power it on via the USB cable, click the icon Img to enter the serial monitor, set the baud rate to 115200. What you will see is: the serial monitor window will print the temperature value read by the LM35 temperature sensor. Hold the LM35 element on the LM35 temperature sensor with your hand, and the temperature value read by the LM35 sensor will change.

Img

5. Temperature Measurement Wiring Diagram:

Now use the LM35 temperature sensor and 3 LEDs to make a temperature test. When the LM35 temperature sensor senses different temperatures, different LEDs will light up. Wire according to the diagram below.

Image not found

6. Project Code:

(Note: The threshold values 20 and 25 in the code can be reset according to actual conditions)

Img

7. Project Phenomenon:

Compile and upload the code to the ESP32. After the code is uploaded successfully, power it on via the USB cable. What you will see is: open the serial monitor, set the baud rate to 115200. The serial monitor window will print the temperature value read by the LM35 temperature sensor. When the LM35 temperature sensor senses different temperatures, different LEDs will light up.

Image not found

Project 28 Joystick-Controlled RGB Light

1. Project Introduction:

A joystick module is a component with two analog inputs and one digital input. It is widely used in game operation, robot control, drone control, and other fields. In this project, we use the ESP32 and a joystick module to control an RGB LED. This way, you can gain a deeper understanding of the principles and operation of the joystick module in practice.

2. Project Components:

Image not found Image not found Image not found Image not found
ESP32*1 Breadboard*1 Joystick module*1 RGB LED*1
Image not found Image not found Image not found Image not found
220Ω Resistor*3 Jumper wires (several) USB cable*1 Female-to-male Dupont wires*5

3. Component Knowledge:

Image not found

Joystick module: It mainly uses a PS2 joystick element. The joystick module actually has 3 signal pins, simulating 3D space. The pins of the joystick module are GND, VCC, and signal terminals (VRx, VRy, SW). The X and Y signal terminals simulate the X and Y axes of space. When controlling, the module's X and Y signal terminals are connected to the control board's analog ports, and the coordinates of an object in the X and Y axes of space are controlled by reading 2 analog input values. The SW signal terminal simulates the Z axis of space and is generally connected to a digital port for use as a button.

VCC connects to the control board's power output V/VCC (3.3/5V), GND connects to the control board's G/GND. In the original state, the read voltage is about 1.65V/2.5V. For the X-axis direction, when gradually moving in the direction of the arrow, the read voltage increases and can reach the maximum voltage; when gradually moving in the opposite direction of the arrow, the read voltage decreases to the minimum voltage. For the Y-axis direction, when gradually pressing in the direction of the arrow on the module, the read voltage increases and can reach the maximum voltage; when gradually pressing in the opposite direction of the arrow, the read voltage decreases to the minimum voltage. For the Z-axis direction, the signal terminal B connects to a digital port, outputting 0 in the original state and outputting 1 when pressed. This way, we can read two analog values and the high/low level of a digital port to determine the working state of the joystick on the module.

Module parameters:

  • Input voltage: DC 3.3V ~ 5V
  • Output signal: X/Y dual-axis analog values + Z-axis digital signal
  • Application range: Suitable for controlling point coordinates moving in a plane, controlling dual-degree-of-freedom servos, etc.
  • Product features: Beautiful appearance, excellent joystick feel, simple operation, sensitive response, long service life.

4. Reading the Values of the Joystick Module:

We must use the ESP32's analog IO ports to read values from the X/Y pins of the joystick module, and use a digital IO port to read the digital signal of the button. Please wire according to the diagram below (due to component changes, B=SW in the figure below):

Image not found

Code Description:

Img

Read the analog values of the joystick module in the X-axis and Y-axis directions.

Img

Read the button value of the joystick module in the Z-axis direction.

You can open the code we provided, or you can write the code yourself, as follows:

  1. Drag out ""Img"" from ""Img"".

  2. Drag out ""Img"" from ""Img"" and place it into ""Img"", set baud rate to 115200.

  3. First, drag out ""Img"" from ""Img"", change "Auto line wrap" to "No line wrap"; then drag out " Img " from " Img ", change hello to X:.

  4. First, drag out ""Img"" from ""Img"", change "Auto line wrap" to "No line wrap"; then drag out " Img " from " Img ", set pin to 36, select x.

  5. Copy the code block ""Img"" 1 time, change X: to Y:, select y, set pin to 39.

  6. First, copy the code block ""Img"" 1 time, change Y: to Z:; then drag out ""Img"" from ""Img"", then drag out " Img " from " Img ", set pin to 14.

  7. Drag out ""Img"" from ""Img"", set delay to 100 milliseconds.

Complete code:

Img

Compile and upload the code to the ESP32. After the code is uploaded successfully, power it on via the USB cable, click the icon Img to enter the serial monitor, set the baud rate to 115200. What you will see is: the serial monitor window will print the current analog and digital values of the joystick. Moving the joystick or pressing the joystick cap will change the analog and digital values in the serial monitor window.

Image not found

Img

5. Joystick Module Controlling RGB Wiring Diagram:

We just read the values of the joystick module. Here we need to do something with the joystick module and RGB LED. Connect according to the diagram below:

Image not found

6. Project Code:

Img

7. Project Phenomenon:

Compile and upload the code to the ESP32. After the code is uploaded successfully, power it on via the USB cable. What you will see is: ① If the joystick is moved to the far left in the X direction, the RGB LED lights up red; ② If the joystick is moved to the far right in the X direction, the RGB LED lights up green; ③ If the joystick is moved to the top in the Y direction, the RGB LED lights up white; ④ If the joystick is moved to the bottom in the Y direction, the RGB LED lights up blue; ⑤ If the joystick cap is pressed, the RGB LED turns off.

Image not found

Image not found

Project 29 Temperature and Humidity Meter

1. Project Introduction:

In winter, the humidity in the air is very low, meaning the air is very dry. Combined with the cold, human skin can become too dry and crack. So you need to use a humidifier to increase the humidity in your home's air. But how do you know when the air is too dry? You need a device to detect air humidity. In this lesson, we will learn to use a temperature and humidity sensor. We will use a temperature and humidity sensor to make a temperature and humidity meter, and also combine it with an LCD 128X32 DOT to display the temperature and humidity values.

2. Project Components:

Image not found Image not found Image not found Image not found
ESP32*1 Breadboard*1 OLED screen*1 Temperature and humidity sensor*1
Image not found Image not found
Dupont wires (several) USB cable*1

3. Component Knowledge:

Image not found

Temperature and humidity sensor: It is a composite temperature and humidity sensor with calibrated digital signal output. Its accuracy is ±5%RH for humidity and ±2℃ for temperature, with a range of 20-90%RH for humidity and 0~50℃ for temperature. The temperature and humidity sensor uses dedicated digital module acquisition technology and temperature and humidity sensing technology, ensuring the product has high reliability and excellent long-term stability. The temperature and humidity sensor includes a resistive humidity sensing element and an NTC temperature measuring element, making it very suitable for temperature and humidity measurement applications where high accuracy and real-time performance are not required. Operating voltage is in the range of 3.3V-5.5V.

The temperature and humidity sensor has three pins: VCC, GND, and S. S is the data output pin. It uses serial communication.

Different batches may also have + - out three pins: + connects to power, - connects to GND, and OUT is the data output pin.

Single-bus format definition of the temperature and humidity sensor:

Name Single-bus format definition
Start signal The microprocessor pulls the data bus (SDA) low for a period of at least 18ms (maximum not exceeding 30ms), notifying the sensor to prepare data.
Response signal The sensor pulls the data bus (SDA) low for 83µs, then high for 87µs to respond to the host's start signal.
Humidity The high byte of humidity is the integer part of humidity data, and the low byte is the decimal part of humidity data.
Temperature The high byte of temperature is the integer part of temperature data, and the low byte is the decimal part of temperature data. If bit 8 of the low byte of temperature is 1, it indicates negative temperature; otherwise, it is positive temperature.
Checksum Checksum = humidity high byte + humidity low byte + temperature high byte + temperature low byte

Temperature and humidity sensor data timing diagram:

After the user host (MCU) sends a start signal, the temperature and humidity sensor switches from low-power mode to high-speed mode. After the host's start signal ends, the sensor sends a response signal, outputs 40-bit data, and triggers a data acquisition. The signal transmission is shown in the figure below.

Image not found

The temperature and humidity sensor can easily add temperature and humidity data to your DIY electronic projects. It is perfect for remote weather stations, home environment control systems, and farm or garden monitoring systems.

Parameters of the temperature and humidity sensor:

  • Operating voltage: +5 V
  • Temperature range: 0-50 °C, error: ± 2 °C
  • Humidity range: 20-90% RH, error: ± 5% RH
  • Digital interface

Schematic of the temperature and humidity sensor:

Image not found

4. Reading Temperature and Humidity Values:

Image not found

Code Description:

Img

This reads the temperature and humidity detected by the temperature and humidity sensor. The sensor has 3 models to choose from depending on the model; here we select the DHT11 model.

You can open the code we provided, or you can write the code yourself, as follows:

  1. Drag out ""Img"" from ""Img"".

  2. Drag out ""Img"" from ""Img"" and place it into ""Img"", set baud rate to 115200.

  3. First, drag out ""Img"" from ""Img"", change "Auto line wrap" to "No line wrap"; then drag out " Img " from " Img ", change hello to RH:.

  4. First, drag out ""Img"" from ""Img"", change "Auto line wrap" to "No line wrap"; then drag out " Img " from " Img ", set pin to 13, select "Get Humidity".

  5. Copy the code block " Img " 1 time, change RH: to %.

  6. Copy the code block ""Img"" 1 time, change RH: to Temp:, select "Get Temperature", change % to C, change the bottom "No line wrap" to "Auto line wrap".

  7. Drag out ""Img"" from ""Img"", set delay to 100 milliseconds.

Complete code:

Img

Compile and upload the code to the ESP32. After the code is uploaded successfully, power it on via the USB cable, click the icon Img to enter the serial monitor, set the baud rate to 115200. What you will see is: the serial monitor window will print the current temperature and humidity data of the environment, as shown in the figure below.

Img

5. Temperature and Humidity Meter Wiring Diagram:

Now we will use the LCD_128X32_DOT to print the values from the temperature and humidity sensor. We will see the corresponding values on the LCD_128X32_DOT screen. Let's start this project. Please wire according to the diagram below:

Image not found

6. Project Code:

Img

7. Project Phenomenon:

Compile and upload the code to the ESP32. After the code is uploaded successfully, power it on via the USB cable. What you will see is: the LCD 128X32 DOT screen displays the corresponding temperature and humidity values detected by the temperature and humidity sensor in the environment.

Image not found

Project 30 Distance Meter

1. Project Introduction:

The HC-SR04 ultrasonic sensor is a very affordable distance sensor, mainly used for object avoidance in various robot projects. It is also used in water level sensors and even as a parking sensor. We treat the ultrasonic sensor as a bat's eyes; in the dark, bats can still identify objects and directions ahead through ultrasonic waves. In this project, we use the ESP32 to control an ultrasonic sensor and LEDs to simulate an ultrasonic distance meter.

2. Project Components:

Image not found Image not found Image not found Image not found
ESP32*1 Breadboard*1 Ultrasonic sensor*1 Dupont wires (several)
Image not found Image not found Image not found Image not found
Red LED*4 220Ω Resistor*4 Jumper wires (several) USB cable*1

3. Component Knowledge:

HC-SR04 ultrasonic sensor: Like bats, it uses sonar to determine the distance to objects, providing accurate non-contact range detection, high precision, and stable readings. Its operation is not affected by sunlight or black materials, just like a precision camera (acoustically, softer materials like cloth are difficult to detect). It comes with an ultrasonic transmitter and receiver.

Image not found

At the front of the ultrasonic sensor are two metal cylinders, which are the transducers. The transducers convert mechanical energy into electrical signals. In the ultrasonic sensor, there is a transmitting transducer and a receiving transducer. The transmitting transducer converts electrical signals into ultrasonic pulses, and the receiving transducer converts the reflected ultrasonic pulses back into electrical signals. If you look at the back of the ultrasonic sensor, you will see an IC behind the transmitting transducer. This is the IC that controls the transmitting transducer. There is also an IC behind the receiving transducer, which is a quad operational amplifier that amplifies the signal generated by the receiving transducer into a signal strong enough to be transmitted to the microcontroller.

Timing diagram:

The figure shows the timing diagram of the HC-SR04. To start a measurement, the SR04's Trig must receive a high (5V) pulse of at least 10us, which will cause the sensor to emit 8 cycles of 40kHz ultrasonic pulses and wait for the reflected ultrasonic pulses. When the sensor detects ultrasonic waves from the receiver, it sets the Echo pin high (5V) and delays a period (width) proportional to the distance. To get the distance, measure the width of the Echo pin.

Image not found

Time = echo pulse width, in us (microseconds)

Distance in cm = time / 58

Distance in inches = time / 148

4. Reading the Ultrasonic Sensor Distance Value

We will start with a simple ultrasonic distance measurement and print the measured distance.

Image not found

The HC-SR04 ultrasonic sensor has four pins: Vcc, Trig, Echo, and GND. The Vcc pin provides the power for generating ultrasonic pulses; connect to Vcc/+5V. The GND pin connects to ground/GND. The Trig pin is where the control board sends a signal to start the ultrasonic pulse. The Echo pin is where the ultrasonic sensor sends information about the duration of the ultrasonic pulse's travel to the control board. Wire according to the diagram below:

Image not found

Code Description:

Img

Read the distance detected by the ultrasonic sensor.

You can open the code we provided, or you can write the code yourself, as follows:

  1. Drag out ""Img"" from ""Img"".

  2. Drag out ""Img"" from ""Img"" and place it into ""Img"", set baud rate to 115200.

  3. First, drag out ""Img"" from " Img" and place it into ""Img"", change "Integer" to "String"; drag out ""Img"" from ""Img"" and place it into ""Img"", change item to distance.

Img

  1. First, drag out ""Img"" from " Img"; then drag out " Img " from " Img ", set Trig pin to 13, Echo pin to 14.

Img

  1. First, drag out ""Img"" from ""Img"", change "Auto line wrap" to "No line wrap"; then drag out " Img " from " Img ", change hello to Distance:.

Img

  1. First, drag out ""Img"" from ""Img"", change "Auto line wrap" to "No line wrap"; then drag out " Img " from " Img".

Img

  1. Copy the code block " Img " 1 time, change "No line wrap" to "Auto line wrap", change Distance: to cm.

Img

  1. Drag out ""Img"" from ""Img"", set delay to 100 milliseconds.

Complete code:

Img

Compile and upload the code to the ESP32. After the code is uploaded successfully, power it on via the USB cable, click the icon Img to enter the serial monitor, set the baud rate to 115200. What you will see is: the serial monitor window will print the distance value between the ultrasonic sensor and the object.

Img

5. Ultrasonic Distance Meter Wiring Diagram:

Next, we will use the ESP32 to control an ultrasonic sensor and 4 LEDs to simulate an ultrasonic distance meter. Wire according to the diagram below:

Image not found

6. Project Code:

Img

7. Project Phenomenon:

Compile and upload the code to the ESP32. After the code is uploaded successfully, power it on via the USB cable, open the serial monitor, set the baud rate to 115200. What you will see is: the serial monitor window will print the distance value between the ultrasonic sensor and the object. Additionally, when you move your hand in front of the ultrasonic sensor, the corresponding LEDs will light up.

Image not found

Project 31 Temperature Meter

1. Project Introduction:

A thermistor is a resistor whose resistance depends on temperature and changes in temperature. It is widely used in devices such as gardening and home alarm systems. Therefore, we can use this characteristic to make a temperature meter.

2. Project Components:

Image not found Image not found Image not found Image not found
ESP32*1 Breadboard*1 OLED screen*1 Thermistor*1
Image not found Image not found Image not found Image not found
10KΩ Resistor*1 Dupont wires (several) USB cable*1 Jumper wires (several)

3. Component Knowledge:

Thermistor: A thermistor is a temperature-sensitive resistor. When the thermistor senses a change in temperature, its resistance changes. We can use this characteristic of the thermistor to detect temperature intensity. The thermistor and its electronic symbol are shown below.

Image not found

The relationship between the resistance of a thermistor and temperature is:

Image not found

Where:

Rt is the resistance of the thermistor at temperature T2;

R is the nominal resistance of the thermistor at temperature T1;

EXP[n] is e to the power of n;

B is the temperature coefficient;

T1, T2 are Kelvin temperatures (absolute temperature), Kelvin temperature = 273.15 + Celsius temperature. For the parameters of the thermistor, we use: B=3950, R=10KΩ, T1=25℃. The circuit connection method of the thermistor is similar to that of the photoresistor, as shown below:

Image not found

We can use the value measured by the ADC converter to obtain the resistance of the thermistor, and then use the formula to obtain the temperature value. Therefore, the temperature formula can be derived as:

Image not found

4. Reading the Value of the Thermistor:

First, we will learn how the thermistor reads the current temperature value and prints it out. Please wire according to the diagram below:

Image not found

Code Description:

Img

Read the temperature value of the analog temperature sensor (thermistor).

You can open the code we provided, or you can write the code yourself, as follows:

  1. Drag out ""Img"" from ""Img"".

  2. Drag out ""Img"" from ""Img"" and place it into ""Img"", set baud rate to 115200.

  3. First, drag out ""Img" " from ""Img""; then drag out ""Img"" from ""Img""; change "Auto line wrap" to "No line wrap" and "hello" to "Temperature:".

  4. First, drag out ""Img" " from ""Img""; then drag out ""Img"" from ""Img"", set pin to 36; change "Auto line wrap" to "No line wrap".

  5. Copy the code block " Img" 1 time, change "No line wrap" to "Auto line wrap" and "Temperature:" to " ℃ "; then drag out ""Img"" from ""Img"", set delay to 100 milliseconds.

Complete code:

Img

Compile and upload the code to the ESP32. After the code is uploaded successfully, power it on via the USB cable, click the icon Img to enter the serial monitor, set the baud rate to 115200. What you will see is: the serial monitor window will continuously display the temperature value detected by the thermistor in the current environment. Try pinching the thermistor with your index finger and thumb (without touching the wires) for a short time; you should see the temperature value increase.

Img

5. Temperature Meter Wiring Diagram:

Image not found

6. Project Code:

Img

7. Project Phenomenon:

Compile and upload the code to the ESP32. After the code is uploaded successfully, power it on via the USB cable. What you will see is: the LCD 128X32 DOT screen displays the temperature value detected by the thermistor in the current environment.

Img

Project 32 RFID - New Version Will Replace This Tutorial

1. Experiment Introduction:

Now many residential doors use card-swiping to open, which is very convenient. In this lesson, we will learn to use RFID (Radio Frequency Identification) wireless communication technology to read and write cards (key fobs/white magnetic cards) and use the RFID-MFRC522 module to control a servo.

2. Experiment Components:

Image not found Image not found Image not found Image not found Image not found
ESP32*1 Breadboard*1 RFID-RC522 module*1 Servo*1 White magnetic card*1
Image not found Image not found Image not found Image not found
Dupont wires (several) Jumper wires (several) USB cable*1 Key fob*1

3. Component Knowledge:

RFID: Radio frequency identification. The card reader consists of an RF module and a high-level magnetic field. The tag transponder is the device to be sensed, and this device does not contain a battery. It only contains a micro integrated circuit chip, a medium for storing data, and an antenna for receiving and transmitting signals. To read the data in the tag, it must first be placed within the reading range of the card reader. The card reader generates a magnetic field. Because magnetism can generate electricity (by Lenz's law), the RFID tag is powered, thereby activating the device.

RFID-RC522 module: It uses the Philips MFRC522 original chip to design the card reading circuit. It is easy to use, low cost, and suitable for users of advanced applications such as device development and card reader development, and users who need to produce/design RF card terminals. This module can be directly installed in various card reader molds. The module uses 5V and can be directly connected to any CPU board or microcontroller via a few simple SPI interface lines for communication.

Image not found

Specifications:

  • Operating voltage: DC 5V
  • Operating current: 13-100mA/DC 5V
  • Idle current: 10-13mA/DC 5V
  • Sleep current: <80uA
  • Peak current: <100mA
  • Operating frequency: 13.56MHz
  • Maximum power: 0.5W
  • Supported card types: mifare1 S50, mifare1 S70, mifare UltraLight, mifare Pro, mifare Desfire
  • Operating temperature: Celsius -20—80℃
  • Storage temperature: Celsius -40—85℃
  • Relative humidity: 5%—95%
  • Data transfer rate: max 10Mbit/s

4. RFID Reading UID:

We will read the unique ID (UID) of the RFID card, identify the type of RFID card, and display the relevant information via the serial port. The wiring diagram is as follows:

Code Description:

Img

Initialize the pins of the RFID RC522 I2C module, etc.

Img

The RFID RC522 I2C module reads the values of the white magnetic card and key fob.

You can open the code we provided, or you can write the code yourself, as follows:

  1. Drag out ""Img"" from ""Img"".

  2. Drag out ""Img"" from ""Img"" and place it into ""Img"", set baud rate to 115200.

  3. Drag out ""Img"" from ""Img"" and place it into ""Img"".

Img

  1. First, drag out ""Img"" from " Img" and place it into ""Img"", change "Integer" to "String"; then drag out ""Img"" from ""Img"" and place it into ""Img"", delete "hello".

Img

  1. Drag out ""Img"" from " Img", then drag out " Img " from " Img ".

Img

  1. First, drag out ""Img" " from ""Img""; then drag out ""Img"" from " Img"; then drag out ""Img"" from ""Img"", set delay to 100 milliseconds.

Img

Complete code:

Img

Compile and upload the code to the ESP32. After the code is uploaded successfully, power it on via the USB cable, click the icon Img to enter the serial monitor, set the baud rate to 115200. What you will see is: when you bring the white magnetic card and key fob close to the module's sensing area respectively, the serial monitor window will display the card number values of the white magnetic card and key fob respectively. As shown in the figure below:

Img

Special note: For different white magnetic cards and key fobs of the RFID-RC522, the white magnetic card values and key fob values may be different.

5. RFID MFRC522 Wiring Diagram:

Now use the RFID-RC522 module, white magnetic card/key fob, and servo to simulate a smart access control system. When the white magnetic card/key fob is brought close to the RFID-RC522 module's sensing area, the servo rotates. Wire according to the diagram below.

Image not found

6. Project Code:

Special note: For different white magnetic cards and key fobs of the RFID-RC522, the values read by the RFID-RC522 may be different. You need to replace the corresponding white magnetic card and key fob values in the program code with the values read by your own RFID-RC522 module, otherwise the white magnetic card and key fob may not be able to control the servo.

For example: Replace the item string in the program code Img with the values of the white magnetic card and key fob read by your own RFID-RC522 module.

Img

7. Project Phenomenon:

Compile and upload the code to the ESP32. After the code is uploaded successfully, power it on via the USB cable, click the icon Img to enter the serial monitor, set the baud rate to 115200. What you will see is: when we use the white magnetic card or key fob to swipe, the serial monitor displays the white magnetic card or key fob information and "open the door", as shown in the figure below, and the servo rotates to the corresponding angle to simulate opening the door.

Img

Image not found

Project 32 Smoke Alarm

1. Project Introduction:

Fire is one of the most common disasters in life. A smoke alarm can sound an alert in the early stages of a fire (when smoke is just produced), buying valuable time for evacuation and firefighting. It is widely used in homes, hotels, malls, factories, and other places.

This experiment uses the MQ-2 smoke/gas sensor to detect smoke and combustible gas concentrations in the air in real time. When the detected concentration exceeds the set threshold, the ESP32 controls the active buzzer to sound an alarm, while printing the detected data through the serial port, implementing a simple "smoke alarm."

Later we will release an IoT APP tutorial: "MQTT IoT," which uses a smoke sensor to monitor the home environment in real time. In case of gas leakage or fire, it can remotely send alert messages to your phone.

2. Project Components:

Image not found Image not found Image not found
ESP32*1 Breadboard*1 Smoke sensor*1
Image not found Image not found
USB cable*1 Jumper wires (several)
Image not found Image not found Image not found
NPN transistor (S8050)*1 Active buzzer*1 1KΩ Resistor*1

3. Component Knowledge:

MQ-2 is a widely used semiconductor gas sensor that can detect smoke and combustible gases such as liquefied petroleum gas, propane, and hydrogen.

The MQ-2 has a gas-sensitive element made of tin dioxide (SnO2) semiconductor material, with a heating wire on its surface. During operation, the heating wire heats the gas-sensitive element to a high temperature, causing oxygen adsorption on its surface to form oxygen negative ions, and the element is in a high-resistance state. When combustible gas or smoke molecules in the air come into contact with the element's surface, a chemical reaction occurs, causing an increase in carrier concentration on the element's surface and a decrease in resistance, thereby changing the output voltage. The higher the gas concentration, the greater the output analog voltage.

Note: The MQ-2 needs to be preheated for a few minutes after power-on before the readings stabilize. This is normal.

4. Project Wiring:

ESP32 Pin Function Connection
GPIO12 ADC input MQ-2 AO
GPIO14 Digital input MQ-2 DO
GPIO15 Digital output Buzzer signal terminal

5. Project Code:

See the downloaded Mixly code for details.

6. Project Phenomenon:

Image not found

You can spray a little gas from a lighter (without igniting) at the sensor, and the buzzer will sound.

Project 33 Password Lock

1. Project Introduction:

For commonly used digital button sensors, one button uses one IO port. But sometimes we need more buttons, which would use too many IO ports. To save IO port usage, multiple buttons are made into a matrix type, achieving control of multiple buttons with fewer IO ports through row/column line control. In this project, we will learn to use the ESP32 and a薄膜4*4 matrix keyboard to control a servo and buzzer.

2. Project Components:

Image not found Image not found Image not found
ESP32*1 Breadboard*1 Servo*1
Image not found Image not found Image not found
Thin film 4×4 matrix keyboard*1 USB cable*1 Jumper wires (several)
Image not found Image not found Image not found
NPN transistor (S8050)*1 Active buzzer*1 1KΩ Resistor*1

3. Component Knowledge:

Thin film 4×4 matrix keyboard: A keyboard is a device that integrates many keys. As shown in the figure below, a thin film 4x4 keyboard integrates 16 keys.

Image not found

Like LED matrix integration, in a thin film 4x4 matrix keyboard, each row of keys is connected by one pin, and each column of keys is the same. Such connections can reduce the occupancy of processor ports. The internal circuit is shown below.

Image not found

The usage is similar to a matrix LED, i.e., using row scanning or column scanning to detect the status of keys on each column or row. Taking column scanning as an example, send a low level to column 4 (Pin4), detect the level status of rows 1, 2, 3, 4, and determine whether keys A, B, C, D are pressed. Then send low levels to columns 3, 2, 1 in sequence to detect whether other keys are pressed. Then you can get the status of all keys.

4. Reading the Key Values of the Thin Film 4×4 Matrix Keyboard:

We first use a simple code to read the key values of the thin film 4×4 matrix keyboard and print them out. The wiring diagram is as follows:

Image not found

Code Description

Img

Initialize the row and column pins of the thin film 4×4 matrix keyboard, define the keyboard, etc.

Img

Get the key values of the thin film 4×4 matrix keyboard.

You can open the code we provided, or you can write the code yourself, as follows:

  1. Drag out ""Img"" from ""Img"".

  2. Drag out ""Img"" from ""Img"" and place it into ""Img"", set baud rate to 115200.

  3. Drag out ""Img"" from ""Img"". The row pins are: 22, 21, 19, 18; the column pins are: 17, 6, 4, 0.

  4. First, drag out ""Img" " from ""Img"", then drag out ""Img"" from ""Img"".

  5. Drag out ""Img"" from ""Img"", set delay to 500 milliseconds.

Complete code:

Img

Compile and upload the code to the ESP32. After the code is uploaded successfully, power it on via the USB cable, click the icon Img to enter the serial monitor, set the baud rate to 115200. What you will see is: when you press a key on the thin film 4×4 matrix keyboard, the serial monitor window will print the corresponding key value, as shown in the figure below.

Img

5. Password Lock Wiring Diagram:

In the previous experiment, we already learned the key values of the thin film 4×4 matrix keyboard. Next, we will use the thin film 4×4 matrix keyboard as a keyboard to control the servo and buzzer.

Image not found

6. Project Code:

Img

7. Project Phenomenon:

Compile and upload the code to the ESP32. After the code is uploaded successfully, power it on via the USB cable. What you will see is: press the key "3" on the thin film 4×4 matrix keyboard, and the servo will rotate 180° and the buzzer will sound; then release, and the servo will return to its original position and the buzzer will stop.

Image not found

Project 34 Infrared Remote Control Sound and LED

1. Project Introduction:

Infrared remote control is a low-cost, easy-to-use wireless communication technology. IR light is very similar to visible light, except its wavelength is slightly longer. This means infrared is undetectable by the human eye, which is perfect for wireless communication. For example, when you press a button on a TV remote control, an infrared LED repeatedly switches on and off at a frequency of 38,000 times per second, transmitting information (such as volume or channel control) to the infrared sensor on the TV.

We will first explain how common infrared communication protocols work. Then we will start this project with a remote control and an infrared receiver module.

2. Project Components:

Image not found Image not found Image not found Image not found
ESP32*1 Breadboard*1 Infrared receiver*1 RGB LED*1
Image not found Image not found Image not found Image not found
Active buzzer*1 220Ω Resistor*3 10KΩ Resistor*1 Infrared remote control*1
Image not found Image not found Image not found Image not found
NPN transistor (S8050)*1 1kΩ Resistor*1 USB cable*1 Jumper wires (several)

3. Component Knowledge:

Infrared (IR) remote control: It is a device that can emit infrared light signals. Pressing different buttons causes the infrared transmitter tube at the front of the remote control to emit infrared rays with different commands. Infrared remote control technology is widely used, such as in TVs and air conditioners. Therefore, in today's technologically advanced society, infrared remote control technology makes it convenient for you to switch TV channels and adjust air conditioner temperatures.

The remote control we use is shown below; this infrared remote control uses NEC encoding.

Image not found

Infrared (IR) receiver: It is the VS1838B infrared receiver sensor element, which can receive infrared light, so it can be used to detect infrared light signals emitted by the infrared remote control.

Image not found

The infrared receiver is a device that integrates reception, amplification, and demodulation. The received infrared light signal is demodulated internally by its IC (converting the infrared light signal back to binary), and the output is a digital signal, which is then passed to the microcontroller. It can receive standard 38KHz modulated remote control signals.

Infrared signal modulation process diagram:

Image not found

4. Decoding Infrared Signals:

We connect the infrared receiver element to the ESP32 according to the wiring diagram below.

Image not found

Code Description:

Img

The infrared receiver receives the key value signal from the infrared remote control and initializes the pins.

Img

The serial port automatically prints the received hexadecimal infrared remote control key value signals with line wrapping.

You can open the code we provided, or you can write the code yourself, as follows:

  1. Drag out ""Img"" from ""Img"".

  2. Drag out ""Img"" from ""Img"" and place it into ""Img"", set baud rate to 115200.

  3. Drag out " Img " from " Img ", set pin to 0.

  4. First, drag out ""Img"" from ""Img"" and place it into " Img "; then drag out " Img " from ""Img"" and place it where the number 0 is in ""Img"", change "No line wrap" to "Auto line wrap".

Img

Complete code:

Img

Compile and upload the code to the ESP32. After the code is uploaded successfully, power it on via the USB cable, click the icon Img to enter the serial monitor, set the baud rate to 115200. What you will see is: point the infrared remote control transmitter at the infrared receiver, press a button on the remote control, and the serial monitor will print the currently received key code value. Press the same button multiple times to ensure you have the correct code value for that button. If you see FFFFFFFF, please ignore it.

Img

Write down the key code values associated with each button on the infrared remote control, as you will need this information later.

Image not found

5. Infrared Remote Control Wiring Diagram:

Image not found

6. Project Code:

Img

7. Project Phenomenon:

Compile and upload the code to the ESP32. After the code is uploaded successfully, power it on via the USB cable. What you will see is: press keys 1~7 on the infrared remote control, and the buzzer will sound once each time, while the RGB LED lights up red, green, blue, yellow, magenta, cyan, and white respectively. Press any other button (except keys 1-7), and the RGB LED turns off.

Image not found

Project 35 Bluetooth

This chapter mainly introduces how to use the ESP32's Bluetooth for simple data transmission with a mobile phone.

1. Project Components:

Image not found Image not found Image not found
ESP32*1 USB cable*1 Smartphone/tablet (self-provided)*1

In this project, we need to use a Bluetooth app called Serial Bluetooth Terminal to assist with the experiment. If you haven't installed it yet, please click to install: https://www.appsapk.com/serial-bluetooth-terminal/. Below is its logo.

Image not found

2. Component Knowledge:

Bluetooth is a short-range communication system that can be divided into two types: Bluetooth Low Energy (BLE) and Classic Bluetooth. There are two modes for simple data transmission: Master mode and Slave mode.

Master mode: In this mode, the work is done on the master device, and it can connect to slave devices. We can search and select nearby slave devices to connect. When a device initiates a connection request in master mode, it needs the address and pairing password of other Bluetooth devices. After pairing is complete, it can connect directly to them.

Slave mode: A Bluetooth module in slave mode can only accept connection requests from the master, but cannot initiate connection requests. After connecting to the master device, it can send data to the master device and also receive data from the master device. Data interaction can occur between Bluetooth devices, with one device in master mode and the other in slave mode. When they perform data interaction, the Bluetooth device in master mode searches and selects a nearby device to connect. During connection establishment, they can exchange data. When a phone exchanges data with the ESP32, the phone is usually in master mode and the ESP32 is in slave mode.

Image not found

3. Project Wiring:

Use a USB cable to connect the ESP32 board to a USB port on the computer.

Image not found

4. Project Code:

Reminder: This code is dragged out together from " Img ", just change " ESP32BT " to " ESP32test ".

Img

5. Project Phenomenon:

Compile and upload the code to the ESP32 main board. After uploading is successful, open the serial monitor and set the baud rate to 115200.

Please make sure your phone's Bluetooth is turned on and the "Serial Bluetooth Terminal" app is installed.

Image not found

Image not found

The phone automatically searches for nearby Bluetooth devices. Click "ESP32 test" to pair. A pairing dialog appears; click "Pair" to connect the "ESP32 test" device.

Image not found

Image not found

Image not found

Open the app, click on the left side of the terminal. Select "Devices".

Image not found

Image not found

Select ESP32test in Classic Bluetooth mode, and a connection success message will appear as shown in the figure below.

Image not found

Image not found

Now, data can be transmitted between your phone and computer via the ESP32.

Enter "Hi!" in the text box in the serial monitor. When the phone receives it, reply "Hi!" from your phone; the phone sends "Hello!", and when the computer receives it, reply "Hello!" from your computer.

Img

Image not found

Project 36 WiFi Operating Modes

In today's rapidly developing technological era, the method of uniformly controlling smart devices through mobile phones is gradually gaining popularity. This method uses a microcontroller to establish a connection between the phone and smart devices through WiFi modules and the Internet, thereby achieving remote control of smart devices. Our development board has a built-in WiFi module. In this chapter, we will focus on ESP32 WiFi development.

The ESP32 has 3 different WiFi operating modes: Station mode, AP mode, and AP+Station mode.

Special note: In this project, we only cover the ESP32 WiFi Station mode.

Project 36.1: WiFi Station Mode

1. Experiment Components:

Image not found Image not found
USB cable x1 ESP32x1

2. Experiment Wiring:

Use a USB cable to connect the ESP32 board to a USB port on the computer.

Image not found

3. Component Knowledge:

Station mode: When the ESP32 selects Station mode, it acts as a WiFi client. It can connect to a router network and communicate with other devices on the router via the WiFi connection. As shown in the figure below, the PC and router are already connected. If the ESP32 wants to communicate with the PC, it needs to connect the PC and router.

Image not found

4. Project Code:

You can open the code we provided, or you can write the code yourself, as follows:

  1. Drag out ""Img"" from ""Img"".

  2. Drag out " Img " from " Img " and place it into " Img ", set baud rate to 115200.

  3. Drag out ""Img"" from ""Img"".

Complete code:

Img

Special note: Since WiFi names and passwords vary by region, users need to enter their own WiFi name and password in the box shown below before running the program code.

Img

5. Project Phenomenon:

After confirming that you have correctly entered your WiFi name and password, compile and upload the code to the ESP32 board. After uploading is successful, click the icon Img to enter the serial monitor and set the baud rate to 115200. When the ESP32 successfully connects to WiFi, the serial monitor will print the IP address assigned to the ESP32 by the WiFi. Then the serial monitor window will display as follows: (If the serial monitor window does not display the following information after opening the serial monitor and setting the baud rate to 115200, you can press the reset button on the ESP32 Image not found)

Img

Project 37 WiFi Test

1. Experiment Introduction:

In this experiment, we first use the ESP32's WiFi Station mode to read the WiFi IP address, then connect to the WiFi via an APP to read the characters sent by the function buttons on the APP.

2. Experiment Components:

Image not found Image not found Image not found
ESP32*1 USB cable*1 Smartphone/tablet (self-provided)*1

3. Experiment Wiring:

Use a USB cable to connect the ESP32 board to a USB port on the computer.

Image not found

4. Install APP:

Only supports Android system devices (phone/tablet) APP:

5. Code Description:

Img

Enter WiFi name and password, connect to WiFi hotspot, and read the IP address assigned by WiFi.

Img

APP buttons send data via wireless WiFi, and the ESP32 receives the relevant data, i.e., reads WiFi data.

6. Project Code:

Special note: Ensure that the computer network, phone/tablet network, ESP32 board, router, and the WiFi name and password you entered in the code are all on the same local area network (WiFi).

Img

You can open the code we provided, or you can write the code yourself, as follows:

  1. Drag out ""Img"" from ""Img"".

  2. Drag out ""Img"" from ""Img"" and place it into ""Img"", set baud rate to 115200.

  3. First, drag out ""Img"" from " Img" and place it into ""Img"", change "Integer" to "String"; then drag out ""Img"" from ""Img"" and place it into ""Img"", delete "hello".

Img

  1. Drag out ""Img"" from ""Img"".

  2. First, drag out ""Img"" from " Img", then drag out ""Img"" from ""Img"".

Img

  1. First, drag out ""Img"" from ""Img"", then drag out ""Img"" from ""Img"".

Img

Complete code:

Img

Special note: You need to first change the user WiFi name (SSID) and user WiFi password (PASSWD) in the project code Img to your own WiFi name and password.

7. Experiment Phenomenon

Special note: Ensure that the computer network, phone/tablet network, ESP32 board, router, and the WiFi name and password you entered in the code are all on the same local area network (WiFi).

Open the serial monitor and set the baud rate to 115200. The serial monitor will print the detected WiFi IP address, then open the WiFi APP. Enter the detected WiFi IP address in the text box in front of the WIFI button (for example, the IP address detected by the serial monitor above: 192.168.8.210), then click the connect button. If the connection is successful, it will display "Connected" as shown in the figure below.

Image not found

Click each function button on the APP with your hand, and the serial monitor will print the corresponding received characters.

Img

Note: If the serial monitor window does not display the following information after opening the serial monitor and setting the baud rate to 115200, you can press the reset button on the ESP32 Image not found. If it still doesn't work, please close and reopen the serial monitor.

Project 38 WiFi Smart Home

1. Experiment Description:

In the previous experiments, we have learned about the three modes of the ESP32: WiFi Station mode, WiFi AP mode, and WiFi AP+Station mode. In this chapter's experiment, we will use the ESP32's WiFi Station mode to connect to WiFi via an APP to control multiple sensors/modules, achieving a WiFi smart home effect.

2. Experiment Equipment:

Image not found Image not found Image not found Image not found
ESP32*1 Breadboard*1 130 DC motor module*1 Relay module*1
Image not found Image not found Image not found Image not found
Servo*1 Temperature and humidity sensor*1 Ultrasonic sensor*1 Dupont wires (several)
Image not found Image not found Image not found Image not found
Smartphone/tablet (self-provided)*1 Breadboard power module*1 6-cell AA battery box*1 Fan blade*1
Image not found Image not found Image not found
AA batteries (self-provided)*6 MicroUSB cable*1 Jumper wires (several)

3. Experiment Wiring Diagram:

Relay ESP32 Board Temperature and Humidity Sensor ESP32 Board
Gnd Gnd - G
Vcc 3.3V + 5V
IN IO32 out IO15
Ultrasonic Sensor ESP32 Board 130 Fan Module ESP32 Board
Vcc 5V G G
Trig IO14 V 5V
Echo IO27 INA IO18
Gnd GND INB IO19
Servo ESP32 Board
Red wire 5V
Brown wire G
Orange wire IO4

Image not found

4. Project Code:

See the example source code in the downloaded archive for details.

Special note: You need to first change the user WiFi name and user WiFi password in the experiment code Image not found to your own WiFi name and password.

5. Experiment Phenomenon:

Special note: Ensure that the computer network, phone/tablet network, ESP32 board, router, and the WiFi name and password you entered in the code are all on the same local area network (WiFi).

After confirming that the WiFi name and password in the program code are correctly modified, compile and upload the code to the ESP32 board.

Open the serial monitor and set the baud rate to 115200. The serial monitor will print the detected WiFi IP address. (Note: If the serial monitor window does not display the following information after opening the serial monitor and setting the baud rate to 115200, you can press the reset button on the ESP32 Image not found. If there is still no output, you can close and reopen the serial monitor, select baud rate 115200, and then press the EN button on the board to try again.

Image not found

Install the APP on your phone or tablet, and enter the IP address from the serial monitor into the APP.

Img

For more details, please refer to the video tutorial "WiFi Smart Home."

Project 39 MQTT IoT

1. Experiment Description:

In the previous experiments, we have mastered the basics of WiFi and built a prototype of a smart home project within a local area network.

This lesson will focus on IoT-related knowledge, further improving the smart home project, and ultimately implementing a cross-network remote anti-theft alarm system.

2. IoT Knowledge:

IoT technology is currently widely used in industry, healthcare, and daily life, mainly enabling terminals such as mobile phones and computers to obtain and control various devices in real time.

In addition to the main boards and various sensors we have encountered in the course, there is also a communication technology involved: MQTT. This lesson will use MQTT communication technology to achieve real-time interaction between a mobile phone and a PIR sensor.

The core of IoT is to enable communication between mobile phones, computers, and hardware devices.

In this tutorial, we can understand the entire process as a "message transmission chain":

Phone App → MQTT Server → ESP32 Board → LED Light / Relay / Sensor

Img

The MQTT server is a free test server provided by Qiguopai, deployed on the internet. Both the app and the ESP32 board connect to this server via WiFi.

You can think of it as a "message relay station":

  • App and Esp32 agree on an identifier;

  • App sends the identifier + message to the MQTT server;

  • The server then forwards the message to the Esp32 board with that identifier;

2. Experiment Equipment:

Image not found Image not found Image not found
ESP32*1 Breadboard*1 PIR sensor*1
Image not found Image not found
LED bulb*1 220Ω or 1K resistor
Image not found Image not found Image not found
Smartphone/tablet (self-provided)*1 Dupont wires (several) Jumper wires (several)

3. Experiment Wiring Diagram:

PIR Sensor ESP32 Board
+ 3.3V
- GND
OUT IO35
LED Bulb ESP32 Board
Negative terminal GND
Positive terminal IO23

3. Experiment Code:

Open the code in the project resource package, then install the IoT protocol library PubSubClient. For code analysis, please refer to the video explanation.

If you want to learn APP development, you can download the following source code. If you are learning microcontroller development, you only need to install the package.

Image not found

4. Experiment Phenomenon:

When someone enters the detection range of the PIR sensor, the LED light will turn on and send data to the phone APP.

For a complete demonstration, please refer to the video tutorial.

Image not found

Image not found

5. Extension Thinking:

  • Can more sensors be connected? For example, a gas sensor can detect whether there is a gas leak in the kitchen in real time;

  • Connect an external 6V power adapter to solve the long-term power supply problem;

  • 3D print a storage box, so it has a beautiful appearance and looks more like a market-ready product.