Ultrasonic sensor with Arduino

2019-09-20 - Authored by: Iain Campbell
ultrasonic sensor

Arduino Ultrasonic range sensor

This project will use an ultrasonic range sensor as a proximity meter.


The following components are required for this project

About this project

This project will use an ultrasonic sensor to detect motion and light different leds and sound a buzzer depending on the distance of an object from the sensor:

We will use Green, Yellow, Red leds and buzzer to signal distance of object as follows:

How the ultrasonic sensor works

The HC-SR04 device emits an ultrasound at 40kHz which travels through the air and if there is an object or obstacle on its path, the sound will bounce back to the module. Considering the travel time and the speed of the sound is 343m/s the object distance from the sensor can be calculated.

The ultrasonic sensor uses sonar to determine the distance to an object. Here’s what happens:

  1. The transmitter (trig pin) sends a signal: a high-frequency sound.
  2. When the signal finds an object, it is reflected
  3. The transmitter (echo pin) receives it.


  Speed of sound, v                = 343/s 
  Expressed in cm per micro second = 0.0343 cm/µs
  Distance = Speed / Time
  Time = Distance / speed
  e.g. Object at 10cm away
  Time = 10 / 0.0343 =  294 µ s (this needs to be halved to cater for the sound performs a round trip - forward and backward) 
  Distance = Time * 0.0343 / 2

For example, if the object is 10 cm away from the sensor, and the speed of the sound is 343 m/s or 0.0343 cm/µs the sound wave will need to travel about 294 µ seconds. But what you will get from the Echo pin will be double that number because the sound wave needs to travel forward and bounce backward. So in order to get the distance in cm we need to multiply the received travel time value from the echo pin by 0.0343 and divide it by 2.

HC-SR04 Timing diagram

Component diagram

The following shows a breadboard component diagram which will be used for this project.


int buzzer = 3, ledRed = 6, ledYellow = 9, ledGreeen = 10;
int triggerPin = 4;
int echoPin = 2;

long readTime(int triggerPin, int echoPin){
  // In order for the sensor to send out a pulse the trigger needs to be set low for 2us
  // Then set HIGH for 10us
  pinMode(triggerPin, OUTPUT);
  digitalWrite(triggerPin, LOW); 

  digitalWrite(triggerPin, HIGH); 
  digitalWrite(triggerPin, LOW); 
  pinMode(echoPin, INPUT); 
  return pulseIn(echoPin, HIGH);

void setup() {
  pinMode(buzzer, OUTPUT); 

void loop() {
  int distance; 
  distance = 0.01715 * readTime(triggerPin, echoPin); // From calculation above Distance = 0.0343 / 2 * readTime
  Serial.print("distance: "); 
  if(distance > 100){
  if(distance > 70 && distance < 100){
  if(distance < 70){

void red(){

    // light the red
    // sound the buzzer 
    // delay 100ms

void green(){

   digitalWrite(ledGreeen, HIGH);
   digitalWrite(ledYellow, LOW); 
   digitalWrite(ledRed, LOW); 
   digitalWrite(buzzer, LOW);

void yellow(){
  // light yellow..


Relay LED Blink
2019-09-25 - Authored by: Iain Campbell
How to use a relay in a circuit to make a LED blink
Arduino resources
2019-02-27 - Authored by: Iain Campbell
Arduino resources for the Electronics Dojo group.