9 Sensor Circuits: Dividers and Filtering
9.1 Start With the Decision
A motor can make a sound sensor value jump even when the sensor is healthy. The divider, filter, supply, and pull-ups shape the reading.
9.2 Route Overview
This is part 1 of 2. Continue with Sensor Circuits: Switching and Interface Safety.
9.3 Part Objectives
- Calculate divider output and ADC loading.
- Choose RC filtering and I2C pull-ups from measured limits.
Start with Voltage Divider: A two-resistor circuit where the output voltage is proportional to the ratio of resistor values; the foundation for reading resistive sensors like thermistors and LDRs. Then Pull-Up Resistor: A resistor connecting a signal line to the supply voltage, defining the default HIGH state for open-drain digital sensors and I2C lines. Next Pull-Down Resistor: A resistor connecting a signal line to ground, defining the default LOW state and preventing floating inputs on buttons and digital sensor outputs. After that RC Low-Pass Filter: Resistor and capacitor combination with cutoff fc = 1/(2piRC); attenuates electrical noise above the cutoff frequency while passing the sensor signal. Continue by Transistor Switch: A bipolar or MOSFET transistor used as a digitally-controlled switch, allowing a low-current GPIO pin to control higher-current loads. Continue by LED Current Limiting Resistor: A series resistor calculated as R = (Vsupply - Vf) / If that prevents LED overcurrent; always required — LEDs have no self-limiting resistance. Continue by Decoupling Capacitor: A 100 nF ceramic capacitor placed close to a sensor’s VCC pin to filter power supply noise and prevent it from coupling into analog readings. Finally Ground Loop: A condition where multiple ground connections create different potentials, introducing voltage offsets and noise into sensor measurements; prevented by star-topology single-point grounding.
9.5 Learning Objectives
By the end of this chapter, you will be able to:
- Design Voltage Dividers: Calculate resistor values for interfacing resistive sensors with ADCs
- Build RC Filters: Create low-pass filters to remove noise from sensor signals
- Implement Transistor Switches: Use transistors to control high-power loads from GPIO pins
- Calculate LED Resistors: Properly size current-limiting resistors for LED indicators
- Debug Basic Circuits: Use multimeters to diagnose common sensor circuit issues
Sensor circuits are the wiring and small components that connect a sensor to a microcontroller. Think of it like plumbing — you need the right pipes (wires), valves (resistors), and filters to get clean water (signal) from the source (sensor) to the tap (microcontroller). Without these circuit building blocks, the sensor’s signal would be too weak, too noisy, or at the wrong voltage for the microcontroller to read properly.
9.6 Introduction
Sensor circuits form the critical bridge between the physical world and digital microcontrollers. A sensor might detect temperature, light, or motion, but its raw electrical output — a changing resistance, a tiny voltage, or a noisy analog signal — is rarely in a form the microcontroller can use directly. This chapter teaches you the four fundamental circuit patterns that solve this problem: voltage dividers, RC filters, transistor switches, and LED current-limiting circuits. Together, these building blocks handle the vast majority of sensor-to-microcontroller interfacing needs in IoT systems like the ESP32.
- Start With the Measurement Story
- In 60 Seconds
- Key Concepts
- For Beginners: Sensor Circuits
- Introduction
- Sensor Circuit Building Blocks
- Phoebe’s Field Notes: Why Sensor Circuits Turn Physics into Voltage
- Voltage Divider Circuit
- Guided Lab: Voltage Divider
- Learning Points
- Putting Numbers to It
- Checkpoint: Divider Range Decisions
- RC (Resistor-Capacitor) Filter Circuit
- Guided Lab: RC Low-Pass Filter
- Learning Points
- Missing I2C Pull-Ups
- Hardware vs Software Filtering
- Checkpoint: Filtering and Pull-Up Evidence
Sensor circuits use four fundamental patterns to connect sensors to microcontrollers:
Step 1: Voltage Divider (Resistive Sensors)
Start by Two resistors in series divide input voltage proportionally. Then Formula: Vout = Vin × (R2 / (R1 + R2)). Next LDR or thermistor acts as variable resistor R1. Finally Result: Resistance changes become voltage changes ADC can measure.
Step 2: RC Filter (Noise Removal)
Start by Resistor + capacitor create time constant: τ = R × C. Then High frequencies pass through capacitor to ground (attenuated). Next Low frequencies (sensor signal) pass through to output smoothly. Finally Result: Clean signal with 60Hz motor noise removed.
Step 3: Transistor Switch (Load Control)
Start by Small base current (2mA from GPIO) controls large collector current (500mA to motor). Then Current gain β amplifies: I_collector = β × I_base. Next Protects microcontroller from high-power loads. Finally Result: 3.3V GPIO safely controls 12V motors/relays.
Step 4: LED Current Limiting (Visual Indicators)
Start by Calculate resistor: R = (Vsupply - VLED) / Iled. Then Example: (5V - 2V) / 0.02A = 150Ω. Next Prevents LED burnout from excessive current. Finally Result: Safe, bright status indicators.
Master these four patterns and you can interface 90% of IoT sensors and actuators.
Analogy: Think of sensor circuits like translating between languages.
Imagine you speak English, but your friend only speaks French. You need:
- A translator (to convert the language)
- Volume adjustment (if they whisper too quietly)
- Noise filtering (to ignore background chatter)
Sensor circuits work the same way—they convert sensor “language” (analog voltages) into “microcontroller language” (digital numbers)!
Use the pipeline diagram in Figure 9.1 to turn that analogy into an engineering path. Start at the sensor output and follow every representation change before deciding where amplification or filtering belongs.
Read Figure 9.1 left to right. The sensor first produces an analogue voltage; amplification expands a useful but weak change; filtering suppresses unwanted variation; the ADC assigns a code; and the microcontroller interprets that code. Each block can only work with what the previous block preserves, so circuit design begins with the sensor’s smallest useful change and the ADC range it must occupy.
When connecting a sensor to a microcontroller, you face three problems:
| Challenge | Real-World Example | Solution |
|---|---|---|
| Signal too weak | Thermistor outputs only 0.01V change | Amplification - Boost the signal |
| Signal too noisy | Electrical interference from motors | Filtering - Remove the noise |
| Wrong format | Sensor speaks analog, MCU needs digital | ADC Conversion - Translate it |
The pipeline names the stages; Figure 9.2 diagnoses why each one is needed. Inspect the weak, noisy, and wrong-format cases in that order, matching each symptom to its corresponding circuit response.
In Figure 9.2, amplification addresses scale, filtering addresses spectral contamination, and conversion addresses representation. They are not interchangeable fixes: extra gain can also amplify interference, filtering cannot recover a clipped signal, and more ADC codes cannot repair a poorly conditioned input. This diagnostic view connects the worked numbers to the component sections that follow.
9.7 Voltage Divider Circuit
Theory: A voltage divider is the most fundamental circuit for interfacing resistive sensors (LDR, thermistors, potentiometers) with microcontrollers. It converts resistance changes into voltage changes that ADCs can measure.
Before applying the divider equation, inspect Figure 9.3 to identify which quantities the schematic assigns to the source, resistors, and output node. That reading order prevents an inverted sensor response from being mistaken for a calculation error.
Read Figure 9.3, trace the series path from input through the two resistors to ground, then locate the output across the lower resistor. The equation therefore expresses a resistance ratio, connecting a resistive sensor’s physical change to the ADC voltage developed at the midpoint.
Formula: V_out = V_in × (R2 / (R1 + R2))
9.7.1 Voltage Divider Calculator
Try it yourself! Use the voltage-divider calculator above to see how resistor ratios become measurable sensor voltages. Predict the output first, then move the sliders.
Set R1 = 10 kΩ and R2 = 10 kΩ. The output should be half of the supply voltage.
Lower R1 to model bright light on an LDR, then raise R1 to model darkness.
Watch the ADC count move with voltage. This is the number your microcontroller actually reads.
Design question: which resistor placement gives the largest voltage swing over the sensor's real resistance range?
9.7.2 LDR Voltage Divider Example
To see how light becomes a voltage an ADC can read, inspect Figure 9.4 before considering applications. The circuit makes the changing LDR resistance and the fixed resistor part of one measurable ratio.
Read Figure 9.4, begin at the 3.3 V supply, follow current through the light-dependent resistor and fixed resistor, and locate the output tap between them. As illumination changes the LDR resistance, that divider ratio moves the output voltage, which is the signal the later applications actually use.
Real-World Applications:
Start with Automatic Street Lights: Turn on when ambient light falls below threshold. Then Display Brightness: Adjust screen based on room lighting (phones, tablets). Next Solar Panel Tracking: Detect sun position for maximum efficiency. After that Camera Exposure: Measure ambient light for automatic settings. Finally Smart Blinds: Open/close based on sunlight intensity.
LDR Voltage Divider Design: An LDR varies from 200Ω (bright sunlight) to 10kΩ (darkness). Design a voltage divider with 3.3V supply to maximize ADC range (0-3.3V).
Option 1: LDR on top, 10kΩ fixed resistor on bottom
Voltage at midpoint:
Bright light (LDR = 200Ω):
Darkness (LDR = 10kΩ):
Voltage range: 1.65V to 3.24V → 1.59V span (48% of ADC range)
Option 2: Fixed 1kΩ on top, LDR on bottom (better for maximizing range)
Bright light (LDR = 200Ω):
Darkness (LDR = 10kΩ):
Voltage range: 0.55V to 3.0V → 2.45V span (74% of ADC range) ✓ Better!
Key insight: Place the variable resistor (sensor) on the bottom of the voltage divider for maximum voltage swing. Fixed resistor value should be near the geometric mean of sensor range: → use 1kΩ standard value.
Checkpoint: Divider Range Decisions
You now know:
- A voltage divider turns resistance into ADC voltage: two equal 10 kOhm resistors on 3.3 V sit at 1.65 V.
- Sensor placement matters: the LDR-on-top example moves from 3.24 V in bright light toward 1.65 V in darkness.
- Resistor choice affects usable range: the 1 kOhm top resistor option gives a 2.45 V span, about 74% of the ADC range.
Once the divider produces a measurable voltage, the next problem is deciding whether that voltage is clean enough to sample.
9.8 RC (Resistor-Capacitor) Filter Circuit
Theory: RC circuits create time delays and filter signals. Essential for debouncing switches, filtering sensor noise, and creating timing circuits.
Time Constant: τ = R × C (tau, in seconds)
Cutoff Frequency: f_c = 1 / (2πRC)
9.8.1 RC Filter Calculator
Try it yourself! Use the RC filter calculator above to trade off noise rejection against response time. The goal is not “maximum filtering”; the goal is “enough filtering while still seeing the real sensor event.”
Start with R = 10 kΩ and C = 100 nF. Notice the cutoff is about 159 Hz.
Increase C to 1 µF. Noise rejection improves, but the sensor response becomes slower.
Push R or C very high and ask whether a fast event would be delayed or missed.
Design question: for a sensor that changes every few seconds, what cutoff removes motor noise without hiding the real change?
The Mistake: Connecting I2C sensors directly to microcontroller GPIO pins without external pull-up resistors, relying solely on internal weak pull-ups (40-100kohm typical).
Why It Happens: Many tutorials skip pull-up resistors because simple single-sensor setups “work” with internal pull-ups. Developers assume if it works on the bench, it will work in production.
The Fix: Always install external pull-up resistors on SDA and SCL lines:
- Standard I2C (100kHz): Use 4.7kohm pull-ups to 3.3V
- Fast I2C (400kHz): Use 2.2kohm pull-ups to 3.3V
- Multiple sensors or long wires (>30cm): Use 1kohm pull-ups
Calculation: Pull-up value = Rise Time / (0.8473 x Bus Capacitance). For 400kHz with 100pF total capacitance: R = 300ns / (0.8473 x 100pF) = 3.5kohm (use 2.2kohm standard value).
Symptoms of weak pull-ups: Intermittent communication failures, sensors work individually but not together, communication fails in humid/hot environments, NACK errors at higher clock speeds.
Option A (Hardware RC Filter): Cutoff frequency fixed at design time (f_c = 1/(2piRC)), no CPU overhead, power consumption adds 0.01—0.1 mW, cost $0.05—0.50 for resistor/capacitor. Provides true anti-aliasing before ADC, removes high-frequency noise physically. Cannot be adjusted after deployment, requires PCB space, component tolerance affects cutoff (+-10—20%).
Option B (Software Digital Filter): Cutoff frequency adjustable in code, CPU overhead 0.1—2% per filter, no additional power for passive filtering but adds MCU active time. No BOM cost increase. Allows adaptive filtering based on conditions, can implement complex filters (Kalman, median). Cannot remove aliased frequencies (must sample fast enough), adds latency of N/2 samples for N-tap filter.
Decision Factors: Choose hardware filtering when noise frequencies exceed half your sampling rate (prevents aliasing that software cannot fix), when CPU resources are constrained, or when EMI/RFI is severe (motor noise, switching power supplies). Choose software filtering when filter characteristics need runtime adjustment, when implementing complex filters like Kalman or adaptive algorithms, when PCB space is limited, or when different filtering is needed for different operating modes.
Checkpoint: Filtering and Pull-Up Evidence
You now know:
- A 10 kOhm resistor with 100 nF gives a 1 ms time constant and about a 159 Hz cutoff.
- I2C reliability depends on rise time: use 4.7kohm at 100kHz, 2.2kohm at 400kHz, and stronger pull-ups for long wires over 30cm.
- The 400kHz, 100pF example computes 300ns / (0.8473 x 100pF) = 3.5kohm, so a 2.2kohm standard value is safer.
Filtering protects the input side of the measurement. The next circuit question is output power: how a tiny GPIO pin controls something larger without being damaged.
9.9 Continue to the Next Part
Carry this evidence into Sensor Circuits: Switching and Interface Safety, which begins with Transistor Switch Circuit.
