Ultrasonic Sensing

I have the urge to publish a project involving basic Ultrasonic Sensor programming as it is one of the most frequently asked how to do’s and is used quite often in my projects.

Photo above: courtesy of http://www.dx.com

The HC-SR04 or Ultrasonic sensor is used to measure a the distance of nearby objects and may be used to set thresholds and collect raw data. It does this by using ultrasonic waves (as the name suggests) and by using a process similar to echolocation, the sensor is able to determine the distance of the closest object to it. When the “Trig” pin is activated, the waves are emitted and as you will see in the code, the “echo” pin waits to receive the waves that bounce back. The time taken for the reflected wave to reach the sensor can be used to estimate the distance of the closest object.

 

Below I have provided the code and wiring “schematics” for the project. I have tried to make it as simple and direct as possible…

Ultrasonic Schematic.png

Do feel free to copy and paste the code below if you are trying to learn. I have no problem with this. Hopefully I have made the code easier to understand by commenting the purpose of each line…


int trigPin = 7; // declares that the trig pin of the Ultrasonic sensor is in pin 13
const int echoPin = 2; // declares that the echo pin of the Ultrasonic sensor is in pin 12 (it has const so it cannot be altered)
int led = 11; // declares that the red LED is in pin 11
int led2 = 10;

void setup() {
Serial.begin (9600); // this starts serial communication and can be viewed on the serial monitor to get results of distances
pinMode(trigPin, OUTPUT); // this states that the trig pin is a source of output
pinMode(echoPin, INPUT); // this states that the echo pin is a source of input
pinMode(led, OUTPUT); // this states that the trig pin is a source of output
pinMode(led2, OUTPUT);
}

void loop() { // everything put beyond the loop means that it will be repeated forever
long duration, distance; // this establishes variables for the duration of the "ping", and the distance
digitalWrite(trigPin, LOW); // trig pin will be off
delayMicroseconds(2); // wait 2 micro seconds
digitalWrite(trigPin, HIGH); // supply trig pin with power
delayMicroseconds(5); // wait five microseconds
digitalWrite(trigPin, LOW); // trig pin will be off
duration = pulseIn(echoPin, HIGH); // the duration is when pulse In (Reads a pulse (either HIGH or LOW) on a pin. For example, if value is HIGH, pulseIn() waits for the pin to go HIGH, starts timing, then waits for the pin to go LOW and stops timing.
distance = (duration/2) / 29.1; // The speed of sound is 29.1 microseconds per centimeter. The "ping" travels out and back, so to find the distance of the object we take half of the distance travelled.
if (distance < 10) { // If the distance is less than 10 cm digitalWrite(led, HIGH); // Activiate the LED } else { // Other wise digitalWrite(led,LOW); // turn the LED off digitalWrite(led2, HIGH); } if (distance >= 200 || distance <= 0){ // if the distance is greater than or equal to 200 or less than or equal to 0
Serial.println("Out of range"); // write out of range on serial moniter
}
else {
Serial.print(distance); // other wise just put the distance in the serial monitor
Serial.println(" centimeters"); // write the unit as centimeters on the serial monitor
}
delay(500); // wait 500 microseconds
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s