Devices & Components
Arduino Uno Rev3
SG90 Micro-servo motor
Ultrasonic Sensor - HC-SR04 (Generic)
Project description
Code
CODE
arduino
1 2#include <Servo.h> 3 4const int trigPin = 7; // Trigger Pin of Ultrasonic Sensor 5const int echoPin = 6; // Echo Pin of Ultrasonic Sensor 6Servo Myservo; 7 8 9void setup() 10{ 11Serial.begin(9600); // Starting Serial Terminal 12 13Myservo.attach(5); // Signal Pin of Servo 14 15pinMode(trigPin, OUTPUT); 16pinMode(echoPin, INPUT); 17Serial.print(" Distance "); 18Serial.print(" Measurement "); 19delay(1000); 20delay(1000); 21Serial.print("Made By :"); 22 23Serial.print(" OverAll Technical Gyan"); 24delay(2000); 25 26} 27 28void loop() 29{ 30 long duration, cm; 31 pinMode(trigPin, OUTPUT); 32 digitalWrite(trigPin, LOW); 33 delayMicroseconds(2); 34 digitalWrite(trigPin, HIGH); 35 delayMicroseconds(10); 36 digitalWrite(trigPin, LOW); 37 38 pinMode(echoPin, INPUT); 39 duration = pulseIn(echoPin, HIGH); 40 cm = microsecondsToCentimeters(duration); 41 42 43Serial.print("Distance:"); 44 Serial.print(cm); 45 Serial.print("cm"); 46 delay(100); 47 48 if(cm >= 20){ 49Myservo.write(0); 50} 51else { 52Myservo.write(90); 53 54 55} 56 57 Serial.println(); 58delay(100); 59 60} 61 62 63long microsecondsToCentimeters(long microseconds) 64{ 65 return microseconds / 29 / 2; 66}
CODE
arduino
1 2#include <Servo.h> 3 4const int trigPin = 7; // Trigger Pin of Ultrasonic Sensor 5const int echoPin = 6; // Echo Pin of Ultrasonic Sensor 6Servo Myservo; 7 8 9void setup() 10{ 11Serial.begin(9600); // Starting Serial Terminal 12 13Myservo.attach(5); // Signal Pin of Servo 14 15pinMode(trigPin, OUTPUT); 16pinMode(echoPin, INPUT); 17Serial.print(" Distance "); 18Serial.print(" Measurement "); 19delay(1000); 20delay(1000); 21Serial.print("Made By :"); 22 23Serial.print(" OverAll Technical Gyan"); 24delay(2000); 25 26} 27 28void loop() 29{ 30 long duration, cm; 31 pinMode(trigPin, OUTPUT); 32 digitalWrite(trigPin, LOW); 33 delayMicroseconds(2); 34 digitalWrite(trigPin, HIGH); 35 delayMicroseconds(10); 36 digitalWrite(trigPin, LOW); 37 38 pinMode(echoPin, INPUT); 39 duration = pulseIn(echoPin, HIGH); 40 cm = microsecondsToCentimeters(duration); 41 42 43Serial.print("Distance:"); 44 Serial.print(cm); 45 Serial.print("cm"); 46 delay(100); 47 48 if(cm >= 20){ 49Myservo.write(0); 50} 51else { 52Myservo.write(90); 53 54 55} 56 57 Serial.println(); 58delay(100); 59 60} 61 62 63long microsecondsToCentimeters(long microseconds) 64{ 65 return microseconds / 29 / 2; 66}
Downloadable files
Circuit Proteus
Circuit Proteus
Circuit Proteus
Circuit Proteus
The download file contains HC SR04 Ultrasonic Sensor Library, Arduino code, Proteus simulation file.
The download file contains HC SR04 Ultrasonic Sensor Library, Arduino code, Proteus simulation file.
Comments
Only logged in users can leave comments