I really like scanners, I wanted to make something that mimics a radar scanner, and stops moving then buzzes if it detects something.
I searched on Arduino project hub forums to find an inspiration, and I found this, it's very similar to what I want to make, but I'll add a few modifications of my own to give it some flair.
Used to design and simulate the complete circuit.
Used to export code to the Arduino hardware
Arduino UNO: Main component to power the circuit and handle code.
Ultrasonic Distance Sensor: To detect any objects in front of it.
Servo Motor: Moves the Ultrasonic sensor back and forth periodically.
LED (Red): Action output to indicate detection visually.
Resistor 220 Ω: for LED current limiting
Buzzer: Action output to indicate detection audibly.
Breadboard: To connect the Arduino and components together.
Jumper wires: To connect components together.
USB cable / power supply for Arduino: To power the Arduino unit.
Cardboard: To build the structure body.
Cutter: To cut the cardboard into the needed shape.
Sellotape: To hold the body & components together.
(Follow along if you want to make the same circuit)
Let's tackle on the hardware first:
Add the parts:
drag an Arduino UNO, HC-SR04 ultrasonic sensor, SG90 servo, piezo buzzer, LED (5 mm), 220 Ω resistor, and a breadboard.
(PS. Position components so wiring is neat: Arduino on one side, breadboard in the middle, sensor and servo nearby.)
Wire the ultrasonic sensor:
VCC → Arduino 5V
GND → Arduino GND
Trig → Arduino digital pin 7
Echo → Arduino digital pin 6
Wire the servo:
Red (VCC) → Arduino 5V
Brown (GND) → Arduino GND
Orange (Signal) → Arduino digital pin 9
Wire the buzzer:
(+) → Arduino digital pin 10
(−) → Arduino GND
Wire the LED (with resistor on cathode):
(PS. Resistor can be placed on either side; I used it on the cathode for a cleaner layout.)
LED anode (long leg) → Arduino digital pin 11
LED cathode (short leg) → 220 Ω resistor → Arduino GND
Hardware part is done, but what about the software?
We'll use CODE BLOCKS to compute the logic needed to make the project, Here's how I made the code:
I start each main loop by reading the ultrasonic sensor on Trig 7 / Echo 6 and checking if the distance is ≤ 15 cm; if true I set pin 10 (buzzer) and pin 11 (LED) HIGH, otherwise I set them LOW.
I made the servo sweep a small 45° arc using a count up loop from 1 to 45, rotating the servo on pin 9 to the current i degrees.
Inside the sweep loop I check the ultrasonic reading again (read ultrasonic >= 15) before moving to the next step; the servo only advances if the path is clear.
After each servo move I wait 0.7 s.
After the count up loop I run a symmetric count down loop from 45 to 1 with the same read-check, rotate, and wait behavior.
Because the top-level if (pins 10 & 11) runs independently of the sweep, the LED/buzzer will turn on immediately when an object enters range even if the servo is mid-sweep.
Working demo, find the Tinker cad project here
I placed the Arduino UNO next to a breadboard and powered the breadboard rails from the Arduino: 5V → + rail, GND → − rail.
I positioned the ultrasonic distance sensor and connected its VCC to the + rail and GND to the − rail. I wired the sensor Trig to digital pin 7 and Echo to digital pin 6 on the Arduino.
I mounted the servo motor and connected its power (red) to the breadboard + rail, ground (brown) to the - rail, and the signal (orange) to digital pin 9 on the Arduino.
I placed the LED on the breadboard, put a 220 Ω resistor in series with the LED’s cathode, which went to the - rail and connected the anode to digital pin 11.
I connected the buzzer with its positive lead to digital pin 10 and its negative lead to the − rail.
I double-checked that all grounds were common (every device GND tied to the Arduino GND).
I copied the code from Tinkercad (text view) into the Arduino IDE, selected the Arduino UNO board and the correct COM port, and uploaded the sketch.
For testing I powered the Arduino via USB. I observed the servo sweep a 45° arc back and forth.
When I placed an object within about 15 cm of the sensor, the servo stopped at that angle, the LED lit up and the buzzer sounded, once the object was removed, the scanner resumed sweeping.
And after assembling the project, then housing it in cardboard, this was the final result:
At one point, a peer suggested that periodically sweeping the servo at a fixed angle might make the radar look more realistic, I agreed and Implemented it in my project
I had to pause for a bit to review how to properly set up the servo motor in the code, since at first it wasn’t responding the way I expected. At one point, the circuit wasn’t working at all, and I wasn’t sure if it was a coding problem or a wiring problem. I used an Avometer to check the voltage across the power rails on the breadboard and discovered that one of my ground connections was loose. Fixing that immediately solved the issue.
A pitfall others could avoid is rushing the wiring step, double-checking each connection before uploading the code saves a lot of time and prevents confusion later. Using tools like an Avometer early on can also help quickly narrow down whether the issue is with the wiring or the code, instead of guessing and wasting time.
I am now able to design and create circuits that include an Arduino and compute logic using Code Blocks, this will be a major factor at completing my final project