Reading sensors with Arduino: digital vs analogue inputs and the serial monitor
TUREFF• •0
Once an LED blinks, the next step is making the board notice the world. That means inputs: buttons, light sensors, temperature sensors and the like. The two ideas to get right are the difference between digital and analogue inputs, and how to see what the board is reading using the serial monitor.
Digital inputs: on or off
A digital input reads only two states, HIGH or LOW. A push button, a tilt switch, a reed switch near a magnet and many motion-sensor modules all give a digital signal.
The catch is the floating pin. A pin connected to nothing does not read LOW; it picks up stray electrical noise and flickers between states. Fix it with a pull-up or pull-down resistor, which holds the pin at a known level until the button changes it.
The Arduino has built-in pull-ups you can switch on in code:
pinMode(2, INPUT_PULLUP);
Wire the button between pin 2 and GND. Now the pin reads HIGH when the button is released and LOW when pressed. That reversed logic surprises everyone once; it is normal.
Mechanical buttons also bounce: the contacts chatter for a few milliseconds, so one press can register as several. A short delay or a debounce routine (there is an official example) solves it.
Analogue inputs: a range of values
Analogue inputs measure a voltage rather than just on or off. On the Uno these are the pins labelled A0 to A5. By default analogRead returns a number from 0 to 1023, where 0 is 0 V and 1023 is the reference voltage (normally the board's supply).
Sensors that give an analogue voltage include:
- Potentiometers (knobs), the easiest way to test analogue input
- Light-dependent resistors, used with a fixed resistor as a voltage divider
- Simple analogue temperature sensors, which output a voltage proportional to temperature
- Soil moisture probes and many other basic modules
Never feed an analogue pin more than the board's operating voltage. A sensor powered from a higher supply can do exactly that.
Sensors that talk in data
Some sensors send readings as data rather than a voltage, using buses called I2C, SPI or a one-wire protocol. Many digital temperature, humidity, distance and motion sensors work this way. You usually install a library from the IDE's library manager and call a function such as readTemperature. Check that a module runs at your board's logic voltage, since some are 3.3 V only.
The serial monitor: your window into the board
The serial monitor shows text the board sends back over USB, which makes it the main debugging tool.
In setup():
Serial.begin(9600);
In loop():
Serial.println(analogRead(A0));
delay(200);
Open the serial monitor in the IDE, set the same speed (9600 baud here), and you will see a stream of numbers. Turn the potentiometer and watch them change. The IDE's serial plotter draws the same numbers as a live graph, which is great for seeing noise or slow trends.
If you see gibberish, the baud rate in the monitor does not match the one in Serial.begin.
Turning readings into decisions
Once you can see the numbers, act on them:
- switch an LED on when the light level falls below a threshold
- average several readings to smooth out noise
- use map() to convert 0 to 1023 into a useful range, such as a percentage
Calibrate in the place you will use the sensor. A light sensor's numbers at your desk will not match a window sill.
Troubleshooting checklist
- Readings jump randomly: look for a floating pin or a loose breadboard wire.
- Always 0 or 1023: check the sensor's power and GND, and that you read the right pin.
- Library examples will not compile: confirm the library supports your board.
Which sensor are you trying to read? Post your wiring and readings in the comments and others can help you work out what is going on.