Skip to content

Mixly Tutorial

Project 01 Hello World

1.Project Introduction:

For beginners of ESP32, 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 ESP32.

2.Project Components:

ESP32*1 USB Cable*1

3.Project Wiring:

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

4.Code Description:

Img

Set the serial baud rate, which is generally set to 9600.

Img

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

Img

Output data from the serial port without line wrapping.

Img

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

5.Project Code:

You can open the code we provide. The code is saved at (i.e., the path): ..\program-code\mixly-code. ( The paths for saving the subsequent project codes are the same, i.e., the same as below。)

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 ", changing 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 main board model " ESP32 Dev Moduel " and the serial port (COM6) (Tip: On different computers, the serial port is different). (Note:Connect the ESP32 board to the computer via a USB cable before you can see the corresponding port.) (The steps for uploading the subsequent project codes are the same, i.e., the 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 fails, you can click Img again and then press and hold the Boot key on the ESP32 board with your hand. Release the Boot key after the upload progress percentage appears, as shown in the image below:

Img

The project code was uploaded successfully!

Img

6.Project Result:

After the project code is uploaded successfully, click the icon Img to enter the serial monitor and set the baud rate to 9600, so that the serial monitor prints "Hello World!".

Img

Img

Img

Project 02 Light Up an 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 so that the LED is lit.

2.Project Components:

ESP32*1 Breadboard*1 Red LED*1
220Ω Resistor*1 Jumper Wires*2 USB Cable*1

3.Component Knowledge:

(1)LED

An LED is a semiconductor known as a "light-emitting diode", an electronic device made of semiconductor materials (silicon, selenium, germanium, etc.). It has a positive electrode and a negative electrode. The short leg is the negative electrode, connected to GND, and the long leg is the positive electrode, connected to 3.3V or 5V.

(2)Five-color-ring 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 used to represent the resistor in a circuit. The unit of resistance (R) is the ohm (Ω), and 1 mΩ= 1000 kΩ, 1kΩ= 1000Ω.

We can use resistors to protect sensitive components, such as LEDs. The strength of the resistor (in Ω) is marked on the body of the resistor with small color rings. Each color represents a number, and you can look it up with a resistor reference card.

In this kit, we provide 3 five-color-ring resistors with different resistance values. Here we take the 3 five-color-ring resistors as an example: 220Ω Resistor×10

10KΩ Resistor×10

1KΩ Resistor×10

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

Do not connect a resistor with a very low resistance value directly across the two poles of the power supply, as this will cause the current to be too high and damage the electronic components. A resistor has no distinction between positive and negative poles.

(3)Breadboard

A breadboard is an important tool used for building circuits in the laboratory. A breadboard has many holes into which circuit elements such as integrated circuits and resistors can be inserted. Proficiency in the use of the breadboard is one of the important foundations for improving experimental efficiency and reducing the probability of experimental faults. The following is a brief introduction to the structure and usage of the breadboard. A typical breadboard is shown below:

The appearance and internal structure of the breadboard are shown in the figure above. The common smallest-unit breadboard is divided into three parts: upper, middle, and lower. The upper and lower parts are generally narrow strips composed of one or two rows of sockets, and the middle part is a strip composed of an isolation groove in the middle and 5 rows of sockets above and below.

The two narrow strips of the breadboard each have two rows of sockets. The two rows are not connected to each other and are generally used as the path for introducing power. The first row marked with "+" on the top has 10 groups of sockets (all internally connected), all of which are positive poles; the second row marked with "-" has 10 groups of sockets (all internally connected), all of which are grounded. The first row and second row at the bottom of the breadboard have the same structure as above. If the entire breadboard needs to be used, usually connect "+" to "+" with a wire and "-" to "-" with a wire.

The wide strip in the middle is composed of an isolation groove in the middle and 5 rows of sockets above and below. The 5 sockets in the same column are connected to each other, while columns are not connected to each other, and the upper and lower parts of the groove are not connected. The appearance and structure are shown below:

The connection holes of the wide strip in the middle are divided into upper and lower parts, which are the main working area of the breadboard, used to plug in components and jumper wires. The 5 sockets in the same column (i.e., a-b-c-d-e, f-g-h-i-j) are connected to each other; columns are not connected to each other, and the upper and lower parts of the groove are not connected. When doing experiments, usually a small unit composed of two narrow strips and one wide strip is used. The main part of the circuit is built on the wide strip; one row of the upper narrow strip is used as power, and one row of the lower narrow strip is used as ground. The middle wide strip is used to connect the circuit. Since the upper and lower parts of the groove are not connected, integrated blocks 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 via a USB cable.

4.Project Wiring Diagram:

First, cut off all power to 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 via a USB cable.

Note:Avoid any possible short circuit (especially connecting 3.3V and GND)!

Warning: A short circuit may cause a large current in the circuit, causing the components to overheat and cause permanent damage to the hardware.

Note:

How to connect the LED

How to identify the five-color-ring 220Ω resistor

5.Code Description:

Img

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

6.Project Code:

You can open the code we provide. The code is saved at (i.e., the path): ..\program-code\mixly-code .(The paths for saving the subsequent project codes are the same, i.e., the same as below。)

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 ", with the pin set to 15 and set to "LOW".

  3. Drag out " Img " again from " Img ", with the pin set to 15 and set to "HIGH".

Complete code:

Img

7.Project Result:

After the project code is uploaded successfully, power it via the USB cable, and the LED is lit.

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:

ESP32*1 Breadboard*1 Red LED*1
220Ω Resistor*1 Jumper Wires*2 USB Cable*1

3.Project Wiring Diagram:

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

Note:Avoid any possible short circuit (especially connecting 3.3V and GND)!

Warning: A short circuit may cause a large current in the circuit, causing the components to overheat and cause permanent damage to the hardware.

Note:

How to connect the LED

How to identify the five-color-ring 220Ω resistor

4.Project Code:

You can open the code we provide: the code is saved at (i.e., the path): ..\program-code\mixly-code .(The paths for saving the subsequent project codes are the same, i.e., the same as below。)

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 ", with the pin set to 15 and set to "LOW".

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

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

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

Complete code:

Img

5.Project Result:

After the project code is uploaded successfully, power it up via the USB cable. The observable result is: the LED in the circuit blinks repeatedly.

Project 04 Breathing LED

1.Project Introduction:

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

2.Project Components:

ESP32*1 Breadboard*1 Red LED*1
220Ω Resistor*1 Jumper Wires*2 USB Cable*1

3.Component Knowledge:

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 sequence composed of a series 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 changing instantly from 0 to 10. However, the value of a digital signal can change instantly. This change is represented digitally as 1 and 0 (the basis of binary code). As shown in the figure below, we can more easily see the difference between them.

In practical applications, we often use binary as a digital signal, i.e., a series of 0s and 1s. Because a binary signal has only two values (0 or 1), it has great stability and reliability. Finally, analog signals and digital signals can be converted into 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 into analog signals) very convenient. PWM technology uses digital pins to send a square wave of a certain frequency, i.e., the output of high level and low level alternates for a period of time. The total time of each group of high level and low level is generally fixed and is called a period (note: the reciprocal of the period is the frequency). The time during which the high level is output is usually called the pulse width, and the duty cycle is the percentage of the ratio of the pulse width (PW) to the total period (T) of the waveform. The longer the high-level output lasts, the longer the duty cycle, and the higher the corresponding voltage in the analog signal. The figure below shows how the analog signal voltage corresponding to a pulse width of 0%-100% varies between 0V-3.3V (high level is 3.3V).

The longer the PWM duty cycle, the higher the output power. Since we understand this relationship, we can use PWM to control the brightness of an LED or the speed of a DC motor, and so on. From the above, we can see that PWM is not a real analog signal; the effective value of the voltage is equivalent 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, and each channel can independently control the 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 the maximum frequency and the bit precision is shown in the following formula:

where the maximum value of the bit is 31. For example: generate 8-bit precision PWM (2ˆ8 = 256. The value range is 0 ~ 255), and the maximum frequency is 80,000,000/255 = 312,500Hz.)

4.Project Wiring Diagram:

5.Code Description:

Img

Set the channel for the specified pin, and the assigned value can be 0 ~ 255.

Img

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

Img

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

Img

Set a variable i. i gradually increases from 0 to 255, increasing by 5 each time, for a total of 51 times, and each time it increases by 5 at a frequency of 10 milliseconds, the LED gradually brightens.

Img

Set a variable i. i gradually decreases from 255 to 0, decreasing by 5 each time, for a total of 51 times, and each time it decreases by 5 at a frequency of 10 milliseconds, 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 open the code we provide. The code is saved at (i.e., the path): ..\program-code\mixly-code .(The paths for saving the subsequent project codes are the same, i.e., the same as below。)

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 ", with the pin set to 15, the channel set to CH0(LT0), and the value assigned 0.

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

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

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

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

Complete code:

Img

7.Project Result:

After the project code is uploaded successfully, power it up via the USB cable. The observable result is: the LED in the circuit gradually brightens from dim, then gradually dims from bright, just like breathing.

Project 05 Traffic Light

1.Project Introduction:

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

2.Project Components:

ESP32*1 Breadboard*1 Red LED*1 Yellow LED*1
Green LED*1 220Ω Resistor*3 Jumper Wires (several) USB Cable*1

3.Project Wiring Diagram:

4.Project Code:

You can open the code we provide. The code is saved at (i.e., the path): ..\program-code\mixly-code .(The paths for saving the subsequent project codes are the same, i.e., the same as below。)

You can also write the code yourself, as follows:

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

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

Img

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

Img

  1. Drag out " Img " from " Img ", and change "from 1 to 10 with a step of 1" to "from 1 to 3 with a step of 1".

  2. Copy the code block " Img " 1 time and place it into " Img ", changing 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, changing the red LED from "HIGH" to "LOW", and changing the green LED from "LOW" to "HIGH".

Img

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

Complete code:

Img

5.Project Result:

After the project code is uploaded successfully, power it up via the USB cable. The observable result is: 1. First, the red light stays on for 5 seconds and then goes off; 2. Second, the yellow light blinks 3 times and then goes off; 3. Then, the green light stays on for 5 seconds and then goes off; 4. Continue running the above steps 1-3.

Project 06 RGB LED

1.Project Introduction:

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

2.Project Components:

ESP32*1 Breadboard*1 RGB LED*1
220Ω Resistor*3 Jumper Wires (several) USB Cable*1

3.Component Knowledge:

Most displays follow the RGB color standard. All colors on a computer screen are composed of red, green, and blue in different proportions.

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 ESP's PWM pins. The PWM pins will give the RGB LED signals of different duty cycles to obtain different colors.

If we use three 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:

5.Code Description:

Img

Set the 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 provide, or write the code yourself, as follows:

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

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

Img

  1. Drag out " Img " from " Img ", and change "from 1 to 10 with a step of 1" to "from 0 to 255 with a step of 1".

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

Img

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

Img

Complete code:

Img

7.Project Result:

Compile and upload the code to the ESP32. After the code is uploaded successfully, power it up via the USB cable. The observable result is: the RGB LED starts displaying random colors.

Project 07 Flowing Light

1.Project Introduction:

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

2.Project Components:

ESP32*1 Breadboard*1 Red LED*10
220Ω Resistor*10 Jumper Wires (several) USB Cable*1

3.Project Wiring Diagram:

Img

4.Project Code:

This project is designed to make a flowing light. These are the actions: 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 "motion" of flowing water.

You can open the code we provide, or 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 ", changing "item" to " i ".

Img

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

Img

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

Img

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

Img

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

Img

Complete code:

Img

5.Project Result:

After the code is uploaded successfully, power it up via the USB cable. The observable result is: the 10 LEDs will light up from right to left, and then return from left to right.

Img

Img

Project 08 Single-Digit Display

1.Project Introduction:

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

2.Project Components:

ESP32*1 Breadboard*1 Single-digit display*1
220Ω Resistor*8 Jumper Wires (several) USB Cable*1

3.Component Knowledge:

Single-digit display principle: A digital display is a semiconductor light-emitting device. Its basic unit is a light-emitting diode (LED). According to the number of segments, digital 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 the decimal point). Each segment of a seven-segment LED display is an individual LED. According to the wiring method of the LED units, digital displays can be divided into common-anode digital displays and common-cathode digital displays.

In a common-cathode 7-segment display, all the cathodes (or negative electrodes) 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 electrodes) of the LEDs of the segments 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.

Each segment of the digital display is composed 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 an ordinary common-cathode single-digit 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 direction of the seven-segment display inserted into the breadboard should be consistent with the wiring diagram, with an extra dot in the lower right corner.

5.Code Description:

Img

Initialize the pins of the single-digit display, a two-dimensional array, and so on.

Img

This is the single-digit display instruction block for displaying a digit, which can display 1 digit: 0 ~ 9.

6.Project Code:

The digital display is divided into 7 segments, and the decimal point display is divided into 1 segment. When certain digits are displayed, the corresponding segments will be lit. For example, when displaying the digit 1, segments b and c will be turned on.

You can open the code we provide, or 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 ", and change the digit 0 to 9.

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

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

Complete code:

Img

7.Project Result:

Compile and upload the code to the ESP32. After the code is uploaded successfully, power it up via the USB cable. The observable result is: the single-digit display will show digits from 9 to 0.

Project 09 Four-Digit Display

1.Project Introduction:

A four-digit display is a very practical display device. The display of electronic clocks, scorekeepers on sports fields, and crowd counts in parks all require it. Because of its low price and ease of use, more and more projects will use the 4-digit display. In this project, we use the ESP32 to control a four-digit display to show digits.

2.Project Components:

ESP32*1 Breadboard*1 Four-digit display*1
220Ω Resistor*8 Jumper Wires (several) USB Cable*1

3.Component Knowledge:

Four-digit display: There are two types of four-digit displays: common-anode and common-cathode. The display principle is similar to that of a single-digit display. Both use 8 GPIO ports to control the display segments of the digital display, i.e., 8 LED lights. However, since it is a 4-digit display here, 4 additional GPIO ports are needed to control the bit-selection end, that is, to select which single digital display is lit. The switching of the digits is very fast, and the naked eye cannot distinguish it, so it looks like multiple digital displays are showing at the same time.

Our four-digit display is common-cathode.

The figure below is the pin diagram of the 4-digit display. G1, G2, G3, and G4 are the pins that control the digits.

The figure below is the internal wiring schematic of the 4-digit display

4.Project Wiring Diagram:

5.Project Code:

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

  1. Drag out " Img " from " Img ", and change the digit 0 to 3456.

Complete code:

Img

6.Project Result:

After the code is uploaded successfully, power it up via the USB cable. The observable result is: the four-digit display shows the four digits 3456.

Img

Project 10 8×8 Dot Matrix Display

1.Project Introduction:

A dot matrix display is an electronic digital display device that can display 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:

ESP32*1 Breadboard*1 8×8 dot matrix display*1
220Ω Resistor*8 Jumper Wires (several) USB Cable*1

3.Component Knowledge:

8×8 dot matrix: It is composed of 64 LED lights, with two types: row common-anode and row common-cathode. Our module is row common-anode, that is, each row has a line connecting the positive electrodes of the LEDs together, and the columns connect the negative electrodes of the LED lights together. See the figure below:

Each LED is placed at the intersection of a row and a column. When the level of a certain row is 1 and the level of a certain column is 0, the corresponding LED will light up. If you want to light up the LED at the first point, you should set pin 9 to high level and pin 13 to low level. If you want to light up the LEDs in the first row, you should set pin 9 to high level and set pins 13, 3, 4, 10, 6, 11, 15, and 16 to low level. If you want to light up the LEDs in the first column, set pin 13 to low level and set pins 9, 14, 8, 12, 1, 7, 2, and 5 to high level.

The external view of the 8×8 dot matrix display is shown below:

4.Project Wiring Diagram:

5.Code Description:

Img

Initialize the pins and two-dimensional array of the 8*8 dot matrix, and so on.

Img

The 8*8 dot matrix can display digits 0 ~ 9.

6.Project Code:

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

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

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

Img

  1. First drag out " Img " from " Img "; then drag out " Img " from " Img ", with a delay of 2000 milliseconds.

Img

  1. Copy the code block " Img " 9 times, and change the digit 0 to 1, 2, 3, 4, 5, 6, 7, 8, 9 in order.

Complete code:

Img

7.Project Result:

Compile and upload the code to the ESP32. After the code is uploaded successfully, power it up via the USB cable. The observable result is: the 8*8 dot matrix display shows the digits 0~9 in order, cycling continuously.

Project 11 Shift Register Controlling 8 LEDs

1.Project Introduction:

In the previous projects, we have already learned how to light up a single LED.

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

2.Project Components:

ESP32*1 Breadboard*1 74HC595N chip*1 Red LED*8
220Ω Resistor*8 Jumper Wires (several) USB Cable*1

3.Component Knowledge:

74HC595N chip: This chip has an 8-bit shift register and a memory, as well as three-state output functionality. In simple terms, its biggest role is to "expand multiple output ports with a small number of I/O ports". Normally, controlling 8 LEDs requires occupying 8 GPIO pins of the ESP32. But with 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 look at related materials Click to jump

Img

Pin Description:

Pin: Pin Description:
Pin 13 OE It is an output enable pin, used to ensure whether the data of the latch is input to the Q0-Q7 pins. At low level, it does not output a high level. In this experiment, we connect it directly to GND to keep it at low level and output data.
Pin 14 SI/DS This is the pin through which the 74HC595 receives data, i.e., the serial data input terminal. It can input only one bit at a time, so by inputting 8 times in a row, a byte can be formed.
Pin 10 SCLR/MR A pin for initializing the storage register. 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 time, data is output in parallel from the Q0~Q7 ports.
Pin 9 SQH This pin is a serial output pin, dedicated to chip cascading, connected to the SI terminal of the next 74HC595.
Q0--Q7(Pin 15, Pins 1-7) Eight-bit parallel output terminals, which can directly control the 8 segments of the digital display.

4.Project Wiring Diagram:

Note:Pay attention to the direction in which the 74HC595N chip is inserted.

5.Project Code:

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

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

  2. First drag out " Img " from " Img " 3 times and place it into " Img "; then drag out " Img " from " Img " 3 times and place it into " Img ", changing item to dataPin, latchPin, and clockPin respectively, and changing the number 0 after the corresponding assignment to 14, 12, and 13.

Img

  1. Drag out " Img " from " Img ", and change "from 1 to 10 with a step of 1" to "from 0 to 255 with a step of 1".

  2. Drag out " Img ", " Img ", " Img " respectively from " Img " and place them into " Img ", then drag out " Img ", " Img ", " Img ", " Img " respectively from " Img ", and place " Img " in the corresponding positions in order; change the "HIGH" after the first pin latchPin to "LOW", and change "most significant bit first" to "least significant bit first".

Img

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

Complete code:

Img

6.Project Result:

Compile and upload the code to the ESP32. After the code is uploaded successfully, power it up via the USB cable. The observable result is: the 8 LEDs start blinking in a flowing pattern.

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, and so on. 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 an active buzzer to emit a "beep beep" sound.

2.Project Components:

ESP32*1 Breadboard*1 Active buzzer*1 NPN transistor (S8050)*1
1KΩ Resistor*1 Jumper Wires (several) USB Cable*1

3.Component Knowledge:

Active buzzer: It has a simple oscillator circuit inside that can convert a constant DC current into a pulse signal of a specific frequency. Once the active buzzer receives a high level, it will produce sound. A passive buzzer is an integrated electronic buzzer without an internal vibration source. It must be driven by a 2K-5K square wave rather than 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 positive polarity, while an active buzzer can, as shown below:

Transistor: Because the buzzer needs a large current, the GPIO of the ESP32's output capability cannot meet the requirement, so 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 and can also be used as a switch. It has three electrodes (pins): base (b), collector (c), and emitter (e). When current passes between "be", "ce" will allow several times the current to pass through (transistor amplification). At this time, the transistor works in the amplification region. When the current between "be" exceeds a certain value, "ce" will no longer allow the current to increase, and the transistor works in the saturation region. There are two types of transistors as shown below: PNP and NPN

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

Based on the characteristics of the transistor, it is often used as a switch in digital circuits. Since the current output capability of the microcontroller is very weak, we will use the transistor to amplify the current and drive components that require large currents. When using an NPN transistor to drive a buzzer, the following method is usually used: if the GPIO outputs a high level, current will flow through R1, the transistor will conduct, and the buzzer will make a 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 used: if the GPIO outputs a low level, current will flow through R1, the transistor will conduct, and the buzzer will make a sound. If the GPIO outputs a high level, no current flows through R1, the transistor will not conduct, and the buzzer will not sound.

4.Project Wiring Diagram:

Note: In this circuit, the power supply for the buzzer is 5V. Under a 3.3V power supply, the buzzer can work, but the loudness will be reduced.

5.Code Description:

Img

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

6.Project Code:

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

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

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

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

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

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

Complete code:

Img

7.Project Result:

Compile and upload the code to the ESP32. After the code is uploaded successfully, power it up via the USB cable. The observable result is: the active buzzer emits a "beep beep" sound.

Project 13 Passive Buzzer

1.Project Introduction:

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

2.Project Components:

ESP32*1 Breadboard*1 Passive buzzer*1 NPN transistor (S8050)*1
1KΩ Resistor*1 Jumper Wires (several) USB Cable*1

3.Component Knowledge:

Passive buzzer: It is an integrated electronic buzzer without an internal vibration source. It must be driven by a 2K-5K square wave rather than a DC signal. Its appearance is very similar to the 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 positive polarity, while an active buzzer can, as shown below:

Transistor: Please refer to Project 12 .

4.Project Wiring Diagram:

5.Code Description:

Img

Turn off the passive buzzer for the specified pin, so that the buzzer does not sound.

Img

Set the tone and beat of the sound produced by the passive buzzer for the specified pin.

Img

Set the passive buzzer to play specific music for the specified pin.

6.Project Code:

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

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

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

  3. First drag out " Img " from " Img ", with the pin set to 15, and select "Jingle Bells"; then drag out " Img ", with the pin set to 15.

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

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

Complete code:

Img

6.Project Result:

Compile and upload the code to the ESP32. After the code is uploaded successfully, power it up via the USB cable. The observable result is: the passive buzzer plays music.

Project 14 Small Desk Lamp

1.Project Introduction:

Do 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:

ESP32*1 Breadboard*1 Button*1
10KΩ Resistor*1 Red LED*1 220Ω Resistor*1
Button cap*1 Jumper Wires (several) USB Cable*1

3.Component Knowledge:

Button: A button can control the on/off of a circuit. When the button is connected to a circuit, the circuit is disconnected when the button is not pressed,

but once the button is pressed, the circuit is connected, and when it is released, it is disconnected again. But why is it only conductive when pressed? This has to be explained from the internal structure of the button. Before it is pressed, the current cannot pass from one end of the button to the other; when pressed, the metal pieces inside the button connect the two sides to let the current pass through.

The internal structure of the button is shown in the figure:, before the button is pressed, 1 and 2 are conductive, and 3 and 4 are also conductive, but 1-3 or 1-4 or 2-3 or 2-4 are disconnected (non-conductive); only when the button is pressed are 1-3 or 1-4 or 2-3 or 2-4 conductive.

When designing circuits, the button switch is one of the most commonly used components.

Button schematic diagram:

What is button bounce?

The switch circuit we imagine is "press the button - immediately conduct" and "press again - immediately disconnect", but in reality this is not the case. Buttons usually use mechanical elastic switches, and at the instant when the mechanical contact opens or closes (usually about 10ms), a series of bounces occurs due to the elastic effect, causing the button switch to not stably connect the circuit immediately when closed, and not instantly disconnect completely when opened.

So how do we eliminate button bounce?

There are two commonly used debounce methods: software method and hardware method. Here we will 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 effect of debouncing.

So we add a 0.02-second delay in the code to implement the button debounce function.

4.Project Wiring Diagram:

Note:

How to connect the LED

How to identify the five-color-ring 220Ω resistor and the five-color-ring 10KΩ resistor

5. Code Description:

Img

Read the digital signal (high/low level) of the button switch from the specified digital pin.

Img

The value of a Boolean (bool) variable is only 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 know that the initial value of ledState is 0.

Img

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

Img

The role of the delay here is software debouncing. When the mechanical contacts of the button open and close, due to the elastic effect of the contacts, the button switch will not stably connect or disconnect immediately, and a series of bounces occur at the instant of closing and opening. The measure taken to prevent this phenomenon is button debouncing. In the code, after detecting that the key is closed, a delay program is executed, a 10ms delay, to let the leading-edge bounce disappear, and then the state of the key is detected again. If it still maintains the closed-state level, it is confirmed that a key is truly pressed.

6.Project Code:

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

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

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

Img

  1. First drag out " Img " from " Img " and place it into " Img ", changing "Integer" to "Boolean" and "item" to "ledState"; then drag out " Img " from " Img " and place it into " Img ", and 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 " = ", with the pin set to 15; finally drag out " Img " from " Img " and place it on the right side of " = ".

Img

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

Img

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

Img

  1. Copy the code block " Img " 1 time, remove the 10-millisecond 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 ", with the pin set to 4, then drag out " Img " from " Img " and place it at "HIGH".

Img

Complete code:

Img

7.Project Result:

Compile and upload the code to the ESP32. After the code is uploaded successfully, power it up via the USB cable. The observable result is: press the button, and the LED lights up; when the button is released, the LED remains lit. Press the button again, and the LED goes off; when the button is released, the LED remains off. Isn't it just like a small desk lamp?

Project 15 Analog Hourglass

1.Project Introduction:

Ancient people did not have electronic clocks, so they invented the hourglass to measure time. The two sides of the hourglass have a relatively large capacity. Fine sand is placed on one side, and there is a very small channel in the middle. When the hourglass is placed upright with the side containing the fine sand on top, due to gravity, the fine sand flows down through the channel to the other side of the hourglass. When all the fine sand has flowed to the bottom, it is turned over, and the number of repetitions in a day is recorded. The next day, the approximate time of the day can be known from the number of times the hourglass flows repeatedly. In this lesson, we will use the ESP32 to control a tilt switch and LED lights to simulate an hourglass and make an electronic hourglass.

2.Project Components:

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

3.Component Knowledge:

A tilt switch is also called a digital switch or ball switch, and there is a metal ball inside it. It is used to detect small-angle tilts.

The principle is very simple: when the switch is tilted at a certain angle, the ball inside will roll downward and touch the two contacts connected to the external pins, thus triggering the circuit. Otherwise, the ball will move away from the contacts, thus disconnecting the circuit.

The internal structure of the tilt switch is used here to explain how it works, as shown in the figure below:

4.Project Wiring Diagram:

Note:

How to connect the LED

How to identify the five-color-ring 220Ω resistor and the five-color-ring 10KΩ resistor

5.Code Description:

Img

Read the digital signal (high/low level) of the tilt switch from the specified digital pin.

6.Project Code:

You can open the code we provide, or 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 ", changing "Integer" to "Byte" and changing "item" to "switch_state".

Img

  1. First drag out " Img " from " Img ", then drag out " Img " from " Img ", with the pin set 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 " , and change "from 1 to 10 with a step of 1" to "from 16 to 19 with a step of 1"; then drag out " Img " from " Img " and place it into " Img "; then drag out " Img " from " Img " and place it at "Pin 0 "; and 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 with a step of 1" to "from 19 to 16 with a step of -1", and change "HIGH" to "LOW".

Img

Complete code:

Img

7.Project Result:

Compile and upload the code to the ESP32. After the code is uploaded successfully, power it up via the USB cable. The observable result is: tilt the breadboard to a certain angle, and the LEDs will light up one by one. When it returns to the previous angle, the LEDs will turn off one by one. Just like an hourglass, over time, the sand leaks out.

Project 16 OLED Display

1.Project Introduction:

In life, we can use modules such as displays to do 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 the display and connect it to the ESP32 control board. We will use the ESP32 board to control the LCD_128X32_DOT display to show various English texts, common symbols, and digits.

Note: For accessories, we have upgraded the LCD screen to the more commonly used OLED screen, and the documentation is being updated continuously. If you find that the code runs incorrectly, please contact customer service to fix it.

2.Project Components:

ESP32*1 Breadboard*1 OLED screen*1
Male-to-female DuPont wires*4 USB Cable*1

3.Component Knowledge:

LCD_128X32_DOT: An LCD module with 128*32 pixels, and its driver chip is ST7567A. The module uses IIC communication. It can display not only English letters and symbols, but also Chinese text and patterns. When using it, you can also set it in the code so that English letters and symbols are displayed in different sizes.

LCD_128X32_DOT schematic diagram:

LCD_128X32_DOT technical parameters:

Display pixels: 128*32 characters

Operating voltage: DC 5V

Operating current: 100mA (5V)

Optimal operating voltage of the module: 5V

Brightness and contrast can be controlled through program instructions

4.Project Wiring Diagram:

6.Project Code:

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

  1. The display control is located as follows; drag it out directly to use:

Img

  1. The complete code is as follows:

Img

7.Project Result:

Compile and upload the code to the ESP32. After the code is uploaded successfully, power it up via the USB cable. The observable result is: the 128X32LCD module display shows "Hello World!" on the first line, "0123456789" on the second line, and "Using SoftI2C" on the third line.

Project 17 Small Fan

1.Project Introduction:

In the hot summer, we need an electric fan to cool us down. So in this project, we will use the ESP32 to control a DC motor and a small fan blade to make a small electric fan.

2.Project Components:

ESP32*1 Breadboard*1 DC motor*1 Breadboard power module*1 6-cell AA battery box*1
NPN transistor (S8050)*1 PNP transistor (S8550)*1 1KΩ Resistor*1 Diode*1 Three-blade soft propeller*1
Jumper Wires (several) AA batteries(bring your own)*6 USB Cable*1

3.Component Knowledge:

Diode: It is an electronic device made of semiconductor materials (silicon, selenium, germanium, etc.). It has two electrodes: the positive electrode, also called the anode, and the negative electrode, also called the cathode. When a forward voltage is applied across the two electrodes of the diode, the diode conducts; when a reverse voltage is applied, the diode blocks. The conduction and blocking of the diode are equivalent to the closing and opening of a switch.

The end with the gray ring is the negative electrode (-), and the other end is the positive electrode (+), as shown in the figure below:

The diode has unidirectional conductivity. When conducting, the current direction is from the anode through the tube to the cathode.

Breadboard power module:

Description:

This module can conveniently provide 3.3V and 5V power to the breadboard. It has a DC2.1 input (DC7-12V), and in addition, it has a USB Type C interface 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: 53mmx26.3mm

Environmental attribute: ROHS

Interface Description:

Motor wiring description:

Because the current batch of motors uses a standard interface that cannot be directly inserted into the breadboard, please use two DuPont wires to insert into the white interface to complete this experiment.

Image does not exist

Schematic diagram:

4. Project Wiring Diagram 1:

(This experiment uses the S8050 (NPN transistor) to control the motor)

(Note: First wire it up, and then install a small fan blade on the DC motor.)

5. Project Code 1:

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 ", with the pin set to 15 and set to "LOW".

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

  4. Drag out " Img " from " Img ", and set the delay to 4000 milliseconds.

  5. Copy the code block " Img " 1 time, and change "HIGH" to "LOW", and change 4000 milliseconds to 2000 milliseconds.

Complete code:

Img

6. Project Result 1:

Compile and upload the code to the ESP32. After the code is uploaded successfully, connect the external power supply. After powering up, the observable result is: the motor rotates for 4 seconds and stops for 2 seconds, repeating in this pattern.

7. Project Wiring Diagram 2:

(This experiment uses the S8550 (PNP transistor) to control the motor)

(Note: First wire it up, and then install a small fan blade on the DC motor.)

8. Project Code 2:

Img

9. Project Result 2:

Compile and upload the code to the ESP32. After the code is uploaded successfully, connect the external power supply. After powering up, the observable result is: the motor rotates for 4 seconds and stops for 2 seconds, repeating in this pattern.

Project 18 Dimming 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 resistor. In a potentiometer, the entire input voltage is applied across the entire length of the resistor, and the output voltage is the voltage value 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 dimming light.

2.Project Components:

ESP32*1 Breadboard*1 Adjustable potentiometer*1 Red LED*1
220Ω Resistor*1 USB Cable*1 Jumper Wires (several)

3.Component Knowledge:

Adjustable potentiometer: An adjustable potentiometer is a type of resistor and analog electronic component. It has two states, 0 and 1 (high level and low level). When the analog value is different, its data state presents a linear state such as 1 ~ 1024.

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

Segment 1: The analog quantity in the range of 0V—(3.3/4095)V corresponds to the digital value 0;

Segment 2: The analog quantity in the range of (3.3/4095)V—2*(3.3/4095)V corresponds to the digital value 1;

......

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

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 the DAC is useful. The ESP32 has two 8-bit-precision DAC output pins, GPIO25 and GPIO26, which can divide VCC (here 3.3V) into 2^8=256 parts. For example, when the digital quantity is 1, the output voltage value is 3.3/256×1V; when the digital quantity is 128, the output voltage value is 3.3/256 ×128=1.65V. The higher the precision of the DAC, the higher the precision of the output voltage value.

The conversion formula is as follows:

ADC on the ESP32 board:

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

DAC on the ESP32 board:

4.Read the analog value of the potentiometer:

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

Code Description:

Img

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

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

Img

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

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

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

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

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

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

Complete code:

Img

Compile and upload the code to the ESP32. After the code is uploaded successfully, power it up via the USB cable, click the icon Img to enter the serial monitor, and set the baud rate to 115200. The observable result is: the serial monitor window will print the analog value of the potentiometer. When you turn the potentiometer handle, the analog value changes.

Img

5.Wiring diagram of the dimming light:

In the previous step, we read the analog value of the potentiometer. Now we need to convert the ADC value of the potentiometer into the brightness of the LED to make a light with adjustable brightness. See the wiring diagram shown below:

6.Project Code:

Img

7.Project Result:

Compile and upload the code to the ESP32. After the code is uploaded successfully, power it up via the USB cable. The observable result is: turn the potentiometer handle, and the brightness of the LED changes accordingly.

Project 19 Flame Alarm

1.Project Introduction:

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

2.Project Components:

ESP32*1 Breadboard*1 Flame sensor*1 Red LED*1
Active buzzer*1 220Ω Resistor*1 10KΩ Resistor*1 Jumper Wires (several)
NPN transistor (S8050)*1 1kΩ Resistor*1 USB Cable*1

3.Component Knowledge:

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

Note:The flame sensor should avoid direct exposure to sunlight, car headlights, and incandescent lamps, and it 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 radio-frequency radiation interference.

4.Read 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 for the wiring:

Code Description:

Img

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

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

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

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

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

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

Complete code:

Img

Compile and upload the code to the ESP32. After the code is uploaded successfully, power it up via the USB cable, click the icon Img to enter the serial monitor, and set the baud rate to 115200. The observable result is: the serial monitor window will print the analog value read by the flame sensor. When the flame approaches the flame sensor, the analog value increases; otherwise, the analog value decreases.

Img

5.Wiring diagram of the flame alarm:

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

6.Project Code:

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

Img

7.Project Result:

Compile and upload the code to the ESP32. After the code is uploaded successfully, power it up via the USB cable. The observable result is: when the flame sensor detects a flame, the LED blinks and the buzzer alarms; otherwise, the LED does not light up and the buzzer does not sound.

Project 20 Night Light

1.Project Introduction:

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

2.Project Components:

ESP32*1 Breadboard*1 Photoresistor*1 Red LED*1
220Ω Resistor*1 10KΩ Resistor*1 Jumper Wires (several) USB Cable*1

3.Component Knowledge:

Photoresistor: It is a photosensitive resistor. The principle is that the brightness (light) received on the surface of the photoresistor reduces its resistance, and the resistance value of the photoresistor changes with the intensity of the detected ambient light. With this characteristic, we can use the photoresistor to detect light intensity. The photoresistor and its electronic symbol are as follows:

The following circuit is used to detect the change in the resistance value of the photoresistor:

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

4.Read 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 for the wiring:

Code Description:

Img

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

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

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

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

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

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

Complete code:

Img

Compile and upload the code to the ESP32. After the code is uploaded successfully, power it up via the USB cable, click the icon Img to enter the serial monitor, and set the baud rate to 115200. The observable result is: the serial monitor window will print the analog value read by the photoresistor. When the light intensity in the environment where the photoresistor is located is gradually reduced, the analog value gradually increases; otherwise, the analog value gradually decreases.

Img

5.Wiring diagram of the light-controlled lamp:

Earlier we made a small dimming light. Now let's make a light-controlled lamp. Their principles are the same, i.e., the ESP32 obtains the analog value of the sensor, and then adjusts the brightness of the LED.

6.Project Code:

Img

7.Project Result:

Compile and upload the code to the ESP32. After the code is uploaded successfully, power it up via the USB cable. The observable result is: when the light intensity in the environment where the photoresistor is located is reduced, the LED becomes brighter; otherwise, the LED becomes dimmer.

Project 21 Temperature Instrument

1.Project Introduction:

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

2.Project Components:

ESP32*1 Breadboard*1 OLED screen*1 Thermistor*1
10KΩ Resistor*1 Male-to-female DuPont wires*4 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.

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

Where:

Rt is the resistance of the thermistor at temperature T2;

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

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

B is the temperature index;

T1 and 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:

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

4.Read the value of the thermistor:

First, we learn to read the current ADC value, voltage value, and temperature value of the thermistor and print them out. Please wire it up according to the following wiring diagram:

Code Description:

Img

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

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

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

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

  3. First drag out " Img " from " Img "; then drag out " Img " from " Img "; change "automatic line wrapping" to "no line wrapping", and change "hello" to "Temperature: ".

  4. First drag out " Img " from " Img "; then drag out " Img " from " Img ", with the pin set to 36; and change "automatic line wrapping" to "no line wrapping".

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

Complete code:

Img

Compile and upload the code to the ESP32. After the code is uploaded successfully, power it up via the USB cable, click the icon Img to enter the serial monitor, and set the baud rate to 115200. The observable result 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 period of time, and you should see the temperature value increase.

Img

5.Wiring diagram of the temperature instrument:

6.Project Code:

Img

7.Project Result:

Compile and upload the code to the ESP32. After the code is uploaded successfully, power it up via the USB cable. The observable result is: the LCD 128X32 DOT screen displays the voltage value of the thermistor and the temperature value in the current environment.

Img

Project 22 Bluetooth

This chapter mainly introduces how to perform simple data transmission between the ESP32's Bluetooth and a mobile phone. Project 22.1 is regular Bluetooth, and Project 22.2 is Bluetooth-controlled LED.

Project 22.1 Regular Bluetooth

1. Project Components:

ESP32*1 USB Cable*1 Smartphone/tablet(bring your own)*1

In this project, we need to use a Bluetooth application called Serial Bluetooth Terminal to assist the experiment. If you have not installed it yet, please click to install: https://www.appsapk.com/serial-bluetooth-terminal/ .

Below is its logo.

2.Component Knowledge:

Bluetooth is a short-distance communication system, which can be divided into two types, namely 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, work is done on the master device, and it can connect to slave devices. We can search for and select nearby slave devices to connect. When a device initiates a connection request in master mode, it needs information such as the address and pairing password of the other Bluetooth device. After pairing is completed, it can connect to them directly.

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 can also receive data from the master device. Bluetooth devices can perform data interaction, with one device in master mode and the other in slave mode. When they perform data interaction, the Bluetooth device in master mode will search for and select nearby devices to connect to. When establishing a connection, they can exchange data. When a mobile phone exchanges data with the ESP32, the mobile phone is usually in master mode, and the ESP32 is in slave mode.

3.Project Wiring:

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

4.Project Code:

Reminder: Drag this code out from " Img " together, and change " ESP32BT " to " ESP32test ".

Img

5.Project Result:

Compile and upload the code to the ESP32. After uploading successfully, open the serial monitor and set the baud rate to 115200. When you see the serial print a string as shown below, it means the ESP32's Bluetooth is ready and waiting to connect with the mobile phone. (If after opening the serial monitor and setting the baud rate to 115200, the serial monitor window does not display the following information, you can press the reset key of the ESP32 )

Please confirm that your mobile phone has Bluetooth enabled and has installed the "Serial Bluetooth Terminal" Bluetooth application.

The mobile phone automatically searches for nearby Bluetooth devices. Click "ESP32 test" to pair, and a pairing dialog box appears. Click "Pair", and then the "ESP32 test" device is connected.

Open the software APP and click the left side of the terminal. Select "Devices".

Select ESP32test in Classic Bluetooth mode, and a connection-success prompt as shown below will appear.

Now, data can be transferred between your mobile phone and the computer through the ESP32.

Enter "Hi!" in the text box in the IDE. When the mobile phone receives it, reply "Hi!" to your mobile phone; the mobile phone sends "Hello!", and when the computer receives it, reply "Hello!" to your computer.

Img

Project 23 WiFi Working Modes

In today's era of rapidly developing technology, the method of uniformly controlling intelligent devices through mobile phones has gradually gained people's favor. This method uses the microcontroller to establish a connection between the mobile phone and the intelligent devices through the wifi module and the Internet network, thereby achieving remote control of the intelligent devices. Our development board has a built-in wifi module. In this chapter, we will focus on the WiFi development of the ESP32.

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

Special reminder: In this project, only the WiFi Station mode of the ESP32 is explained。

Project 23.1: WiFi Station Mode

1.Experimental Components:

USB Cable x1 ESP32x1

2.Experimental Wiring:

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

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 through the WiFi connection. As shown in the figure below, the PC and the router are already connected. If the ESP32 wants to communicate with the PC, the PC and the router need to be connected.

4.Project Code:

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

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

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

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

Complete code:

Img

Special reminder: Because the WiFi names and passwords differ from place to place, before running the program code, users need to enter their own WiFi name and password in the box shown in the figure below。

Img

5.Project Result:

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

Img