9 Sensor Circuits: Dividers and Filtering
More
Depth: Practitioner
Progress saves as you read. Mark sections complete as you go; your progress appears in the sidebar.
9.1 Start With the Decision
This section: Start With the Decision — A motor can make a sound sensor value jump even when the sensor is healthy.
A motor can make a sound sensor value jump even when the sensor is healthy. A voltage divider sets a measurable voltage from two resistances. A filter reduces unwanted changes in a signal. The supply provides circuit power; a pull-up resistor holds a signal line high when no device drives it low. Each can shape the reading.
9.2 Route Overview
This section: Route Overview — This is part 1 of 2.
This is part 1 of 2. Continue with Sensor Circuits: Switching and Interface Safety.
9.3 Part Objectives
This section: Part Objectives
- Calculate divider output and ADC loading.
- Choose RC filtering and I2C pull-ups from measured limits.
- 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.
- 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.
- 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.
- 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.
- Transistor Switch: A bipolar or MOSFET transistor used as a digitally-controlled switch, allowing a low-current GPIO pin to control higher-current loads.
- 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.
- 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.
- 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
This section: 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
This section: Introduction — Sensor circuits form the critical bridge between the physical world and digital microcontrollers.
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 A small GPIO signal controls a separately powered load through a transistor or driver. Then Size base drive for a BJT from the data-sheet saturation test conditions, not active-region β alone. Next Add a suitable flyback path for an inductive load. Finally Check GPIO, driver, supply, and load ratings before connecting a motor or relay.
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 many common IoT sensors and actuators.
This section:
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.
Sensor voltage passes through amplifier, filter, ADC and microcontroller. The gain example expands 0.01 V to 0.10 V; the separate 12-bit code range is 0–4095, not a calculated reading.
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.
This section:
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.
Problem-solution diagram showing three sensor circuit challenges and their fixes: (1) weak thermistor signal (0.01V change) solved by op-amp amplification with 100x gain to produce 1V output, (2) noisy signal with motor interference causing random voltage spikes solved by RC low-pass filter for smooth clean signal, (3) wrong format with analog continuous signal solved by 12-bit ADC conversion to digital values 0-4095.
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.8 Voltage Divider Circuit
This section: Voltage Divider Circuit — Theory: A voltage divider is the most fundamental circuit for interfacing resistive sensors (LDR, thermistors, potentiometers) with microcontrollers.
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 the source, resistors, and output node in an order that prevents an inverted sensor response from being mistaken for a calculation error.
A voltage divider labels Vin, R1 at 10kΩ, Vout and R2 at 20kΩ. The formula yields 3.33V from 5V for sensor scaling and ADC input use cases.
Read Figure 9.3 by tracing the series path from the 5 V input through R1 and R2 to ground, then locate the output across the lower resistor. With R1 = 10 kΩ and R2 = 20 kΩ, the shown relation gives 5 V × 20/(10 + 20) = 3.33 V. The arithmetic confirms which resistor belongs in the numerator and connects a resistive sensor’s physical change to the ADC voltage developed at the midpoint.
Formula: V_out = V_in × (R2 / (R1 + R2))
9.8.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.8.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.
A 3.3 V LDR divider feeds GPIO 34 above a fixed 10 kΩ resistor. Output examples fall from 3.24 V in bright sunlight to 1.65 V in darkness.
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:
- Automatic Street Lights: Turn on when ambient light falls below threshold.
- Display Brightness: Adjust screen based on room lighting (phones, tablets).
- Solar Panel Tracking: Detect sun position for maximum efficiency.
- Camera Exposure: Measure ambient light for automatic settings.
- 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: 1 kΩ fixed resistor on top, LDR on bottom (larger span because the fixed resistor changed)
Bright light (LDR = 200Ω):
Darkness (LDR = 10kΩ):
Voltage range: 0.55V to 3.0V → 2.45V span (74% of ADC range) ✓ Better!
Key insight: The larger swing in Option 2 comes from changing the fixed resistor from 10 kΩ to 1 kΩ, not from placing the sensor on the bottom. With the same fixed value, swapping the sensor between top and bottom reverses the direction but preserves the swing magnitude. A fixed value near the geometric mean of the sensor range is a useful starting choice: ; 1.5 kΩ is a closer standard value than 1 kΩ. The worked Option 2 still uses 1 kΩ.
This section:
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.9 RC (Resistor-Capacitor) Filter Circuit
This section: RC (Resistor-Capacitor) Filter Circuit — Theory: RC circuits create time delays and filter signals.
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.9.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 depends on the filter and sample rate, and the extra MCU active time can increase power use. 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.
This section:
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.10 Continue to the Next Part
This section: Continue to the Next Part — Carry this evidence into Sensor Circuits: Switching and Interface Safety, which begins with Transistor Switch Circuit.
Carry this evidence into Sensor Circuits: Switching and Interface Safety, which begins with Transistor Switch Circuit.
Find help
3 reviewed answers.
Where should I start in a module?
Open the module page from the sidebar to see its chapters in order. Use the module map to return to a topic you need.
Modules and pathsReviewed
What can I try when a project does not work?
Check the symptom, then work through the diagnostic steps. Keep the error message and the smallest example that reproduces it.
When you are stuckReviewed
How do interactive examples work?
Use the interactive guide to check how examples and simulations work before changing your project.
Interactive examples guideReviewed
