CircuitPlanning:
ArduinotoUltrasonicSensor
Arduino-----> HC-SR04
Vin -----> Vcc
GND -----> GND
Digital 2 -----> trigPin
Digital 3 -----> echoPin
ArduinotoServoMotor
Arduino -----> Servo Motor
Vin -----> Red wire (+)
GND -----> Black or Dark wire (-)
Digital pin 9 -----> Yellow/Orange wire (data)
Code://the code is entirely created by: SHEEKAR BANERJEE (at December 2017)
//Dept. of CSE, IUBAT
//AI-ML-IOT Solution Engineer and Researcher
#include<Servo.h> //This calls the Header of the repository containing the library files of Servo Motor Driver Modules
#define trigPin 2 //This initializes the Trig pin of HC-05 Ultrasonic Sensor to the pin no. 2 of Arduino Microcontroller
#define echoPin 3 //This initializes the Echo pin of HC-05 Ultrasonic Sensor to the pin no. 3 of Arduino Microcontroller
Servo sm; //Servo is a Class which is providing an independent data type // sm is a variable which is representing the servo motor
int sound=250;
void setup() {
Serial.begin(9600); //Streaming 9600 bits of sensor data per second
pinMode(trigPin,OUTPUT); //Here, Trig pin triggers the electrical impulses into the sensor as an output
pinMode(echoPin,INPUT); //Here, Echo pin inputs the reflective signals into the sensor
sm.attach(9); //Servo motor data pin is attached to Arduino Digital Pin 9
}
void loop() {
long duration, distance;
digitalWrite(trigPin, LOW);
delayMicroseconds(2); // It will wait for 2 microseconds after every single electrical implulse of Trig pin if it is sending LOW(less than 1.6V) signal
digitalWrite(trigPin, HIGH);
delayMicroseconds(10); //It will wait for 10 microseconds after every single electrical implulse of Trig pin if it is sending HIGH(greater than 1.6V) signal
digitalWrite(trigPin, LOW);
duration=pulseIn(echoPin, HIGH); //Sensor inputs the signal through the Echo pin
distance=(duration/2)/29.1; //Calculation of distance
if(distance<10) //According to the reflection time calculation, if the distance is less than 10 cm
{
sm.write(80);
}
else {
sm.write(0); //The Servo Motor will rotate for 80 degrees
}
}
Results:0 projects • 6 followers
A motivated Computer Scientist & Engineer with years of experience over IoT, Robotic Systems, Machine Learning Algorithms and Software.
Comments
Please log in or sign up to comment.