Holiday

Search Site

Arduino circuit for Ultra-Sonic distance sensing

Ultra-Sonic Distance Sensing – Intermediate/Advanced


Overview of the project:

Ultrasonic Distance


This project uses the HC-SR04 ultra-sonic distance sensor to read in the distance between it and an object in front of it and using this information, we can flash an LED if an object is too close. This could be useful for all sorts of applications such as an assistant to help you park your car and stop you hitting walls.
The way ultra-sonic distance sensing works is actually fairly simple. A short burst of sound (much higher frequency than humans can hear!) is sent out from the speaker in the module, and then we simply measure the time it takes for that to return in micro-seconds. We then take that time and divide it by 2 (to get only the time it took to reach the object) and then take that number and divide it by 29.1 to give us the distance in cm rather than the time. The value of 29.1 was determined using the speed of sound through air.
To do all of this we will use a micro-processor, in our case we will use the Arduino Uno, though any Arduino will do for this project. We will use this little chip to do all the timing and calculations for us and then control an LED depending on how close the object is to the sensor.
A micro-controller is like a little computer, but in a very tiny chip. It can be used in projects which cannot be done simply on components from a store but which do not require a full computer. An Arduino is one of these micro-controllers with all the pins broken out into little headers for easy access and all the power regulation done for you. They are really easy ways to prototype new project ideas.

Im watching you


Building the project:


For this project you will need:
⦁    One Arduino
⦁    One Breadboard
⦁    Some Hook-up wire
⦁    A HC-SR04 Ultra-sonic distance sensor
⦁    One LED
⦁    One 220Ω Resistor
⦁    One power supply for Arduino
⦁    The appropriate USB cable for your Arduino


First you should connect your Ultra-Sonic sensor to the Arduino, the pin labelled Vcc should be connected to the pin labelled 5V on the Arduino (red wire), the pin labelled Gnd should be connected to one of the pins labelled Gnd on the Arduino (black wire) and the 220Ω resistor, the pin labelled Trig should be connected to pin 13 on the Arduino (blue wire) and the pin labelled Echo should be connected to pin 12 on the Arduino (green wire). Also connect an LED up to pin 11 with the 220Ω resistor (yellow wire).

Connections to ArduinoConnections to Sensor

Then connect your power supply. In our case we’ve used a battery pack to make it more portable, but you can use a DC adapter if you like, or the USB on your PC.

Next you need to program the Arduino, to do this you need to download the Arduino IDE (integrated development environment) from the arduino website: http://arduino.cc
Follow their instructions to set up your Arduino if it is your first time using one, then plug it in and upload the sketch you can find below. It is explained within the sketch how it works.

 

Code:

#define echoPin 12 //Echo Pin
#define trigPin 13 //Trigger Pin
#define LEDPin 11 //LED

long duration, distance; //Variables for holding the distance and duration of pulse
int ledDelay = 10; //Adjust for speed of LED flash
int ledCount = 0; //Variable needed for LED flash

boolean ledState = 0; //State of the LED

void setup() {
 Serial.begin (9600);  //Begin Serial communication with the computer
 pinMode(trigPin, OUTPUT); //Define trigger pin as output
 pinMode(echoPin, INPUT); //Define echo pin as input
 pinMode(LEDPin, OUTPUT); //Define LED pin as output
}

void loop() { //Main loop of code, will repeat this
 digitalWrite(trigPin, LOW); //Sequence to send out sound pulse
 delayMicroseconds(2);

 digitalWrite(trigPin, HIGH);
 delayMicroseconds(10);
 
 digitalWrite(trigPin, LOW);
 duration = pulseIn(echoPin, HIGH); //Time for return pulse
 
 distance = (duration/2)/29.1; //Calculate distance using speed of sound (distance in cm)

 Serial.println(distance); //Send distance to computer

 if (distance < 50) ledState = 1; //If distance is unnder 50cm turn LED on
 
 if (distance >= 50 && distance < 100) ledCount++; //If distance is between 50 and 100, increase ledCount by 1
 if (distance >= 100) { //If distance is greater than or equal to 100, turn the LED off and set ledCount to 0
   ledCount = 0;
   ledState = 0;
 }
 
 
/* Below is the code that makes the LED flash, each iteration takes time, so we use this to time the LED flash */
/* Every time we measure the distance we increase the count, until it has reached the set value above, then    */
/* we reset the count and swap the LED state. This means it will be on for (in our case) 10 measurements, and  */
/* off for 10 measurements. */

 if (ledCount >= ledDelay) { //If led count is greater than or equal to the delay value
   ledState = !ledState; //Swap the LED state i.e. if it's on turn it off and vice versa
   ledCount = 0; //Reset the count
 }

 digitalWrite(LEDPin, ledState); // set the LED to whatever state it should be

 delay(50);
}




Back to top