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
*/
int distance = 0; //Define a variable to receive the diatance value
int EchoPin = 14; //Connect Echo pin to io14
int TrigPin = 13; //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); //Delay 2um
//Trigger the sensor by a high pulse of 10um or longer
digitalWrite(TrigPin, HIGH);
delayMicroseconds(10); //Delay 10um
digitalWrite(TrigPin, LOW);
(continues on next page)
136 Chapter 8. Arduino Tutorial