But you can use any other suitable GPIOs-read ESP32 Pinout Reference: Which GPIO pins should you use? In this example, we’re using GPIO 5 and GPIO 18. How the Code Worksįirst, define the trigger and the echo pins. Continue reading if you want to learn how the code works or skip to the demonstration section. Upload the code to your board and it will work straight away. Prints the distance in the Serial Monitor Reads the echoPin, returns the sound wave travel time in microseconds Sets the trigPin on HIGH state for 10 micro seconds PinMode(echoPin, INPUT) // Sets the echoPin as an Input PinMode(trigPin, OUTPUT) // Sets the trigPin as an Output Serial.begin(115200) // Starts the serial communication The above copyright notice and this permission notice shall be included in allĬopies or substantial portions of the Software. Of this software and associated documentation files. Permission is hereby granted, free of charge, to any person obtaining a copy The following sketch is a simple example of how you can get the distance between the sensor and an object using the ESP32 board with the Arduino core. Getting Started with VS Code and PlatformIO IDE for ESP32 and ESP8266 (Windows, Mac OS X, Linux Ubuntu)Ĭode – Getting Distance to an Object using the HC-SR04 Ultrasonic Sensor and ESP32.If you want to use VS Code with the PlatformIO extension, follow the next tutorial instead to learn how to program the ESP32: So, make sure you have the ESP32 add-on installed. We’ll program the ESP32 board using Arduino IDE. The ultrasound receiver (echo pin) receives the reflected sound (echo).If it finds an object, it bounces back to the module. The ultrasound transmitter (trig pin) emits a high-frequency sound (40 kHz).The ultrasonic sensor uses sonar to determine the distance to an object. How Does the HC-SR04 Ultrasonic Sensor Work? Here’s the pinout of the HC-SR04 Ultrasonic Sensor. TTL pulse proportional to the distance range For more information, you should consult the sensor’s datasheet. The following table shows the key features and specs of the HC-SR04 ultrasonic sensor. You can check the Ultrasonic Sensor HC-SR04 sensor on Maker Advisor to find the best price:
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |