Nothing Special   »   [go: up one dir, main page]

(QUICK START GUIDE) Set Up An Ultrasonic Range Finder On An Arduino

Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

Circuit Basics

Raspberry Pi, Arduino, and DIY Electronics Tutorials

QUICK START GUIDE


How to Set Up an Ultrasonic Range Finder
on an Arduino
SUMMARY
The HC-SR04 ultrasonic range finder uses sound to measure
the distance to solid objects. This guide will show you how
to build three different ultrasonic range finder circuits with MATERIALS
an Arduino. The first circuit is simple and has good
• Arduino Uno
accuracy, but the second circuit factors in temperature for
• Breadboard
more accurate distance measurements. The third circuit
• 16X2 LCD (Optional)
factors in both temperature and humidity for the greatest
• Jumper Wires
accuracy.
• Resistors
• Thermistor
How Ultrasonic Range Finders Work
• DHT11 Humidity Sensor
Ultrasonic range finders measure distance by emitting a • HC-SR04 Ultrasonic Range
pulse of sound that travels through the air until it hits an Finder
object. The pulse of sound is reflected off the object and is
detected by the ultrasonic range finder. The time it takes the
sound pulse to travel in its round trip journey back to the RESOURCES
sensor is recorded and used to calculate the distance to the
object with the formula: • Arduino Forums
• Circuit Basics
speed of sound = distance / time
• DHTLib Library
Rearranging this formula, we get:

distance = speed of sound x time

Temperature and Humidity Effects


The speed of sound in air changes with temperature and
humidity. Therefore, in order to accurately calculate distance
we need to factor in temperature and humidity into our
calculations.
Circuit Basics
Raspberry Pi, Arduino, and DIY Electronics Tutorials

The formula for the speed of sound in air is:

c = 331.4 + (0.606 x T) + (0.0124 x H)

c = Speed of sound in meters per second (m/s)

331.4 = Speed of sound (in m/s) at 0 °C and 0% humidity

T = Temperature in °C

H = % Humidity (relative humidity)

The HC-SR04 ultrasonic range finder has four pins:

Vcc = 5V power input used to generate the ultrasonic pulses.

GND = Connected to ground.

Trig = The Arduino sends a signal to this pin to start the distance measurement.

Echo = The ultrasonic range finder returns the time data to the Arduino at this pin.
Circuit Basics
Raspberry Pi, Arduino, and DIY Electronics Tutorials

A Basic Ultrasonic Range Finder Circuit


This circuit doesn’t factor humidity or temperature into the speed of sound formula.

Start by connecting the ultrasonic range finder to your Arduino:

Now upload this code to the Arduino, then open the serial monitor to see the distance
measurements:

#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;

if (distance >= 400 || distance <= 2){


Serial.print("Distance = ");
Serial.println("Out of range");
}

Program continued on next page…


Circuit Basics
Raspberry Pi, Arduino, and DIY Electronics Tutorials

else {
Serial.print("Distance = ");
Serial.print(distance);
Serial.println(" cm");
delay(500);
}
delay(500);
}

Temperature Compensated Ultrasonic Range Finder Circuit


The temperature data from a thermistor will allow us to get a more accurate calculation
of the speed of sound.

Connect the ultrasonic range finder and thermistor to your Arduino like this:

R1 = 10K Ohm resistor

Th = 10K Ohm thermistor

If you want to use a 100K Ohm thermistor instead, change the resistor to 100K Ohms,
and change line 7 in the code below to:
Temp = log(100000.0*((1024.0/RawADC-1)));
Circuit Basics
Raspberry Pi, Arduino, and DIY Electronics Tutorials

Now upload this program to the Arduino and open up the serial monitor:

#include <math.h>
#define trigPin 10
#define echoPin 13

double Thermistor(int RawADC) {


double Temp;
Temp = log(10000.0*((1024.0/RawADC-1)));
Temp = 1/(0.001129148+(0.000234125+(0.0000000876741*Temp*Temp))*Temp);
Temp = Temp - 273.15;
return Temp;
}

void setup() {
Serial.begin (9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}

void loop() {
int val;
double temp;
val=analogRead(0);
temp=Thermistor(val);

float duration, distance;


float spdSnd;
digitalWrite(trigPin, LOW);
delayMicroseconds(2);

digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);

duration = pulseIn(echoPin, HIGH);


spdSnd = 331.4 + (0.606 * temp) + 0.62;
distance = (duration / 2) * (spdSnd / 10000);

if (distance >= 400 || distance <= 2){


Serial.print("Distance = ");
Serial.println("Out of range");
}
else {
Serial.print("Distance = ");
Serial.print(distance);
Serial.println(" cm");
delay(500);
}
delay(500);
}
Circuit Basics
Raspberry Pi, Arduino, and DIY Electronics Tutorials

Temperature and Humidity Compensated Ultrasonic Range


Finder Circuit
You can get an even more accurate distance measurement by factoring humidity into
the speed of sound equation.

The DHT11 temperature and humidity sensor provides both temperature and
humidity data. To use the DHT11 on the Arduino, you’ll need to install the DHTLib
library on your computer. First, download the .zip file from here, then open your
Arduino IDE and go to Sketch > Include Library > Add ZIP Library, then select the
DHTLib.zip file.

Now, connect the ultrasonic range finder and DHT11 to your Arduino:
Circuit Basics
Raspberry Pi, Arduino, and DIY Electronics Tutorials

Next, upload this code to your Arduino and open the serial monitor:
#include <dht.h>

#define trigPin 10
#define echoPin 13
#define DHT11_PIN 7

dht DHT;

void setup() {
Serial.begin (9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}

void loop() {

float duration, distance;


float speed;

digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);

duration = pulseIn(echoPin, HIGH);


speed = 331.4 + (0.606 * DHT.temperature) + (0.0124 * DHT.humidity);
distance = (duration / 2) * (speed / 10000);

if (distance >= 400 || distance <= 2){


Serial.print("Distance = ");
Serial.println("Out of range");
}
else {
Serial.print("Distance = ");
Serial.print(distance);
Serial.println(" cm");
delay(1000);
}
delay(1000);
}

You might also like