C - Tutorial 212 300
C - Tutorial 212 300
com █
Component List
Breadboard x1
Component knowledge
Stepper Motor
Stepper motors are an open-loop control device, which converts an electronic pulse signal into angular
displacement or linear displacement. In a non-overload condition, the speed of the motor and the location
of the stops depends only on the pulse signal frequency and number of pulses and is not affected by changes
in load as with a DC motor. A small four-phase deceleration stepper motor is shown here:
The outside case or housing of the stepper motor is the stator and inside the stator is the rotor. There are a
specific number of individual coils, usually an integer multiple of the number of phases the motor has, when
the stator is powered ON, an electromagnetic field will be formed to attract a corresponding convex
diagonal groove or indentation in the rotor’s surface. The rotor is usually made of iron or a permanent
magnet. Therefore, the stepper motor can be driven by powering the coils on the stator in an ordered
sequence (producing a series of “steps” or stepped movements).
In the course above, the stepper motor rotates a certain angle once, which is called a step. By controlling the
number of rotation steps, you can control the stepper motor rotation angle. By controlling the time between
two steps, you can control the stepper motor rotation speed. When rotating clockwise, the order of coil
powered on is: ABCDA…… . And the rotor will rotate in accordance with the order, step by step
down, called four steps four pats. If the coils is powered on in the reverse order, DCBAD… , the
rotor will rotate in anti-clockwise direction.
There are other methods to control stepper motors, such as: connect A phase, then connect A B phase, the
stator will be located in the center of A B, which is called a half-step. This method can improve the stability of
the stepper motor and reduces noise. The sequence of powering the coils looks like this: A ABB BCC
CDDDAA……, the rotor will rotate in accordance to this sequence at a half-step at a time, called
four-steps, eight-part. Conversely, if the coils are powered ON in the reverse order the stepper motor will
rotate in the opposite direction.
The stator in the stepper motor we have supplied has 32 magnetic poles. Therefore, to complete one full
revolution requires 32 full steps. The rotor (or output shaft) of the stepper motor is connected to a speed
reduction set of gears and the reduction ratio is 1:64. Therefore, the final output shaft (exiting the stepper
motor’s housing) requires 32 X 64 = 2048 steps to make one full revolution.
Circuit
When building the circuit, note that rated voltage of the stepper motor is 5V, and we need to use the
breadboard power supply independently. Additionally, the breadboard power supply needs to share Ground
with ESP32.
Schematic diagram
Hardware connection. If you need any support, please feel free to contact us via: [email protected]
Sketch
This code uses the four-step, four-part mode to drive the stepper motor in the clockwise and anticlockwise
directions.
Sketch_19.1_Drive_Stepper_Motor
Compile and upload the code to the ESP32-WROVER, the stepper motor will rotate 360° clockwise and stop
for 1s, and then rotate 360° anticlockwise and stop for 1s. And it will repeat this action in an endless loop.
The moveSteps function can control the direction of the stepper motor, the number of rotation steps, and the
speed of rotation. According to the previous knowledge, the stepper motor needs 32*64 steps for one
revolution. The speed of rotation is determined by the parameter ms. The larger the ms is, the slower the
rotation speed is. There is a range for the speed of the motor, which is determined by the motor itself and
according to our test, the value of ms is limited to 3-20.
20 //Suggestion: the motor turns precisely when the ms range is between 3 and 20
21 void moveSteps(bool dir,int steps, byte ms) {
22 for (unsigned long i = 0; i < steps; i++) {
23 moveOneStep(dir); // Rotate a step
24 delay(constrain(ms,3,20)); // Control the speed
25 }
26 }
The function moveTurns() is a further package of moveSteps(), which is used to control the stepper motor to
rotate a specified number of turns. The parameter "turns" represents the number of turns that need to be
rotated.
44 void moveAround(bool dir, int turns, byte ms){
45 for(int i=0;i<turns;i++)
46 moveSteps(dir,32*64,ms);
47 }
The function moveAround () is a further package of moveSteps (), which is used to control the stepper motor
to rotate by a specified angle, and the parameter "angle" represents the angle to be rotated.
48 void moveAngle(bool dir, int angle, byte ms){
49 moveSteps(dir,(angle*32*64/360),ms);
50 }
In the loop function, call the moveSteps function to loop the stepper motor: rotate clockwise one turn and
stop for 1s, then rotate counterclockwise one turn and stop for 1s.
11 void loop(){
12 // Rotate a full turn
13 moveSteps(true, 32 * 64, 3);
14 delay(1000);
15 // Rotate a full turn towards another direction
16 moveSteps(false, 32 * 64, 3);
17 delay(1000);
18 }
Chapter 20 LCD1602
In this chapter, we will learn about the LCD1602 Display Screen
Component List
Breadboard x1
Component knowledge
I2C communication
I2C (Inter-Integrated Circuit) is a two-wire serial communication mode, which can be used for the connection
of micro controllers and their peripheral equipment. Devices using I2C communication must be connected to
the serial data (SDA) line, and serial clock (SCL) line (called I2C bus). Each device has a unique address and
can be used as a transmitter or receiver to communicate with devices connected to the bus.
LCD1602 communication'
The LCD1602 display screen can display 2 lines of characters in 16 columns. It is capable of displaying numbers,
letters, symbols, ASCII code and so on. As shown below is a monochrome LCD1602 display screen along with
its circuit pin diagram
I2C LCD1602 display screen integrates a I2C interface, which connects the serial-input & parallel-output
module to the LCD1602 display screen. This allows us to only use 4 lines to the operate the LCD1602.
The serial-to-parallel IC chip used in this module is PCF8574T (PCF8574AT), and its default I2C address is
0x27(0x3F). You can also view the ESP32 bus on your I2C device address through command "i2cdetect -y 1".
Below is the PCF8574 pin schematic diagram and the block pin diagram:
PCF8574 chip pin diagram: PCF8574 module pin diagram
PCF8574 module pin and LCD1602 pin are corresponding to each other and connected with each other:
So we only need 4 pins to control the 16 pins of the LCD1602 display screen through the I2C interface.
In this project, we will use the I2C LCD1602 to display some static characters and dynamic variables.
Circuit
Schematic diagram
Hardware connection. If you need any support, please feel free to contact us via: [email protected]
Sketch
Sketch_20.1_Display_the_string_on_LCD1602
Compile and upload the code to ESP32-WROVER and the LCD1602 displays characters.
If you cannot see anything on the display or the display is not clear, try rotating the white knob on back of
LCD1602 slowly, which adjusts the contrast, until the screen can display clearly.
In this project, we use ultrasonic ranging module to measure distance, and print out the data in the terminal.
Component List
Breadboard x1
Component Knowledge
The ultrasonic ranging module uses the principle that ultrasonic waves will be sent back when encounter
obstacles. We can measure the distance by counting the time interval between sending and receiving of the
ultrasonic waves, and the time difference is the total time of the ultrasonic wave’s journey from being
transmitted to being received. Because the speed of sound in air is a constant, about v=340m/s, we can
calculate the distance between the ultrasonic ranging module and the obstacle: s=vt/2.
2S=V·t.
The HC-SR04 ultrasonic ranging module integrates both an ultrasonic transmitter and a receiver. The
transmitter is used to convert electrical signals (electrical energy) into high frequency (beyond human hearing)
sound waves (mechanical energy) and the function of the receiver is opposite of this. The picture and the
diagram of the HC SR04 ultrasonic ranging module are shown below:
Pin description:
Pin Description
VCC power supply pin
Trig trigger pin
Echo Echo pin
GND GND
Technical specs:
Working voltage: 5V Working current: 12mA
Minimum measured distance: 2cm Maximum measured distance: 200cm
Instructions for use: output a high-level pulse in Trig pin lasting for least 10us, the module begins to transmit
ultrasonic waves. At the same time, the Echo pin is pulled up. When the module receives the returned
ultrasonic waves from encountering an obstacle, the Echo pin will be pulled down. The duration of high level
in the Echo pin is the total time of the ultrasonic wave from transmitting to receiving, s=vt/2.
Circuit
Hardware connection. If you need any support, please feel free to contact us via: [email protected]
Sketch
Sketch_21.1_Ultrasonic_Ranging
Download the code to ESP32-WROVER, open the serial port monitor, set the baud rate to 115200 and you
can use it to measure the distance between the ultrasonic module and the object. As shown in the following
figure:
25 digitalWrite(trigPin, HIGH);
26 delayMicroseconds(10);
27 digitalWrite(trigPin, LOW);
28 // Wait HC-SR04 returning to the high level and measure out this waiting time
29 pingTime = pulseIn(echoPin, HIGH, timeOut);
30 // calculate the distance according to the time
31 distance = (float)pingTime * soundVelocity / 2 / 10000;
32 return distance; // return the distance value
33 }
First, define the pins and the maximum measurement distance.
1 #define trigPin 13 // define trigPin
2 #define echoPin 14 // define echoPin.
3 #define MAX_DISTANCE 700 //define the maximum measured distance
If the module does not return high level, we cannot wait for this forever, so we need to calculate the time
period for the maximum distance, that is, time Out. timeOut= 2*MAX_DISTANCE/100/340*1000000. The
result of the constant part in this formula is approximately 58.8.
5 float timeOut = MAX_DISTANCE * 60;
Subfunction getSonar () function is used to start the ultrasonic module to begin measuring, and return the
measured distance in cm units. In this function, first let trigPin send 10us high level to start the ultrasonic
module. Then use pulseIn () to read the ultrasonic module and return the duration time of high level. Finally,
the measured distance according to the time is calculated.
21 float getSonar() {
22 unsigned long pingTime;
23 float distance;
24 // make trigPin output high level lasting for 10μs to triger HC_SR04?
25 digitalWrite(trigPin, HIGH);
26 delayMicroseconds(10);
27 digitalWrite(trigPin, LOW);
28 // Wait HC-SR04 returning to the high level and measure out this waitting time
29 pingTime = pulseIn(echoPin, HIGH, timeOut);
30 // calculate the distance according to the time
31 distance = (float)pingTime * soundVelocity / 2 / 10000;
32 return distance; // return the distance value
33 }
Lastly, in loop() function, get the measurement distance and display it continually.
14 void loop() {
15 delay(100); // Wait 100ms between pings (about 20 pings/sec).
16 Serial.printf("Distance: ");
17 Serial.print(getSonar()); // Send ping, get distance in cm and print result
18 Serial.println("cm");
19 }
Component List and Circuit are the same as the previous section.
Sketch
Sketch_21.2_Ultrasonic_Ranging
Download the code to ESP32-WROVER, open the serial port monitor, set the baud rate to 115200. Use the
ultrasonic module to measure distance. As shown in the following figure:
In this project, we will attempt to get every key code on the matrix keypad to work.
Component List
Breadboard x1
Component knowledge
Similar to the integration of a LED matrix, the 4x4 keypad matrix has each row of keys connected with one pin
and this is the same for the columns. Such efficient connections reduce the number of processor ports
required. The internal circuit of the Keypad Matrix is shown below.
The usage is similar to the LED matrix, using a row or column scanning method to detect the state of each
key’s position by column and row. Take column scanning method as an example, send low level to the first 1
column (Pin1), detect level state of row 5, 6, 7, 8 to judge whether the key A, B, C, D are pressed. Then send
low level to column 2, 3, 4 in turn to detect whether other keys are pressed. Therefore, you can get the state
of all of the keys.
Circuit
Schematic diagram
Hardware connection. If you need any support, please feel free to contact us via: [email protected]
Sketch
This code is used to obtain all key codes of the 4x4 matrix keypad, when one of the keys is pressed, the key
code will be printed out via serial port.
Sketch_22.1_Get_Input_Characters
Download the code to ESP32-WROVER, open the serial port monitor, set the baud rate to 115200, press the
keyboard, the value of the pressed keys will be printed out via the serial port. As shown in the following
figure:
First, add header file, define 4*4 matrix keyboard key value and the matrix keyboard pin.
1 #include <Keypad.h>
2 // define the symbols on the buttons of the keypad
3 char keys[4][4] = {
4 {'1', '2', '3', 'A'},
5 {'4', '5', '6', 'B'},
6 {'7', '8', '9', 'C'},
7 {'*', '0', '#', 'D'}
8 };
9 byte rowPins[4] = {14, 27, 26, 25}; // connect to the row pinouts of the keypad
10 byte colPins[4] = {13, 21, 22, 23}; // connect to the column pinouts of the keypad
Second, define a matrix keyboard object and associate the keys and pins with it.
13 Keypad myKeypad = Keypad(makeKeymap(keys), rowPins, colPins, 4, 4);
Finally, get the key value and print it out via the serial port.
20 void loop() {
21 // Get the character input
22 char keyPressed = myKeypad.getKey();
23 // If there is a character input, sent it to the serial port
24 if (keyPressed) {
25 Serial.println(keyPressed);
26 }
27 }
Reference
class Keypad You need to add the library each time you use the Keypad.
Keypad(char *userKeymap, byte *row, byte *col, byte numRows, byte numCols);
Constructor, the parameters are: key code of keyboard, row pin, column pin, the number of rows, the
number of columns.
char getKey();
Get the key code of the pressed key. If no key is pressed, the return value is NULL.
void setDebounceTime(uint);
Set the debounce time with a default time of 10ms.
void setHoldTime(uint);
Set the duration for the key to keep stable state after pressed.
bool isPressed(char keyChar);
Judge whether the key with code "keyChar" is pressed.
char waitForKey();
Wait for a key to be pressed, and return key code of the pressed key.
KeyState getState();
Get the state of the keys.
bool keyStateChanged();
Judge whether there is a change of key state, then return True or False.
For More information about Keypad, please visit: http://playground.arduino.cc/Code/Keypad
In this project, we use keypad as a keyboard to control the action of the servo motor.
Component List
Breadboard x1
Circuit
Schematic diagram
Hardware connection. If you need any support, please feel free to contact us via: [email protected]
Sketch
Sketch_22.2_Keypad_Door
Verify and upload the code to the ESP32-WROVER and press the keypad to input password with 4 characters.
If the input is correct, the servo will move to a certain degree, then return to the original position. If the input
is wrong, an input error alarm will be generated.
Third, if the button has been pressed for four times, ESP32 begins to judge if the password is correct.
47 if (keyInNum == 4) {
48 bool isRight = true; // Save password is correct or not
49 for (int i = 0; i < 4; i++) { // Judge each character of the password is correct or
50 not
51 if (keyIn[i] ! = passWord[i])
52 isRight = false; // Mark wrong password if there is any wrong character.
53 }
54
If the password is correct, control the servo motor to open the lock and wait for 2 seconds before closing the
lock. If it is not correct, the buzzer makes a long sound and prints the error message through the serial port.
55 if (isRight) { // If the input password is right
56 myservo.write(90); // Open the switch
57 delay(2000); // Delay a period of time
58 myservo.write(0); // Close the switch
59 Serial.println("passWord right! ");
60 }
61 else { // If the input password is wrong
62 digitalWrite(buzzerPin, HIGH);// Make a wrong password prompt tone
63 delay(1000);
64 digitalWrite(buzzerPin, LOW);
65 Serial.println("passWord error! ");
66 }
Finally, remember to empty the keyInNum every time.
67 keyInNum = 0; // Reset the number of the input characters to 0
For More information about Keypad, please visit: http://playground.arduino.cc/Code/Keypad .
First, we need to understand how infrared remote control works, then get the command sent from infrared
remote control.
Component List
Breadboard x1
Component knowledge
Infrared Remote
An infrared(IR) remote control is a device with a certain number of buttons. Pressing down different buttons
will make the infrared emission tube, which is located in the front of the remote control, send infrared ray with
different command. Infrared remote control technology is widely used in electronic products such as TV, air
conditioning, etc. Thus making it possible for you to switch TV programs and adjust the temperature of the
air conditioning when away from them. The remote control we use is shown below:
Pull out
Infrared receiver
An infrared(IR) receiver is a component which can receive the infrared light, so we can use it to detect the
signal emitted by the infrared remote control. DATA pin here outputs the received infrared signal.
When you use the infrared remote control, the infrared remote control sends a key value to the receiving
circuit according to the pressed keys. We can program the ESP32-WROVER to do things like lighting, when a
key value is received.
The following is the key value that the receiving circuit will receive when each key of the infrared remote
control is pressed.
ICON KEY Value ICON KEY Value
FFA25D FFB04F
FFE21D FF30CF
FF22DD FF18E7
FF02FD FF7A85
FFC23D FF10EF
FFE01F FF38C7
FFA857 FF5AA5
FF906F FF42BD
FF6897 FF4AB5
FF9867 FF52AD
Circuit
Schematic diagram
Hardware connection. If you need any support, please feel free to contact us via: [email protected]
Sketch
This sketch uses the infrared receiving tube to receive the value sent form the infrared remote control, and
print it out via the serial port.
Sketch_23.1_Infrared_Remote_Control
Download the code to ESP32-WROVER, open the serial port monitor, set the baud rate to 115200, press the
IR remote control, the pressed keys value will be printed out through the serial port. As shown in the
following figure: (Note that when the remote control button is pressed for a long time, the infrared receiving
circuit receives a continuous high level, that is, it receives a hexadecimal "F")
Normal
Abnormal
First, include header file. Each time you use the infrared library, you need to include the header file at the
beginning of the program.
1 #include <Arduino.h>
2 #include <IRremoteESP8266.h>
3 #include <IRrecv.h>
4 #include <IRutils.h>
Second, define an infrared receive pin and associates it with the receive class. Apply a decode_results to
decode the received infrared value.
6 const uint16_t RecvPin = 15; // Infrared receiving pin
7 IRrecv irrecv(RecvPin); // Create a class object used to receive class
8 decode_results results; // Create a decoding results class object
Third, enable infrared reception function, if you do not use this function, you won't receive the value from the
infrared remote control.
12 irrecv.enableIRIn(); // Start the receiver
Finally, put the received data into the results class and print out the data through the serial port. Note that
you must use resume() to release the infrared receive function every time when you receive data, otherwise
you can only use the infrared receive function once and cannot receive the data next time.
20 void loop() {
21 if (irrecv.decode(&results)) { // Waiting for decoding
22 serialPrintUint64(results.value, HEX); // Print out the decoded results
23 Serial.println("");
24 irrecv.resume(); // Release the IRremote. Receive the next value
25 }
26 delay(1000);
27 }
Reference
class IRrecv You need to add the library each time you use the Infrared Reception.
IRrecv irrecv(Pin):Create a class object used to receive class, and associated with Pin.
enableIRIn():Before using the infrared decoding function, enable the infrared receiving function. Otherwise the
correct data will not be received.
decode(&results): Determine whether the infrared has received data, and if so, return true and store the data
in the decode_results class. If no data is received, false is returned.
resume():Release the IRremote. Or, the infrared reception and decoding function cannot be used again.
For more information about Infrared Remote Control, please visit:
https://github.com/crankyoldgit/IRremoteESP8266/tree/master/src
In this project, we will control the brightness of LED lights through an infrared remote control.
Component List
Breadboard x1
Circuit
Schematic diagram
Hardware connection. If you need any support, please feel free to contact us via: [email protected]
Sketch
The sketch controls the brightness of the LED by determining the key value of the infrared received.
Sketch_23.2_Control_LED_through_Infrared_Remote
Compile and upload the code to the ESP32-WROVER. When pressing "0", "1", "2", "3" of the infrared remote
control, the buzzer will sound once, and the brightness of the LED light will change correspondingly.
rendering
47 }
48 }
Each time when the command is received, the function above will be called in the loop() function.
22 void loop() {
23 if (irrecv.decode(&results)) { // Waiting for decoding
24 handleControl(results.value); // Handle the commands from remote control
25 irrecv.resume(); // Receive the next value
26 }
27 }
Hygrothermograph is an important tool in our lives to give us data on the temperature and humidity in our
environment. In this project, we will use the ESP32 to read temperature and humidity data of the DHT11
Module.
Component List
Breadboard x1
Component knowledge
The temperature & humidity sensor DHT11 is a compound temperature & humidity sensor, and the output
digital signal has been calibrated by its manufacturer.
DHT11 uses customized single-line communication protocol, so we can use the library to read data more
conveniently.
After being powered up, it will initialize in 1s. Its operating voltage is within the range of 3.3V-5.5V.
The SDA pin is a data pin, which is used to communicate with other devices.
The NC pin (Not Connected Pin) is a type of pin found on various integrated circuit packages. Those pins
have no functional purpose to the outside circuit (but may have an unknown functionality during
manufacture and test). Those pins should not be connected to any of the circuit connections.
Circuit
Schematic diagram
Hardware connection. If you need any support, please feel free to contact us via: [email protected]
Sketch
Sketch_24.1_Temperature_and_Humidity_Sensor
Compile and upload the code to the ESP32-WROVER, turn on the serial monitor, and set the baud rate to
115200. Print out data of temperature and humidity sensor via the serial port.
In this project code, we use a third party library, DHTesp, and we need to define the objects for it first;
Otherwise we could not use its functionality.
1 #include "DHTesp.h"
3 DHTesp dht; //Define the DHT object
Initialize the connection pin of DHT and select the type of temperature and humidity sensor as DHT11. If the
temperature and humidity sensor is DHT12, we can also change it to DHT12.
7 dht.setup(dhtPin, DHTesp::DHT11);//Initialize the dht pin and dht object
Due to the use of the single-line protocol, data may be lost in the transmission process. So each time when
getting the data of the temperature and humidity sensor, we need to call the getStatus function to determine
whether the data is normal. If not, use goto to go back to line 12 and re-execute the program.
12 flag:TempAndHumidity newValues = dht.getTempAndHumidity();//Get the Temperature and
13 humidity
14 if (dht.getStatus() ! = 0) { //Judge if the correct value is read
15 goto flag; //If there is an error, go back to the flag and re-read
16 the data
17 }
Get the temperature and humidity data and store it in a TempAndHumidity class called newValues.
12 TempAndHumidity newValues = dht.getTempAndHumidity(); //Get the Temperature and humidity
Reference
class DHTesp
Make sure that the library and header files are added before using the object every time.
setup(Pin, DHTesp::DHTxx): Select the type of DHTxx and associate Pin with the DHTesp class.
Parameter 1: the pin to be associated.
Parameter 2: select the type of sensor, DHT11 or DHT12.
getTempAndHumidity():Obtain temperature and humidity data. The received data must be stored in the
‘TempAndHumidity’ class.
getStatus():To judge whether the obtained data format is normal, the return value of 0 means the data is normal,
and the return value of non-0 means the data is abnormal or the data fails to be obtained.
Component List
Breadboard x1
Circuit
Schematic diagram
Hardware connection. If you need any support, please feel free to contact us via: [email protected]
Sketch
This code uses the DHTesp and LiquidCrystal_I2C libraries, so make sure the relevant library files are added
before writing the program.
Sketch_24.2_Temperature_and_Humidity_Sensor
Download the code to ESP32-WROVER. The first line of LCD1602 shows the temperature value, and the
second line shows the humidity value. Try to “pinch” the thermistor (without touching the leads) with your
index finger and thumb for a brief time to observe the change in the LCD display value.
In this project, we will make a motion detector, with the human body infrared pyroelectric sensors.
When someone is in close proximity to the motion detector, it will automatically light up and when there is
no one close by, it will be out.
This infrared motion sensor can detect the infrared spectrum (heat signatures) emitted by living humans and
animals.
Component List
Breadboard x1
Component knowledge
Description:
Working voltage: 5v-20v(DC) Static current: 65uA.
Automatic Trigger. When a living body enters into the active area of sensor, the module will output high
level (3.3V). When the body leaves the sensor’s active detection area, it will output high level lasting for time
period T, then output low level(0V). Delay time T can be adjusted by the potentiometer R1.
According to the position of jumper cap, you can choose non-repeatable trigger mode or repeatable
mode.
L: non-repeatable trigger mode. The module output high level after sensing a body, then when the delay
time is over, the module will output low level. During high level time, the sensor no longer actively senses
bodies.
H: repeatable trigger mode. The distinction from the L mode is that it can sense a body until that body
leaves during the period of high level output. After this, it starts to time and output low level after delaying T
time.
Induction block time: the induction will stay in block condition and does not induce external signal at
lesser time intervals (less than delay time) after outputting high level or low level
Initialization time: the module needs about 1 minute to initialize after being powered ON. During this
period, it will alternately output high or low level.
One characteristic of this sensor is when a body moves close to or moves away from the sensor’s dome
edge, the sensor will work at high sensitivity. When a body moves close to or moves away from the sensor’s
dome in a vertical direction, the sensor cannot detect well (please take note of this deficiency). Note: The
sensing range (distance before a body is detected) is adjusted by the potentiometer.
We can regard this sensor as a simple inductive switch when in use.
Circuit
Schematic diagram
Hardware connection. If you need any support, please feel free to contact us via: [email protected]
Sketch
In this project, we will use the infrared motion sensor to trigger a LED, essentially making the infrared motion
sensor act as a motion switch. Therefore, the code is very similar to the earlier project "push button switch
and LED”. The difference is that, when infrared motion sensor detects change, it will output high level; when
button is pressed, it will output low level. When the sensor output high level, the LED turns ON, or it will turn
OFF.
Sketch_25.1_Infrared_Motion_Sensor
Verify and upload the code, and put the sensor on a stationary table and wait for about a minute. Then try to
move away from or move closer to the infrared motion sensor and observe whether the LED turns ON or OFF
automatically.
You can rotate the potentiometer on the sensor to adjust the detection effect, or use different modes by
changing the jumper.
Apart from that, you can also use this sensor to control some other modules to implement different functions
by reediting the code, such as the induction lamp, induction door.
In this project, we will read acceleration and gyroscope data of the MPU6050 sensor
Component List
Breadboard x1
Component knowledge
MPU6050
MPU6050 sensor module is a complete 6-axis motion tracking device. It combines a 3-axis gyroscope, a 3-
axis accelerometer and a DMP (Digital Motion Processor) all in a small package. The settings of the
accelerometer and gyroscope of MPU6050 can be changed. A precision wide range digital temperature sensor
is also integrated to compensate data readings for changes in temperature, and temperature values can also
be read. The MPU6050 Module follows the I2C communication protocol and the default address is 0x68.
MPU6050 is widely used to assist with balancing vehicles, robots and aircraft, mobile phones and other
products which require stability to control stability and attitude or which need to sense same.
Circuit
Note that the power supply voltage for MPU6050 module is 5V in the circuit.
Schematic diagram
Hardware connection. If you need any support, please feel free to contact us via: [email protected]
Sketch
Sketch_26.1_Acceleration_Detection
Download the code to ESP32-WROVER, open the serial port monitor, set the baud rate to 115200 and you
can use it to measure the distance between the ultrasonic module and the object. As shown in the following
figure:
48 }
Finally, the original data of the gyroscope is updated and acquired every second, and the original data, the
processed acceleration and angular velocity data are printed out through the serial port.
20 void loop() {
21 if(millis() - timer > 1000){ //each second print the data
22 mpu6050.update(); //update the MPU6050
23 getMotion6(); //gain the values of Acceleration and Gyroscope value
24 Serial.print("\na/g:\t");
25 Serial.print(ax); Serial.print("\t");
26 Serial.print(ay); Serial.print("\t");cc
27 Serial.print(az); Serial.print("\t");
28 Serial.print(gx); Serial.print("\t\t");
29 Serial.print(gy); Serial.print("\t\t");
30 Serial.println(gz);
31 Serial.print("a/g:\t");
32 Serial.print((float)ax / 16384); Serial.print("g\t");
33 Serial.print((float)ay / 16384); Serial.print("g\t");
34 Serial.print((float)az / 16384); Serial.print("g\t");
35 Serial.print((float)gx / 131); Serial.print("d/s \t");
36 Serial.print((float)gy / 131); Serial.print("d/s \t");
37 Serial.print((float)gz / 131); Serial.print("d/s \n");
38 timer = millis();
39 }
40 }
Reference
Class MPU6050
This is a class library used to operate MPU6050, which can directly read and set MPU6050. Here are some
member functions:
MPU6050 mpu6050(Wire): Associate MPU6050 with IIC.
begin(): Initialize the MPU6050.
calcGyroOffsets(true): If the parameter is true, get the gyro offset and automatically correct the offset.
If the parameter is false, the offset value is not obtained and the offset is not corrected.
getRawAccX():Gain the values of X axis acceleration raw data.
getRawAccY():Gain the values of Y axis acceleration raw data.
getRawAccZ():Gain the values of Z axis acceleration raw data.
getRawGyroX():Gain the values of X axis Gyroscope raw data.
getRawGyroY():Gain the values of Y axis Gyroscope raw data.
getRawGyroZ():gain the values of Z axis Gyroscope raw data.
getTemp():Gain the values of MPU6050’temperature data.
update():Update the MPU6050. If the updated function is not used, the IIC will not be able to retrieve the
new data.
Chapter 27 Bluetooth
This chapter mainly introduces how to make simple data transmission through Bluetooth of ESP32-WROVER
and mobile phones.
Project 27.1 is classic Bluetooth and Project 27.2 is low power Bluetooth.If you are an iPhone user, please start
with Project 27.2.
Component List
In this tutorial we need to use a Bluetooth APP called Serial Bluetooth Terminal to assist in the experiment. If
you've not installed it yet, please do so by clicking: https://www.appsapk.com/serial-bluetooth-terminal/ The
following is its logo.
Component knowledge
ESP32's integrated Bluetooth function 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, works are done in the master device and it can connect with a slave device. And we can search
and select slave devices nearby to connect with. When a device initiates connection request in master mode,
it requires information of the other Bluetooth devices including their address and pairing passkey. After
finishing pairing, it can connect with them directly.
Slave mode
The Bluetooth module in slave mode can only accept connection request from a host computer, but cannot
initiate a connection request. After connecting with a host device, it can send data to or receive from the host
device.
Bluetooth devices can make data interaction with each other, as one is in master mode and the other in slave
mode. When they are making data interaction, the Bluetooth device in master mode searches and selects
devices nearby to connect to. When establishing connection, they can exchange data. When mobile phones
exchange data with ESP32, they are usually in master mode and ESP32 in slave mode.
Circuit
Sketch
Sketch_27.1_SerialToSerialBT
Compile and upload the code to the ESP32-WROVER, open the serial monitor, and set the baud rate to
115200. When you see the serial printing out the character string as below, it indicates that the Bluetooth of
ESP32 is ready and waiting to connect with the mobile phone.
Make sure that the Bluetooth of your phone has been turned on and Serial Bluetooth Terminal has been
installed.
Click "Search" to search Bluetooth devices nearby and select "ESP32 test" to connect to.
Turn on software APP, click the left of the terminal. Select "Devices"
Select ESP32test in classic Bluetooth mode, and a successful connecting prompt will appear as shown on the
right illustration.
And now data can be transferred between your mobile phone and computer via ESP32-WROVER.
Send 'Hello!'' from your phone, when the computer receives it, reply "Hi" to your phone.
Reference
Class BluetoothSerial
This is a class library used to operate BluetoothSerial, which can directly read and set BluetoothSerial.
Here are some member functions:
begin(localName,isMaster): Initialization function of the Bluetooth
name: name of Bluetooth module; Data type: String
isMaster: bool type, whether to set Bluetooth as Master. By default, it is false.
available(): acquire digits sent from the buffer, if not, return 0.
read(): read data from Bluetooth, data type of return value is int.
readString(): read data from Bluetooth, data type of return value is String.
write(val): send an int data val to Bluetooth.
write(str): send an Srtring data str to Bluetooth.
write(buf, len): Sends the first len data in the buf Array to Bluetooth.
setPin(const char *pin): set a four-digit Bluetooth pairing code. By default, it is 1234
connet(remoteName): connect a Bluetooth named remoteName, data type: String
connect(remoteAddress[]): connect the physical address of Bluetooth, data type: uint8-t.
disconnect():disconnect all Bluetooth devices.
end(): disconnect all Bluetooth devices and turn off the Bluetooth, release all occupied space
Component List
Circuit
Sketch
Sketch_27.2_BLE
Serial Bluetooth
Compile and upload code to ESP32, the operation is similar to the last section.
First, make sure you've turned on the mobile phone Bluetooth, and then open the software.
Click "Search" to search Bluetooth devices nearby and select "ESP32 test" to connect to.
Turn on software APP, click the left of the terminal. Select "Devices"
Select BLUETOOTHLE, click SCAN to scan Low Energy Bluetooth devices nearby.
Select"ESP32-Bluetooth"
Lightblue
If you can't install Serial Bluetooth on your phone, try LightBlue.If you do not have this software installed on
your phone, you can refer to this link:
https://apps.apple.com/us/app/lightblue/id557428110#?platform=iphone.