Learn / Sensors and actuators / Sensor basics

Sensor Interfaces

ReviewedBeginnerFreeAbout 5 min
BME280
In short

Sensor Interfaces: the short answer

Common interfaces include analog voltage, I²C, SPI, and UART. Wiring, bus addressing, pull-ups and electrical levels must be validated. Example: Plan pins by interface first: ADC pins for analog, any GPIO for digital, and one shared pair for all I²C parts.

Level
Beginner
Time
About 5 min
Topic
Sensors and actuators
You will learn to

Recognise analog, digital, I2C and SPI sensor outputs.

Common interfaces include analog voltage, I²C, SPI, and UART. Wiring, bus addressing, pull-ups and electrical levels must be validated.

An analog interface transfers a quantity as voltage or current; digital buses transfer encoded information. A sensor breakout may include its own regulator or level shifters, or it may expose raw chip pins with stricter limits.

For an I²C breakout, identify supply limits, SDA/SCL electrical levels, addressing and required pull-ups. A successful library call is not proof the interface is within its electrical ratings.

Four ways a sensor talks

Analog sensors, such as a thermistor divider or a light-dependent resistor, give a voltage that changes with the quantity. You read it with the ADC, and the quality of the reading depends on your reference voltage, wiring and noise. Digital sensors, like a PIR motion sensor or a limit switch, give just high or low on one pin.

Bus sensors send numbers over a few wires. I²C uses two shared lines (SDA and SCL) and gives each device an address. SPI uses four lines (clock, data in, data out and one chip-select per device), and runs faster. UART sends data in framed bytes at an agreed baud rate. Some sensors, like the DHT22, use their own one-wire timing that a library handles.

Reading an analog sensor properly

A 12-bit ADC at 3.3 V resolves about 0.8 mV a step, but real readings on an ESP32 are noisy and bend near the ends of the range. Average 8 to 16 samples, keep analog wires short and away from motors and Wi-Fi antennas, add a 100 nF capacitor from the input to ground, and avoid the ADC2 pins when Wi-Fi is on, since they are used by the radio.

Sensors that behave as variable resistors (thermistors, LDRs) need a fixed resistor to turn resistance into a voltage. Choose the fixed resistor close to the sensor's resistance in the middle of the range you care about, so the output moves as much as possible.

I²C in practice

Devices sit on the same two wires and answer to a 7-bit address. A BME280 uses 0x76 or 0x77, an SSD1306 OLED 0x3C or 0x3D, an MPU6050 0x68 or 0x69. Two devices with the same address clash, and some breakouts have a jumper to change it.

Both lines need pull-up resistors to the supply, usually 4.7 kΩ at 100 kHz. Most breakout boards already include them. Many boards on one bus mean many pull-ups in parallel, and the total can get low enough to overload the pins. An I²C scanner sketch that lists every address it sees is the first tool to use when a sensor doesn't respond.

Voltage and breakout boards

A bare sensor chip often has a narrow supply window, for example 1.7 to 3.6 V. A breakout board may add a regulator and level shifters so you can use 5 V, or may not. Read the board's listing, not just the chip's datasheet, and note the pin names, which differ between makers (VCC, VIN, 3V3).

A library call that works doesn't prove the wiring is within ratings. A 3.3 V-only chip can respond for days on 5 V signals and then fail.

Common mistakes
  • Skipping the I²C scan and guessing the address.
  • Powering a sensor from 5 V and connecting its output to a 3.3 V pin without checking the board.
  • Reading an analog sensor once and trusting the number instead of averaging.
Worked example

Which interface does each sensor use?

  1. LDR with a resistor: analog, one ADC pin.
  2. PIR motion: digital, one GPIO HIGH or LOW.
  3. BME280: I²C, shares SDA and SCL with other parts.
  4. DHT22: its own one-wire protocol on one GPIO.

Result. Plan pins by interface first: ADC pins for analog, any GPIO for digital, and one shared pair for all I²C parts.

Try it

Describe the difference between I²C clock and data lines.

Check yourself

3 quick questions

  1. An LDR with a fixed resistor gives a changing voltage. Which pin type reads it?

  2. A PIR motion sensor output is…

  3. You add a second I²C sensor. How many extra data pins do you need?

Common questions

What will I learn in "Sensor Interfaces"?

Recognise analog, digital, I2C and SPI sensor outputs.

Which interface does each sensor use?

LDR with a resistor: analog, one ADC pin. PIR motion: digital, one GPIO HIGH or LOW. BME280: I²C, shares SDA and SCL with other parts. DHT22: its own one-wire protocol on one GPIO. Plan pins by interface first: ADC pins for analog, any GPIO for digital, and one shared pair for all I²C parts.

An LDR with a fixed resistor gives a changing voltage. Which pin type reads it?

Analog (ADC) pin. A varying voltage needs the ADC; a digital pin only sees HIGH or LOW.

A PIR motion sensor output is…

Digital HIGH or LOW. The PIR module outputs HIGH when it detects motion.

You add a second I²C sensor. How many extra data pins do you need?

None, it shares SDA and SCL. I²C parts share the same two lines, each with its own address.