Sub topic 8
Displaying Sensor Readings Clearly
Assume you know the basic Arduino sketch structure, variables, if statements, analogRead(), and uploading a sketch from the Arduino IDE. This chapter uses an Uno-style board with a 5 V analog reference.
A sensor reading is easier to understand when the sketch prints:
- A label that identifies the value
- The raw reading from the analog-to-digital converter
- The calculated value and its unit
- A newline after each complete reading
Raw readings and calculated values
analogRead() returns a raw integer. On an Uno, the value normally ranges from 0 to 1023. The number represents the sensor voltage compared with the analog reference voltage.
For a 5 V reference, convert the raw value to volts with this formula:
voltage = raw × 5.0 ÷ 1023.0
The 5.0 and 1023.0 values make the calculation use floating-point arithmetic. Floating-point arithmetic can represent values with a fractional part.
const int SENSOR_PIN = A0;
const float REFERENCE_VOLTAGE = 5.0;
void setup() {
Serial.begin(9600);
}
void loop() {
int raw = analogRead(SENSOR_PIN);
float volts = raw * (REFERENCE_VOLTAGE / 1023.0);
Serial.print(F("raw="));
Serial.print(raw);
Serial.print(F(" volts="));
Serial.println(volts, 2);
delay(500);
}The second argument in Serial.println(volts, 2) requests two digits after the decimal point. A typical line might look like this:
raw=512 volts=2.50
Serial.print() continues the current line. Serial.println() prints a value and then moves to the next line. Combining them lets you create labeled records that are readable in the Serial Monitor.
Choose labels and units carefully
A label should explain what the number means. Include the unit when the value is not obvious.
| Output | Meaning |
|---|---|
| `raw=512` | The converter's integer reading |
| `volts=2.50` | The estimated input voltage |
| `temperature=23.4 C` | A converted temperature value |
If you convert a sensor reading into another measurement, document the sensor's conversion formula. For example, a voltage output sensor may use a relationship such as temperature = (volts - 0.5) × 100.
Check the reference voltage
The voltage formula is only correct when REFERENCE_VOLTAGE matches the board's actual analog reference. A 3.3 V board needs a different value. A sketch using analogReference() may also use an internal or external reference.
Keep readings separate and useful
Print one complete measurement per line. Avoid mixing status messages into data that another program will parse. For example, this format is easy to read and process:
raw=700 volts=3.42
A common error is printing only a changing number, such as 700 3.42, without labels. Labels make it clear which value is which when you add more sensors or change the sketch.
The sensor wiring also matters. A floating analog input, which is an input not connected to a defined voltage, can produce values that jump unpredictably. Connect the sensor ground to the Arduino ground and verify the signal wire is connected to the intended analog pin.
Exercise
An Uno reports raw=256. What voltage should the sketch display when the reference is 5 V, rounded to two decimal places?
Show answer
Use 256 × 5.0 ÷ 1023.0, which is about 1.25 V.