
Build it for real
To build this project for real, get the parts from hobidevre.com → hobidevre.com →
More build guides: hobidevre.com/blog
Start with a shared ground
Every device in the circuit must reference the same 0 V. If the sensor has its own supply, connect that supply ground to the board ground. Without this, the signal voltage has no meaningful reference and readings drift or stick.
Check the ground wire with continuity mode with power off. A wire that looks seated can still sit in the wrong breadboard row.
Confirm the supply voltage
Many sensors require 3.3 V and are damaged by 5 V, while others need 5 V to give a stable reading. Read the sensor datasheet and measure the rail where the sensor actually sits.
A sensor powered from a noisy rail can reset midway through a measurement. A small capacitor across its supply and ground pins often stabilises it.
Wrong pin or wrong protocol
Digital sensors, I2C devices and analogue modules use different pins and different libraries. Confirm which one you have before writing code. An I2C device on an analogue pin will never answer.
A reading stuck at the maximum value often means the input is not connected to the sensor at all, so the pin sees whatever the internal circuitry gives it.
Add a pull-up or pull-down
A button or open-drain sensor output left floating returns random values. Use a resistor to a known level, or enable the internal pull-up with INPUT_PULLUP and write the logic as active-low.
In the robot workshop, the same rule applies to switch-style sensors: the input must have a defined state when nothing is pressing it.
Give the sensor start-up time
Some sensors need hundreds of milliseconds after power-up before they answer. Read the first valid value after a short delay rather than immediately in setup().
A request sent during start-up can leave the sensor in a confused state. A clean power cycle followed by a delay is often enough to recover.
Always use a timeout
Pulse-based distance sensors measure the width of a returned pulse. If no pulse arrives, a blocking read can wait forever and freeze the whole sketch. Bound the wait and treat a timeout as a failed reading.
The robot workshop uses this pattern for ultrasonic sensors: if the echo never comes, the code returns a failure value and the loop continues instead of hanging.
Test with a minimal sketch
Wire only the sensor and print one raw value per second. If it changes, the sensor is fine and the fault is in your project code. If it stays fixed, try a second sensor of the same type.
Remember that a sensor can have more than one read path in a library. Check that the function you call is the one that actually talks to your hardware.
Get the parts
If you want to build this project in real life, you can get the parts from hobidevre.com.
Frequently asked questions
Why does my sensor return zero all the time?
Usually no shared ground, the wrong pin, or a request sent too soon after power-up. Check all three.
Why do values freeze while the sketch keeps running?
A blocking read without a timeout is waiting for a pulse that never arrives. Add a bounded wait.
Why does it work alone but not in my project?
Another device is sharing a pin or supply, or grounds are not joined across the whole circuit.