0% found this document useful (0 votes)
53 views

Ultrosonic Code

The document describes interfacing an ultrasonic sensor with an Arduino Uno board to measure distance. It includes the hardware and software used, theory of operation of the ultrasonic sensor, connection diagram, step-by-step procedure, and code to output distance measurements to the serial monitor. Connecting the ultrasonic sensor's trigger and echo pins to digital pins on the Arduino and powering the board allows it to output real-time distance readings between the sensor and detected objects.

Uploaded by

Amin Shaik
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
53 views

Ultrosonic Code

The document describes interfacing an ultrasonic sensor with an Arduino Uno board to measure distance. It includes the hardware and software used, theory of operation of the ultrasonic sensor, connection diagram, step-by-step procedure, and code to output distance measurements to the serial monitor. Connecting the ultrasonic sensor's trigger and echo pins to digital pins on the Arduino and powering the board allows it to output real-time distance readings between the sensor and detected objects.

Uploaded by

Amin Shaik
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Date: 21 / 3 /2023

Aim: To interface Arduino Uno with ultrasonic sensor.


Apparatus:
Hardware: Arduino Uno, ultrasonic Sensor, Jumper Wires.
Software: Arduino IDE
Theory:
The HC-SR04 ultrasonic sensor uses SONAR to determine the distance of an object just like the bats
do. It offers excellent non-contact range detection with high accuracy and stable readings in an easy-
to-use package from 2 cm to 400 cm or 1” to 13 feet.
The operation is not affected by sunlight or black material, although acoustically, soft materials like
cloth can be difficult to detect. It comes complete with ultrasonic transmitter and receiver module.

Technical Specifications

 Power Supply − +5V DC


 Quiescent Current − <2mA
 Working Current − 15mA
 Effectual Angle − <15°
 Ranging Distance − 2cm – 400 cm/1″ – 13ft
 Resolution − 0.3 cm
 Measuring Angle − 30 degree
Connection Diagram:

Procedure:
Step 1. Open the Arduino IDE software on your computer. Coding in the Arduino language will
control your circuit. Open a new sketch File by clicking New.

Step 2. The Ultrasonic sensor has four terminals - +5V, Trigger, Echo, and GND connected
as follows

1. Connect the +5V pin of ultrasonic sensor to +5v on your Arduino board.
2. Connect Trigger to digital pin 9 on your Arduino board.
3. Connect Echo to digital pin 10 on your Arduino board.
4. Connect GND with GND on Arduino.

Step 3. Power the Arduino board via computer USB connection.

Step 4. Write the code, and remove the errors if any.


Step 5. After successful compilation of the code, upload the code.
Step 6. Put some object in front of the ultrasonic sensor.

Step 7. Observe the output in the serial monitor


Code:
#include <NewPing.h>
const int trigPin = 9;
const int echoPin = 10;
long duration;
int distance;

void setup() {
Serial.begin(9600);
pinMode (trigPin, OUTPUT);
pinMode (echoPin, INPUT);
// put your setup code here, to run once:

void loop() {
digitalWrite (trigPin, LOW);
delay(2);
digitalWrite (trigPin, HIGH);
delay(10);
digitalWrite (trigPin, LOW);

duration = pulseIn(echoPin, HIGH);


distance= duration*0.034/2;
Serial.print ("Distance: ");
Serial.print(distance);
Serial.println(" cm");
delay(10);
// put your main code here, to run repeatedly:

}
Description of the Command:

Conclusion:

You will see the distance measured by sensor in cm on Arduino serial monitor.

You might also like