CHAPTER 1.INTRODUCTION Keyestudio ESP32 Core board is a Mini development board based on the ESP-WROOM-32 module. The board has brought out most I/O ports to pin headers of 2.54mm pitch. These provide an easy way of connecting peripherals according to your own needs.
CHAPTER 2.SPECIFICATIONS Microcontroller: ESP-WROOM-32 Module USB-serial port chip: CP2102-GMR Working voltage: DC 5V Working current80mAAverage Current supply500mAMinimum Working temperature range : -40°C ~ +85°C WiFi modeStation/SoftAP/SoftAP+Station/P2P WiFi protocol 802.11 b/g/n/e/i802.11nspeed up to 150 Mbps WiFi frequency range2.4 GHz ~ 2.5 GHz Bluetooth protocol conform to Bluetooth v4.2 BR/EDR and BLE Standard Dimensions552613mm Weight9.3g...
ESP32 has fewer pins than commonly used processors, but it doesn’t have any problems reusing multiple functions on pins. Warning: The pin voltage level of the ESP32 is 3.3V. If you want to connect the ESP32 to another device with an operating voltage of 5V, you should use a level converter to convert the voltage level.
Page 12
DAC: ESP32 module has two A/D converters with 8-bit precision. Touch pad: There are 10 pins on the ESP32 module that are sensitive to capacitance changes. You can attach these pins to certain PCB’s pads and use them as touch switches.
This expansion board is compatible with the Keyestudio Nano Plus development board and Keyestudio ESP32 devel- opment board. The voltage on VCC can be set to 3.3V (ESP32) or 5V(Nano) via a DIP switch. It integrates four buttons, four LEDs(red, yellow, green and blue), six WS2812 RGB LEDs, a buzzer, a PIR motion sensor, an IR receiver, a sound sensor, a photoresistor, a 8002 amplifier, a potentiometer, an ultrasonic sensor, a 4-bit digital tube display, a 8x8 dot matrix display and a LCD 1602 display.
6.4 Copyright The Keyestudio trademark and logo are the copyright of KEYES DIY ROBOT co.,LTD. All products under Keyestudio brand can’t be copied, sold or resold without authorization by anyone or company. If you are interested in our products, please contact to our sales representatives: fennie@keyestudio.com...
ESP32 Starter Kit 6.5 ESP32 Inventor Learning Kit 6.5.1 1. Description This learning kit is a programmable tool specialized for kids above 6, which boasts 15 modules and sensors such as LEDs, buttons, a LCD, a photosensor, a sound sensor, an IR receiver, a temperature and humidity sensor as well as 30 interesting projects.
Page 21
1. Easy wiring: The wiring of VCC and GND are removed 2. Multiple functions: Nano or ESP32 development board are available as the control board and 15 sensors are integrated. When the external power supply is connected, the DIP switch can be used to control the VCC voltage to 3.3V or 5V.
CHAPTER SEVEN INSTALLATION STEPS Step 1: Remove the protective film from the acrylic board. Step 2: Stick the four rubber pads in the positioning circle under the acrylic board.
Page 26
ESP32 Starter Kit Step 3: Push the screws one by one through the holes from the bottom of the acrylic board (the side with rubber pad), and then screw copper pillars onto them, as shown below. Chapter 7. Installation Steps...
Page 27
ESP32 Starter Kit Step 4: Align the holes on the expansion board with copper pillars. Do note whether the middle two holes are aligned. If not, rotate the board to 180°. After adjustment, tighten the board with screws in place.
Page 28
ESP32 Starter Kit Step 5: Install the button caps and potentiometer caps. Chapter 7. Installation Steps...
Page 29
ESP32 Starter Kit Step 6: Install the development board. Pay attention to the position of USB interface, which is marked on the expansion board.
CHAPTER EIGHT ARDUINO TUTORIAL 8.1 1.Install Development Software and Driver 8.1.1 1).Install Arduino IDEWindows Arduino official websitehttps://www.arduino.cc/ Enter the website and click SOFTWARE to download the latest Arduino software: Arduino boasts multiple versions such as Widows, mac and Linux(as shown below), please ensure that the one you download is compatible with your computer.
Page 32
ESP32 Starter Kit Here, we will take Windows system as an example to introduce how to download and install it. Two versions are provided for Windows: for installing and for downloading(a zipped file). Click JUST DOWNLOAD to download the software.
ESP32 Starter Kit 8.1.2 2).Install Arduino IDEMac Its download method is similar to the Windows. 8.2 2.Install Development Board and Driver 8.2.1 1).Windows System Connect the control board to computer via USB. For Windows 10, the driver will be automatically installed. For Windows 7 and others, you should install it manually.
Page 34
ESP32 Starter Kit If the CP2102 driver is not installed successfully, there is a yellow exclamation mark, and “Update drive. . . ” to update the driver. Chapter 8. Arduino Tutorial...
Page 35
ESP32 Starter Kit Click “Browse my computer for drivers” 8.2. 2.Install Development Board and Driver...
Page 36
ESP32 Starter Kit Enter“Browse. . . ” to find the folder , and tap “Next”. Chapter 8. Arduino Tutorial...
Page 37
ESP32 Starter Kit Close the page after installation, and then the serial port number appears. 8.2. 2.Install Development Board and Driver...
ESP32 Starter Kit 8.2.2 2).MAC System Link: https://wiki.keyestudio.com/How_to_Install_the_Driver_of_CP2102_on_MAC_System 8.3 3.Install the Development Board Link for the ESP32 Development Boardhttps://docs.espressif.com/projects/arduino-esp32/en/latest/getting_started. html#about-arduino-esp32 8.3.1 1). Windows System Open the Arduino IDE 8.3. 3.Install the Development Board...
Page 40
ESP32 Starter Kit Click Tools Board ESP32(we need to install it) Installation Steps of the ESP32 Tap “FilePreferences”add the link“ https://espressif.github.io/arduino-esp32/package_esp32_index.json” in Addi- tional boards manager URLs and click OK. Chapter 8. Arduino Tutorial...
Page 41
ESP32 Starter Kit Select the icon to open the the board options Search for ESP32 In the search box and click Install 8.3. 3.Install the Development Board...
Page 42
ESP32 Starter Kit Choose the corresponding port Chapter 8. Arduino Tutorial...
Page 43
ESP32 Starter Kit 8.3.2 2).MAC System The setting method of Arduino IDE resembles that of Windows. The only difference is COM port: Tap Arduino IDE Preferencesadd the link“ https://espressif.github.io/arduino-esp32/package_esp32_index.json” in Additional boards manager URLs and click OK. 8.3. 3.Install the Development Board...
Page 44
ESP32 Starter Kit Search for ESP32 In the search box and click Install Chapter 8. Arduino Tutorial...
Page 45
ESP32 Starter Kit If you use an Apple laptop, slid the scroll bar on the right to the bottom, and the **“INSTALL” ** installation button can’t be displayed, you need to adjust the “Interface scale” of the Arduino IDE. 8.3. 3.Install the Development Board...
Page 46
ESP32 Starter Kit Click “Arduino IDE Preferences Interface scale” to select the appropriate size until you see the “INSTALL” button. Chapter 8. Arduino Tutorial...
ESP32 Starter Kit 8.4 4.Install Libraries 8.4.1 1).What are Libraries? A library is a collection of codes, and it facilitates the connection of sensors, displays and modules. For instance, the LiquidCrystal library simplifies the communication with LCD display. Moreover, hundreds of libraries are available on Internet.
Page 48
ESP32 Starter Kit 8.4.2 2).How to Install the Libraries? Tap Sketch>Include Library>Add .Zip Library. . . Select the zipped files you need and click ok to add a library. If success, the message bar will show “Library installed”. For more tutorials, please visit our official websitehttps://www.keyestudio.com/...
ESP32 Starter Kit 8.5 Arduino Project 8.5.1 Download the Arduino code files and library files Click on the link to download the arduino code file and library file:Download-Arduino-Codes-and-Libraries 8.5.2 Project 1: LED Blinking 1. Description LED blinking is a simple project designed for starters. You only need to install an LED on Arduino board and upload the code on Arduino IDE.
Page 50
3. Wiring Diagram 4. Test Code According to previous principles, we can control LED via levels of ESP32 pins. 1. Define an variable as pin IO5 used for pin number modification. 2. Set pin IO5 to output mode in setup( ).
Page 51
ESP32 Starter Kit (continued from previous page) void loop() { digitalWrite(ledPin, HIGH); //Output a high level, LED lights up delay(1000);//Delay 1000ms digitalWrite(ledPin, LOW); //Output a low level, LED goes off delay(1000); 5. Test Result After uploading the code and powering on, LED will light up for 1s and off for 1 s.
Page 52
In other words, output ratio of 0 and 1 affects the voltage value, the more 0 and 1 signals are output per unit time, the more accurate the control will be. The GPIO34, 35, 36, and 39 of ESP32 cannot use PWM. Chapter 8. Arduino Tutorial...
Page 53
By the way, a delay time may reinforce the control of LED shining time. Next, we use another “for” statement to decrease it from 255 to 0 with a delay time to control LED dimming process. keyestudio ESP32 Inventor Learning Kit Project 2: Breathing LED http://www.keyestudio.com...
Page 54
ESP32 Starter Kit (continued from previous page) (int 0; i < 255; i++) { //make light fade in ledcWrite(CHN, i); delay(10); (int 255; i > -1; i--) { //make light fade out ledcWrite(CHN, i); delay(10); 5. Test Result After uploading the code, we can see that the LED dims gradually rather than all of a sudden. It “breathes” evenly.
Page 55
What we should clear firstly is how SOS distress light blinks: LED quickly blinks 3 times for “S” and slowly blinks 3times for “O”. And then, we control the blinking times and duration via “for” statement and set interval time among letters. keyestudio ESP32 Inventor Learning Kit Project 3SOS Distress Device http://www.keyestudio.com...
Page 56
ESP32 Starter Kit (continued from previous page) void setup() { pinMode(ledPin, OUTPUT); void loop() { //Three quickly blinks mean “S” for(int x=0;x<3;x++){ digitalWrite(ledPin,HIGH); //Set LED to light up delay(150); //Delay 150ms digitalWrite(ledPin,LOW); //Set LED to turn off delay(100); //Delay 100ms //delay 200ms to generate the space between letters delay(200);...
Page 57
ESP32 Starter Kit 8.5.5 Project 4: Traffic Light 1. Description The traffic light module is a device used to control the route of pedestrians and vehicles. It includes a red, a yellow and a green light, which implies different instructions.
Page 58
5s. And we set this to loop. The blinking of yellow LED can utilize for()statement we have mentioned in project 3. Thus, we only need to set the lighting time to complete a traffic light. keyestudio ESP32 Inventor Learning Kit Project 4 Traffic Light http://www.keyestudio.com...
Page 59
ESP32 Starter Kit 2. Working Principle The data protocol adopts communication mode of single-line return-to-zero code. After the pixel is reset on power, DIN terminal receives data from the controller. The firstly arriving 24bit data will be extracted by the first pixel and be sent to the inner data register.
Page 60
ESP32 Starter Kit 3. Wiring Diagram 4. Test Code Before uploading the code, please ensure the library file is loaded to arduino IDE. keyestudio ESP32 Inventor Learning Kit Project 5.1 Rainbow Ambient Light http://www.keyestudio.com //Add 2812RGB library file #include <NeoPixel_ESP32.h>...
Page 61
After uploading the code, wiring up and powering on, the LED will light up in different colors. 6. Extended Code Specifically, we replace RGB value with variables. And then we control these variables to form an expected light show. The wirings remain unchanged. keyestudio ESP32 Inventor Learning Kit Project 5.2 Rainbow Ambient Light http://www.keyestudio.com...
Page 62
ESP32 Starter Kit (continued from previous page) colorWipe(strip.Color(0, 0, 255), 50); // Blue // Send a theater pixel chase in... theaterChase(strip.Color(127, 127, 127), 50); // White theaterChase(strip.Color(127, 0), 50); // Red theaterChase(strip.Color( 0, 127), 50); // Blue rainbow(20); rainbowCycle(20); theaterChaseRainbow(50);...
Page 63
ESP32 Starter Kit (continued from previous page) strip.show(); delay(wait); (int i=0; i < strip.numPixels(); i=i+3) { strip.setPixelColor(i+q, 0); //turn every third pixel off //Theatre-style crawling lights with rainbow effect void theaterChaseRainbow(uint8_t wait) { (int j=0; j < 256; j++) {...
Page 64
ESP32 Starter Kit 7. Code Explanation #include <NeoPixel_ESP32.h> : Libraries are included, so that codes in library can be directly recalled. Adafruit_NeoPixel strip = Adafruit_NeoPixel(6, PIN); Define an instance strip and set the number of RGB. Here we input 6.
Page 65
A water flow light means that LED lights go from left to right and then from right to left. In this experiment, we use continuous pins, so that “for” statement can be utilized not only to set output mode (replace pins with circular variable in code) but to output. keyestudio ESP32 Inventor Learning Kit Project 6 Water Flow Light http://www.keyestudio.com...
Page 66
ESP32 Starter Kit (continued from previous page) digitalWrite(i,HIGH); delay(200); digitalWrite(i,LOW); for(int 15; i >= 12; i--){ //Use "for" loop statement to light up␣ LED on IO15-IO12 pin in sequence digitalWrite(i,HIGH); delay(200); digitalWrite(i,LOW); 4. Test Result After uploading code and powering on, the LEDs go from left to right and then from right to left 6.
Page 67
ESP32 Starter Kit 2. Working Principle An active buzzer integrates a multi-vibrator, so it makes sound only via DC voltage. Pin 1 of the buzzer connects to VCC and pin 2 is controlled by a triode. When a high level is provided for the base (pin 1) of the triode, its collector (pin 3) and emitter (pin 2) link to GND, and then the buzzer emits sound.
Page 68
If the development board outputs a high level, the buzzer will emit sound. If it outputs a low level, the buzzer will stop ringing. Thus, its code is similar to light up an LED. keyestudio ESP32 Inventor Learning Kit Project 7 Active Buzzer http://www.keyestudio.com...
Page 69
ESP32 Starter Kit (continued from previous page) delay(1000); 5. Test Result After uploading code and powering on, the buzzer emits sound for 1s and stays quiet for 1s. 8.5.9 Project 8Music Performer 1. Description In this project, we will use a power amplifier speaker to play music. This speaker can not only play simple songs, but also perform what you desire.
Page 70
According to the comparison table, we set a pin to output mode. And we use function “tong(Pin , frequency);” to generate square waves in certain frequency to emit corresponding sound. Finally, the notes will be output after adding a delay time. keyestudio ESP32 Inventor Learning Kit Project 8.1 Music Performer http://www.keyestudio.com...
Page 71
ESP32 Starter Kit (continued from previous page) tone(beeppin, 330);//Flat Mi plays 500ms delay(500); tone(beeppin, 349);//Flat Fa plays 500ms delay(500); tone(beeppin, 392);//Flat So plays 500ms delay(500); tone(beeppin, 440);//Flat La plays 500ms delay(500); tone(beeppin, 494);//Flat Si plays 500ms delay(500); noTone(beeppin);//Stop for 1s delay(1000);...
Page 72
ESP32 Starter Kit Comparison Diagram of Flat, Natural and Sharp Code keyestudio ESP32 Inventor Learning Kit Project 8.2 Music Performer http://www.keyestudio.com beeppin //Define the speaker pin to IO5 // doremifasolasi doremi[] {262, 294, 330, 370, 392, 440, 494, //Falt 0-6...
Page 73
ESP32 Starter Kit 7. Code Explanation doremi[]{ . . . }; Linear array is used to store data, which generally are considered as a series of variables of the same type. Analogically, data are neatly put in ordered boxes, so that we can take the sequenced numbers to use corresponding data.
Page 74
ESP32 Starter Kit 2. Working Principle TM1650 utilizes IIC protocol and adopts two bus lines (SDA and SCL). Data Command: 0x48. This command tells TM1650 to light up the digital tubes rather than key scanning. Display Command: Chapter 8. Arduino Tutorial...
Page 75
ESP32 Starter Kit Actually, it is one byte of data with different bits representing different functions. bit[6:4]: Set the brightness of LED. Note that 000 indicates the brightest. bit[3]: Determine whether there is a decimal dot. bit[0]: Determine whether to turn on the display.
Page 76
ESP32 Starter Kit 3. Wiring Diagram 4. Test Code Upload library files on Arduino IDE firs. Code keyestudio ESP32 Inventor Learning Kit Project 9.1 Digital Tube Display http://www.keyestudio.com #include "TM1650.h" #define CLK 22 //pins definitions for TM1650 and can be changed to other ports #define DIO 21 TM1650 DigitalTube(CLK,DIO);...
Page 77
Let’s have some difficult operations. Rather than static numbers, we handle it to show some dynamic ones. The following code manipulates the tubes to display 1~9999 by “for” loop. The wiring remains unchanged. Code: keyestudio ESP32 Inventor Learning Kit Project 9.2 Digital Tube Display http://www.keyestudio.com #include "TM1650.h"...
Page 78
ESP32 Starter Kit (continued from previous page) delay(100); 7. Code Explanation TM1650 DigitalTube(CLK,DIO); Create an example for DigitalTubeand import the pin number connecting CLK to DIO into the code. DigitalTube.clear(); Clear the display DigitalTube.displayFloatNum(num); This is the digital tube display function, the maximum number is 9999.
Page 81
ESP32 Starter Kit 3.Set “Endian” to “Big Endian”. 4.Click the white tiles to form a pattern you want(click again for deselecting), and then click “Generate” to gen- erate an array for this icon. Copy this array and paste it in code, and then the pattern will be displayed on the dot...
Page 84
ESP32 Starter Kit (continued from previous page) // Clear the display lc.clearDisplay(0); void loop(){ for(int i=0; i < 8; i++){ lc.setRow(0,i,IMAGES[i]); 5. Test Result After connecting the wiring and uploading code, a heart will be displayed on the dot matrix, as shown below.
Page 85
ESP32 Starter Kit 8.5.12 Project 11: LCD 1. Description Arduino I2C 1602 LCD is a commonly-used auxiliary device for MCU development board to connect with external sensors and modules. It features a 16-bit wide character, 2-line LCD screen and adjustable brightness. This programable module is convenient for data editing, display and management .
Page 86
ESP32 Starter Kit 3. Wiring Diagram 4. Test Code Add libraries to Arduino IDE first. keyestudio ESP32 Inventor Learning Kit Project 11 LCD http://www.keyestudio.com #include <Wire.h> #include <LiquidCrystal_I2C.h> LiquidCrystal_I2C lcd(0x27,16,2); // set the LCD address to 0x27 for a 16 chars and 2␣...
Page 87
5. Test Result After connecting the wiring and uploading code, turn on the LCD, “Hello, world!” and “keyestudio!” will be displayed on the LCD. If the characters are unclear, please fix the backlight potentiometer by the small slotted screwdriver(Please use appro- priate force to adjust.
Page 88
ESP32 Starter Kit 6. Code Explanation #include <LiquidCrystal_I2C.h> #include is a “include” command of libraries, so we can recall functions in file.h. LiquidCrystal_I2C lcd(0x27,16,2); Define an LCD. 0x27 is its IIC address, and 16 means the number of columns(display 16 characters in total), and 2 is the number of rows.
Page 89
ESP32 Starter Kit Control Principle: The rotation angle is controlled via duty cycle of PWM. Theoretically, standard PWM cycle is 20ms(50Hz), so pulse width should distribute within 1ms~2ms. However, the actual pulse width reaches 0.5ms~2.5ms, which corresponds to 0°180°. Pay attention that, for the same signal, the rotation angle may vary from servo brands.
Page 90
ESP32 Starter Kit 4. Test Code Add libraries to Arduino IDE first. keyestudio ESP32 Inventor Learning Kit Project 12 Servo http://www.keyestudio.com servoPin 4;//servo PIN void setup() { pinMode(servoPin, OUTPUT);//servo pin is set to output void loop() { for(int <= ; i++) { servopulse(servoPin, i);//Set the servo to rotate from 0°...
Page 91
ESP32 Starter Kit 8.5.14 Project 13: Mini Lamp 1. Description In this project, we are going to control a lamp via Arduino UNO and a button. When we press the button, the state of the lamp will shift(ON or OFF).
Page 92
ESP32 Starter Kit 3. Wiring Diagram 4. Test Code We can read the pin voltage value via “digitalRead(Pin)”, 1 for high and 0 for low. keyestudio ESP32 Inventor Learning Kit Project 13.1 Mini Lamp http://www.keyestudio.com button value void setup() { Serial.begin(9600);...
Page 93
ESP32 Starter Kit (continued from previous page) lines 5. Test Result After connecting the wiring and uploading code, open the serial monitor and set the baud rate to 9600. When we press the button, serial port prints “Key status: 0”; When we release it, serial port prints “Key status: 1”.
Page 95
ESP32 Starter Kit Code Make a mini lamp with a button and a LED. keyestudio ESP32 Inventor Learning Kit Project 13.2 Mini Lamp http://www.keyestudio.com #define led #define button 15 bool ledState false; void setup() { // initialize digital pin PIN_LED as an output.
Page 96
ESP32 Starter Kit (continued from previous page) (digitalRead(button) LOW) { //judge whether the button value is 0 ledState !ledState; //ledStart is equal to the inverse of its␣ original value, which can be used to light the LED on and off digitalWrite(led, ledState);...
Page 97
ESP32 Starter Kit 8.5.15 Project 14: Counter 1. Description Arduino 4-bit digital tube counter can record numbers within 0~9999. It features display speed, count mode adjustment as well as reset function. This module is wildly applied in real-time counter (such as button-press and DC motor rotation count), gaming and experiment equipment.
Page 99
A counter includes three buttons: plus, minus, and reset(return to zero). We program “if” to determine the state of button, “pressed” for execution. For better results, we need to add a 200ms delay. keyestudio ESP32 Inventor Learning Kit Project 14 Counter http://www.keyestudio.com...
Page 100
ESP32 Starter Kit (continued from previous page) //set the pin connecting with button to input pinMode(res,INPUT); pinMode(add,INPUT); pinMode(subtract,INPUT); for(char b=0;b<4;b++){ DigitalTube.clearBit(b); //DigitalTube.clearBit(0 to 3); Clear bit display. void loop(){ DigitalTube.displayFloatNum(item);//Digital tube displays item value red_key digitalRead(res); //Red button is the reset button yellow_key digitalRead(subtract);...
Page 102
Imagine a question-master and three respondents. Respondents are allowed to grab questions only when the master presses the red button. Otherwise, their replies are invalid and lights are all off. Plus, if one of the three presses his/her button, the remaining two buttons are also invalid. keyestudio ESP32 Inventor Learning Kit Project 15 Responder http://www.keyestudio.com...
Page 103
ESP32 Starter Kit (continued from previous page) void setup(){ //Set the pin connecting with button to input pinMode(blue_key,INPUT); pinMode(green_key,INPUT); pinMode(yellow_key,INPUT); pinMode(red_key,INPUT); //Set the pin connecting with LED to output pinMode(blue_led,OUTPUT); pinMode(green_led,OUTPUT); pinMode(yellow_led,OUTPUT); pinMode(red_led,OUTPUT); void loop(){ red_key_val digitalRead(red_key); //Read the red button value digitalWrite(red_led,HIGH);...
Page 104
ESP32 Starter Kit 5. Test Result Connect the wiring and upload the code. The answers of respondents are only valid when the red LED is off(red button is pressed). When someone presses his/her button(yellow, green or blue), the appropriate LED as well as the red counterpart light up.
Page 106
We set blue for plus, green for minus, yellow for counting down and red for resetting. The time (unit: s) is displayed on digital tube and the 8x8 dot matrix shows the state of bomb(smile for safe and cry for explosion). keyestudio ESP32 Inventor Learning Kit Project 16 Timebomb http://www.keyestudio.com #include "TM1650.h"...
Page 107
ESP32 Starter Kit (continued from previous page) addition //Set the plus button to IO32 subtraction //Set the minus button to IO33 start //Set the start button to IO34 reset //Set the reset button to IO35 buzz //Set the buzzer to IO26...
Page 108
ESP32 Starter Kit (continued from previous page) if(yellow_key while(1){ //whlie()loop DigitalTube.displayFloatNum(item); //Digital tube displays the item value item--; //item--equals to item = item - 1 delay(1000); buzz_val !buzz_val; //“”takes the inverse runner. invert buzz_val digitalWrite(buzz,buzz_val); if(item digitalWrite(buzz,LOW); break; //break to exit the loop...
Page 109
ESP32 Starter Kit 2. Working Principle Human body(37°C) always emits infrared ray with a wavelength of 10m, which approximates to that of the sensor detected. On this account, this module is able to detects human beings movement. If there is, PIR sensor outputs a high level about 3s.
Page 110
3. Wiring Diagram 4. Test Code From the working principle, we can read the level of the sensor pin to judge whether there are people nearby. keyestudio ESP32 Inventor Learning Kit Project 17.1 Invasion Alarm http://www.keyestudio.com //Define IO5 as PIR sensor pin...
Page 111
ESP32 Starter Kit (continued from previous page) 5. Test Result After connecting the wiring and uploading the code, open serial monitor to set baud rate to 9600, and the serial port prints the PIR value. 6. Knowledge Expansion Let’s make an invasion alarm. When the PIR sensor detects human, LED lights up and the buzzer emits sound. In contrast, LED goes off and the buzzer stays quiet.
Page 113
ESP32 Starter Kit Code To fulfil an invasion alarm, an “if() else” statement is necessary. keyestudio ESP32 Inventor Learning Kit Project 17.2 Invasion Alarm http://www.keyestudio.com //Set PIR sensor pin to IO5 red_led //Set red LED to pin IO18 buzz //Set buzzer to pin IO19...
Page 114
ESP32 Starter Kit (continued from previous page) digitalWrite(buzz,HIGH); else{ digitalWrite(red_led,LOW); digitalWrite(buzz,LOW); 8.5.19 Project 18: Beating Heart 1. Description In this project, a beating heart will be presented via an Arduino board, a 8X8 dot matrix display, a circuit board and some electronic components.
Page 115
ESP32 Starter Kit 3. Test Code When a large heart and a small heart switch display according to a certain time will give people a feeling of beating. keyestudio ESP32 Inventor Learning Kit Project 18 Beating Heart http://www.keyestudio.com #include "LedControl.h"...
Page 116
ESP32 Starter Kit 8.5.20 Project 19 : Dimming Lamp 1. Description The dimming lamp adjusts the brightness of LED via a potentiometer and an Arduino controller. The brightness is subject to resistance value, which can be read and adjusted by connecting the ends of the potentiometer to digital or analog pins on board.
Page 117
Our potentiometer is 10K. In this project, the maximum resistance is 10K. The ESP32 board will equally divide the voltage of 3V into 4095 parts (3/4095=0.0007326007326). The analog voltage is obtained by multiplying the read value and 0.0007326007326.
Page 118
4. Test Code Here we adopts analogRead(Pin) to read the analog value. Input the analog pin number connecting with the sensor into this function, and the analog value can be read. keyestudio ESP32 Inventor Learning Kit Project 19.1 Dimming Lamp http://www.keyestudio.com...
Page 119
ESP32 Starter Kit (continued from previous page) 5. Test Result After connecting the wiring and uploading code, open serial monitor to set baud rate to 9600, and the analog value will be displayed, within the range of 0-4095. 6. Knowledge Expansion We will control the brightness of LED via a potentiometer.
Page 120
ESP32 Starter Kit Code keyestudio ESP32 Inventor Learning Kit Project 19.2 Dimming Lamp http://www.keyestudio.com //Define LED to IO25 //Define pot to IO34 void setup() { // put your setup code here, to run once: pinMode(led,OUTPUT); //Set LED pin to output...
Page 121
ESP32 Starter Kit 7. Code Explanation analogRead(pot); Read the analog value. Put the input pin of analog value in brackets. map(value, fromLow, fromHigh, toLow, toHigh) map(value,0,4095,0,255); Convert the range of value from 0-4095 to 0-255. Because the range of value does not conform to that of PWM, a conversion is necessary.
Page 122
3. Wiring Diagram 4. Test Code We adopts analogRead(Pin) function to read the analog value. Connect the sensor to IO34 pin, and the value will be printed on the serial monitor. keyestudio ESP32 Inventor Learning Kit Project 20.1 Light Pillar http://www.keyestudio.com light...
Page 123
ESP32 Starter Kit 5. Test Result After connecting the wiring and uploading code, open serial monitor to set baud rate to 9600, the analog value will be displayed, withing the range of 0-4095. 6. Knowledge Expansion We will use this photoresistor to sense the ambient light intensity. The two columns of middle are included in this experiment to represent light intensity.
Page 124
ESP32 Starter Kit Code keyestudio ESP32 Inventor Learning Kit Project 20.2 Light Pillar http://www.keyestudio.com #include "LedControl.h" LedControl lc=LedControl(DIN,CLK,CS,1); const byte IMAGES[8] {0x01,0x03,0x07,0x0F,0x1F,0x3F,0x7F,0xFF}; //Data of light pillar light void setup() { lc.shutdown(0,false); // Set brightness to a medium value lc.setIntensity(0,8); // Clear the display lc.clearDisplay(0);...
Page 125
ESP32 Starter Kit 2. Working Principle When detecting a sound, the electret film in microphone vibrates, which changes the capacitance and generates a subtle change of voltage. Next, we make use of LM3 chip to build a proper circuit to amplify the detected sound up, which can be adjusted by a potentiometer.
Page 126
3. Wiring Diagram 4. Test Code Connect the sensor to pin IO33. Read the sound analog value through analogRead(Pin) function and print it on serial monitor. keyestudio ESP32 Inventor Learning Kit Project 21.1Sound Controlled LED http://www.keyestudio.com sound //Define sound as IO33...
Page 127
ESP32 Starter Kit 5. Test Result After connecting the wiring and uploading code, open serial monitor to set baud rate to 9600, the analog value will be displayed. Sensitivity adjustment If you feel that the sensitivity of the sound sensor is suitable, we can adjust the potentiometer of the sound sensor(right for the highest sensitivity, left for the lowest sensitivity).
Page 129
ESP32 Starter Kit Code keyestudio ESP32 Inventor Learning Kit Project 21.2Sound Controlled LED http://www.keyestudio.com sound //Define sound to IO33 //Define led to IO25 void setup(){ pinMode(led,OUTPUT); //Set IO25 to output void loop(){ value analogRead(sound); //Read analog value of IO33 and assign it to value if(value >...
Page 130
Arduino noise meter embodies the sound signal to a series of dots, which are converted into patterns displayed on dot matrix. 2. Wiring Diagram 3. Test Code The noise meter is able to detect the ambient noise. keyestudio ESP32 Inventor Learning Kit Project 22 Noisemeter http://www.keyestudio.com #include <LedControl.h>...
Page 132
ESP32 Starter Kit 5. Code Explanation data_val[ ] [ ]{ . . . }; Two-dimensional array. If we use an axis X metaphor for linear array, two-dimensional array is axis X and Y. In this code, the value in the first square brackets is on axis X, and the second is on axis Y. For instance, column 3 and row 4, that is data_val[ 3] [4 ].
Page 133
ESP32 Starter Kit 3. Wiring Diagram 4. Test Code Add libraries to Arduino IDE first. keyestudio ESP32 Inventor Learning Kit Project 23.1 Smart Cup http://www.keyestudio.com #include <xht11.h> xht11 xht(26); //The DHT11 sensor connects to IO26 unsigned char dat[] {0,0,0,0}; //Define an array to store temperature and humidity data...
Page 134
ESP32 Starter Kit (continued from previous page) Serial.print("RH:"); Serial.print(dat[0]); //The integral part of humidity,dht[1] is the decimal part Serial.print("% "); Serial.print("Temp:"); Serial.print(dat[2]); //The integer part of the temperature,dht[3] is the decimal␣ part Serial.println("C"); else //Read error Serial.println("sensor error"); delay(1500); //Delay 1500ms 5.
Page 135
ESP32 Starter Kit Code keyestudio ESP32 ESP32 Inventor Learning Kit Project 23.2 Smart Cup http://www.keyestudio.com #include <xht11.h> xht11 xht(26); //Define DHT11 to pin IO26 unsigned char dat[4] { 0, 0, 0, //Define an array to store temperature and␣ humidity data...
Page 136
ESP32 Starter Kit (continued from previous page) void loop() { // put your main code here, to run repeatedly: (xht.receive(dat)) { //Check correct return to true temperature dat[2]; (temperature > 75) { // Determine whether value is greater than 75 digitalWrite(green_led, LOW);...
Page 137
Moreover, it empowers to adjust temperature and humidity values according to environmental parameters as a way to achieve comfortable environmental conditions. 2. Wiring Diagram 3. Test Code It is a simple weather device that responds to ambient humidity and temperature keyestudio ESP32 Inventor Learning Kit Project 24Weather Station http://www.keyestudio.com #include <LiquidCrystal_I2C.h>...
Page 138
ESP32 Starter Kit (continued from previous page) #include <xht11.h> LiquidCrystal_I2C lcd(0x27, 16,2); // set the LCD address to 0x27 for a 16 chars and 2␣ line display xht11 xht(26); //The DHT11 sensor connects to IO26 unsigned char dat[] { 0, 0, 0, //Define an array to store temperature and␣...
Page 139
ESP32 Starter Kit What’s more, it supports cooperative works with computers. As a result, the measured value can be transmitted to computers via Arduino board. In daily life, it is widely used for motors, servos and LEDs as well as systems(automatic navigation, control and security monitoring systems).
Page 140
ESP32 Starter Kit 3. Wiring Diagram 4. Test Code As its principle, we need to use a pulseIn(pin, value) function. keyestudio ESP32 Inventor Learning Kit Project 25.1Ultrasonic Rangefinder http://www.keyestudio.com distance //Define a variable to receive the diatance value EchoPin //Connect Echo pin to io14...
Page 141
ESP32 Starter Kit (continued from previous page) //Read the signal from the sensor: a high level pulse //Duration is detected from the point sending "ping" command to the time receiving␣ echo signal (unit: um). float distance pulseIn(EchoPin, HIGH) 58.00; //Convert into distance delay(10);...
Page 142
6. Knowledge Expansion Let’s make a rangefinder. We display characters on LCD 1602. Program to show “Keyestudio” at (3,0) and “distance:” at (0,1) followed by the distance value at (9,1). When the value is smaller than 100(or 10), a residue of the third(or the second) bit still exists. Therefore, an “if”...
Page 143
ESP32 Starter Kit (continued from previous page) TrigPin //Connect Trig pin to io13 float checkdistance() { //Acquire the distance // preserve a short low level to ensure a clear high pulse: digitalWrite(TrigPin, LOW); delayMicroseconds(2); //Trigger the sensor by a high pulse of 10um or longer digitalWrite(TrigPin, HIGH);...
Page 144
ESP32 Starter Kit 7. Code Explanation float checkdistance() :Self-defining function. It greatly reduces loop() by collecting some specific codes which can be directly recalled. delayMicroseconds(); Delay function. delay()is in ms while delayMicroseconds() is in um for some precise delays. pulseIn(pin, value) :Pulse-grabbing function. pin: the number of the Arduino pin on which you want to read the pulse.
Page 146
ESP32 Starter Kit 3. Wiring Diagram 4. Test Code It is interesting that the played tones vary from distance of human’s body. keyestudio ESP32 Inventor Learning Kit Project 26 Human Body Piano http://www.keyestudio.com distance //Define a variable to receive the distance...
Page 147
ESP32 Starter Kit (continued from previous page) delayMicroseconds(10); digitalWrite(TrigPin, LOW); // Read the signal from the sensor: a high level pulse //Duration is detected from the point sending "ping" command to the time receiving␣ echo signal (unit: um). float distance pulseIn(EchoPin, HIGH) 58.00;...
Page 148
ESP32 Starter Kit 5. Test Result Connect the wirings and upload the code. Play Do when the distance is less than 10. Play Re when the distance is within 10~20. Play Mi when the distance is within 20~30. Play Fa when the distance is within 30~40. Play So when the distance is within 40~50.
Page 150
4. Test Code When parking, we can use the ultrasonic to know the situation of blind spots. In this project, lines displayed on the dot matrix indicates the distance of the car. keyestudio ESP32 Inventor Learning Kit Project 27 Intelligent Parking http://www.keyestudio.com #include <LedControl.h>...
Page 151
ESP32 Starter Kit (continued from previous page) float checkdistance() { //Acquire distance // preserve a short low level to ensure a clear high pulse: digitalWrite(TrigPin, LOW); delayMicroseconds(2); // Trigger the sensor by a high pulse of 10um or longer digitalWrite(TrigPin, HIGH);...
Page 152
ESP32 Starter Kit 5. Test Result After connecting the wiring and uploading code, lines will be displayed on the dot matrix. If the detected distance is less than 50cm, there will be fewer lines. 8.5.29 Project 28: Intelligent Gate 1. Description The intelligent gate is an intelligent parking lot system that integrates MCU and ultrasonic sensor, which automatically controls the gate according to the distance of cars, so as to better control the car access.
Page 154
ESP32 Starter Kit 3. Wiring Diagram 4. Test Code keyestudio ESP32 Inventor Learning Kit Project 28 Intelligent Gate http://www.keyestudio.com #define servo_pin distance //Define a variable to receive the distance EchoPin //Connect Echo pin to IO14 TrigPin //Connect Trig pin to IO13...
Page 155
ESP32 Starter Kit (continued from previous page) //Servo rotation program void Set_Angle(int angle_val) { //Impulse function pulsewidth map(angle_val, 0, 180, 500, 2500); //Map Angle to pulse width (int 0; i < 10; i++) { //Output a few more pulses digitalWrite(servo_pin, HIGH);//Set the servo interface level to high...
Page 156
ESP32 Starter Kit 2. Working Principle In this project, we often use a carrier of about 38K for modulation. IR remote control system includes modulation, emitting and receiving. It sends data through modulating, which im- proves the transmission efficiency and reduces the power consumption.
Page 157
ESP32 Starter Kit Schematic diagram of remote control buttons: 8.5. Arduino Project...
Page 158
ESP32 Starter Kit 3. Wiring Diagram 4. Test Code Add libraries to Arduino IDE first. keyestudio ESP32 Inventor Learning Kit Project 29.1 IR Remote Control http://www.keyestudio.com #include <Arduino.h> #include <IRremoteESP8266.h> #include <IRrecv.h> #include <IRutils.h> const uint16_t recvPin // Infrared receiving pin IRrecv irrecv(recvPin);...
Page 159
ESP32 Starter Kit (continued from previous page) Serial.begin(9600); // Initialize the serial port and set the baud rate to 9600 irrecv.enableIRIn(); // start receiving signals void loop() { (irrecv.decode(&results)) { ir_rec results.value; //assign the signal to the variable ir_rec if(ir_rec //Prevente the code from repeating execute when the␣...
Page 160
ESP32 Starter Kit Code keyestudio ESP32 Inventor Learning Kit Project 29.2 IR Remote Control http://www.keyestudio.com #include <Arduino.h> #include <IRremoteESP8266.h> #include <IRrecv.h> #include <IRutils.h> led_val const uint16_t recvPin // Infrared receiving pin IRrecv irrecv(recvPin); // Create a class object used to receive class decode_results results;...
Page 161
ESP32 Starter Kit (continued from previous page) void loop() { (irrecv.decode(&results)) { ir_rec results.value; //assign the signal to the variable ir_rec (ir_rec 0) { //Prevente the code from repeating execute when the␣ button is pressed (ir_rec 0xFF02FD) { //Determine whether the received IR signal is from␣...
Page 163
With the IR remote control, this smart home reveals various sensor values on LCD, including a temperature and hu- midity sensor, a sound sensor, a photoresistor, a potentiometer and an ultrasonic sensor. keyestudio ESP32 Inventor Learning Kit Project 30 Smart Home http://www.keyestudio.com...
Page 164
ESP32 Starter Kit (continued from previous page) distance //Define a variable to receive the distance EchoPin //Connect Echo pin to IO14 TrigPin //Connect Trig pin to IO13 ligth_sensor //Define the photoresistor pin sound_sensor //efine the sound sensor pin pot_sensor //Define the potentiometer pin...
Page 166
ESP32 Starter Kit (continued from previous page) (distance < 10) { //Eliminate the second residual digit when the value drops to␣ one digit lcd.setCursor(10, 1); lcd.print(" "); void show_luminance() { //Read the analog value of photoresistor value analogRead(ligth_sensor); lcd.setCursor(0, 1);...
Page 167
ESP32 Starter Kit (continued from previous page) lcd.print(value); (value < 1000) { lcd.setCursor(14, 1); lcd.print(" "); (value < 100) { lcd.setCursor(13, 1); lcd.print(" "); (value < 10) { lcd.setCursor(12, 1); lcd.print(" "); 5. Test Result After connecting the wiring and uploading code, we can see the corresponding contents on LCD by pressing buttons.
CHAPTER NINE SCRATCH TUTORIAL 9.1 Scratch Code file download Click on the link to download the Scratch code file:Download-Scartch-Codes 9.2 Scratch Project 9.2.1 Project 1: LED Blinking 1. Description LED blinking is a simple project designed for starters. You only need to install an LED on Arduino board and upload the code on Arduino IDE.
Page 170
ESP32 Starter Kit Triode switch: Briefly, LED lights up when the base(pin 1) is at a high level. In the same breath, the collector(pin 3) and emitter(pin 2) are connected, and then VCC passes through a current-limiting resistor to LED and finally to GND, which forms a circuit.
Page 171
ESP32 Starter Kit 3. Drag the following block in “Pins” part and set the IO5 pin to output. 4. Drag the following block in “LED” part and set the IO5 pin to HIGH. 5. Drag the following block in “Control” part .
Page 172
ESP32 Starter Kit 9.2.4 5. Test Result After uploading the code and powering on, LED will be on for 1s and off for 1s. 6. Code Explanation 1. Code blocks will not execute if the following block is not exist.
Page 173
ESP32 Starter Kit 3. It is a module used to set the pin mode(control LED and buzzer for “output” mode, and read sensor module for “input” ). 4. It is a module used to set the pin and the levels(“HIGH” and “LOW”).
Page 175
ESP32 Starter Kit 3. Wiring Diagram 4. Test Code We adopt “for” statement to increase a variable from 0 to 255, and define the variable as PWM output (analogWrite(pin, value)). By the way, a delay time may reinforce the control of LED shining time. Next, we use another “for” statement to decrease it from 255 to 0 with a delay time to control LED dimming process.
Page 176
ESP32 Starter Kit 2. Drag the following block from “Variables” part, and define the name to “item” with an initial assignment “0”. Put this block in “forever” block. 3. Drag the following block from “Control” part and set it to 255 times, which is the maximum value of PWM.
Page 178
ESP32 Starter Kit 5. Test Result After uploading the code, we can see the LED dims gradually. It “breathes” evenly. Chapter 9. Scratch Tutorial...
Page 179
ESP32 Starter Kit 6. Code Explanation 1. This block is used to set variable usable range, variable type , name and its initial value. 2. Repeating times can be assigned in the blank of this repeat block. 3. Input a variable name in the blank and its value will add 1 each time the code executes. “++” can be altered to “–...
Page 180
ESP32 Starter Kit 2. Wiring Diagram 3. Test Code What we should clear firstly is how SOS distress light blinks: LED quickly blinks 3 times for “S” and slowly blinks 3times for “O”. And then, we control the blinking times and duration via “for” statement and set interval time among letters.
Page 181
ESP32 Starter Kit 2. Drag the following block in “Pins” part and set the IO5 pin to output. Letter “S” 3. Drag the following block from “Control” part and set to 3 times, as “S” means blink for 3 times.
Page 182
ESP32 Starter Kit Letter S 7. Operate step 3 ,4 and 5 again. 8. Add a delay time of 5s to the end, and “SOS” will repeat every 5s. Chapter 9. Scratch Tutorial...
Page 185
ESP32 Starter Kit 4.Test Result After uploading the code, LED respectively blinks for 3 times quickly and slowly. 9.2.7 Project 4: Traffic Light 1. Description The traffic light module is a device used to control the route of pedestrians and vehicles. It includes a red, a yellow and a green light, which implies different instructions.
Page 186
ESP32 Starter Kit 3. Test Code We simply stimulate the traffic lights: green LED lights up for 5s, yellow LED blinks for 3 times, and red LED lights up for 5s. And we set this to loop. The blinking of yellow LED can utilize for()statement we have mentioned in project 3. Thus, we only need to set the lighting time to complete a traffic light.
Page 187
ESP32 Starter Kit 5. Repeat step 3, and set the pin to IO25. Complete Code 9.2. Scratch Project...
Page 189
ESP32 Starter Kit 4. Test Result After uploading the code, green LED will light up for 5s, yellow LED will blink for 3 times, and red LED will be on for 5s. 9.2.8 Project 5Rainbow Ambient Light 1. Description 2812RGB LED is a programable colorful dreamy light, whose color, brightness and rhythm are adjustable. This rain- bow ambient light can be used as a dynamic decoration at will.
Page 190
ESP32 Starter Kit 3. Wiring Diagram 4. Test Code Let’s learn how to light up 2812 RGB and set its colors. 1. Drag the two code blocks. 2. Drag the following block from “RGB LED” part and set the pin to IO15 and the number of LED to 6.
Page 191
ESP32 Starter Kit 3. Drag the following block from “RGB LED” part and set brightness to 20. 4. Drag the following blocks and set the number of LED to 0 ,1, 2, 3, 4 and 5, then choose red, green, blue, yellow, purple and white colors.
Page 193
ESP32 Starter Kit 5. Test Result After uploading code, connecting the wiring and powering on, the LED will light up in different colors, as show below: 6. Knowledge Expansion In this expansion project, let’s make a mini light show! Nest four “repeat” blocks and add a “variable +” in them, then clear the corresponding variables to 0 at the end of each loop.
Page 194
ESP32 Starter Kit Put the above three variables in “RGB” block so that these color values are controlled. Then add a refresh module. Put the RGB in a “show color” block to display colors. And define a variable item to control the displayed LED.
Page 197
ESP32 Starter Kit 7. Code Explanation 1. Set the number of 2812 RGB. A development board pin can control multiple 2812 RGB LEDs, so we need to set the number in advance and select the connected pin. 2. Set the brightness of 2812 RGB. Input the brightness value within 0-255, in which 255 is the brightest.
Page 198
ESP32 Starter Kit 9.2.9 Project 6Water Flow Light 1. Description This simple water flow light project enables to help you learn electronic packaging. In this project, we will control LEDs to change the color in a specified speed via a Arduino board.
Page 199
ESP32 Starter Kit 3. Test Code A water flow light consists of a stream of LED lighting from left to right. 1. Drag the two basic code blocks. 2. Set the pin mode to “output” 2. Drag the following blocks from “LED” part and set the IO15 pin to LOW, the IO12 pin to HIGH. Then set the delay time to 0.2s.
Page 200
ESP32 Starter Kit 4. Drag the following blocks from “LED” part and set the IO13 pin to LOW, the IO14 pin to HIGH. Then set the delay time to 0.2s. 5. Drag the following blocks from “LED” part and set the IO14 pin to LOW, the IO15 pin to HIGH. Then set the delay time to 0.2s.
Page 202
ESP32 Starter Kit 4. Test Result After uploading code and powering on, the LEDs light up from left to right. 9.2.10 Project 7Active Buzzer 1. Description An active buzzer is a component used as an alarm, a reminder or an entertaining device, which boasts a reliable sound.
Page 203
ESP32 Starter Kit 3. Wiring diagram 4. Test Code If the development board outputs a high level, the buzzer will emit sound. If it outputs a low level, the buzzer will stop ringing. 1. Drag the two basic code blocks.
Page 204
ESP32 Starter Kit 3. Drag the following blocks from “Buzzer” part and set the IO5 pin to LOW. Then set the delay time to 1s. Complete Code Chapter 9. Scratch Tutorial...
Page 205
ESP32 Starter Kit 5. Test Result After uploading code and powering on, the buzzer emits sound for 1s and stays quiet for 1s. 6. Code Explanation Buzzer output block. We firstly define the pin to IO5 and then set the output to “HIGH” or “LOW”. The buzzer will beep when at HIGH, while it will be quiet at LOW.
Page 206
ESP32 Starter Kit 9.2.11 Project 8Music Performer 1. Description In this project, we will use a power amplifier speaker to play music. This speaker can not only play simple songs, but also perform what you desire. Thus, you can program other interesting codes in the project to accomplish splendid learning outcomes.
Page 209
ESP32 Starter Kit 5. Test Result After uploading code and powering on, the amplifier circularly plays music tones with corresponding frequency: DO, Re, Mi, Fa, So, La, Si. 6. Knowledge Expansion Let’s make it play a birthday song. We have already added some songs in library so you can directly drag these song blocks from “Music”.
Page 210
ESP32 Starter Kit 3. Stop playing module, we only need to set the corresponding pin to stop the music 9.2.12 Project 9Digital Tube Display 1. Description This 4-Digit tube display is a device used to display counting or time, which is able to display numbers from 0 ~ 9 and simple letters.
Page 211
ESP32 Starter Kit 2. Working Principle TM1650 utilizes IIC protocol and adopts two bus lines (SDA and SCL). The code is provided in our blocks, and the digital tube will display numbers via this code. 9.2. Scratch Project...
Page 212
ESP32 Starter Kit 3. Wiring Diagram 4. Test Code To show numbers on the display, you only need to drag a “TM 1650 display” block from “Digital tube” and set the number string to 9999. Chapter 9. Scratch Tutorial...
Page 213
ESP32 Starter Kit 5. Test Result After connecting the wiring and uploading code, the digital tube display shows “9999”, as shown below. 6. Extended Code Let’s have some difficult operations. Rather than static numbers, we handle it to show some dynamic ones. The following code manipulates the tubes to display 1~9999.
Page 214
ESP32 Starter Kit 5. Drag a “TM 1650 display” block from “Digital tube” and replace the string value with variable item. Add a delay time of 0.5s after it. 6. Add a “set variable” block after the “repeat” block. Set item variable by 0. Otherwise, the item value will be out of display range after 9999 loops.
Page 215
ESP32 Starter Kit 7. Code Explanation 1. Set the display string. Directly type numbers or letters you want to display in the blank 2. Set the ON or OFF of this TM 1650 digital tube. Each tube can be controlled separately.
Page 216
ESP32 Starter Kit 9.2.13 Project 10Dot Matrix Display 1. Description This module consists of a 8x8 LED dot matrix with one control pin for each row as well as each column to adjust the brightness of LED. Connecting with Arduino board, the brightness of LED is controlled to display characters and figures via Arduino programming.
Page 217
ESP32 Starter Kit 2. Wiring Diagram 3. Test Code 1.Drag the two basic code blocks. 2.Drag a “init matrix display” from “Matrix” and set CS to IO15. DIN and CLK are fixed pin respectively to IO23 and IO18. 9.2. Scratch Project...
Page 218
ESP32 Starter Kit 3. Drag a “set brightness” block and set it to 3. 4. Drag a “image” block and choose heart icon. 5. Add a “refresh” block at the end. Complete Code Chapter 9. Scratch Tutorial...
Page 219
ESP32 Starter Kit 4. Test Result After connecting the wiring and uploading code, a heart will be displayed on the dot matrix, as shown below 9.2. Scratch Project...
Page 220
ESP32 Starter Kit 5. Code Explanation 1. Set the CS pin. In the code, DIN is fixed to io23 and SLK to io18, while CS pin is optional. For convient wiring, we select io15. 2. Draw pixels. This code block will light up or turn off pixels on the dot matrix by axis x and y, with red for on and black for off.
Page 221
ESP32 Starter Kit 8. Display fill colors. You may set to black (LED goes off) or red(LED lights up). 9. Refresh the display. The dot matrix must be refreshed if it displays something. Or else, an error may occur. 10. Set the brightness. You can lower the brightness when debugging to avoid offending to your eyes.
Page 222
ESP32 Starter Kit 2. Working Principle It is the same as IIC communication principle. Underlying functions have been packaged in libraries so that you can recall them directly. If you are interested in these, you may have a further look of underlying driving principles.
Page 223
ESP32 Starter Kit 3. Wiring Diagram 4. Test Code 1. Drag the two basic code blocks. 2. Drag “init LCD” block from “LCD” and set the I2C address to 0x27. 9.2. Scratch Project...
Page 224
3. Drag the “LCD back light” block and set it to ON. Characters are not easy to read if there is no back light. 4. Drag a “LCD cursor position” block and set x to 3 and y to 0. Add an “LCD print” block and type “keyestudio”...
Page 225
ESP32 Starter Kit 5. Test Result After connecting the wiring and uploading code, turn on the LCD, and “Hello, world!” and “keyestudio!” will be displayed on the LCD. If the characters are unclear, please fix the backlight potentiometer by the small slotted screwdriver.
Page 226
ESP32 Starter Kit 6. Code Explanation 1. Set the IIC communication address. In this project, the address of LCD 1602 is 0x27. 2. Control the LCD backlight. The displayed characters will be seen much clearly if the back light is on.
Page 227
ESP32 Starter Kit 9.2.15 Project 12: Servo 1. Description This servo features high performance and high precision with a maximum rotation angle of 180°. Weighting only 9g, it is perfectly suitable for any mini device in multiple occasions. What’s more, it enjoys short startup time, low noise and strong stability.
Page 228
ESP32 Starter Kit 3. Wiring Diagram 4. Test Code 1.Drag the two basic blocks and put a “variable” block between them. Set the variable type to int, name to angle, and assign 0 as its initial value. 2. Servo gradually rotates from 0° to 180°: Add a repeat block and set the repeat times to 180(180 angles).
Page 229
ESP32 Starter Kit 3. Servo gradually rotates from 180° to 0°: Repeat step 2, but set the variable mode to “- -”. Complete Code 9.2. Scratch Project...
Page 230
ESP32 Starter Kit 5. Test Result After connecting the wiring and uploading code, the servo starts to rotate from 0° to 180° and then from 180° to 0°. 6. Code Explanation 1. Set the values of Servo. Servo pin and rotation angle can be controlled by setting parameters on this block.
Page 231
ESP32 Starter Kit 2. Working Principle When the button is released, a voltage VCC passing through R29 provides a high level for S terminal. When pressed, pin 1 and 3, pin 2 and 4 are connected and voltage on S1 arrives GND as a low level. At this moment, R29 avoids a short circuit between VCC and GND.
Page 232
ESP32 Starter Kit 3. Wiring Diagram 4. Test Code 1. Add two basic blocks. 2. Drag a “baud rate” from “Serial” and set it to 9600. Chapter 9. Scratch Tutorial...
Page 233
ESP32 Starter Kit 3. Then drag a “print” block from “Serial”, type “Key status:” in the blank and set it to “no-warp”. 4. Set the IO15 pim to “input”. 5. Drag another “Serial print” block from “Serial” and set the mode to “warp”. Add a “state value of button” from “Button”...
Page 234
ESP32 Starter Kit 5. Test Result After connecting the wiring and uploading code, open the serial monitor and set the baud rate to 9600. When we press the button, serial port prints “Key status: 0”; When we release the button, serial port prints “Key status: 1”.
Page 236
ESP32 Starter Kit Code: 1.Drag two basic blocks. 2. Set the LED pin to “output”and the button pin to “input”. Chapter 9. Scratch Tutorial...
Page 237
ESP32 Starter Kit 3. Drag a “if else” block from “Control”. Add a “button pin” block from “Button” after “if” and set its pin to IO15. Put an “LED output” block under “if” and set the output to HIGH, and put another under “else” and set to LOW.
Page 238
ESP32 Starter Kit 8. Code Explanation Note: Pin mode must be set to “input” when using the button module. 1. Judge whether the button is pressed. If so, this block expresses true. 2. Read the button value. When the button is not pressed, the value is 1. Or else, it is 0.
Page 239
ESP32 Starter Kit 4. Set the baud rate. Please guarantee the serial baud rate fit the counterpart of serial monitor, or it won’t print anything. The commonly used baud rate are 9600 and 115200, and here we set to 9600.
Page 241
ESP32 Starter Kit 3. Wiring Diagram 4. Test Code 1. Drag the two basic blocks. 2. Set the button pin to “input”. 9.2. Scratch Project...
Page 242
ESP32 Starter Kit 3. put a “variable” block. Set the variable type to int and name to item. Assign 0 as its initial value. 4. Drag an “if” block from “Control” (it executes only when its condition is satisfied). Put a “Button pressed” block from “Button”...
Page 243
ESP32 Starter Kit 7. Drag a “if” block from “Control”. Find the “” block in “Operators” and fill the left blank with “variable item” and the right with “9999”. Also, put a variable setting block after “then” and set the “variable by 0”.
Page 245
ESP32 Starter Kit 5. Test Result After connecting the wiring and uploading code, press green button to add 1, yellow to minus 1, and red to reset. 6. Code Explanation “>” block is used for judgment between two values. These two blanks can be replaced with either numbers or variables.
Page 247
ESP32 Starter Kit 3. Wiring Diagram 4. Test Code 1. Drag the two basic blocks and put a “variable” block between them. Set the variable type to int and name to item with an initial assignment of 0. Set the LED pin to “output”, and the button pin to “input”.
Page 248
ESP32 Starter Kit 2. Add an “LED output” block, define its pin to IO27 and set the output to HIGH. 3. .Drag an “if” block and add the condition “interface IO19 button was be pushed?”. 4. Add a variable setting and four LED output blocks under “then”. Among them, we name the variable “item”...
Page 249
ESP32 Starter Kit 5. Add a “repeat until” block and set the “until” to “item = 1”, as shown below. When item = 1, exit the loop. 6. Drag another “if” block and set the condition “Interface IO16 button was be pushed?”. Add an “LED output”...
Page 250
ESP32 Starter Kit 8. Operate step 6 again, but set interface to IO18 and LED pin to IO14. Complete Code: Chapter 9. Scratch Tutorial...
Page 252
ESP32 Starter Kit 5. Test Result Connect the wiring and upload the code. The answers of respondents are only valid when the red LED is off(red button is pressed). When someone presses his/her button(yellow, green or blue), the appropriate LED as well as the red counterpart light up.
Page 254
ESP32 Starter Kit 3. Wiring Diagram 4. Test Code 1. Drag the two basic blocks. 2. Set the button pin to “input”. Chapter 9. Scratch Tutorial...
Page 255
ESP32 Starter Kit 3. Add an “init matrix display” block from “Matrix” and set the pin CS to IO15. What follows it are a “brightness” block with its value of 3 and a “variable” block (set variable type to int and name to item, assign 0 as its initial value).
Page 256
ESP32 Starter Kit 6. Repeat the operation in step 5, but set the interface to IO32 and the mode to “- -”. 7. Drag an “if” block to judge whether pin IO26 is pushed. In this “if”, we add a repeat block and set its condition to “item”...
Page 257
ESP32 Starter Kit 8. Program another loop code and define the condition as “interface IO25 button was be pushed?”. The following executions are in this loop. Put a “TM1650 display” block and define the showing string as “variable item” block.
Page 258
ESP32 Starter Kit 9. Drag an “if then” block and fill the blank with a condition: item 9999. Add a statement “set item variable by 0” in this condition block. 10. Drag a “TM1650 display” from “Digital tube” and define the showing string as “variable item”. For the same, don’t forget to delay 0.2s.
Page 260
ESP32 Starter Kit 5. Test Result After connecting the wiring and uploading code, press blue button to add time, green to reduce and red to reset. Press yellow button for counting down. When it is over, the bomb explodes. 9.2.20 Project 17: Invasion Alarm 1.
Page 261
ESP32 Starter Kit 3. Wiring Diagram 4. Test Code 1. Add the two basic blocks and drag a “baud rate” block from “Serial” between them. Set the serial baud rate to 9600. 9.2. Scratch Project...
Page 262
ESP32 Starter Kit 2. Add an “if else” block. Put a “read PIR motion sensor” block in the hexagon box and set the interface to IO5, thus it will determine whether there is a human motion. Add two “serial print” blocks after “then” and “else”...
Page 263
ESP32 Starter Kit 5. Test Result After connecting the wiring and uploading code, open serial monitor to set baud rate to 9600. When the sensor detects movement, the serial port prints “Someone Invaded”, or else, it prints “No One”. 9.2. Scratch Project...
Page 264
ESP32 Starter Kit 6. Expansion Code Let’s make an invasion alarm. When the PIR sensor detects human, LED lights up and the buzzer emits sound. In contrast, LED goes off and the buzzer stays quiet. Flow Chart Chapter 9. Scratch Tutorial...
Page 267
ESP32 Starter Kit 7.Code Explanation When PIR senses human motions, it outputs a high level. Therefore, we can judge whether there is a movement by reading the development board pin connected to this sensor. 9.2. Scratch Project...
Page 268
ESP32 Starter Kit 9.2.21 Project 18: Beating Heart 1. Description In this project, a beating heart will be presented via an Arduino board, a 8X8 dot matrix display, a circuit board and some electronic components. By programming, you can control the beating frequency, heart dimension and its brightness.
Page 269
ESP32 Starter Kit 4. Repeat step 3 but draw lines as the picture below to show a bigger heart. 9.2. Scratch Project...
Page 272
ESP32 Starter Kit 4. Test Result After connecting the wiring and uploading code, the two sizes of hearts are displayed alternately. 9.2.22 Project 19 : Dimming Lamp 1. Description The dimming lamp adjusts the brightness of LED via a potentiometer and an Arduino controller. The brightness is subject to resistance value, which can be read and adjusted by connecting the ends of the potentiometer to digital or analog pins on board.
Page 273
Our potentiometer is 10K. In this project, the maximum resistance is 10K. The ESP32 board will equally divide the voltage of 3V into 4095 parts (3/4095=0.0007326007326). The analog voltage is obtained by multiplying the read value and 0.0007326007326.
Page 274
ESP32 Starter Kit 3. Wiring Diagram 4. Test Code The analog value of the potentiometer can be read: 1. Drag the two basic blocks. Put the baud rate setting block between them and set to 9600. 2. Add a “serial print” block in “forever” loop, and select “warp” as the print mode.
Page 275
ESP32 Starter Kit 5. Test Result After connecting the wiring and uploading code, open serial monitor to set baud rate to 9600, and the analog value will be displayed within the range of 0-4095. 9.2. Scratch Project...
Page 276
ESP32 Starter Kit 6. Expansion Code We will control the brightness of LED via a potentiometer. As we know, it is influenced by PWM. However, the range of analog value is 0-4095 while that of PWM is 0-255. Thus, a “map(value, fromLow, fromHigh, toLow, toHigh)”...
Page 277
ESP32 Starter Kit Complete Code: 7. Code Explanation 1. map function. analog value range converted from 0-4095 0-255. 2. Read the analog value of potentiometer by setting its pin. 9.2.23 Project 20: Light Pillar 1. Description The resistance(less than 1K) of the photoresistor varies from the light, thus it can control the brightness of the dot matrix.
Page 278
ESP32 Starter Kit 2. Working Principle When it is totally in dark, the resistance equals 0.2M, and the voltage at signal terminal (point 2) approaches to 0V. The stronger the light is , the smaller the resistance and voltage will be.
Page 279
ESP32 Starter Kit 3. Wiring Diagram 4. Test Code The analog value of the photoresistor can be read: 1. Drag the two basic blocks. Put the baud rate setting block between them and set to 9600. 2. Add a “serial print” block in “forever” loop with the mode “warp”.
Page 280
ESP32 Starter Kit 5. Test Result After connecting the wiring and uploading code, open serial monitor to set baud rate to 9600, the analog value will be displayed, within the range of 0-4095. Chapter 9. Scratch Tutorial...
Page 281
ESP32 Starter Kit 6. Expansion Code In this expansion project, we use this photoresistor to sensing the ambient light intensity. The middle two columns are included in this experiment to represent light intensity. The lighter it is, the more the lighting LED will be. This forms a “light pillar”.
Page 282
ESP32 Starter Kit 4. Assign a map function to the variable. Add “read the value of light IO33” from “Light” to the value of map function, whose range is from (0,4095) to (0,7). 5. Find the following blocks in “Matrix”. Clear the display first, and then draw lines on the display at dots (x0:3 y0:0, x1:3 y1: variable light) and (x0:4 y0:0, x1:4 y1: variable light).
Page 283
ESP32 Starter Kit 7.Code Explanation Read the analog value of photoresistor by setting the pin. 9.2.24 Project 21: Sound Controlled LED 1. Description Sound controlled LED is a device used to detect sound in a way that controls the brightness of LED, which is composed of a Arduino board and some components.
Page 284
ESP32 Starter Kit 3. Wiring Diagram 4. Test Code Find the “read the value” block in “Sound”, and print the read sound in the serial port. Construct blocks as follows. Pay attention that do not add a delay when using the sound sensor.
Page 285
ESP32 Starter Kit 5. Test Result After connecting the wiring and uploading code, open serial monitor to set baud rate to 9600, the analog value will be displayed. 6. Expansion Code The commonly seen corridor light is a kind of sound controlled light. Meanwhile, it also includes a photoresistor.
Page 287
ESP32 Starter Kit Code 1. Drag two basic blocks. 2. Drag an “if else” block, and fill the hexagon with a item100 block. Set the value to “read the value of sound IO33”. If the condition is satisfied, LED outputs a HIGH level at pin IO25 with a delay of 2s; or else, it outputs a LOW level at the same pin without a delay.
Page 289
ESP32 Starter Kit 7. Code Explanation Read the value of sound by setting the related pin. 9.2.25 Project 22: Noise Meter 1. Description Arduino noise meter embodies the sound signal to a series of dots, which are converted into patterns displayed on dot matrix.
Page 290
ESP32 Starter Kit 3. Test Code 1. Drag the basic blocks and initialize the display. Set the pin CS to IO15 and brightness to 3. Then add a variable block and select int and name it as “item” with an initial assignment of 0.
Page 295
ESP32 Starter Kit 4. Test Result After connecting the wiring and uploading code, the noise level view is displayed on dot matrix, as shown below. 9.2.26 Project 23: Smart Cup 1. Description In this project, we mainly adopt the Arduino development board to create a programmable smart cup, which reveals the temperature of inner liquid through a RGB indicator.
Page 296
ESP32 Starter Kit 2. Working Principle Related settings in DHT11 is provided by manufacturers, so you only need to orderly read and process data according to its sequence chart. Besides, the relevant codes are packaged in our libraries, which is convenient for you to set pins and read values.
Page 297
ESP32 Starter Kit 3. Wiring Diagram 4. Test Code 1. Drag two basic blocks. Add the serial baud rate module and set the baud rate to 9600. 2. Drag the DHT module from “Temperature and humidity” and set the pin to IO26, mode to dht11.
Page 298
ESP32 Starter Kit 5. Test Result After connecting the wiring and uploading code, click to open serial monitor to set baud rate to 9600, and the temperature and humidity value will be displayed. Chapter 9. Scratch Tutorial...
Page 299
ESP32 Starter Kit 6. Expansion Code In this expansion experiment, we will make a smart cup which can show liquid temperature. We divide 100 into four parts with an LED representing for each: **Red LED: ** 100-75°C **Yellow LED: ** 75-50°C **Green LED: ** 50-25°C...
Page 301
ESP32 Starter Kit Code 1. Drag two basic blocks. Then set the 4 LED pins to “output” , the DHT11 pin to IO26, mode to dht11 and the variable name to temp. 9.2. Scratch Project...
Page 302
ESP32 Starter Kit 2. Assign the temperature value of DHT11 to the variable temp 3. Use the “if else” to judge the variable temp. If the conditions are met, the corresponding LED will be on, otherwise it will be off.
Page 304
ESP32 Starter Kit 7. Code Explanation 1. In this code block, the marked number can be filled in the blank so that multiple temperature and humidity sensors can be connected. After setting the pin and mode, the value can be read. In this project, we set the mode to DHT11.
Page 305
ESP32 Starter Kit 2. Wiring Diagram 3. Test Code 1. Add two basic modules. Initialize the LCD 1602 and turn ON the backlight of the LCD 1602 (remember to switch the LCD to ON). Set the pin of dht to IO26 and mode to dht11. Set two int variables to “RH“ and “temp“...
Page 306
ESP32 Starter Kit 2. Assign humidity value to the variable RH, and the temperature value to the variable temp. 3. Set the LCD display position to x: 0 and y: 0. Add the lcd display module and set the display character to “humidity:”.
Page 309
ESP32 Starter Kit 4. Test Result After connecting the wiring and uploading code, LCD display will directly discover the ambient humidity and temper- ature value. 9.2.28 Project 25: Ultrasonic Rangefinder 1. Description This ultrasonic rangefinder measures distance of obstacles by emitting sound waves and then receiving the echo. That is to say, the distance is not an immediate value, but an observed one by a theoretical calculation of time difference between emitter and receiver.
Page 310
ESP32 Starter Kit 2. Working Principle As we all know, ultrasonic is a kind of inaudible sound wave signal with high frequency. Similar to a bat, this module measures distance of obstacles by calculating the time difference between wave-emitting and echo-receiving.
Page 311
ESP32 Starter Kit 3. Wiring Diagram 4. Test Code In “forever” block, construct two “serial print” blocks and drag a “read distance” block from “Ultrasonic”. Set trig pin to IO13 and echo pin to IO14 both in cm. Do not forget a delay of 0.5s.
Page 312
6. Knowledge Expansion Let’s make a rangefinder. We display characters on LCD 1602. Program to show “Keyestudio” at (3,0) and “distance:” at (0,1) followed by the distance value at (9,1). When the value is smaller than 100(or 10), a residue of the third(or the second) bit still exists. Therefore, an “if”...
Page 313
1. Drag the two basic blocks. 2. In “LCD”, initialize the LCD. Drag an “LCD print” block and add character string “Keyestudio” (It also can be put out of “forever” block as this display is fixed). Add a “variable” block and set type to int and name to “distance”...
Page 314
ESP32 Starter Kit 4. Build a “clear display residue” block when the number of displayed bits decrease. We firstly adopts a condition to judge whether the distance is smaller than 100(or 10). If so, a space will be printed at the residue of the third (or the second) bit to clear previous display.
Page 316
ESP32 Starter Kit 7. Code Explanation Read the distance after setting the trig pin and echo pin. The unit of displayed value is optional (cm or inch). 9.2.29 Project 26: Human Body Piano 1. Description The analog piano includes a development board and an ultrasonic sensor. It enables to play different tones by detecting the position of your fingers.
Page 318
ESP32 Starter Kit 3. Wiring Diagram 4. Test Code Assign the distance value to item, and the played tones vary from distance. Seven tones are included: DoReMiFaSo- LaSi. Chapter 9. Scratch Tutorial...
Page 320
ESP32 Starter Kit 5. Test Result Connect the wirings and upload the code. Play Do when the distance is less than 10. Play Re when the distance is within 10~20. Play Mi when the distance is within 20~30. Play Fa when the distance is within 30~40. Play So when the distance is within 40~50.
Page 322
ESP32 Starter Kit 3. Wiring Diagram 4. Test Code Assign the detected distance value to a variable, and judge whether it is greater than the set threshold value. If so, corresponding lines on the dot matrix light up. In this way, a distance can be revealed by lighting lines.
Page 325
ESP32 Starter Kit 5. Test Result After connecting the wiring and uploading code, lines will be displayed on the dot matrix. If the detected distance is less than 50cm, there will be fewer lines. 9.2.31 Project 28: Intelligent Gate 1. Description The intelligent gate is an intelligent parking lot system that integrates MCU and ultrasonic sensor, which automatically controls the gate according to the distance of cars, so as to better control the car access.
Page 327
ESP32 Starter Kit 3. Wiring Diagram 4. Test Code Define a variable “distance” with the assignment of detected distance value by the ultrasonic module. Next, Compare the distance value with 30cm. If it is smaller than 30cm, the servo will rotate to 180° for 5s. Otherwise, the servo returns to 0°.
Page 328
ESP32 Starter Kit 5. Test Result After connecting the wiring and uploading code, the servo will rotate to 180° for 5s if the detected distance is less than 30cm. On the contrary, the servo will rotate to 0°. 9.2.32 Project 29: IR Remote Control 1.
Page 329
ESP32 Starter Kit 2. Working Principle In this project, we often use a carrier of about 38K for modulation. IR remote control system includes modulation, emitting and receiving. It sends data through modulating, which im- proves the transmission efficiency and reduces the power consumption.
Page 330
ESP32 Starter Kit Schematic diagram of remote control buttons: Chapter 9. Scratch Tutorial...
Page 331
ESP32 Starter Kit 3. Wiring Diagram 4. Test Code 1. Drag the two basic blocks. 2. Find and drag the “IR remote init” block from “IR Remote” and set its pin to IO19. Add a “baud rate” block from “serial” and set to 9600.
Page 332
ESP32 Starter Kit 4. Drag another “if” block and set its condition to “Read the data 0”. Only when this condition is satisfied, serial port starts to print data. This sensor works so fast that the code may run twice or more when you are pressing control buttons. However, the second time of a same command will send out a value of 0, so a “>”...
Page 335
ESP32 Starter Kit 5. Test Result After connecting the wiring and uploading code, open the serial monitor and set the baud rate to 9600. Press the button on the remote control unit, and you will see the value in hexadecimal.
Page 338
ESP32 Starter Kit 7. Code Explanation 1. Initialize the IR remote module after setting its receiving pin. 2. .Judge whether the sensor has received data. If so, related code blocks will run. 3. Read the received data from IR remote control.
Page 341
ESP32 Starter Kit 3. Wiring Diagram 4. Test Code With the IR remote control, this smart home reveals various sensor values on LCD, including a temperature and hu- midity sensor, a sound sensor, a photoresistor, a potentiometer and an ultrasonic sensor.
Page 343
ESP32 Starter Kit 5. Test Result After connecting the wiring and uploading code, we can see the corresponding contents on LCD by pressing buttons. OK button clears the sensor display. 6. Code Explanation The blocks are so many that we adopt “Make a Block” function. By doing this, numerous blocks are packaged and can be directly recalled, which vastly simplify the whole program.
Need help?
Do you have a question about the ESP32 and is the answer not in the manual?
Questions and answers