I wanted to use the ultrasonic sensor for this week's task, so i decided to go with a (keep your distance device). Where if anything is within 5 cm it will turn the RGB LED red and a buzzer goes off, otherwise the buzzer is off and the LED is white.
Used to simulait electrical circuit
Avometer
Breadboard
Arduino Uno
220 ohm resistor
Jumper wires
RGB LED
ultrasonic sensor
Tinker circuit
Code blocks
Circuit:
Ultrasonic sensor pins connected (trigger -> 13, echo -> 12)
Connecting the piezo to pin 5
connecting RGB LED pins with 220-ohm resistors (red -> 2, blue -> 3, green -> 4)
Code:
Created a variable "distance" to store the sensor reading
If the reading is between 10 to 0 cm, the led turns red and buzzer is on (setting red and buzzer to high, while blue and green to low)
otherwise, the red will turn white and buzzer sound turned off (setting the pin to low)
1) Connecting the sensor to breadboard and connecting the terminals to corresponding pins on the Arduino by jumpers
2) Connecting the RGB LED to the breadboard and its terminals to corresponding pins in the Arduino as well by resistors directly
3) Connecting the buzzer to its terminals as well
4) Complete assembly
Ultrasonic sensor take the distance as input
The RGB LED gives a colour output depending on specified distance
The buzzer makes a sound depending on the input from the Ultrasonic
When I finished the assembly and uploaded the code to my Arduino, the circuit was not behaving as expected and the buzzer was always working and the led was always white.
After checking my wirings again, it turns out that I switched the Trigger and echo pins places which led to that behaviour
The solution is to simply check the wirings again 🤷
Wiring issue