Sensors
Sensors convert physical quantities into electrical signals. Temperature, pressure, light, motion, humidity, distance — if you can measure it, there's a sensor for it. The electrical signal you get out is usually a voltage, current, resistance change, or digital signal, which you then read with an ADC or digital interface. Knowing what kind of output a sensor produces determines how you connect it to a microcontroller.
Temperature Sensors
Thermistors (NTC)
Resistance drops as temperature rises (negative temperature coefficient). Cheap, small, decent accuracy. Non-linear — the Steinhart-Hart equation handles the conversion:
Typical circuit: thermistor in series with a fixed resistor to form a voltage divider V_out = V_cc × R_fixed / (R_thermistor + R_fixed)
Read V_out with an ADC, calculate R_thermistor from the voltage, then apply the Steinhart-Hart equation or a lookup table to get temperature. Pick R_fixed to equal the thermistor resistance at your midrange temperature for maximum sensitivity there.
Thermocouples
Two dissimilar metals joined at one end. The junction produces a small voltage proportional to temperature difference between the hot junction and a reference (cold junction). Millivolt-level output, needs amplification and cold-junction compensation. Type K is the most common (−200°C to 1350°C). Use a dedicated IC like the MAX31855 or AD8495 for the amplification and compensation — doing it with a raw op-amp is fiddly.
Digital Temperature Sensors
The DS18B20 is the classic: 1-Wire digital interface, ±0.5°C accuracy, up to 127 sensors on one wire. Reads directly into a microcontroller. No analog chain, no calibration, just library code. Good enough for most applications and far easier than analog sensors.
Light Sensors
Photodiodes and Phototransistors
A photodiode generates current proportional to light intensity when reverse-biased. Very fast response. A phototransistor uses a photosensitive base junction to amplify the current — more sensitive but slower. Both need a transimpedance amplifier (op-amp with feedback resistor) to convert current to voltage for ADC reading.
LDRs (Light-Dependent Resistors)
Resistance drops with increasing light. Slow (response time 10–100ms), temperature-dependent, imprecise, but very cheap. Fine for simple light/dark sensing. Put in a voltage divider with a fixed resistor and connect to an ADC pin.
Digital Ambient Light Sensors
The TSL2561, BH1750, and VEML7700 give you lux readings over I2C. Calibrated, compensated, done. Use these instead of LDRs when you need actual light level measurement rather than just light-or-dark detection.
Motion and Position
Accelerometers
Measure acceleration along one, two, or three axes. At rest, they measure gravitational acceleration — useful for tilt detection. Most modern ones are MEMS devices with I2C or SPI output (ADXL345, MPU-6050). The MPU-6050 combines a 3-axis accelerometer with a 3-axis gyroscope and has an internal DMP (digital motion processor) for sensor fusion.
PIR Sensors (Passive Infrared)
Detect changes in infrared radiation from moving warm bodies. The HC-SR501 is the classic hobby module — it outputs a digital HIGH when motion is detected. Adjustable sensitivity and hold time. The Fresnel lens on the front determines the detection pattern. These are passive — they don't emit anything, they just detect thermal changes.
Ultrasonic Distance (HC-SR04)
Sends a 40kHz pulse and measures the echo return time. Works out to roughly 2cm–400cm. Trigger with a 10µs pulse on the TRIG pin, measure the ECHO pulse width:
Distance (cm) = pulse_width_µs / 58 Or: Distance (cm) = (pulse_width_µs × 0.034) / 2 (speed of sound ≈ 340m/s, round trip → divide by 2)
Pressure and Humidity
The BME280 and BMP280 cover most use cases — barometric pressure, humidity, and temperature in one small I2C package. Bosch's devices are well-supported by every major platform. For higher pressure ranges (industrial, hydraulic) piezoelectric pressure transducers with 4–20mA or 0–5V analog output are standard.
Interfacing to Microcontrollers
- Analog voltage output → ADC pin. Make sure sensor output range matches ADC reference (0–3.3V for most MCUs).
- Resistance change → voltage divider + ADC pin.
- I2C → SDA/SCL pins, usually with 4.7kΩ pull-ups to VCC.
- SPI → MOSI, MISO, SCK, CS pins.
- 1-Wire → single data pin with 4.7kΩ pull-up. Pull the line low to communicate.
- Digital pulse width → timer input capture or interrupts for echo measurement.
dispelled