PROJECT Nº 4: Automatic garage door

Learn to build and program a garage door that opens and closes automatically using the components of the Maker Control Kit and Maker Kit 3.

You will open the door by pressing a button. Then, the distance sensor will check that the vehicle is not in the entrance and close the door. If the vehicle is still in the entrance, the door will stay open.

DIFFICULTY LEVEL: Beginner.

DURATION OF THE EXERCISE: 60 min.

MATERIALS:

  • 1 Button
  • 1 Ultrasonic distance sensor
  • 1 Servomotor
  • 1 Cardboard box
  • 1 4in1 Build&Code board
  • 1 USB – Micro USB cable
  • 1 Computer
  • Material for the door structure
  • Adhesive or tape

BUILDING THE STRUCTURE:

You will use a cardboard box to build the door and a sheet of poster board to build the wall that will hold the button, the ultrasonic distance sensor, and the 4in1 Build&Code board. Download the quick assembly guide and follow the steps provided to assemble the structure.

Once the structure is assembled, connect all the electronic elements to the 4in1 Build&Code board as shown in the section on connections found below.

CONNECTIONS:

  1. Connect button 1 to digital port 4 on the 4in1 Build&Code board.
  2. Connect the servomotor to digital port 9 on the 4in1 Build&Code board.
  3. Connect the ultrasonic distance sensor to digital ports 12 and 13 on the 4in1 Build&Code board.

Look at the colors of the cables and the colors of the terminals on the 4in1 Build&Code board to guide you. Each cable should be connected to its color.

PROGRAMMING CODE:

The program is comprised of the following:

  1. Reading the current status of the button. If the button is pushed, the door will open, it will stay open for 2 seconds, and the status variable will change to 1.
  2. With the door open, the reading of the ultrasonic distance sensor will be checked. If the reading is greater than 20 cm, it means that there is no object entering the garage. Then, 2 seconds will pass, the status variable will be changed to 0, and the door will shut.
  3. If the reading of the ultrasonic sensor is less than 20 cm, it means that there is a vehicle entering the garage, and the door will stay open. When the vehicle has entered and the reading of the ultrasonic distance sensor is greater than 20 cm, 2 seconds will pass, the status variable will change to 0, and the door will shut.

This sequence will continue to repeat as long as the whole system has a power supply.

You can do this activity by using Arduino and Bitbloq software, as well as other compatible block programming software. Below you will find the programming code needed for each software.

Arduino Code

  1. Download the Arduino software and go through the installation process.
  2. Open the Arduino program, and once in, copy the following program:
    #include <Servo.h>
    Servo motor1; // motor1 = Name of the servomotor 
    int TrigPin = 13;  // Connection ports of the ultrasonic sensor
    int EchoPin = 12;  
    float SSound = 0.0343;  // SPEED OF SOUND IN cm/µs
    long Lengh, Distance ;  // VARIABLES TO CALCULATE THE DISTANCE IN cm
    int VButton = 0, PButton = 4; // VARIABLE OF BUTTON READING; Digital port 4 connected button
    int State = 0; // Status variable 
    void setup() {
      // put your setup code here, to run once:
      motor1.attach (9); // Servomotor connected to digital port 9
      pinMode(TrigPin, OUTPUT); // Configuration ports of the ultrasonic sensor
      pinMode(EchoPin, INPUT);
      pinMode (PButton, INPUT); // Configuration port button as input
    }
    
    void loop() {
      // put your main code here, to run repeatedly:
      VButton = digitalRead (PButton); // Read the status of the button
      if ((VButton == 1) && (State == 0)) // if button = pushed and State = 0
      {
        motor1.write(15); // Servomotor = Open door
        delay (2000); // Wait 2 seconds
        State = 1; // State = 1;
      }
      DistanceCM(); // Activate function to measure the distance of the ultrasonic distance sensor
      if ((Distance > 20) && (State == 1)) // If the distance is greater than 20 cm and State = 1
      {
        delay (2000); // Wait 2 seconds
        motor1.write(105); // Servomotor = Close door
        State = 0; // State = 0
      }
    }
    
    void DistanceCM()// FUNCTION TO CALCULATE DISTANCE
    {
      // DISTANCE CALCULATION IN CM
      digitalWrite(TrigPin, LOW);        // We check that the trigger is deactivated
      delayMicroseconds(4);              // To make sure that the trigger is LOW
      digitalWrite(TrigPin, HIGH);       // We activate the output pulse
      delayMicroseconds(14);             // We wait 10 µs. The pulse is still active this time
      digitalWrite(TrigPin, LOW);        // We cut the pulse and wait for ECHO
      Lengh = pulseIn(EchoPin, HIGH) ; //pulseIn measures the time that elapses from when the declared pin (echoPin) changes from low status to high (from 0 to 1)
      Distance = SSound* Lengh / 2;  // CALCULATION OF THE DISTANCE
    }
    
  3. Configure and load the code, following the instructions given in the document First Steps for the 4in1 Build&Code board.

Code for compatible block programming software

  1. Download the software and go through the installation process.
  2. Open the program, and once in, copy the following code:
  3. Configure and load the code, following the instructions given in the document First Steps for the 4in1 Build&Code board.

BitBloq Code

  1. Get the BitBloq software.
  2. Open the BitBloq program, and once in, copy the following code:
    • Hardware
    • Software
  3. Configure and load the code, following the instructions given in the document First Steps for the 4in1 Build&Code board.

RESULT OF THE EXERCISE:

When you push the button, the garage door will open. After two seconds, the ultrasonic sensor will check that the car is no longer in front of the door. If the reading is greater than 20 cm, the door will close, and if it is less than 20 cm, the door will stay open until the vehicle as passed. You have created your automatic garage door!

1 0

0