Chapters

9 Sensor Circuits: Dividers and Filtering

sensors
sensor
circuits

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

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

Check your understanding: Use the questions below to check the key idea Check your understanding: Use the questions below to check the key idea

9.7 Phoebe’s Field Notes: Why Sensor Circuits Turn Physics into Voltage

The mathematical gist. 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. An isolated 10 kΩ/1 µF RC stage has tau=10.0\\tau=10.0 ms and fc=15.9f_c=15.9 Hz; a 20 kHz, 50 mV sinusoid is reduced by about −62.0 dB to 0.0398 mV, with five-time-constant settling of 50.0 ms. Connected directly to that divider, its 8.25 kΩ source resistance makes the effective time constant about 18.25 ms; check the loaded circuit separately.

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

This section:

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.

Shows: Figure 9.1: Sensor Signal Conditioning Pipeline: Amplification, Filtering, and ADC Conversion
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.

This section:

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.

Shows: Figure 9.2: Signal Conditioning Challenges and Solutions: Amplification, Filtering, ADC
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.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.

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

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:

  • Voltage Division: Output voltage = Input × (R2 / (R1 + R2)).
  • Variable Resistance: Changing R1 or R2 changes V_out proportionally.
  • Current Flow: Same current flows through both resistors (series circuit).
  • 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: These resistive sensors (LDR, thermistor, flex sensor, potentiometer) commonly use a voltage divider to interface with microcontroller ADCs.

Check your understanding: Use the questions below to check the key idea

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.

Shows: Figure 9.4: LDR Voltage Divider: Light-to-Voltage Conversion Circuit
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.
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:

  • 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:

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: 1 kΩ fixed resistor on top, LDR on bottom (larger span because the fixed resistor changed)

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: 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: 200×10,000≈1,414Ω\sqrt{200 \times 10{,}000} \approx 1{,}414\Omega; 1.5 kΩ is a closer standard value than 1 kΩ. The worked Option 2 still uses 1 kΩ.

Practise

Hands-on lab · 20 min · Falstad CircuitJS

Photoresistor voltage divider

I have explained dividers; now let's change a light-sensor resistance and compare the output voltage.

Open the lab

This section:

Physics Phoebe, Physics and measurement-foundations expertCheckpoint: 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

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:

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

Sensor Applications:

  • Noise Filtering: Remove electrical noise from analog sensor readings.
  • Debouncing: Smooth mechanical switch bounces.
  • Anti-Aliasing: Filter high frequencies before ADC sampling.
  • 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.

Check your understanding: Use the questions below to check the key idea Check your understanding: Use the questions below to check the key idea
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 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:

Physics Phoebe, Physics and measurement-foundations expertCheckpoint: 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 paths

Reviewed

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 stuck

Reviewed

How do interactive examples work?

Use the interactive guide to check how examples and simulations work before changing your project.

Interactive examples guide

Reviewed