In: Electrical Engineering
Using an arduino, breadboard, ultrasonic sensor, buzzer, lcd and red led display distance in centimeters,
If needed, use a rotary potentiometer, male to male cable (m-m) female to male cable (f-m) or any resistor provided in the kit.
display distance in centimeters to lcd. If the object is in distance, turn on Active buzzer(play sound)
and turn on red led. If the object is not in range, display out of range or "Object now close'. on the lcd, make sure the buzzer is off and red led is off.
Provide code and schematic
Provide code and schematic
So first i will like to discuss that how does ultrasonic sensor works and then i will try to answer the above question.
Ultrasonic Sensor : Ultrasonic sensor emits short and high frequency pulses at regular interval which travel with the speed of sound.When they strike an object they reflected back as the echo signals and the sensor himself compute the distance of object from the sensor.The range of distance upto which ultrasonic sensor can work is 20 mm to 8 m.
Formula
Velocity = (Speed)/(2*Time) where take velocity =343 m/sec(speed of sound).
CONNECTION
The circuit:
LCD RS pin to digital pin 12
LCD Enable pin to digital pin 11
LCD D4 pin to digital pin 5
LCD D5 pin to digital pin 4
LCD D6 pin to digital pin 3
LCD D7 pin to digital pin 2
LCD R/W pin to ground
220 ῼ resistor: lcd pin 15 and ground
Potentiometer wiper to LCD VO pin (pin 3) and ends to +5v and
ground of arduino kit
LCD pin 16 to ground
Trig pin of the sensor to 9
Echo pin of the sensor to 10
Gnd of the sensor to ground pin of arduino.
VCC of the sensor to 5V pin
Code
#include <LiquidCrystal.h>
#define trigPin 9
#define echoPin 8
float duration;
float Inches;
float Cm;
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
void setup() {
pinMode(trigPin,OUTPUT); //sets trigpin as output to send ultrasound
pinMode(echoPin,INPUT); //sets echopin as input to receive ultrasound
Serial.begin(9600); //sets 9600 as baud rate
lcd.begin(16, 2); //enable 16 columns and 2 rows in the lcd
}
void loop()
{
lcd.setCursor(0,1); //sets cursor at 1st row and 2nd coulumn
digitalWrite(trigPin, LOW); //sets trigpin to LOW so that it do not emits ultrasound
// initially
delayMicroseconds(2);
digitalWrite(trigPin, HIGH); //sets trigpin to HIGH state to emit ultrasound
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH); //reads the travel time of the pulse
Inches = microsecondsToInches(duration); //function
Cm = microsecondsToCentimeters(duration); //function
lcd.setCursor(0,0); //sets cursor to initial position
lcd.print("INCHES");
lcd.setCursor(7,0);
lcd.print(Inches);
lcd.setCursor(0,1);
lcd.print("CM");
lcd.setCursor(7,1);
lcd.print(Cm);
delay(100);
}
long microsecondsToInches(long microseconds)
{
return microseconds / 74 / 2; /* sounds travels at the speed of 1130 feet per second. There are 74.16 microseconds per inch. The time that will be received is of full journey of ultrasound i.e. forward and backward. So to calculate the distance we have to divide total time by number of microseconds per inch i.e. 74. We have to further divide it by 2 in order to calculate the distance of only forward path and not the total distance*/
}
long microsecondsToCentimeters(long microseconds)
{
return microseconds / 29/ 2; /*Speed of sound is 340m/s. So it takes 29 microseconds to travel 1 centimeter length of the path.All the working is same as explained in calculation of inches*/
}
Check the image for Schematic Diagram