Hc sr04 ultrasonic sensor arduino code12/1/2023 ![]() ![]() * 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 ESP8266 pin for HC-SR04*/ * Ultrasonic Sensor HC-SR04 interfacing with ESP8266 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. ESP8266 NodeMCU 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. ![]()
0 Comments
Leave a Reply.AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |