Chapters

9 Sensor Circuits: Dividers and Filtering

sensors
sensor
circuits

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 the Measurement Story

Follow One Sensor Voltage Into the Code

Picture a temperature reading that jumps whenever a nearby motor starts. The sensor may be healthy while its wiring, supply, filter, or input circuit changes the value.

A microcontroller means the small computer that reads inputs and controls a device. Inter-Integrated Circuit, or I2C, is a short-distance digital link that lets chips share a clock and data line.

Apply one known input, switch the noisy load, disturb one connection, and repeat after restart. Record the circuit, supply, raw reading, bus result, timing, filter choice, and error.

This runway does not prove every sensor or circuit is accurate. The deeper sections explain dividers, pull resistors, filters, switches, protection, grounding, digital links, and safe interface choices.

A sensor signal usually reaches the microcontroller through a circuit before it becomes data. Start with the path from physical change to voltage, current, resistance, or charge, then ask what the circuit does to preserve or distort that signal.

9.4 In 60 Seconds

Sensor circuit fundamentals cover four essential building blocks: voltage dividers (convert resistance changes from LDRs and thermistors into measurable voltages), RC filters (remove high-frequency electrical noise using a resistor-capacitor pair), transistor switches (let low-power microcontroller pins control high-power loads like motors and relays), and LED current-limiting circuits (protect LEDs from burnout). These four patterns solve the vast majority of sensor-to-microcontroller interfacing challenges.

Key Concepts

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.

Chapter Roadmap
  • 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 Circuit Building Blocks

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.

The mathematical gist. In the chapter’s heavy-rain chain, a 47 kΩ pull-up and 10 kΩ sensor at 3.3 V give 0.0579 mA and 0.579 V, or ideal 12-bit code 718. Its 10 kΩ/1 µF filter has tau=10.0\\tau=10.0 ms and fc=15.9f_c=15.9 Hz; a 20 kHz, 50 mV disturbance is reduced by about −62.0 dB to 0.0398 mV, while five-time-constant settling is 50.0 ms.

Math Bridge · guided foundationsHow does wet resistance become a clean ADC code?Let Phoebe follow the rain sensor through its divider and RC filter.

Sensor Circuits Basics

Analogy: Think of sensor circuits like translating between languages.

Imagine you speak English, but your friend only speaks French. You need:

  1. A translator (to convert the language)
  2. Volume adjustment (if they whisper too quietly)
  3. 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.
Figure 9.1: Sensor Signal Conditioning Pipeline: Amplification, Filtering, and ADC Conversion

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.

The Three Main Challenges

When connecting a sensor to a microcontroller, you face three problems:

ChallengeReal-World ExampleSolution
Signal too weakThermistor outputs only 0.01V changeAmplification - Boost the signal
Signal too noisyElectrical interference from motorsFiltering - Remove the noise
Wrong formatSensor speaks analog, MCU needs digitalADC 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.
Figure 9.2: Signal Conditioning Challenges and Solutions: Amplification, Filtering, ADC

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.

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.
Figure 9.3: Voltage Divider: Two resistors in series create a proportional output voltage based on their ratio.

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

Guided Lab: Voltage Divider

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.

1. Start balanced

Set R1 = 10 kΩ and R2 = 10 kΩ. The output should be half of the supply voltage.

2. Make the sensor change

Lower R1 to model bright light on an LDR, then raise R1 to model darkness.

3. Connect the meaning

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?

Learning Points

Observe:

Start with Voltage Division: Output voltage = Input × (R2 / (R1 + R2)). Then Variable Resistance: Changing R1 or R2 changes V_out proportionally. Next Current Flow: Same current flows through both resistors (series circuit). Finally Sensor Application: LDR or thermistor replaces one resistor, ADC reads V_out.

Example Calculations:

Start by With a 3.3 V supply and two equal 10 kOhm resistors, the output sits halfway at 1.65 V. Then If the top resistor is an LDR and bright light drops it to 5 kOhm, the output rises to about 2.2 V. Next If darkness raises the LDR to 20 kOhm, the output falls to about 1.1 V. Finally The important idea is not memorizing the arithmetic: the ADC number moves because the sensor changes the resistor ratio.

Real-World Application: Every analog sensor (LDR, thermistor, flex sensor, potentiometer) uses this circuit to interface with microcontroller ADCs.

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.

A 3.3V LDR divider feeds GPIO 34 above a fixed 10k ohm resistor. Output examples fall from 3.24V in bright light to 0.30V in darkness.
Figure 9.4: LDR Voltage Divider: Light-to-Voltage Conversion Circuit

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:

Vout=Vin×RbottomRtop+RbottomV_{out} = V_{in} \times \frac{R_{bottom}}{R_{top} + R_{bottom}}

Bright light (LDR = 200Ω):

Vout=3.3V×10,000200+10,000=3.3V×0.9804=3.24VV_{out} = 3.3V \times \frac{10{,}000}{200 + 10{,}000} = 3.3V \times 0.9804 = 3.24V

Darkness (LDR = 10kΩ):

Vout=3.3V×10,00010,000+10,000=3.3V×0.5=1.65VV_{out} = 3.3V \times \frac{10{,}000}{10{,}000 + 10{,}000} = 3.3V \times 0.5 = 1.65V

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Ω):

Vout=3.3V×2001,000+200=3.3V×0.167=0.55VV_{out} = 3.3V \times \frac{200}{1{,}000 + 200} = 3.3V \times 0.167 = 0.55V

Darkness (LDR = 10kΩ):

Vout=3.3V×10,0001,000+10,000=3.3V×0.91=3.0VV_{out} = 3.3V \times \frac{10{,}000}{1{,}000 + 10{,}000} = 3.3V \times 0.91 = 3.0V

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: 200×10,000=1,414Ω\sqrt{200 \times 10{,}000} = 1{,}414\Omega → use 1kΩ standard value.

Physics PhoebeCheckpoint: 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

Guided Lab: RC Low-Pass Filter

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.”

1. Baseline

Start with R = 10 kΩ and C = 100 nF. Notice the cutoff is about 159 Hz.

2. More filtering

Increase C to 1 µF. Noise rejection improves, but the sensor response becomes slower.

3. Too much filtering

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?

Learning Points

Observe:

Start with Low-Pass Filtering: High frequencies (noise) blocked, low frequencies (signal) pass through. Then Time Constant τ: Larger R or C = slower response, more filtering. Next Exponential Response: Capacitor charges/discharges exponentially. Finally Rise Time: Output takes ~5τ to reach steady state.

Sensor Applications:

Start with Noise Filtering: Remove electrical noise from analog sensor readings. Then Debouncing: Smooth mechanical switch bounces. Next Anti-Aliasing: Filter high frequencies before ADC sampling. Finally Smoothing: Reduce jitter in sensor measurements.

Example: A 10 kOhm resistor with a 100 nF capacitor gives a time constant of 1 ms and a cutoff of about 159 Hz. That removes faster electrical noise while preserving slower sensor changes.

Missing I2C Pull-Ups

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.

Hardware vs Software Filtering

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.

Physics PhoebeCheckpoint: 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.