Interfacing Sensors
A sensor interface is the boundary between the physical world and firmware. Most sensor problems come from wrong voltage levels, missing grounds, noisy wiring, weak pullups, overloaded ADC inputs, or no protection against the real environment.
Learning Objectives
By the end of this lesson, you should be able to classify sensor outputs, match voltage range to MCU input limits, choose pullups and filters, protect inputs from cable faults, and debug noisy or stuck readings.
Sensor Output Types
| Output type | Examples | Main interface risk |
|---|---|---|
| Analog voltage | thermistor divider, pressure sensor | ADC range, source impedance, noise |
| Resistive | NTC, LDR, strain gauge | excitation current, self-heating |
| Digital logic | Hall switch, PIR module | logic level and pull resistor |
| Pulse or frequency | flow meter, encoder | interrupt rate and debounce |
| I2C | temperature, IMU, EEPROM | pullups, bus capacitance, addresses |
| SPI | ADC, display, IMU | level compatibility and chip select |
| Industrial | 4-20 mA, 0-10 V, NPN/PNP | isolation, protection, scaling |
Analog Sensor Inputs
An MCU ADC measures voltage relative to its ADC reference. Keep the sensor signal inside the valid range:
$$
0 \le V_\text{ADC} \le V_\text{REF}
$$
For a divider that scales an input signal:
$$
V_\text{ADC}=V_\text{IN}\frac{R_2}{R_1+R_2}
$$
Choose the ratio so the worst-case input and tolerance still stay below the absolute maximum pin voltage. ADC inputs also need low enough source impedance to charge the sample capacitor; otherwise readings may depend on the previous channel.
Filtering and Protection
A protected analog input often uses a series resistor to limit fault current, a capacitor to ground for a low-pass filter, and a clamp or TVS diode for long cables. The RC cutoff frequency is:
$$
f_c=\frac{1}{2\pi RC}
$$
Example: R = 4.7 kOhm and C = 100 nF gives about 339 Hz, useful for slow sensors but too low for fast waveforms.
Digital and Open-Drain Sensors
Open-drain and open-collector outputs can only pull a line low, so a pullup resistor creates the high level. Lower pullup resistance gives faster edges but wastes more current; higher resistance saves current but is slower and more noise-sensitive.
For I2C, rise time depends strongly on bus capacitance:
$$
t_r \approx 0.8473 R_P C_B
$$
Long cables, many devices, and weak pullups can make the bus fail even if the firmware is correct.
Grounding and Cabling
Single-ended sensors need a shared reference. For long cables, use twisted pair for signal and return, add shielding with a deliberate grounding plan, prefer differential or current-loop interfaces in noisy systems, and place ESD protection near the connector.
Debugging Checklist
- Measure sensor supply at the sensor while operating.
- Confirm signal voltage never exceeds MCU limits.
- Confirm the ground reference is continuous.
- Check pullups on I2C or open-drain outputs.
- Scope the signal if readings jump or communications fail.
- Test with a known voltage or simulator before blaming the sensor.
Common Mistakes
- Feeding a
5 Vor12 Vsensor output into a3.3 Vpin. - Forgetting a common ground in a single-ended interface.
- Using a high-value divider that the ADC cannot sample accurately.
- Adding too much filtering and destroying response time.
- Running I2C over long unprotected cables.
Summary
A reliable sensor interface matches voltage, reference, bandwidth, impedance, protocol, and environment. Design the electrical boundary first, then write firmware. Measure the real signal at the MCU pin before debugging code.
Further Reading
- Microchip, ADC Input Source Impedance application notes.
- Texas Instruments, I2C Bus Pullup Resistor Calculation.
- Analog Devices, Sensor Signal Conditioning tutorials.