Nothing Special   »   [go: up one dir, main page]

تحكم تجربة 5 ⬇️

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

Ultrasonic sensor

ITROUCTION:

Arduino - Ultrasonic Sensor: 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
1
Components Required

You will need the following components −

1 × Breadboard

• 1 × Arduino Uno R3
• 1 × ULTRASONIC Sensor (HC-SR04).
…………………………………………………………………………………………………………………………………

Project (1): Design MCU system to find distance of an object and print the
distance on LCD with 3 seconds update period?
#include <LiquidCrystal.h>
int trigger=8;
int echo=9;
LiquidCrystal lcd(12,11,5,4,3,2);
float time=0,distance=0;
void setup)( {
lcd.begin(16,2);
pinMode(trigger,OUTPUT);
pinMode(echo,INPUT);
lcd.print(" Ultra sonic");
lcd.setCursor(0,1);
lcd.print("Distance Meter);
delay(2000);
lcd.clear();
lcd.print(" Circuit Digest");
delay(2000); }
void loop() {
lcd.clear();
digitalWrite(trigger,LOW);
delayMicroseconds(2);
digitalWrite(trigger,HIGH);
delayMicroseconds(10);
digitalWrite(trigger,LOW);
delayMicroseconds(2);
time=pulseIn(echo,HIGH);
distance=time*0.034/2;
lcd.clear();
lcd.print("Distance");
2
lcd.print(distance);
lcd.print("cm");
lcd.setCursor(0,1);
lcd.print("Distance");
lcd.print(distance/1000);
lcd.print("m");
delay(1000); }

.…………………………………………………………………………………………………………………………………..

Project(2): Design MCU system to find distance of an object and print the
distance on LCD, when the distance is less than 20 cm, a green led is turn on
and when the distance become less than 12 cm, a red led should be turned on
and when it is less than 10 cm, a warning sound generated from buzzer is
turned on.
#define echoPin 9
#define trigPin 8 long duration;
int distance;
void setup() { pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
Serial.begin(9600);
Serial.println("Ultrasonic Sensor HC-SR04 Test");
Serial.println("with Arduino UNO R3"); }
void loop() { digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = duration *0.034/2;
Serial.print(distance);
Serial.println("cm");
if(distance<20){
digitalWrite(3,1); }
else if(distance>20){
digitalWrite(2,1); } }

3
Code to Note:

The Ultrasonic sensor has four terminals - +5V, Trigger, Echo, and GND
connected as follows −
• Connect the +5V pin to +5v on your Arduino board.
• Connect Trigger to digital pin 7 on your Arduino board.
• Connect Echo to digital pin 6 on your Arduino board.
• Connect GND with GND on Arduino.
In our programs, we have displayed the distance measured by the sensor in
inches and cm via the serial port.
Result

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

How Does it Work?


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

1. The ultrasound transmitter (trig pin) emits a high-frequency sound (40


kHz).
2. The sound travels through the air. If it finds an object, it bounces back
to the module.
3. The ultrasound receiver (echo pin) receives the reflected sound
(echo).

You might also like