So I was inspired by the idea of ​​opening the garage door with sensors.
how the circuit works:
When a car or person is located at a distance of 15 cm or less from the sensor or pressing on a pushbutton, the motor automatically opens the garage door and the red led and the buzzer will turn on.
Arduino IDE
Used to compile the code written in the programming sketches and upload it directly to the Arduino Board.
List of Components Used:
Arduino Uno.
2-led.
Ultrasonic sensor.
Buzzer.
Cable.
Wires.
Buzzer.
pushbutton.
Resistor 10 k ohm
2-Resistor 220 ohm
The circuit diagram
At first, I included the library for the servo motor and defined two static variables for Chopin and trigger pin, and defined a variable of type long for the duration and a variable of type int for distance, and the last variable of type int for SW and named the servo "myservo".
in void setup, I connected the motor to pin 5, and made trigpin as output and echo pin as input and connect the LEDs to pin 6,7 and made him as output and the pushbutton as input on pin 4, Then I enabled the serial monitor.
In the void loop, I wrote some lines of code to make some calculations and give me the distance by the ultrasonic sensor, Then I put the if statement does the following if the distance is less than 15 cm or the SW is HIGH
do the following;
first, make the angle of servo motor zero degrees and turn on the red led and turn off the green led , and make the buzzer on pin 11 made tone by 300hz for 500ms.
else, do the following:
make the angle of servo motor 90 degrees and turn on the green led and turn off the red led.
Simulation on Tinker CAD
finally,I converted the code blocks made in Tinkercad to text code, then this code is copied and pasted in a new sketch in the Arduino IDE.
I started to make the connections on the real breadboard according to the design on Tinker CAD
the servo is connected to pin 5 and 5v vcc on the arduino board
I connected the trigger pin to pin 3 and echo pin to pin 4
and connected the red led to pin 6 and green led to pin 7
finally I connect the switchto the 10k ohm resistor to pin 4 .
I used an Avometer to know the value of the resistances.
I had a problem with the switch when I press it to make something like a noise in the signal So I googled and I knew that the problem solves by connecting a 10k ohm resistance with the switch.
The circuit diagram connect the pushbutton with Arduino.
connections of circuit