Automating Garage Entrance with Raspberry Pi: A Guide to Building a Car Detection System - Python Language

Here is a sample Python program that uses a Raspberry Pi and a ultrasonic sensor to detect a car entering a garage and automatically close the gate:




The program uses an ultrasonic sensor to measure the distance to an object in front of it. The sensor is set up to trigger a pulse and measure the time it takes for the pulse to be returned. The distance is then calculated based on the time it took for the pulse to return.

The program uses a while loop to continuously check the distance measured by the sensor. If the distance is less than a set threshold (in this case 20cm), it is assumed that a car is present and the gate motor is triggered to close the gate for a set amount of time (in this case 5 seconds).

For this program to work you need: (If you do not have these, then you buy it from Amazon, i have provided the links)

  1. Raspberry Pi 4
  2. Ultrasonic sensor (HC-SR04)
  3. Motor driver board (L298N)
  4. DC geared Motor
  5. Jumper Wires
Power Supplies
  1. For the Raspberry Pi:
    1. CanaKit 5V 2.5A Raspberry Pi Power Supply OR
  2. For the Motor Driver Board and the DC geared motor: (Depending upon your specification choose one)
    1. AC 100-240V to DC 24V 4A 96W Switching Power Supply OR
    2. Meanwell Power supply

You will connect the Ultrasonic sensor to the Raspberry Pi, according to the pinout of the Raspberry Pi. Connect the Echo pin of the sensor to the 11th pin of the Raspberry Pi, and the Trig pin of the sensor to the 7th pin of the Raspberry Pi.

The Motor driver board will be connected to the Raspberry Pi, according to the pinout of the Raspberry Pi. Connect the Input1 pin of the driver board to the 16th pin of the Raspberry Pi, and the Enable1 pin of the driver board to the 18th pin of the Raspberry Pi.

Finally, Connect the DC geared Motor to the Motor driver board, according to the pinout of the Motor driver board. Connect the Motor A pin of the driver board to the Motor, and the Motor B pin of the driver board to the GND.

In this circuit diagram, the ultrasonic sensor is connected to the Raspberry Pi using the TRIG and ECHO pins. The gate motor is connected to the Raspberry Pi through a motor driver board, which is used to control the direction and speed of the motor. The motor driver board is connected to the Raspberry Pi using the IN1 and EN1 pins.

Please note that this is just a sample program and circuit diagram, you may need to adjust the code and circuit to suit your specific requirements and hardware.

You will also need to install the RPi.GPIO library for python for the raspberry pi to communicate with the sensor and the motor driver board.

In addition to the hardware and circuit setup, you will also need to install the RPi.GPIO library for Python on your Raspberry Pi. This library allows you to control the Raspberry Pi's GPIO pins, which are used to interact with the ultrasonic sensor and the motor driver board.

You can install the library by running the following command in the terminal on your Raspberry Pi:

Once the library is installed, you can run the program I provided earlier on your Raspberry Pi to detect a car entering the garage and automatically close the gate.

It is important to note that you may need to make adjustments to the program depending on the specific setup of your garage, such as the distance between the sensor and the gate, the size of the car, etc.

It is also important to make sure that the motor driver board and the DC geared motor are connected correctly and are working properly, otherwise it may cause damage to the car or the gate.

Also make sure that the sensor is positioned correctly, to detect the car correctly.

Please test everything thoroughly before putting it into use and also share your comments.

No comments:

Post a Comment

Please disable your ad blocker to support this website.

Our website relies on revenue from ads to keep providing free content.