What is a sensor?
What you’ll learn
- What a sensor does
- The difference between digital and analog signals
- How an Arduino reads each one
Sensors turn the world into numbers
A sensor measures something (light, distance, touch, tilt, temperature) and turns it into an electrical signal the controller can read. It’s the sense part of sense, think, act.
Digital signals: on or off
A digital signal has two states: HIGH (about 5 V on an UNO) or LOW (0 V). Buttons, bump switches and many obstacle modules are digital.
int state = digitalRead(2); // HIGH or LOWAnalog signals: anything in between
An analog signal can be any voltage in a range, like 0 to 5 V. Knobs (potentiometers), light sensors and some distance sensors are analog. The UNO’s analog pins A0–A5 measure the voltage and give a number from 0 to 1023:
| Voltage on A0 | analogRead(A0) |
|---|---|
| 0 V | 0 |
| 2.5 V | about 512 |
| 5 V | 1023 |
int raw = analogRead(A0); // 0–1023float volts = raw * 5.0 / 1023.0; // back to voltsCommon robot sensors
| Sensor | Measures | Signal |
|---|---|---|
| Push button, bump switch | Touch | Digital |
| Potentiometer | Knob position | Analog |
| LDR (light-dependent resistor) | Brightness | Analog |
| IR line sensor | Dark or light floor | Digital or analog |
| Ultrasonic (HC-SR04) | Distance | Pulse timing |
| IMU (MPU6050) | Tilt and rotation | I²C (a data bus) |
| Encoder | Wheel turns | Pulses |
Example: turning a reading into a decision
int light = analogRead(A0);if (light < 300) { digitalWrite(LED_BUILTIN, HIGH); // dark: turn the light on} else { digitalWrite(LED_BUILTIN, LOW);}Example: mapping to a useful range
map() rescales a number from one range to another:
int raw = analogRead(A0); // 0–1023int angle = map(raw, 0, 1023, 0, 180); // 0–180 degrees for a servoTry it in the virtual Arduino
Press Start simulator, replace diagram.json with the wiring below, paste the sketch into sketch.ino, press ▶, then turn the knob (click and drag it) and watch the Serial Monitor.
{ "version": 1, "author": "RoboLearn", "editor": "wokwi", "parts": [ { "type": "wokwi-arduino-uno", "id": "uno", "top": 0, "left": 0, "attrs": {} }, { "type": "wokwi-potentiometer", "id": "pot1", "top": -80, "left": 120, "attrs": {} } ], "connections": [ [ "pot1:GND", "uno:GND.1", "black", [] ], [ "pot1:VCC", "uno:5V", "red", [] ], [ "pot1:SIG", "uno:A0", "green", [] ] ], "dependencies": {}}void setup() { Serial.begin(9600);}
void loop() { int raw = analogRead(A0); // 0 to 1023 float volts = raw * 5.0 / 1023.0; // convert to volts Serial.print("raw: "); Serial.print(raw); Serial.print(" volts: "); Serial.println(volts); delay(200);}Runs on Wokwi, free, no account needed.
HalfwayEasy
Turn the knob until the reading is about 512. What voltage is that? About 2.5 V, half of 5 V.
PercentMedium
Print the knob position as a percentage (0–100%).
int pct = map(analogRead(A0), 0, 1023, 0, 100); then Serial.println(pct);
A light switch from a knobHard
Turn the built-in LED on only when the knob is past three quarters.
Show an answer
void setup() { pinMode(LED_BUILTIN, OUTPUT); }void loop() { digitalWrite(LED_BUILTIN, analogRead(A0) > 767 ? HIGH : LOW);}Quick check
Finished this lesson?