![]() These two variables are declared to store duration and distance values. Hence, through the program, we will use these symbolic names instead of pin numbers. Similarly, the GPIO2 pin is assigned the name LED. This defines that GPIO5 and GPIO18 pins of ESP32 are used to control trigger and echo pins of the HC-SR04 sensor. print measured distance value on Arduino serial monitorįirst, define the names of ESP32 pins using #define preprocessor directives. * if distance greater than 10cm, turn on LED */ *Measure the Echo output signal duration or pulss width */ĭuration = pulseIn(Echo_pin, HIGH) // save time duration value in "duration variableĭistance= duration*0.034/2 //Convert pulse duration into distance *send 10 microsecond pulse to trigger pin of HC-SR04 */ĭigitalWrite(trigger_pin, HIGH) // make trigger pin active highĭelayMicroseconds(10) // wait for 10 microsecondsĭigitalWrite(trigger_pin, LOW) // make trigger pin active low Serial.begin(9600) // Enable the serial with 9600 baud rateĭigitalWrite(trigger_pin, LOW) //set trigger signal low for 2us PinMode(Echo_pin, INPUT) // configure the Echo_pin(D11) as an Input ![]() PinMode(LED, OUTPUT) // Set the LED (D13) pin as a digital output PinMode(trigger_pin, OUTPUT) // configure the trigger_pin(D9) as an Output * configure D9 and D11 as digital input and output respectively */ * two variables to store duraion and distance value */ * Define the names for ESP32 pin for HC-SR04*/ * Ultrasonic Sensor HC-SR04 interfacing with ESP32 This code takes the distance measurement and if the measured distance value is less than 10cm, LED will turn on. ESP32 has an onboard LED that is connected with the GPIO2 pin. ![]() In this example code, we will control an LED based on measured distance value. HC-SR04Īrduino Code for Ultrasonic Sensor with LED In this schematic diagram, we use the GPIO5 pin of ESP32 to provide a trigger signal and GPIO18 to capture the echo output pulse. Now make the connection of the ESP32 with the HC-SR04 sensor according to this connection diagram. Similarly, one GPIO pin will be used as a digital input pin to capture echo output signal of output sensor. One GPIO pin of the ESP32 will be used as a digital output pin to provide a trigger signal to the ultrasonic sensor. Now we know that to interface an HC-SR04 sensor with ESP32, we need four pins out of which two are power supply pins and two are digital input output pins. Until now we have seen the working of the ultrasonic sensor and the pin details. Therefore, the formula to calculate distance becomes : S = 17000 * t // distance in cm How to Interface HC-SR04 Ultrasonic sensor with ESP32 We want to find the distance between HC-SR04 and the object only. Because ultrasonic waves travel from the transmitter to the obstacle and then reflect back to the receiver circuit by traveling the same distance. ![]() The time given in the above formula should also be divided by two. The following figure illustrates the echo output signal with respect input trigger signal and 8 sonar pulses. ![]() In short, by measuring the on-time of the Echo output pulse signal, we can measure the distance. ESP32 detects this transition of echo output signal from active high to an active low level and stops the measurement. As soon as the ultrasonic waves received by the receiver circuit after striking with an object, the echo pin goes low.These waves travel through the air and if there is any object placed in parallel to the sensor, these waves reflect back after a collision with the object.When the Echo pin goes high, We start to measure time with the ESP32 duration measurement function.At the same time, the Echo pin also makes a transition from a logic low level to a logic high level. As soon as 10µs input trigger signal becomes active low, the transmitter circuit produces a burst of 8 ultrasonic sonar pulses.To start ranging with HC-SR04, first, we apply 10µs pulse to the trigger pin of the HC-SR04 sensor from the ESP32 digital output pin. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |