Difference between revisions of "Ultrasonic Sensor (HC-SR04) + Arduino"
Jessieshi97 (talk | contribs) |
m (Ethanshry moved page Controlling HC-SR04 Ultrasonic distance sensor with Arduino to Ultrasonic Sensor (HC-SR04) + Arduino: Fall 2018 Wiki Reworks) |
||
(5 intermediate revisions by 2 users not shown) | |||
Line 1: | Line 1: | ||
==Overview== | ==Overview== | ||
− | The HC-SR04 ultrasonic distance sensor uses sonar to determine distance to an object with stable readings and high accuracy of 3mm. The module includes ultrasonic transmitter, receiver and control circuit. | + | The HC-SR04 ultrasonic distance sensor uses sonar to determine distance to an object with stable readings and high accuracy of 3mm. The module includes ultrasonic transmitter, receiver and control circuit. In order to generate the ultrasound, you have to set the trig on high state for 10 microseconds. The trig pin will send out the sonic burst which travels at the speed of sound. The soundwave will be bounced back once it interacts with a solid or liquid and will then be received in the echo pin. The distance between the sensor and the object will be calculated using the time of travel. More explanations of work principles are outlined [https://cdn.sparkfun.com/datasheets/Sensors/Proximity/HCSR04.pdf here] if interested. One of the outstanding features of this distance sensor is that it can detect not only the distance between itself to a solid object, it can also detect liquids (used in the [http://classes.engineering.wustl.edu/ese205/Cheers! Cheers] group). |
+ | |||
+ | If you are planning to use the Ultrasonic distance sensor on a Rasberry Pi instead of an Arduino, click [http://classes.engineering.wustl.edu/ese205/Connecting_HC-SR04_sensor_to_Raspberry_Pi here]. | ||
==Arduino== | ==Arduino== | ||
+ | A sample code is outlined below. It uses an if-else statement to check if the distance is out of range (2cm-400cm). The Serial.print commands are used for the user to visually see the the measured distances. You can delete it in the final code for your project. | ||
+ | |||
+ | <source lang="c++"> | ||
+ | #define trigPin 10 | ||
+ | #define echoPin 13 | ||
+ | |||
+ | void setup() { | ||
+ | Serial.begin (9600); | ||
+ | pinMode(trigPin, OUTPUT); | ||
+ | pinMode(echoPin, INPUT); | ||
+ | } | ||
+ | |||
+ | void loop() { | ||
+ | float duration, distance; | ||
+ | digitalWrite(trigPin, LOW); | ||
+ | delayMicroseconds(2); | ||
+ | |||
+ | digitalWrite(trigPin, HIGH); | ||
+ | delayMicroseconds(10); | ||
+ | digitalWrite(trigPin, LOW); | ||
+ | |||
+ | duration = pulseIn(echoPin, HIGH); | ||
+ | distance = (duration / 2) * 0.0344; | ||
+ | |||
+ | // see if the distance is out of range | ||
+ | if (distance >= 400 || distance <= 2){ | ||
+ | Serial.print("Distance = "); | ||
+ | Serial.println("Out of range"); | ||
+ | } | ||
+ | else { | ||
+ | Serial.print("Distance = "); | ||
+ | Serial.print(distance); | ||
+ | Serial.println(" cm"); | ||
+ | delay(10); | ||
+ | } | ||
+ | delay(10); | ||
+ | } | ||
+ | |||
+ | </source> | ||
==Circuit== | ==Circuit== | ||
Line 17: | Line 58: | ||
* http://howtomechatronics.com/tutorials/arduino/ultrasonic-sensor-hc-sr04/ | * http://howtomechatronics.com/tutorials/arduino/ultrasonic-sensor-hc-sr04/ | ||
[[Category:HowTos]] | [[Category:HowTos]] | ||
+ | [[Category:Arduino]] | ||
+ | [[Category:Electronics]] |
Latest revision as of 02:37, 17 August 2018
Overview
The HC-SR04 ultrasonic distance sensor uses sonar to determine distance to an object with stable readings and high accuracy of 3mm. The module includes ultrasonic transmitter, receiver and control circuit. In order to generate the ultrasound, you have to set the trig on high state for 10 microseconds. The trig pin will send out the sonic burst which travels at the speed of sound. The soundwave will be bounced back once it interacts with a solid or liquid and will then be received in the echo pin. The distance between the sensor and the object will be calculated using the time of travel. More explanations of work principles are outlined here if interested. One of the outstanding features of this distance sensor is that it can detect not only the distance between itself to a solid object, it can also detect liquids (used in the Cheers group).
If you are planning to use the Ultrasonic distance sensor on a Rasberry Pi instead of an Arduino, click here.
Arduino
A sample code is outlined below. It uses an if-else statement to check if the distance is out of range (2cm-400cm). The Serial.print commands are used for the user to visually see the the measured distances. You can delete it in the final code for your project.
#define trigPin 10
#define echoPin 13
void setup() {
Serial.begin (9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}
void loop() {
float duration, distance;
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration / 2) * 0.0344;
// see if the distance is out of range
if (distance >= 400 || distance <= 2){
Serial.print("Distance = ");
Serial.println("Out of range");
}
else {
Serial.print("Distance = ");
Serial.print(distance);
Serial.println(" cm");
delay(10);
}
delay(10);
}
Circuit
There are four pins on the HC-SR04 Ultrasonic distance sensor:
- Vcc: power.
- Trig: sends out the sonic burst, connected to pin 10.
- Echo: receive the returned soundwave, connected to pin 13.
- GND: ground.