Control a servo with distance
An interactive 3D build with a live HTML servo controller and Arduino code.
Build a distance-controlled continuous rotation servo with the Little Bird Uno and a full-size breadboard. Move a flat object towards the sensor to slow and stop the servo.
Before you start: this servo controls speed and direction, not an exact angle. You also need a USB data cable, a computer with Chrome or Edge, and a separate regulated 5V supply suitable for the servo, with insulated jumper-compatible outputs. The supply is an additional requirement. Keep the servo unloaded during initial testing. Do not power it from a GPIO pin, connect the external positive rail to Uno 5V, or connect the supply while rewiring.
Step 1 — Watch the complete build
Press Play for the narrated build. Drag the view to explore in 3D, or follow the individual steps below.
Loading assembly…
Place the Uno beside the full size breadboard. Keep USB and the separate servo power supply disconnected while wiring. This view is interactive: drag to turn it, and scroll to zoom. The animation will guide you through each connection. Connect five volts on the Uno to the upper positive rail at contact five. Connect Uno ground to the upper negative rail at contact five. The sensor will use these rails. The lower positive rail stays separate for the servo supply. Place the ultrasonic sensor with its two silver transducers facing the object you want to measure. Its four pins are V C C, trigger, echo, and ground. Use female to male jumper wires to connect this header. Connect the sensor V C C pin to the upper positive rail at contact twenty four. Connect ground to the upper negative rail at contact twenty five. This sensor uses five volts from the Uno, separate from the servo power rail. Connect digital pin seven on the Uno to trigger on the sensor. Then connect digital pin six to echo. The sketch sends a short pulse on trigger, then measures the echo pulse to calculate the distance in centimetres. This is a continuous rotation servo. It controls speed and direction, rather than moving to an exact angle. Check its cable colours: brown or black is ground, red is power, and orange or yellow is the control signal. Connect the servo ground lead to the lower negative rail at contact forty two. Connect the red power lead to the lower positive rail at contact forty one. Male to male jumpers fit into the female sockets on the servo cable. Join the upper and lower negative rails at contact thirty eight. This gives the Uno and servo a common ground. A separate regulated five volt servo supply connects to the lower positive and negative rails at contact forty nine. Leave that supply off for now. Connect digital pin nine on the Uno to the servo signal socket. The sketch uses pulses around fifteen hundred microseconds: the neutral pulse stops rotation, and pulses on either side change speed and direction. Your servo may need a small neutral adjustment. Check your wiring, then connect the square USB plug to the Uno and the other end to your computer. Keep the servo supply off while uploading the program below. Open the App, check that it is ready, then power the unloaded servo from its separate supply. Select distance sensor mode in the App and hold Run. Beyond sixty centimetres, the servo runs at full requested speed. Bring an object closer to slow it down; at fifteen centimetres it stops. Releasing Run or losing a valid echo also stops it.
Step 2 — Set up the workbench
Place the Uno beside the full-size 830-contact breadboard. Leave USB and any servo supply disconnected while wiring.
Step 3 — Prepare the rails
Uno 5V → upper + rail; GND → upper − rail. Use male-to-male jumpers. The lower + rail remains separate for servo power.
Step 4 — Add the distance sensor
Place the HC-SR04 with both transducers facing the object to measure. Use female-to-male jumpers on its four header pins.
Step 5 — Power the sensor
Connect VCC to the upper + rail and GND to the upper − rail with female-to-male jumpers. The HC-SR04 uses the Uno’s 5V supply.
Step 6 — Connect trigger and echo
Use female-to-male jumpers: D7 → TRIG and D6 → ECHO. Direct signal connections keep the circuit easy to follow and match the downloadable sketch.
Step 7 — Add the continuous servo
Use male-to-male jumpers in the servo’s female cable connector. Check its lead colours: brown/black is ground, red is supply, orange/yellow is signal.
Step 8 — Connect the servo cable
Ground → lower − rail; supply → lower + rail. Use male-to-male jumpers in the servo’s female connector.
Step 9 — Prepare separate servo power
Join both negative rails at contact 38. A separate regulated 5V servo supply will connect at lower +49 and −49. Keep its positive rail separate from Uno 5V. Leave the supply switched off until the program has been uploaded.
Loading assembly…
Join the upper and lower negative rails at contact thirty eight. This gives the Uno and servo a common ground. A separate regulated five volt servo supply connects to the lower positive and negative rails at contact forty nine. Leave that supply off for now.
Step 10 — Connect the control signal
D9 → servo signal using a male-to-male jumper. The pulse sets speed and direction. Approximately 1500 µs stops the servo; calibrate the neutral setting for your unit.
Loading assembly…
Connect digital pin nine on the Uno to the servo signal socket. The sketch uses pulses around fifteen hundred microseconds: the neutral pulse stops rotation, and pulses on either side change speed and direction. Your servo may need a small neutral adjustment.
Step 11 — Connect USB and upload
USB-B → Uno; USB-A → computer. Leave the servo supply off while uploading, then connect it after checking the unloaded servo.
Step 12 — Upload the Arduino program
Leave the separate servo supply off. Pair the Uno in Chrome or Edge, choose Arduino Uno, then Verify and Upload. The App HTML is already loaded under the App tab. After upload, wait for Ready before switching on the servo supply.
The sketch starts stopped and uses 115200 baud. Without Run heartbeats it stops within one second. A suitable regulated 5V servo supply and a shared ground are required.
Step 13 — Control the servo with distance
The connection is shared with the code editor above. Set a modest speed and hold Run; release to stop. Negative values reverse the servo. Choose Distance sensor to slow down from 60 cm to 15 cm and stop at 15 cm or on a missing echo. If the horn creeps at zero, release Run, open Neutral calibration and adjust the neutral pulse in small increments before testing again. Power off before touching the horn or wiring.
Download the App HTML to reuse in English → App → Edit HTML. The on-screen horn follows commands; it is not position feedback.
Step 14 — Try distance control
Select Distance sensor in the App and hold Run. The servo slows from 60 to 15 cm and stops on release or loss of a valid echo.
Step 15 — Plot the readings
View distance and commanded speed on the same shared serial connection. Click legend entries to hide channels you do not need. Distance is −1 when no valid echo is received. Hold Run in the App while experimenting; changing guide steps releases the control and stops the servo.
Parts List
Required Parts (6)
Continuous rotation; speed and direction rather than an angle.
x1
$8.20
In stockOptional Extras (1)
Additional USB data cable; use an existing one if available.
Optional$2.00
Project Summary
7 parts total
Unavailable items will be skipped