Why Is My Sensor Giving Random Readings?
Short answer: your sensor input is floating, noisy, or underpowered. This is almost always wiring or power, not the sensor itself.
Common causes
- Floating input: No pull-up or pull-down resistor.
- Loose wiring: Breadboard jumpers move or lose contact.
- Power noise: Motors or servos share the same power rail.
- Wrong voltage: 3.3V sensor on 5V logic or the reverse.
Fast fixes
- Add a pull-up or pull-down resistor or enable the internal pull-up.
- Shorten wires and re-seat all connections.
- Power sensors from a stable supply and share ground.
- Read the sensor at a steady rate and average a few samples.
Quick test
If your readings change when you touch the wire, the input is floating.
Pull-up and pull-down basics
Digital inputs need a defined state. Without a resistor, they pick up noise from the air and your hand. Use a 10k pull-up or pull-down, or enable the internal pull-up in code.
Power and grounding
Noise is the most common cause of random readings. If you run a motor, servo, or relay, use a separate power supply and connect grounds together.
Random readings come from floating inputs, bad wiring, or noisy power. Fix the wiring, add pull-ups, and separate noisy loads.
Related: What is the difference between digital and analog pins? · Why does my Arduino reset when the motor turns on? · Why can't I upload code?