<!--date=20250221 -->


<h1>Sensors</h1>
<p>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.</p>

<h2>Sensor Output Types</h2>

<table>
  <thead><tr><th>Output type</th><th>Interface method</th><th>Examples</th><th>Notes</th></tr></thead>
  <tbody>
    <tr><td>Analog voltage</td><td>ADC pin</td><td>LM35, TMP36, MQ-series gas sensors</td><td>Match sensor output range to ADC reference (0–3.3V, 0–5V)</td></tr>
    <tr><td>Resistance change</td><td>Voltage divider + ADC</td><td>NTC thermistor, LDR, strain gauge, FSR</td><td>Choose divider resistor ≈ sensor mid-range value for best sensitivity</td></tr>
    <tr><td>Current (4–20mA)</td><td>Shunt resistor + ADC or current receiver IC</td><td>Industrial pressure transducers, flow meters</td><td>4mA = zero, 20mA = full scale; loop powered; immune to voltage drop over long cable</td></tr>
    <tr><td>Frequency / PWM</td><td>Timer input capture or interrupt</td><td>Flow sensors, tachometers, DHT22 (RH)</td><td>Count edges or measure period with MCU timer</td></tr>
    <tr><td>I2C digital</td><td>SDA/SCL with 4.7kΩ pull-ups</td><td>DS18B20, BMP280, BME280, MPU-6050</td><td>Multi-device on one bus; address must be unique per device</td></tr>
    <tr><td>SPI digital</td><td>MOSI, MISO, SCK, CS</td><td>MAX31855, ADXL345, MS5611</td><td>Faster than I2C, one CS line per device</td></tr>
    <tr><td>1-Wire</td><td>Single data pin + 4.7kΩ pull-up to VCC</td><td>DS18B20, DS2401</td><td>Up to 127 devices on one wire; parasite power mode possible</td></tr>
    <tr><td>UART serial</td><td>RX/TX at specific baud rate</td><td>GPS modules, LoRa modules, PM2.5 sensors</td><td>Fixed baud rate in datasheet; often 9600 or 115200</td></tr>
  </tbody>
</table>

<h2>Temperature Sensors</h2>

<table>
  <thead><tr><th>Type</th><th>Range</th><th>Accuracy</th><th>Interface</th><th>Notes</th></tr></thead>
  <tbody>
    <tr><td>NTC Thermistor</td><td>−50 to 150°C</td><td>±1–3°C</td><td>Voltage divider + ADC</td><td>Non-linear — needs Steinhart-Hart or lookup table. Very sensitive near nominal temp.</td></tr>
    <tr><td>LM35 / TMP36</td><td>−40 to 150°C</td><td>±1°C</td><td>Analog voltage (10mV/°C)</td><td>Linear, calibrated, directly readable by ADC. Easy. Lower accuracy than digital.</td></tr>
    <tr><td>DS18B20</td><td>−55 to 125°C</td><td>±0.5°C</td><td>1-Wire digital</td><td>The default choice for most hobbyist/maker work. Cheap, accurate, parasitic power mode.</td></tr>
    <tr><td>BME280</td><td>−40 to 85°C</td><td>±1°C</td><td>I2C / SPI</td><td>Also measures humidity (±3%) and pressure (±1 hPa). One IC for three parameters.</td></tr>
    <tr><td>Type K thermocouple + MAX31855</td><td>−200 to 1350°C</td><td>±2°C</td><td>SPI (via MAX31855)</td><td>For high temperatures that silicon sensors can't handle. Cold junction compensation in the IC.</td></tr>
    <tr><td>Pt100 / Pt1000 RTD</td><td>−200 to 600°C</td><td>±0.1°C</td><td>Wheatstone bridge + ADC</td><td>Platinum resistance thermometers — highest accuracy, used in industrial/laboratory work</td></tr>
  </tbody>
</table>

<h3>Thermistor Voltage Divider</h3>
<pre>
VCC ──[R_fixed]──┬── V_out (to ADC)
                 │
          [NTC thermistor]
                 │
                GND

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
</pre>

<h2>Light and Optical Sensors</h2>

<table>
  <thead><tr><th>Type</th><th>What it measures</th><th>Speed</th><th>Interface</th><th>Notes</th></tr></thead>
  <tbody>
    <tr><td>LDR (photoresistor)</td><td>Light level (rough)</td><td>Slow (10–100ms)</td><td>Voltage divider + ADC</td><td>Non-linear, imprecise. Only use for light/dark detection — not measurement.</td></tr>
    <tr><td>Photodiode</td><td>Light intensity (broad)</td><td>Fast (ns–µs)</td><td>Transimpedance amp + ADC</td><td>Low dark current, fast response. Needs op-amp to convert current to voltage.</td></tr>
    <tr><td>Phototransistor</td><td>Light level</td><td>Moderate (µs)</td><td>Collector resistor + ADC</td><td>More sensitive than photodiode but slower. Used in optocouplers.</td></tr>
    <tr><td>TSL2561 / BH1750</td><td>Lux (calibrated)</td><td>Moderate</td><td>I2C</td><td>Digital output in lux. Temperature-compensated. Use these when you need real light level.</td></tr>
    <tr><td>VEML7700</td><td>Lux, white light</td><td>Moderate</td><td>I2C</td><td>16-bit, auto-gain, very low light capable. Better dynamic range than TSL2561.</td></tr>
    <tr><td>TCS3200 / TCS34725</td><td>RGB colour</td><td>Moderate</td><td>Frequency / I2C</td><td>RGB + Clear channels. Used in colour sorting, white balance.</td></tr>
  </tbody>
</table>

<h2>Motion and Position Sensors</h2>

<table>
  <thead><tr><th>Sensor</th><th>Measures</th><th>Interface</th><th>Notes</th></tr></thead>
  <tbody>
    <tr><td>ADXL345</td><td>3-axis acceleration ±2–16g</td><td>I2C / SPI</td><td>Mature, well-documented, good for tilt sensing and vibration</td></tr>
    <tr><td>MPU-6050</td><td>3-axis accel + 3-axis gyro</td><td>I2C</td><td>6-DOF IMU with internal DMP for sensor fusion. Ubiquitous in drone/flight controllers.</td></tr>
    <tr><td>ICM-42688-P</td><td>6-DOF IMU (high performance)</td><td>SPI / I2C</td><td>Lower noise than MPU-6050, InvenSense successor</td></tr>
    <tr><td>HC-SR501 PIR</td><td>Infrared motion (passive)</td><td>Digital HIGH/LOW</td><td>Detects warm moving objects. Adjustable sensitivity and hold time. Very common in security/alarm.</td></tr>
    <tr><td>HC-SR04</td><td>Distance (ultrasonic)</td><td>Trigger/Echo pulses</td><td>2–400cm, ±3mm. Sound-based, affected by soft/angled surfaces. Simple to use.</td></tr>
    <tr><td>VL53L0X / VL53L1X</td><td>Distance (laser ToF)</td><td>I2C</td><td>Time-of-flight laser, 2–200cm. Works on all surfaces including glass. Better than HC-SR04 for robotics.</td></tr>
    <tr><td>AS5600</td><td>Absolute magnetic angle</td><td>I2C / Analog</td><td>12-bit, 0–360°, contactless (Hall effect over magnet). Motor shaft angle sensing.</td></tr>
  </tbody>
</table>

<h3>HC-SR04 Distance Calculation</h3>
<pre>
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
</pre>

<h2>Environmental Sensors</h2>

<table>
  <thead><tr><th>Sensor</th><th>Measures</th><th>Range</th><th>Interface</th><th>Notes</th></tr></thead>
  <tbody>
    <tr><td>BME280</td><td>Pressure, humidity, temperature</td><td>300–1100 hPa, 0–100%RH, −40–85°C</td><td>I2C / SPI</td><td>The default all-in-one environmental sensor. Well-supported.</td></tr>
    <tr><td>BMP390</td><td>Pressure, temperature</td><td>300–1250 hPa</td><td>I2C / SPI</td><td>Higher accuracy and lower noise than BME280 for altitude sensing</td></tr>
    <tr><td>SHT31 / SHT40</td><td>Humidity + temperature</td><td>0–100%RH, −40–125°C</td><td>I2C</td><td>Better RH accuracy (±2%) than BME280 (±3%). Sensirion Swiss quality.</td></tr>
    <tr><td>CCS811 / SGP30</td><td>eCO2, TVOC</td><td>ppm range</td><td>I2C</td><td>MOX gas sensors — need warm-up time, affected by humidity. For air quality indication.</td></tr>
    <tr><td>SCD40 / SCD41</td><td>CO2 (true), humidity, temperature</td><td>0–40,000 ppm CO2</td><td>I2C</td><td>Photoacoustic CO2 measurement — true CO2, not eCO2 estimate. Sensirion.</td></tr>
    <tr><td>MQ-2 / MQ-7 / MQ-135</td><td>Various gases (flammable, CO, air quality)</td><td>ppm range</td><td>Analog voltage</td><td>Cheap but require significant calibration. Output varies with temperature, humidity, and warm-up.</td></tr>
  </tbody>
</table>

<h2>Analog Sensor Conditioning</h2>
<pre>
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
</pre>

<h2>References</h2>
<ul>
  <li>Horowitz &amp; Hill — <em>The Art of Electronics</em>, 3rd ed. Chapters 15 (measurements)</li>
  <li>Monk — <em>Programming Arduino: Getting Started with Sketches</em></li>
  <li><a href="https://www.adafruit.com/category/35" target="_blank" rel="noopener">Adafruit Sensors — practical descriptions and libraries ↗</a></li>
  <li><a href="https://www.sensirion.com/products/catalog/" target="_blank" rel="noopener">Sensirion — environmental sensor datasheets ↗</a></li>
</ul>
