Controlling a Servo With Ultrasonic Sensor Signal Using Arduino ( Automated Gate System )

55,203

97

29

Introduction: Controlling a Servo With Ultrasonic Sensor Signal Using Arduino ( Automated Gate System )

In this project we will simulate an automated parking gate
by using an ultrasonic sensor to open and close the gate if a specific distance is achieved, and we will use ( EPILOG LASER ) the laser CNC to build our structure

Step 1: Hardware

  • Ultrasonic sensor
  • Arduino uno Microcontroller
  • Servo motor
  • Jumper caple

Step 2: How Does It Work

The ultrasonic sensor will detect the distance infront of the gate if a car came closer than 20 cm to the gate the ultrasonic sensor will send a command to the arduino microcontroller to open the gate , then the ardino will send a command to the servo to open the gate 90 Degree to open the gate

Step 3: Connection: Ultrasonic Sensor

  • Vcc to 5V in the arduino
  • trig to port 2 in the arduino
  • echo to port 4 in the arduino
  • Gnd to Gnd in the arduino

Step 4: Connection : Servo Motor

  • Black wire to Gnd in the arduino
  • Red wire to vcc in the arduino
  • Yellow wire to port 9 in the arduino

Step 5: Structure

The structure for this project had been designed by a laser cutter and connected to each other by super glue, you can see the picture below.

Step 6: Conclusion

At the end this is how the project will look like , it looks kinda funny , but I built to merely visualize the idea.

whenever something gets withing the range of the ultrasonic sensor , it will measure its distance from sensor , and if it was less that a certain amount, the servo will turn , opening the gate.

it's just an idea for a simple application of using sensor and actuators to make something useful easily with Arduino

Step 7: The Code

As usual , just copy the code to Arduino IDE and upload it :

#include

Servo myservo; // create servo object to control a servo

const int trigPin = 2;

const int echoPin = 4;

void setup() {

// initialize serial communication:

Serial.begin(9600);

myservo.attach(9); // attaches the servo on pin 9 to the servo object

}

void loop() {

// and the distance result in centimeters:

long duration, cm;

pinMode(trigPin, OUTPUT);

digitalWrite(trigPin, LOW);

delayMicroseconds(2);

digitalWrite(trigPin, HIGH);

delayMicroseconds(20);

digitalWrite(trigPin, LOW);

pinMode(echoPin, INPUT);

duration = pulseIn(echoPin, HIGH);

// convert the time into a distance

cm = microsecondsToCentimeters(duration);

// the condition for the distance

if ( cm > 7 && cm < 14)

{

myservo.write(140); // sets the servo position according to the scaled value

delay(4000);

}

else if ( cm < 8)

{

myservo.write(40); // sets the servo position according to the scaled value

delay(100);

}

else

{

myservo.write(40); // sets the servo position according to the scaled value

delay(100);

}

Serial.print(cm);

Serial.print("cm");

Serial.println();

delay(100);

}

long microsecondsToCentimeters(long microseconds) {

// The speed of sound is 340 m/s or 29 microseconds per centimeter.

// The ping travels out and back, so to find the distance of the

// object we take half of the distance travelled.

return microseconds / 29 / 2;

}

1 Person Made This Project!

Recommendations

  • Make it Glow Contest

    Make it Glow Contest
  • First Time Author Contest

    First Time Author Contest
  • PCB Challenge

    PCB Challenge

29 Discussions

0
Vedanth_K
Vedanth_K

5 months ago

Very Nice, It helped me in understanding the coding. Thank you.

0
dperrakas
dperrakas

Question 6 months ago

can i ask how to fix the error of microsecondtocentimeters

0
dperrakas
dperrakas

6 months ago

can i ask how to fix the error of microsecondstocentmeter

0
minaashraf490
minaashraf490

Question 2 years ago on Step 3

i have an ultrasonic sensor .. but its pins are : Vcc - Gnd - NC - SIG

excuse me, what are the ( NC - SIG ) pins and where can i attach them in the arduino ?

0
BHADRAPPAH
BHADRAPPAH

Answer 8 months ago

NC = Trig
SIG = Echo
in HC-SR04 ultrasonic

0
Gabrielthegreat
Gabrielthegreat

1 year ago

not trying to be nasty but the instructions are very unclear with the VCC and the pictures as they don't match the captions. and the code wont work. can any one help me

0
BHADRAPPAH
BHADRAPPAH

Reply 8 months ago

VCC is nothing more its 5v written on all arduino board on power side

0
Thiruppugazhan
Thiruppugazhan

Tip 11 months ago

IF YOU ARE USING NODE MCU (Wi-Fi)-SMART DUSTBIN OR SMART GATE;PINS ARE INCLUDED IN COMMAND LINE.
#include <Servo.h>
Servo servo1;
// defines pins numbers
const int trigPin = 4; //D2
const int echoPin = 0; //D3
// defines variables
long duration;
int distance;
void setup() {
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
servo1.attach(2); //D4
servo1.write(0);
Serial.begin(9600); // Starts the serial communication
}
void loop() {
// Clears the trigPin
if(trigPin>=3)
Serial.println("Object found");
else
Serial.println("No Object found");
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);
// Calculating the distance
distance= duration*0.034/2;
if (distance<3){
Serial.print("Person is nearby open the lid");
servo2();}
else
{
servo3();
Serial.print("Lid Closed");}
// Prints the distance on the Serial Monitor
Serial.print("Distance: ");
Serial.println(distance);
delay(700);
}
void servo2() {
servo1.write(90);
delay(1000);
}
void servo3() {
servo1.write(0);
delay(1000);
}

0
Rachaelgreen
Rachaelgreen

1 year ago

Still getting the error message even after adding <Servo.h>, it's telling me "'Servo' was not declared in this scope"

0
kiran268
kiran268

1 year ago

i am using it with arduino atmega2560 ,,, no compiling error but it is not working properly ... anybody can help?

0
MohammadR137
MohammadR137

1 year ago

Please give the exact code

1
garciaj123z
garciaj123z

1 year ago

I keep getting error include file name please test before you post please

0
podar.budhimaan2006
podar.budhimaan2006

Reply 1 year ago

In this they are asking me duration not declared in this scope

0
MohammadR137
MohammadR137

1 year ago

my this project is not working. I used arduino nano.why isn't it working??

1
Mahmood979
Mahmood979

1 year ago

I cant find the VCC in my Arduino