-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path3_ultrasonic_sensor.ino
30 lines (26 loc) · 1.36 KB
/
3_ultrasonic_sensor.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
// Define the pin numbers for the ultrasonic sensor
const int echoPin = 3;
const int trigPin = 4;
void setup() {
Serial.begin(9600); // Start serial communication with a baud rate of 9600
pinMode(echoPin, INPUT); // Set echo pin as input
pinMode(trigPin, OUTPUT); // Set trig pin as output
Serial.println("Ultrasonic sensor:"); // Print a message indicating the ultrasonic sensor is ready
}
void loop() {
float distance = readDistance(); // Call the function to read the sensor data and get the distance
Serial.print(distance,1); // Print the distance value
Serial.println(" cm"); // Print " cm" to indicate the unit of measurement
delay(200); // Delay for 400 milliseconds before repeating the loop
}
// Function to read the sensor data and calculate the distance
float readDistance() {
digitalWrite(trigPin, LOW); // Set trig pin to low to ensure a clean pulse
delayMicroseconds(2); // Delay for 2 microseconds
digitalWrite(trigPin, HIGH); // Send a 10 microsecond pulse by setting trig pin to high
delayMicroseconds(10);
digitalWrite(trigPin, LOW); // Set trig pin back to low
// Measure the pulse width of the echo pin and calculate the distance value
float distance = pulseIn(echoPin, HIGH) / 58.00; // Formula: (340m/s * 1us) / 2
return distance;
}