Capstone · #16 of 52

Sensor Selection + I²C Wiring

From Physical Question to Address Scan

A SparkFun weather sensor shield with environmental sensors on a small circuit board.
A sensor board is a measurement chain in miniature: sensing element, interface chip, passives, connector, address pins and layout choices. · SparkFun, CC BY 2.0

The capstone board wakes from deep sleep, samples the room, and decides whether the enclosure is healthy. The firmware wants a number. The world does not provide numbers. It provides humidity on a polymer film, temperature through a package, pressure on a membrane, light on a junction, and noise from every wire nearby.

Choosing a sensor is choosing how the physical world becomes bits.

The old beginner move is to search for "I2C temperature sensor" and buy the first board that appears. The engineering move is slower: define the measurement, choose the sensor whose limits match it, wire the bus so edges are real, then prove the device is present with an address scan before trusting any reading.

By the end, you can

  1. Start sensor selection from the physical quantity, range, accuracy and update rate
  2. Read power, sleep current, conversion time, address and interface constraints from a datasheet
  3. Wire an I2C sensor to 3.3 V logic with SDA, SCL, shared ground and pull-ups
  4. Explain why cable length, connector count and bus capacitance slow I2C rising edges
  5. Use an address scan to separate wiring faults from firmware parsing faults
  6. Plan a low-power sample cycle for a sleeping environmental node

The measurement comes first

Before part numbers, write the sentence the product needs answered:

Is the sealed electronics enclosure staying inside its safe temperature and humidity range between radio uploads?

That sentence already rules out bad choices. A fast accelerometer is irrelevant. A temperature-only probe may miss humidity condensation. A gorgeous high-accuracy sensor that never sleeps may ruin the battery budget. The physical question defines the measurement chain.

A sensor selection matrix listing range, accuracy, sample rate, power and interface constraints.
Good sensor selection is a constraints table. Range and accuracy are only two rows; power, package, address and wiring can decide the design. · TooFoo original

For a simple environmental capstone, common candidates include:

A waterproof DS18B20 temperature probe.
A packaged probe solves a different problem than a board-mounted sensor. It measures temperature at the probe tip, not necessarily at the PCB. · SparkFun Electronics, CC BY 2.0

Board placement is part of the sensor

The sensor does not read "the room." It reads its own die or sensing element. If the sensor sits next to a warm regulator, it reads the regulator's weather. If it sits in dead air inside an enclosure, it lags the outside environment. If the PCB copper pours connect it to a heater, the layout becomes part of the error term.

A small temperature sensor board.
Small sensor boards are convenient, but they still inherit board placement, airflow and self-heating errors. · Andy Oakley, CC0

For the capstone, the useful habit is to log three things with every reading:

  1. The raw sensor value.
  2. The board state: asleep, just woke, radio active or charging.
  3. The time since wake and time since last radio transmit.

Those extra columns tell you when the sensor is measuring the environment and when it is measuring your own electronics.

I2C wiring is a shared bus, not point-to-point magic

I2C uses two signal lines: SDA for data and SCL for clock. Both are open-drain. Devices pull a line low, but nobody drives it high directly. Pull-up resistors to the logic rail restore the high level.

That means the wiring has four essential connections:

An ESP32 connected to two I2C sensors with SDA, SCL, power, ground and pull-up resistors.
Every sensor shares the same SDA and SCL lines. The address tells the firmware which device is answering; the pull-ups make the high level possible. · TooFoo original
A SenseBox shield with connectors and sensor-related circuitry.
Connectorized sensor ecosystems make wiring easier, but they do not remove electrical limits. Cable and connector capacitance still sit on the bus. · Raimond Spekking, CC BY-SA 4.0

Why does an I2C bus need pull-up resistors?

Rise time is the hidden wiring bug

The I2C falling edge is easy: a device turns on a transistor and pulls the line low. The rising edge is passive: the pull-up resistor charges all the bus capacitance. More wire, more modules and more connector metal all add capacitance. Too much capacitance or too weak a pull-up makes the signal rise slowly, so the receiver sees a rounded edge instead of a clean logic high.

An I2C bus showing added capacitance and slow versus fast rising edges.
I2C edge speed is an RC problem. Lower pull-up resistance speeds the edge, but it also increases the current every device must sink for a LOW. · TooFoo original

Use the pull-up explorer below. The same bus can pass at 100 kHz and fail at 400 kHz. That is not a software mystery. It is edge timing.

Rise time tr
398 ns
Spec limit
300 ns
Rise verdict
FAIL
Sink current Isink
0.70 mA

Try these moves:

  1. Set the bus to 100 kHz and try a 10 kΩ pull-up with 100 pF bus capacitance.
  2. Move to 400 kHz without changing the wiring.
  3. Increase bus capacitance toward 400 pF and watch the edge fail.
  4. Lower the pull-up. Notice that sink current rises while rise time improves.

The address scan is your first bring-up test

Before parsing temperature registers, prove the device answers. An I2C scanner sends an address and records whether any device ACKs. If nothing ACKs, you have a physical or bus configuration problem. If the expected address ACKs but the reading is nonsense, now you debug register protocol, conversion timing or driver code.

A grid of I2C addresses showing devices responding at 0x44 and 0x76.
An address scan turns vague failure into a first fact: who, if anyone, is electrically present on the bus. · TooFoo original

The common environmental addresses are easy to remember only until the board changes: SHT31 often appears at 0x44 or 0x45; BME280 often appears at 0x76 or 0x77. Modules may expose an address-select solder jumper. Two devices with the same fixed address cannot share one I2C bus unless you add a mux, switch one device off, or choose a different part.

A sensor breakout board annotated with regulator, pull-ups, level shifting, address jumper and connector.
Breakout boards are helpful, but they are not electrically invisible. Know which helper circuits are already on the module. · TooFoo original

Power is part of the measurement schedule

A sleeping environmental node should not leave every sensor awake all day unless the budget says it can. Some sensors have a low-current sleep mode. Some can be powered from a switched rail or MOSFET. Some need a conversion delay after wake before the first reading is valid.

A low-power sensor sample cycle from sleep to wake, read, store, optional transmit and sleep again.
Power belongs in the schedule because every measurement has a before and after: wake cost, settle time, read time, storage and maybe radio transmit. · TooFoo original

For the capstone, the sample cycle should read like a small state machine:

  1. Wake the ESP32 from deep sleep.
  2. Power or wake the sensor.
  3. Wait the datasheet conversion time.
  4. Read and sanity-check the value.
  5. Store the reading in RTC memory or flash policy.
  6. Decide whether to transmit now or batch the data.
  7. Put the sensor and ESP32 back to sleep.
A pull-up value is bounded from both sides

Too large a pull-up makes the rising edge slow because the RC time constant is large. Too small a pull-up makes every LOW expensive and may exceed the device's allowed sink current. The rough lower bound is set by the maximum LOW current:

RminVCCIsink,maxR_{min} \approx \frac{V_{CC}}{I_{sink,max}}

At 3.3 V and a 3 mA sink limit, RminR_{min} is about 1.1 kΩ. The upper bound comes from rise time and bus capacitance. In practice, 4.7 kΩ is a common starting point for short 3.3 V buses, while long cables or 400 kHz operation often need a lower value or a different wiring plan.

Practice 1 warm-up

Your scanner finds 0x76, but the code was written for an SHT31 at 0x44. What is the first conclusion?

Show worked solution

The bus is alive, but the expected device is probably not what is connected. 0x76 is common for BME280-like boards. Check the module label, address-select jumper and driver. Do not debug temperature-register parsing until the address matches the intended part.

Practice 2 core

A short prototype works at 100 kHz with 10 kΩ pull-ups. After adding a cable and a second sensor, 400 kHz reads fail intermittently. Name two likely electrical fixes.

Show worked solution

The added wiring likely increased bus capacitance and slowed the rising edge. Lower the pull-up value while keeping sink current within device limits, shorten or improve the wiring, reduce bus speed, split the bus, or use an I2C buffer if the physical span must stay long.

Key takeaways

  • Choose a sensor from the physical measurement: range, accuracy, update rate, power, package and placement.
  • I2C wiring needs SDA, SCL, 3.3 V, ground and suitable pull-ups to the correct logic rail.
  • Bus capacitance and pull-up resistance set rise time, so wiring changes can break a bus that used to work.
  • Address scanning is the first bring-up test because it separates physical presence from register-level code.
  • A low-power sensor node needs a measurement schedule: wake, settle, read, store, maybe transmit, then sleep.

A sensor is not the tiny square on a breakout. It is the whole path from the physical world to a number the firmware can trust. Choose that path deliberately, wire the bus so the edges are real, and prove the address before believing the data. The next lesson turns those facts into firmware architecture.

full glossary →