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 25
int distance = 0; //Define a variable to receive the distance
int EchoPin = 14; //Connect Echo pin to IO14
int TrigPin = 13; //Connect Trig pin to IO13
//Ultrasonic ranging program
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);
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; //Convert into distance
delay(10);
return distance;
(continues on next page)
150 Chapter 8. Arduino Tutorial