Note
Hello, welcome to the SunFounder Raspberry Pi & Arduino & ESP32 Enthusiasts Community on Facebook! Dive deeper into Raspberry Pi, Arduino, and ESP32 with fellow enthusiasts.
Why Join?
Expert Support: Solve post-sale issues and technical challenges with help from our community and team.
Learn & Share: Exchange tips and tutorials to enhance your skills.
Exclusive Previews: Get early access to new product announcements and sneak peeks.
Special Discounts: Enjoy exclusive discounts on our newest products.
Festive Promotions and Giveaways: Take part in giveaways and holiday promotions.
👉 Ready to explore and create with us? Click [here] and join today!
Lesson 13 Ultrasonic¶
Introduction¶
When you are reversing, you will see the distance between the car and the surrounding obstacles to avoid collision. The device for detecting the distance is an ultrasonic sensor. In this experiment, you will learn how the ultrasonic wave detects the distance.
Components¶
Schematic Diagram¶
Experimental Procedures¶
Step 1: Build the circuit.
Step 2: Open the code file.
Step 3: Select the Board and Port.
Step 4: Upload the sketch to the board.
Now, if you use a piece of paper to approach or keep it far away from the sensor. You will see the value displayed on the LCD changes, which indicates the distance between the paper and the ultrasonic sensor.
Code¶
Code Analysis¶
1. Initialize the ultrasonic sensor and LCD1602
#include <LiquidCrystal.h>
LiquidCrystal lcd(4, 6, 10, 11, 12, 13); //lcd(RS,E,D4,D5,D6,D7)
const int trigPin = 2; // trig pin on the ultrasonic sensor attach to pin2 .
const int echoPin = 3; // echo pin on the ultrasonic sensor attach to pin3.
void setup() {
lcd.begin(16, 2); // set the position of the characters on the LCD as Line 2, Column 16
Serial.begin(9600);
pinMode(echoPin, INPUT);
pinMode(trigPin, OUTPUT);
}
2. Display the distance on the LCD1602
void loop() {
float distance = readSensorData();
//Serial.println(distance); //Print the distance on the serial monitor
lcd.setCursor(0, 0); //Place the cursor at Line 1, Column 1. From here the characters are to be displayed
lcd.print("Distance:"); ////Print Distance: on the LCD
lcd.setCursor(0, 1); //Set the cursor at Line 1, Column 0
lcd.print(" "); //Here is to leave some spaces after the characters so as to clear the previous characters that may still remain.
lcd.setCursor(9, 1); //Set the cursor at Line 1, Column 9.
lcd.print(distance); // print on the LCD the value of the distance converted from the time between ping sending and receiving.
lcd.setCursor(12, 1); //Set the cursor at Line 1, Column 12.
lcd.print("cm"); //print the unit "cm"
}
3. Convert the time to distance
float readSensorData(){// ...}PING is triggered by a HIGH pulse of 2 or more microseconds. (Give a short LOW pulse beforehand to ensure a clean HIGH pulse.)
digitalWrite(trigPin, LOW); delayMicroseconds(2); digitalWrite(trigPin, HIGH); delayMicroseconds(10); digitalWrite(trigPin, LOW);The echo pin is used to read signal from PING, a HIGH pulse whose duration is the time (in microseconds) from the sending of the ping to the reception of echo of the object.
microsecond=pulseIn(echoPin, HIGH);The speed of sound is 340 m/s or 29 microseconds per centimeter.
This gives the distance travelled by the ping, outbound and return, so we divide by 2 to get the distance of the obstacle.
float distance = microsecond / 29.00 / 2;