Вы находитесь на странице: 1из 3

EEE342 - Microprocessor Systems and Interfacing

Lab Report # 13
Interfacing of Ultrasonic sensor and Servo motor with Arduino

Name Muhammad ZIA-UD-DIN Reg # FA17-BEE-060

Class & Section BEE-5D Lab Instructor Engr. M. Zubair Khaliq


Performed Date 27 12 2019 Submitted Date 27 12 2019

Task -1: Radar which detects item around 10 feet at 360’ using following components,
1. Arduino
2. Servo motor
3. Ultrasonic sensor
4. 16x2 LCD
CODE:
#include<Servo.h>
#include<LiquidCrystal.h>
LiquidCrystal lcd (12,11,5,4,3,2);

const int trigPin = 9;


const int echoPin = 10;
Servo s1;
Servo s2;
long duration;
int distance;
int angle;
int pulsewidth;
int stime;

void setup() {
s1.attach(3);

1|Page Department of Electrical and Computer Engineering | CUI Wah


EEE342 - Microprocessor Systems and Interfacing

lcd.begin(16,2);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
Serial.begin(9600);
}
void loop() {
s1.writeMicroseconds(1000); //1 Milisecond =0 Degree
delay(1000);
lcd.clear();
lcd.display();
lcd.setCursor(0,0);
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance= duration*0.034/2; //Distance in cm ,v=340 m/s, v=0.034 cm/us
distance= distance*0.01 ; //Distance in m
distance= distance*3.2808; //Distance in feet ,1 Meter = 3.2808 Feet
lcd.print("Distance: ");
lcd.print( distance);
delay(1000) ; //1 Second delay
stime=distance/0.034; // we get time in micro second
angle=(stime-1000)/5.56; // stime,delay=1000+(angle*5.56)
if (angle>0&&angle<=180)
{
s1.writeMicroseconds(stime);

2|Page Department of Electrical and Computer Engineering | CUI Wah


EEE342 - Microprocessor Systems and Interfacing

lcd.setCursor(0,1);
lcd.print("Angle: ");
lcd.print(angle);
delay(1000);
}
else
{
s2.writeMicroseconds(stime);
lcd.setCursor(0,1);
lcd.print("Angle: ");
lcd.print(angle);
delay(1000);
}

3|Page Department of Electrical and Computer Engineering | CUI Wah

Вам также может понравиться