Automatic Arduino Drawbridge

by khangtran in Circuits > Arduino

170 Views, 2 Favorites, 0 Comments

Automatic Arduino Drawbridge

Capture.PNG

The project I decided on doing is a automatic drawbridge which rotates up and down depending on the distance of an object. A drawbridge is a type of moveable bridge consisting of moveable parts that allow it to lift above, swing above, or rotate to allows boats and ships to pass through channels or waterways. The reason I chose to create my final project on a drawbridge is that I get to use my previous learnings with the ultrasonic sensor, LEDs and more while also getting to use a component I am not very familiar with which is the servo motor. The project works by using an ultrasonic sensor to determine the distance of an object to the bridge, and then if that distance is smaller than 10 inches, the code will make the servo motor rotate 65 degrees allowing the boat to pass. A red LED will also signal when a boat cannot pass and a green LED will signal when it can.


Brainstorming:

While thinking about projects I could do, my two main choices were blackjack and and a simple calculator. The main components of both would be a 7 segment display. Since it would be impossible to hook up multiple 7 segment display monitors to one arduino due to the sheer number of pins required, I would use a 4 digit 7 segment display monitor I had at home.


Research & Trials:

Since I do not have keypad that can be hooked up with an arduino, numbers and symbols would have to be declared through a pushbutton. Each pushbutton would have to be equal to a variable and once the button was pressed would have to go into an equation which would be printed out on the 4 digit 7 segmend display monitor. I tried to code this but got stuck. Even with the help of resources such as W3schools, I could not find a solution to my problems and was forced to pivot. I then did some more research online on possible project until I stumbled on one which used a servo motor to randomly choose between the 3 options of rock, paper or scissors. Basically, the project automatically played against you in rock, paper, scissors. Though I did not like the idea of doing that game very much, I was intrigued by the possibility of using a servo motor which I had at home. I did some research on things that rotated in the real world and stumbled onto the drawbridge.

Supplies

IMG_3467.JPG
  1. Arduino UNO x1
  2. Solderless Breadboard x1 (I used 400 pin however 170 pin should be enough)
  3. 5mm LED x2 (1 red and 1 green LED)
  4. 330 Ohm Resistor x2
  5. Ultrasonic Sensor HC-SR04 x1
  6. Servo Motor x 1
  7. Dupont Wires

Circuit Diagram and Schematic

Screenshot 2025-06-09 223322.png
Screenshot 2025-06-09 223524.png
IMG_3468.JPG

On the wiring end of the project, there are three main components which are the ultrasonic sensor, the two LEDs and the servo motor. The ultrasonic sensor has four pins.

The two pins called VCC and GND are connected to the positive and negative terminals respectively while pins trig and echo are connected to pins 3 and 2 on the arduino respectively.

For the connection of the servo motor, there are 3 components which are ground, power and signal. The ground is ground and power is connected in the same way the VCC and GND are in the ultrasonic sensor which is just the positive and negative terminals, and the signal pin is connected to pin 12.

The final components of the circuit are the two LEDs. The red LED is connected to pin 4 and this turns on when the drawbridge is down, while the green LED is connected to pin 5 and is turned on when the drawbridge is up.

Attached is a video of the circuit working.

Downloads

Code

Capture.PNG

Attached is a photo of the entire code.


Setting up Variables:

int trig = 3;

int echo = 2;

int ledopen = 5;

int ledclose = 4;

Servo myservo;

int pos = 0;

bool isOpen = false;

Variables trig and echo are set up to 3 and 2 so later on when we want to use digitalWrite to turn on and off the functions for the ultrasonic sensor, the code is easier to understand. Same thing with variables ledopen and ledclose. ledopen is connected to pin 5 and ledclose is connected to pin 4 so it is easier to use the named variables when calling upon those pins.

Servo myservo is just declaring that there is a servo motor being used and that it is now named myservo which means I can use that similarly to a variable. The variable pos is setup so that later on in the code, I can set the degrees rotation of the servo motor to be 0.

The final part is bool isOpen = false and this is a boolean flag which declares if something is true or false. This will be used in the code to know if the bridge is open or it is closed.


Void Setup:

void setup() {

myservo.attach(12);

pinMode(trig, OUTPUT);

pinMode(echo, INPUT);

pinMode(ledopen, OUTPUT);

pinMode(ledclose, OUTPUT);

Serial.begin(9600);

myservo.write(0); // start closed

}

myservo.attach(12) makes it so that it is known that the servo motor is connected to pin 12.

All the pinMode code declares whether the variables (all pins that were given a name in the setting up variables portion) is an OUTPUT or an INPUT. When a pin is set as INPUT, it's ready to receive signals from external sources while a pin set as OUTPUT is ready to give signals to external devices.

Serial.begin(9600) just initiates the serial monitor. Although the serial monitor is not directly needed for this project, I would recommend using it to make sure if you have problems in your code, the issue would not be with the ultrasonic sensor.


Void Loop:

void loop() {

long duration, inches;


// Trigger the ultrasonic sensor

digitalWrite(trig, LOW);

delayMicroseconds(2);

digitalWrite(trig, HIGH);

delayMicroseconds(10);

digitalWrite(trig, LOW);


// Read echo time and convert to inches

duration = pulseIn(echo, HIGH);

inches = duration / 74 / 2;

// print out the distance between ultrasonic sensor and object

Serial.print("Distance: ");

Serial.print(inches);

Serial.println(" inches");

// Open gate if object is detected

if (inches < 10 && !isOpen) {

digitalWrite(ledclose, LOW);

digitalWrite(ledopen, HIGH);

for (pos = 0; pos <= 65; pos++) {

myservo.write(pos);

delay(15);

}

isOpen = true;

}


// Close gate if object is no longer detected

if (inches >= 10 && isOpen) {

digitalWrite(ledopen, LOW);

digitalWrite(ledclose, HIGH);

delay(1000);

for (pos = 65; pos >= 0; pos--) {

myservo.write(pos);

delay(15);

}

isOpen = false;

}


delay(100); // small delay between readings

}

Build the Bridge!

Capture.PNG

Only cardboard and glue/tape required to build the bridge. First, build two pillars from cardboard rectangles and connect a ramp to both of them. I then made a third pillar about 3mm shorter than the other two and taped the servo motor down to it. Then just glue a piece of cardboard to the servo motor. Make sure to place the ultrasonic sensor where it can sense the object!


You're Done!

Click on me for Video of Finished Product!