Arduino

How to Interface Distance Sensor with Arduino Uno

To interface different devices with microcontrollers the Arduino boards can be used.  The use of an Arduino board can solve a lot of problems of interfacing faced when a microcontroller is connected with different sensors or input output devices.

The Arduino comes with various types of boards and the most common type of board used is the Arduino Uno board because of its compatibility with a wide range of devices. So, to connect a distance sensor with a microcontroller we have interfaced a distance sensor with an Arduino Uno board in this article.

Ultrasonic distance sensor (HC-SR04)

The distance sensor is used for various applications like measuring distance and obstacle detection. This sensor comes with one receiver and one transmitter and operates on the 5 volts. The sensors work in such a way that when a transmitter sends a signal, and the reflected signal is received at the receiver of the sensor it measures the distance covered by the received wave.

The maximum range for this sensor is 4 meters and generates a frequency of 40 KHz.

The sensor comes with the 4 pins in total and the detail of each pin is given in the table below:

Pin Description
1-(Vcc) To supply power to the sensor
2-(ECHO) The pin which produces signal when reflected wave is received
3-(Trig) The pin which produces ultrasonic wave by the transmitters
4(GRND) Pin used for grounding the sensor

Interfacing ultrasonic distance sensor with Arduino Uno

To interface the distance sensor the Arduino code is given followed by the schematic of the circuit design:

Hardware assembly for interfacing distance sensor with Arduino Uno

To interface the distance sensor with Arduino we have used the following list of components that are

  • Arduino Uno
  • Breadboard
  • One LED
  • Connecting wires
  • Ultrasonic distance sensor (SC-HR04)
  • One 220 ohm resistor

We have provided an image  below for assembling the hardware to give  a clear understanding of how we can interface the distance sensor with Arduino.

The brown wires are  connecting the trigger and echo pins of the ultrasonic distance sensor with Arduino Uno. Moreover, the blue wire connects the LED with Arduino and we have used the 5 volt supply pin of the Arduino to power the components.

Arduino code  for interfacing ultrasonic distance sensor with Arduino Uno

The Arduino code for  interfacing the distance sensor with Arduino Uno is given as

#define trig 7// Initialize the Trigger pin for sensor
#define echo 6 // Initialize the Echo pin for sensor
#define led 5 //Initialize pin for LED
int duration;
void setup(){
Serial.begin (9600); //initialize the Serial communication
pinMode(trig, OUTPUT);//giving the pin mode to  Trigger pin as output
pinMode(echo, INPUT);//giving the pin mode to Echo pin as input
pinMode(led, OUTPUT); //giving the pin mode to LED pin as output
}
void loop()
{
  long time, dist; /* variable for strong the distance and time  value*/
  digitalWrite(trig, LOW); // giving the state to trigger pin low
  delayMicroseconds(2); // time for which the trigger pin will be on LOW state
  digitalWrite(trig, HIGH); //giving the trigger pin is as high
  delayMicroseconds(10);//time for which the trigger pin will be on HIGH state
  digitalWrite(trig, LOW); // giving the trigger pin the state of low
  duration = pulseIn(echo, HIGH);//Reading the echo pin
  dist = (time / 2) / 29.1; // calculate the distance in cm
  if (dist <= 10) // if distance is less than 10 cm turn on the LED
  {
    Serial.print(dist);//displaying the distance value on serial port
    digitalWrite(led, HIGH);// giving the LED a HIGH state
    Serial.println("cm : LED is on state  ");
    delay(700);
  }
  else { // else keep the LED in the LOW state
    Serial.print(dist);//displaying the distance value on serial port
    digitalWrite(led, LOW);// giving the LED a LOW state
    Serial.println(" cm : LED is off state  ");
    delay(700);
  }
}

In the Arduino code first, we have assigned pins for the trig and echo pins of the distance sensor. After that the pins are given their respective modes using pinMode() function.

In the loop function we have generated the ultrasonic pulse with the delay of 2 microseconds and using the function of pulseIn() the pulse at the echo pin is received.

Similarly, to calculate the distance we have used this formula:

distance = (duration/2)/29.1;

Here the duration is the time given by the sensor and it is divided by the 2 because the ultrasonic wave sent by the sensor, and it was received by hitting a nearby object. So, we have calculated the time which the wave took to reach the sensor after deflecting. Furthermore, to calculate the distance in centimeters we have divided it from 29.1.

In the last we have used the if else condition that if the distance is less than 10, turn on the LED otherwise keep the LED in off state.

Simulation

The simulation is carried out using a simulation software and in the simulation, as you can see if  the distance is less than 10 the LED will turn on and the LED will turn off as the distance increases from 10.

Arduino Code output of interfacing distance with Arduino on hardware

We have posted the image of the hardware assembled for interfacing the distance sensor with Arduino:

Here is the working of the distance sensor:

Conclusion

The distance sensor is an ultrasonic sensor having a range of 4 meters which can be used for either measuring the distance or detection of any obstacle. This sensor is mostly used in the robots or in the safety system of cars to avoid any collision from incoming objects. Moreover, we can use this sensor by interfacing it with Arduino Uno for making collision detection or obstacle detection systems.

About the author

Aaliyan Javaid

I am an electrical engineer and a technical blogger. My keen interest in embedded systems has led me to write and share my knowledge about them.