Tempareture
Tempareture
Tempareture
Sensors are widely used for detecting devices by approximating their distance from the
source. One such example of a sensor is the HC-SR04 ultrasonic sensor which uses the
SONAR technique for the sensing purpose. The main feature of this sensor is to mimic the
nature of bats and therefore predict the distance of objects without actually establishing
contact with the device.
In this article, we will study how to design this ultrasonic sensor using the Arduino board and
programming software. We will study the specifications needed for this sensor, and the
methods and circuit diagrams used for designing this sensor. In addition to this, we will see
the Arduino code written on IDE that will result in a sensor.
Table of Content
Ultrasonic Sensor
Technical Specifications
Apparatus
Working
Circuit Diagram
Arduino Code
Arduino is used for creating this sensor since it needs to be programmed to detect the
reflected rays from objects and display the distance in the desired format.
Technical Specifications
Before getting started with the sensor, we need to mention some specifications of the sensor
that can ensure proper working conditions for the sensor.
The Arduino board needs a minimum power supply of 5V which is required to turn
the Arduino on. This must be Direct Voltage.
The Arduino board needs a Quiescent Current which is at most 2mA and
a working current of 15mA. Higher current values can burn the Arduino
components.
It is preferred that the object must be placed at the center line of the sensor but it can
also be present at a certain angle from the center. This is known as effectual angle
which should be less than 15°.
The distance at which an object must be placed so that it can be detected by a sensor
is around 2cm – 400 cm. The accuracy of detection decreases with an increase in
distance.
Apparatus
Apparatus refers to the components that are needed for the project. The components
required for designing a sensor using Arduino include.
One Breadboard
Carefully observe the diagram given above for making connections on the breadboard. Note
that the ultrasonic sensor has four terminals namely +5V, Trigger, Echo, and GND, follow the
steps given for making connections:
Connect a wire to supply Vcc with 5V to the appropriate pin on Arduino.
Make a ground-to-ground connection from GND to GND on Arduino.
Connect the trigger i.e. Trig to any pin on Arduino like pin 8.
Connect the Echo to any pin on Arduino like pin 9.
Arduino Code
Here is the pseudocode and code for making an ultrasonic Sensor using Arduino.
Code
const int pingPin = 8; // Trigger Pin of Ultrasonic Sensor
const int echoPin = 9; // Echo Pin of Ultrasonic Sensor
void setup() {
Serial.begin(9600); // Starting Serial Terminal
}
void loop() {
long duration, inches, cm;
pinMode(pingPin, OUTPUT); //Set which pin will be used for output
digitalWrite(pingPin, LOW); //Ensure that the pin in low first
delayMicroseconds(2); //Introducing delay of 2 milliseconds
digitalWrite(pingPin, HIGH); //Set which pin will be used for input
delayMicroseconds(10);
digitalWrite(pingPin, LOW);
pinMode(echoPin, INPUT);
duration = pulseIn(echoPin, HIGH); //Function for collecting the waves
inches = microsecondsToInches(duration);
cm = microsecondsToCentimeters(duration);
Serial.print(inches);
Serial.print("in, ");
Serial.print(cm);
Serial.print("cm");
Serial.println();
delay(100);
}
long microsecondsToInches(long microseconds) {
return microseconds / 74 / 2;
}
long microsecondsToCentimeters(long microseconds) {
return microseconds / 29 / 2;
}
Conclusion
Once you have uploaded this code on Arduino, the sensor will begin working. The
Arduino will successfully display the distance measured by a sensor in inches and cm. It will
appear on the computer screen depending on the function that you use. The sensor will work
accurately in different conditions like sunlight but it might not detect certain light items. You
can make further modifications to improve the project according to your needs by
incorporating some advanced boards.