# Reading Sensors and Voltage Dividers — Arduino & Embedded C

Source: https://www.geekswithgeeks.com/en/arduino-embedded-c/sensors

> Potentiometers, temperature sensors and scaling voltages.

## From physical quantity to voltage

Many simple sensors produce a voltage. A **potentiometer** is itself a voltage divider. A **thermistor** or **photoresistor (LDR)** changes resistance, so you pair it with a fixed resistor to form a **voltage divider**: Vout = Vin × R2 / (R1 + R2). The same formula scales a higher voltage down to the ADC range, for example to monitor a battery; choose resistors so the output never exceeds the ADC limit and the divider does not drain the battery quickly. Analog temperature sensors such as the **TMP36** output a voltage proportional to temperature; its datasheet specifies 10 mV per °C with a 500 mV offset, so °C = (V − 0.5) × 100.

## TMP36 temperature reading

Uno R3 at 5 V: TMP36 Vs to 5 V, GND to GND, Vout to A1. Verify the pinout in the sensor datasheet before wiring.

```cpp
const float VREF = 5.0;

float readTempC() {
  long sum = 0;
  for (int i = 0; i < 8; i++) sum += analogRead(A1);  // average 8 samples
  float volts = (sum / 8.0) * VREF / 1023.0;
  return (volts - 0.5) * 100.0;   // TMP36: 10 mV/degC, 500 mV offset
}

void setup() { Serial.begin(9600); }

void loop() {
  Serial.print("Temperature: ");
  Serial.print(readTempC(), 1);
  Serial.println(" C");
  delay(1000);
}
```

## Sizing a divider for a 12 V battery

Worked example for a 5 V ADC (Uno R3).

```text
Goal: map up to ~15 V into 0-5 V
R1 (top) = 20 kOhm, R2 (bottom) = 10 kOhm
Vout = Vin x 10k / (20k + 10k) = Vin / 3
15 V -> 5.0 V (at the ADC limit, so 15 V is the hard maximum)
12.6 V -> 4.2 V
Current drawn: 12.6 V / 30 kOhm = 0.42 mA (continuous drain)
Code: Vin = analogRead(A2) * 5.0 / 1023.0 * 3.0
Add margin and protection if the source can spike above 15 V.
```

## Ground the sensor with the board

A sensor powered from a separate supply must share ground with the microcontroller; otherwise the ADC has no common reference and readings are meaningless.

**Quiz:** A divider has R1 = 10 kΩ (top) and R2 = 10 kΩ (bottom) on a 5 V input. What is Vout?

- [ ] 1.25 V
- [ ] 5 V
- [x] 2.5 V
- [ ] 10 V

*Answer:* 2.5 V. Vout = 5 × 10k / (10k + 10k).
