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

Picture of 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

Picture of 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

Picture of 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

Picture of 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

Picture of 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;

}

Comments

harithshahmis (author)2016-07-23

thanks

About This Instructable

5,559views

78favorites

Add instructable to: