Things used in this project

Hardware components

Ultrasonic Sensor – HC-SR04 (Generic)






Arduino MKR WiFi 1010



Jumper wires (generic)



9V battery (generic)



Linear Regulator (7805)



General Purpose Transistor NPN



Resistor 10k ohm



Software apps and online services

Taoglas Fritzing

Arduino IDE

Arduino IoT Cloud

Hand tools and fabrication machines

Tape, Double Sided

Tape, Electrical

Hot glue gun (generic)

Problem of Quarantine enforcement

The reason for community spread of the virus in many countries is because many citizens defy quarantine rules and tread out of quarantine centres thus putting the society to risk at large. Law enforcement personnel have to constantly stay on guard outside such quarantine centres but as the number of cases skyrockets this is not a viable option

What needs to be done

I would make an alerter system that monitors the gates of a quarantine centre or home for persons leaving premises. In the event of a person moving out, law enforcement agencies are immediately alerted via notifications on an app that connects to the node through a webservice

How the Arduino MKR1010 can help

The node would be made of an Arduino MKR 1010 Wifi board and take input from Ultrasonic sensors placed at the gates of a quarantine home or centre.

On being triggered the node sends an MQTT message over Wifi to a webservice that sends notifications through a mobile application to concerned personnel.

The webservice would be made on NodeRed and act as a storage for the count of such extrusions. This would help nab mischief-mongers in time so that society is safe.

The Build

Circuit diagram

The chief component in my project as I have outlined earlier also is the ultrasonic sensor HC-SR04 which returns the distance of the nearest object in front of it. In my project, I am going to place it at the head of the main door to my house. Whenever, a person is going in or coming out the sensors would be able to detect the movement and raise an alarm.

But how does this HC-SR04 work anyway?? Lets find out.

HC-SR04 Hardware Overview

At its core, the HC-SR04 Ultrasonic distance sensor consists of two ultrasonic transducers. The one acts as a transmitter which converts electrical signal into 40 KHz ultrasonic sound pulses. The receiver listens for the transmitted pulses. If it receives them it produces an output pulse whose width can be used to determine the distance the pulse travelled. As simple as pie!

The sensor is small, easy to use in any robotics project and offers excellent non-contact range detection between 2 cm to 400 cm (that’s about an inch to 13 feet) with an accuracy of 3mm. Since it operates on 5 volts, it can be hooked directly to an Arduino or any other 5V logic microcontrollers.

Here are complete specifications:

Operating Voltage : DC 5V

Operating Current : 15mA

Operating Frequency : 40KHz

Max Range : 1.8m

Min Range : 2cm

HC-SR04 Ultrasonic Sensor Pinout

Let’s take a look at its Pinout.

VCC is the power supply for HC-SR04 Ultrasonic distance sensor which we connect the 5V pin on the Arduino.

Trig (Trigger) pin is used to trigger the ultrasonic sound pulses.

Echo pin produces a pulse when the reflected signal is received. The length of the pulse is proportional to the time it took for the transmitted signal to be detected.

GND should be connected to the ground of Arduino.

How Does HC-SR04 Ultrasonic Distance Sensor Work?

It all starts, when a pulse of at least 10 µS (10 microseconds) in duration is applied to the Trigger pin. In response to that the sensor transmits a sonic burst of eight pulses at 40 KHz. This 8-pulse pattern makes the “ultrasonic signature” from the device unique, allowing the receiver to differentiate the transmitted pattern from the ambient ultrasonic noise.

The eight ultrasonic pulses travel through the air away from the transmitter. Meanwhile the Echo pin goes HIGH to start forming the beginning of the echo-back signal.

In case, If those pulses are not reflected back then the Echo signal will timeout after 38 mS (38 milliseconds) and return low. Thus a 38 mS pulse indicates no obstruction within the range of the sensor.

If those pulses are reflected back the Echo pin goes low as soon as the signal is received. This produces a pulse whose width varies between 150 µS to 25 mS, depending upon the time it took for the signal to be received.

The width of the received pulse is then used to calculate the distance to the reflected object. This can be worked out using simple distance-speed-time equation, we learned in High school. In case you forgot, an easy way to remember the distance, speed and time equations is to put the letters into a triangle.

Let’s take an example to make it more clear. Suppose we have an object in front of the sensor at an unknown distance and we received a pulse of width 500 µS on the Echo pin. Now let’s calculate how far the object from the sensor is. We will use the below equation.

Distance = Speed x Time

Here, we have the value of Time i.e. 500 µs and we know the speed. What speed do we have? The speed of sound, of course! Its 340 m/s. We have to convert the speed of sound into cm/µs in order to calculate the distance. A quick Google search for “speed of sound in centimetres per microsecond” will say that it is 0.034 cm/µs. You could do the math, but searching for it is easier. Anyway, with that information, we can calculate the distance!

Distance = 0.034 cm/µs x 500 µs

But this is not done! Remember that the pulse indicates the time it took for the signal to be sent out and reflected back so to get the distance so, you’ll need to divide your result in half.

Distance = (0.034 cm/µs x 500 µs) / 2

Distance = 8.5 cm

So, now we know that the object is 8.5 centimetres away from the sensor.

Wiring – Connecting HC-SR04 to Arduino Uno

Now that we have a complete understanding of how HC-SR04 ultrasonic distance sensor works, we can begin hooking it up to our Arduino!

Connecting the HC-SR04 to the Arduino is pretty easy. Start by placing the sensor on to your breadboard. Connect VCC pin to the 5V pin on the Arduino and connect GND pin to the Ground pin on the Arduino.

When you’re done you should have something that looks similar to the illustration shown below.

// This uses Serial Monitor to display Range Finder distance readings

// This uses Serial Monitor to display Range Finder distance readings


 // Include NewPing Library

 #include "NewPing.h"


 // Hook up HC-SR04 with Trig to Arduino Pin 9, Echo to Arduino pin 10

 #define TRIGGER_PIN 9

 #define ECHO_PIN 10


 // Maximum distance we want to ping for (in centimeters).

 #define MAX_DISTANCE 400          


 // NewPing setup of pins and maximum distance.


 float duration, distance;


 void setup() 





 void loop() 


               // Send ping, get distance in cm

               distance = sonar.ping_cm();


               // Send results to Serial Monitor

               Serial.print("Distance = ");


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


                              Serial.println("Out of range");





                              Serial.println(" cm");





Code Explanation:

The above sketch is simple and works well but it only has a resolution down to one centimeter. If you want to bring back the decimal point values you can use NewPing in duration mode instead of in distance mode. You need to replace this line

// Send ping, get distance in cm

 distance = sonar.ping_cm();

with below lines

duration = sonar.ping();

 distance = (duration / 2) * 0.0343;

To improve the accuracy of your HC-SR04 to the next level, there’s another function in NewPing library called “iterations”. To iterate means to go over something more than once, and that’s precisely what the iteration mode does. It takes many duration measurements instead of just one, throws away any invalid readings and then averages the remaining ones. By default it takes 5 readings but you can actually specify as many as you wish.

int iterations = 5;

 duration = sonar.ping_median(iterations);

What are the limitations?

In terms of accuracy and overall usefulness, HC-SR04 ultrasonic distance sensor is really great, especially compared to other low-cost distance detection sensors. That doesn’t mean that the HC-SR04 sensor is capable of measuring “everything”. Following diagrams shows a few situations that the HC-SR04 is not designed to measure:

a) The distance between the sensor and the object/obstacle is more than 1.8m

b) The object has its reflective surface at a shallow angle so that sound will not be reflected back towards the sensor.

c) The object is too small to reflect enough sound back to the sensor. In addition, if your HC-SR04 sensor is mounted low on your device, you may detect sound reflecting off of the floor.

d) Some objects with soft, irregular surfaces (such as stuffed animals) absorb rather than reflect sound and therefore can be difficult for the HC-SR04 sensor to detect.

None of these limitations would affect my project in any way due to the clever placement of the sensors and the considerations of clearances in the code.

Setting up Arduino IoT Cloud

The other major part of my project is uploading ultrasonic sensor data to the Arduino IoT Cloud.

·       Go to Arduino IoT Cloud

·       Make an account and verify your email address

·       Go to the Devices tab and connect your Arduino MKR1010 Wifi to your computer via a microUSB-B cable

This message would be displayed 

Name it

·       After pressing Next, you will be greeted with this screen

·       Create a new Thing

Things are nothing but a software abstraction of a physical object like an Arduino board. Properties are the entities that help a thing interact with the physical world. This concept is nothing new to those familiar with Java, C++ or any Object-Oriented Programming language.

NOTE: Before I reached this point, I did get an error stating that the hardware security certificate on my board is corrupted. Simply pressing the RESET button, the tiny blue one around the centre, opposite the RGB LED, solved it.

·       Go to the Add Property tab and add all the necessary data configuration

·       Your device is all set to send data to the cloud

·       A Sketch would automatically be generated. Edit the Sketch and upload to your board using Arduino Create Agent

NOTE: The interface is not the same as the Arduino IDE that we are familiar with though it is largely similar.

·       Before uploading the code do ensure the fact that your Wifi credentials that correspond to the connection that the Arduino board would have to connect to are entered in the spaces given

·       Go to your Dashboards tab and create a Dashboard. The UI is really cool and intuitive too. Therefore, I am assuming that the reader is smart enough to build one. There are many resources online from where one may learn about setting it up. Here is a very good introductory tutorial to set it up.


·       If you have issues, let me know in the comments.

Making and setting it up : Placing two ultrasonic sensors for entry and exit

I took a cardboard box I found lying around and made two holes to fit the two ultrasonic sensors. I kept all the other things like the power source, buzzer and wiring inside.

For the Arduino to receive a strong Wifi signal, I had to leave an opening at the top as I have shown here.

Space for Wifi antenna

Oblique view

I placed this setup at the head of the main entrance to our house and for testing it out I had to convince family members to walk in and out of the house a few times. Recreation during lockdown!!! lol!!

This is the final circuit diagram for the setup

Dashboard for monitoring the bad ones!!

As my dashboard clearly depicts, the distances from both the ultrasonic sensors are logged and the number of inmates is changed according to the pattern of data received from the sensors.

Here, distance_1 is the reading of the sensor on the inward side of the door and vice-versa. Thus, if a person leaves premises, then the distance_1 drops to a low value before distance_2 and vice-versa as can be seen from the generated graph.

Summing it up

All in all, I would say that I have accomplished what I set out to do. My system can easily be installed in any quarantine centre at minimal cost and can be used by enforcement personnel for remote monitoring and catching mischief-makers red-handed if they plan to break rules.

However, a lot can be bettered here. The entire setup can be placed in a 3D-printed enclosure. OTA update feature can be added for more configurability. Also, instead of the ultrasonic sensors, TOF LIDAR sensors can be used for more accuracy.

Thanks for reading. Stay safe.

Detect and Protect with Arduino!!






Thanks for reading!!


Leave a reply

Your email address will not be published. Required fields are marked *



Log in with your credentials


Forgot your details?


Create Account