This Arduino project controls a servo-operated gate using an HC-SR04 ultrasonic sensor. When an object (e.g., a person) is detected within 50cm, the gate opens (servo rotates), a blue LED lights up, and a gentle buzzer beeps. After 5 seconds of no detection, the gate closes, and a red LED turns on.
- Arduino (Uno/Nano)
- HC-SR04 Ultrasonic Sensor
- SG90 Servo Motor
- Red & Blue LEDs (with resistors, ~220Ξ©)
- Buzzer (passive)
- Jumper Wires
| Component | Arduino Pin |
|---|---|
| HC-SR04 Trigger | D2 |
| HC-SR04 Echo | D3 |
| Red LED (Anode) | D4 |
| Red LED (Cathode) | D8 |
| Blue LED (Cathode) | D7 |
| Blue LED (Anode) | D5 |
| Servo Signal | D6 |
| Buzzer (+) | D12 |
Note: Ensure proper grounding (
GND) for all components.
This code uses the Servo library, which comes pre-installed with Arduino IDE. If missing:
- Open Arduino IDE β Sketch β Include Library β Manage Libraries β Search for "Servo" β Install.
- Copy the provided code into a new Arduino sketch.
- Select the correct board (Tools β Board β Arduino Uno/Nano).
- Select the correct port (Tools β Port).
- Click Upload (
βΆοΈ ).
- Detection: Ultrasonic sensor measures distance (object within 50cm triggers the gate).
- Gate Control:
- Open: Servo moves to 90Β°, blue LED on, buzzer beeps gently.
- Close: After 5 seconds of no detection, servo returns to 0Β°, red LED on.
- Buzzer: Short beeps (50ms ON, 500ms cycle) while gate is open.
- Servo jitters? Check power supply (use external power if needed).
- Sensor not working? Verify wiring (
TriggerβD2,EchoβD3). - LEDs not lighting? Confirm correct polarity and resistor values.
MIT License β Free to use, modify, and distribute.
Happy Coding! π»β‘
Open an issue or contribute on GitHub.
"Born to Code" β Because every great project starts with a single line. π