#define SCREEN_HEIGHT 64 // OLED display height, in pixels #define SCREEN_WIDTH 128 // OLED display width, in pixels Then, simply copy the following code to your Arduino IDE and upload the code to the board. Also, don’t forget to select the right COM port in Tools > Port.Īfter installing the libraries, restart your Arduino IDE. Don’t forget to select the board you’re using in Tools > Boards. Serial.println(distanceInch) Demonstration distanceCm = duration * SOUND_SPEED/2 Ĭonvert the distance to inches: distanceInch = distanceCm * CM_TO_INCH Īnd finally, print the results on the Serial Monitor. Then, we simply calculate the distance to an object taking into account the sound speed. The pulse length corresponds to the time it took to travel to the object plus the time traveled on the way back. It returns the length of the pulse in microseconds. It accepts as arguments the pin and the state of the pulse (either HIGH or LOW). The pulseIn() function reads a HIGH or a LOW pulse on a pin. We use the pulseIn() function to get the sound wave travel time: duration = pulseIn(echoPin, HIGH) Note that before sending the pulse, we give a short LOW pulse to ensure you’ll get a clean HIGH pulse. In the loop(), the following lines produce a 10uS HIGH pulse on the trigger pin-this means the pin will emit an ultrasound. PinMode(echoPin, INPUT) // Sets the echoPin as an Input loop() pinMode(trigPin, OUTPUT) // Sets the trigPin as an Output And define the echo pin as an INPUT-the echo pin receives the reflected wave and sends a signal to the ESP8266 that is proportional to the travel time. Serial.begin(115200) // Starts the serial communicationĭefine the trigger pin as an OUTPUT-the trigger pin emits the ultrasound. In the setup(), initialize a serial communication at a baud rate of 115200 so that we can print the measurements on the Serial Monitor. The distanceCm and distanceInch, as the names suggest, save the distance to an object in centimeters and inches. The duration variable saves the travel time of the ultrasonic waves (time elapsed since transmission and reception of the pulse wave). Then, initialize the following variables. The CM_TO_INCH variable allows us to convert distance in centimeters to inches. The SOUND_SPEED variable saves the velocity of sound in the air at 20✬. But you can use any other suitable GPIOs-read ESP8266 Pinout Reference: Which GPIO pins should you use? In this example, we’re using GPIO 12 and GPIO 14. 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 on the Serial Monitor Reads the echoPin, returns the sound wave travel time in microsecondsĭistanceCm = duration * SOUND_VELOCITY/2 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 ESP8266 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 ESP8266.If you want to use VS Code with the PlatformIO extension, follow the next tutorial instead to learn how to program the ESP8266: Install the ESP8266 Board in Arduino IDE.So, make sure you have the ESP8266 add-on installed. We’ll program the ESP8266 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 |