Sensors

Sensors convert physical quantities into electrical signals. Temperature, pressure, light, motion, humidity, distance, acceleration — if you can measure it, there's a sensor for it. The output is usually a voltage, current, resistance change, frequency, or digital protocol, which a microcontroller reads via ADC or a serial bus. Knowing what kind of output a sensor produces determines how to interface it, and knowing what accuracy and response speed you need narrows down which sensor family to use.

Sensor Output Types

Output typeInterface methodExamplesNotes
Analog voltageADC pinLM35, TMP36, MQ-series gas sensorsMatch sensor output range to ADC reference (0–3.3V, 0–5V)
Resistance changeVoltage divider + ADCNTC thermistor, LDR, strain gauge, FSRChoose divider resistor ≈ sensor mid-range value for best sensitivity
Current (4–20mA)Shunt resistor + ADC or current receiver ICIndustrial pressure transducers, flow meters4mA = zero, 20mA = full scale; loop powered; immune to voltage drop over long cable
Frequency / PWMTimer input capture or interruptFlow sensors, tachometers, DHT22 (RH)Count edges or measure period with MCU timer
I2C digitalSDA/SCL with 4.7kΩ pull-upsDS18B20, BMP280, BME280, MPU-6050Multi-device on one bus; address must be unique per device
SPI digitalMOSI, MISO, SCK, CSMAX31855, ADXL345, MS5611Faster than I2C, one CS line per device
1-WireSingle data pin + 4.7kΩ pull-up to VCCDS18B20, DS2401Up to 127 devices on one wire; parasite power mode possible
UART serialRX/TX at specific baud rateGPS modules, LoRa modules, PM2.5 sensorsFixed baud rate in datasheet; often 9600 or 115200

Temperature Sensors

TypeRangeAccuracyInterfaceNotes
NTC Thermistor−50 to 150°C±1–3°CVoltage divider + ADCNon-linear — needs Steinhart-Hart or lookup table. Very sensitive near nominal temp.
LM35 / TMP36−40 to 150°C±1°CAnalog voltage (10mV/°C)Linear, calibrated, directly readable by ADC. Easy. Lower accuracy than digital.
DS18B20−55 to 125°C±0.5°C1-Wire digitalThe default choice for most hobbyist/maker work. Cheap, accurate, parasitic power mode.
BME280−40 to 85°C±1°CI2C / SPIAlso measures humidity (±3%) and pressure (±1 hPa). One IC for three parameters.
Type K thermocouple + MAX31855−200 to 1350°C±2°CSPI (via MAX31855)For high temperatures that silicon sensors can't handle. Cold junction compensation in the IC.
Pt100 / Pt1000 RTD−200 to 600°C±0.1°CWheatstone bridge + ADCPlatinum resistance thermometers — highest accuracy, used in industrial/laboratory work

Thermistor Voltage Divider

Thermistor Voltage Divider A voltage divider with R_fixed from VCC to the output node, and an NTC thermistor from the output node to ground. VCC R_fixed V_out (to ADC) NTC thermistor
Thermistor voltage divider: a fixed resistor and NTC thermistor create a temperature-dependent V_out for the ADC.
V_out = VCC × NTC_resistance / (R_fixed + NTC_resistance)

Best R_fixed = NTC value at your target/midpoint temperature
(maximises sensitivity at that temperature)

At 25°C with 10kΩ NTC: use R_fixed = 10kΩ
ADC reading → calculate NTC resistance → Steinhart-Hart → temperature

Light and Optical Sensors

TypeWhat it measuresSpeedInterfaceNotes
LDR (photoresistor)Light level (rough)Slow (10–100ms)Voltage divider + ADCNon-linear, imprecise. Only use for light/dark detection — not measurement.
PhotodiodeLight intensity (broad)Fast (ns–µs)Transimpedance amp + ADCLow dark current, fast response. Needs op-amp to convert current to voltage.
PhototransistorLight levelModerate (µs)Collector resistor + ADCMore sensitive than photodiode but slower. Used in optocouplers.
TSL2561 / BH1750Lux (calibrated)ModerateI2CDigital output in lux. Temperature-compensated. Use these when you need real light level.
VEML7700Lux, white lightModerateI2C16-bit, auto-gain, very low light capable. Better dynamic range than TSL2561.
TCS3200 / TCS34725RGB colourModerateFrequency / I2CRGB + Clear channels. Used in colour sorting, white balance.

Motion and Position Sensors

SensorMeasuresInterfaceNotes
ADXL3453-axis acceleration ±2–16gI2C / SPIMature, well-documented, good for tilt sensing and vibration
MPU-60503-axis accel + 3-axis gyroI2C6-DOF IMU with internal DMP for sensor fusion. Ubiquitous in drone/flight controllers.
ICM-42688-P6-DOF IMU (high performance)SPI / I2CLower noise than MPU-6050, InvenSense successor
HC-SR501 PIRInfrared motion (passive)Digital HIGH/LOWDetects warm moving objects. Adjustable sensitivity and hold time. Very common in security/alarm.
HC-SR04Distance (ultrasonic)Trigger/Echo pulses2–400cm, ±3mm. Sound-based, affected by soft/angled surfaces. Simple to use.
VL53L0X / VL53L1XDistance (laser ToF)I2CTime-of-flight laser, 2–200cm. Works on all surfaces including glass. Better than HC-SR04 for robotics.
AS5600Absolute magnetic angleI2C / Analog12-bit, 0–360°, contactless (Hall effect over magnet). Motor shaft angle sensing.

HC-SR04 Distance Calculation

Trigger: send 10µs HIGH pulse on TRIG pin
Echo: measure duration of HIGH pulse on ECHO pin

Distance (cm) = pulse_duration_µs / 58
Distance (mm) = pulse_duration_µs × 0.1724

Or: Distance = (pulse_duration × speed_of_sound) / 2
            = (pulse_µs × 0.000343m/µs) / 2

Maximum range: ~400cm (echo too weak beyond this)
Minimum range: ~2cm (pulse and echo overlap below this)
Beam angle: ~15–30° cone — surfaces must face sensor squarely

Environmental Sensors

SensorMeasuresRangeInterfaceNotes
BME280Pressure, humidity, temperature300–1100 hPa, 0–100%RH, −40–85°CI2C / SPIThe default all-in-one environmental sensor. Well-supported.
BMP390Pressure, temperature300–1250 hPaI2C / SPIHigher accuracy and lower noise than BME280 for altitude sensing
SHT31 / SHT40Humidity + temperature0–100%RH, −40–125°CI2CBetter RH accuracy (±2%) than BME280 (±3%). Sensirion Swiss quality.
CCS811 / SGP30eCO2, TVOCppm rangeI2CMOX gas sensors — need warm-up time, affected by humidity. For air quality indication.
SCD40 / SCD41CO2 (true), humidity, temperature0–40,000 ppm CO2I2CPhotoacoustic CO2 measurement — true CO2, not eCO2 estimate. Sensirion.
MQ-2 / MQ-7 / MQ-135Various gases (flammable, CO, air quality)ppm rangeAnalog voltageCheap but require significant calibration. Output varies with temperature, humidity, and warm-up.

Analog Sensor Conditioning

Problem: sensor output is 0–100mV, MCU ADC is 0–3.3V
Solution: non-inverting op-amp amplifier

V_out = V_sensor × (1 + R_f/R_g)
Gain = 3.3/0.1 = 33 → R_f/R_g = 32 → use R_g=1kΩ, R_f=33kΩ

Problem: sensor has 4–20mA current output
Solution: shunt resistor (150Ω gives 0.6–3V) + optional op-amp buffer

At 4mA:  V = 0.004 × 150 = 0.6V (represents zero)
At 20mA: V = 0.020 × 150 = 3.0V (represents full scale)
Map 0.6–3.0V → 0–100% in MCU code

References