Ultrasonic Sensor SR04 with arduino
Welcome to the second Tutorial in the series of Arduino Learn by Doing.Today our task is to learn about Ultrasonic sensor. How and why to use ultrasonic sensor and the implementation of ultrasonic SR04 SONAR sensor module with Arduino. So today We will discuss some of the projects with ultrasonic sensor later in coming posts but before that, we will see the working and usage of ultrasonic sensor SR04 with Arduino.
So Let’s start with the contents list.
Contents:
- Introduction to Ultrasonic SR04 SONAR sensor
- How Ultrasonic sensor works
- Components list
- Circuit diagram
- Arduino code for Ultrasonic SR04 sensor
Introduction to Ultrasonic SR04 SONAR sensor
The basic phenomenon of measuring distance or detecting an obstacle with ultrasonic sound waves comes from underwater communication.And If I’m not wrong we can say the communication way of fishes.
How Ultrasonic SR04 SONAR sensor works
The SR04 module has 4 pins other than the power supply and ground one is an echo that is used for receiving signals and the other is the trigger that is used for triggering the signal.
Components List
- 1x Arduino Board Uno (any of your choice)
- HC-SR04 Ultrasonic SONAR sensor
- 1x Bread board
- jumper wires
Circuit Diagram
- Connect the Vcc pin to the 5v on Arduino and Ground pin to GND on Arduino
- Connect Trig pin to any digital pin on Arduino
- Connect echo pin on any digital PWM pin on Arduino as echo pin is our INPUT
Connect all pins correctly and test the sample code first then try to implement more sensor and complex code.The Arduino code is, as usual, simple so let’s see the coding part.
Arduino Code
#define trigPin 11 ......... #define echoPin 10 ........ void setup() { // ................. Serial.begin (9600); pinMode(trigPin, OUTPUT); //................. pinMode(echoPin, INPUT); ........... } //.................... void loop() // ............ { long duration, distance; digitalWrite(trigPin, LOW); delayMicroseconds(2); digitalWrite(trigPin, HIGH); delayMicroseconds(10); digitalWrite(trigPin, LOW); duration = pulseIn(echoPin, HIGH); // ................................. distance = (duration/2) / 29.1; // .................................. Serial.print(distance); Serial.print("cm"); Serial.println(); delay(100); }
You can connect any output motors or LEDs depends on your application.For that, we need to add some extra code just add conditions to the code but remember to define any output first.
if (distance < 4) { digitalWrite(led,HIGH); digitalWrite(led2,LOW); } else { digitalWrite(led,LOW); digitalWrite(led2,HIGH); } if (distance >= 200 || distance <= 0){ Serial.println("Out of range"); } else { Serial.print(distance); Serial.println(" cm"); } delay(100); }
Define the led pins as we define echo and trig pin as an in put or output.
We will see some of the great projects using the Ultrasonic sensor in coming posts.Here is the picture of the Obstacle avoiding Robot that I have made.The Robot has many functionalities we will see this in coming soon so stay connected and subscribe to our YouTube channel for video Tutorials.Stay motivated and happy.