Skip to content
Tutorials Center
My learning

Topics

Lesson 6 of 36 ยท Sensors and Actuators

Common Sensor Types

A tour of the sensors used in IoT: temperature, humidity, light, motion, distance, accelerometer, gas, GPS and soil moisture, with code and conversions.

There are hundreds of sensor parts but only a few dozen kinds of measurements. Once you know the main families, how each works, and what mistakes beginners make, you can read almost any datasheet. This lesson covers the sensors you will meet most often in IoT projects, with a short code listing for the Arduino-style boards you will see in the next chapter, and simulations of the math you do on the readings.

Temperature and humidity

Thermistors (NTC) lower their resistance as they warm; they are cheap and fast but non-linear. Analog chips such as TMP36 or LM35 give a voltage proportional to temperature. Digital sensors (DS18B20, BME280, SHT31) send a calibrated number over a bus; the DS18B20 even allows many sensors on a single wire. Thermocouples measure very high temperatures and need an amplifier chip.

Humidity sensors (capacitive) report relative humidity, which depends on temperature: warm air holds more water. Combined chips such as the BME280 report temperature, humidity and pressure.

A read-only listing for a DHT-style sensor on Arduino looks like this:

C++
#include <DHT.h>
DHT dht(4, DHT22);            // data pin 4

void setup() {
  Serial.begin(115200);
  dht.begin();
}

void loop() {
  float h = dht.readHumidity();
  float t = dht.readTemperature();
  if (isnan(h) || isnan(t)) {      // always check for failed reads
    Serial.println("read failed");
  } else {
    Serial.print(t); Serial.print(" C  "); Serial.print(h); Serial.println(" %");
  }
  delay(2000);                     // DHT22 can be read about every 2 s
}

Hardware listings are read-only. The runnable examples simulate the same calculations.

Light sensors

An LDR (light-dependent resistor) drops its resistance in light; connected in a voltage divider it gives an analog voltage. A photodiode or phototransistor is faster. Digital ambient-light chips (BH1750, TSL2561) report lux. Color sensors compare red, green and blue channels; UV sensors guard sun exposure.

Motion, presence and distance

  • PIR (passive infrared) senses heat movement from people; output is on or off.
  • Ultrasonic (HC-SR04) sends a sound pulse and times the echo.
  • Time-of-flight (VL53L0X) times a laser pulse for millimeter precision.
  • mmWave radar detects presence even when people are still.
  • Magnetic reed switch detects an open door when a magnet moves away.

An ultrasonic sensor measures a round-trip time. Sound travels about 343 m/s at 20 C, so distance is half of speed times time. Speed of sound also changes with temperature, which is why accurate designs compensate for it.

Python Ultrasonic distance from echo time, with temperature compensation

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Acceleration, rotation and orientation

An accelerometer measures acceleration including gravity, so a device lying flat reads about 1 g on the Z axis. A gyroscope measures rotation rate. A magnetometer finds magnetic north. A combined module (MPU6050, BNO055) is an IMU. Uses: step counting, tilt, vibration monitoring, drop detection.

You can compute tilt from the three accelerometer axes:

Python Tilt angle from accelerometer readings

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Note that a moving device mixes motion with gravity; real firmware filters the signal (see filtering).

Pressure, flow and level

Barometric sensors (BMP280) measure air pressure, which also tells altitude. Industrial pressure transducers output 4-20 mA signals. Flow sensors count pulses from a small turbine. Level can be measured by ultrasound, floats or capacitive probes.

Altitude from pressure uses the barometric formula:

Python Estimating altitude from air pressure

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Weather changes pressure by a few hPa, so altitude from pressure drifts through the day.

Gas, air quality and chemistry

Metal-oxide gas sensors (MQ series) change resistance in the presence of gases but need a warm-up and are non-selective. Dedicated CO2 sensors (NDIR, such as the SCD40) are far more accurate. Particulate sensors count dust with a laser. Soil sensors measure moisture (capacitive is better than resistive probes, which corrode), pH and conductivity.

Position: GPS and beyond

A GPS/GNSS receiver reports latitude, longitude, altitude, speed and time, typically as NMEA text sentences. It needs a view of the sky and uses a lot of power while acquiring a fix. Indoors, systems use Wi-Fi/BLE positioning or tags instead.

Cameras, microphones and special sensors

Small camera modules and microphones feed machine-learning at the edge (see later lessons). Others include current sensors for power metering, load cells for weight and RFID/NFC readers for identification.

Choosing a sensor

A quick checklist:

  1. Does it measure the right quantity, in the range you need?
  2. Is the accuracy sufficient (not more than needed)?
  3. Analog or digital interface; is your voltage 3.3 V or 5 V compatible?
  4. Power use while measuring and while sleeping.
  5. Environmental rating (water, dust, temperature, sunlight).
  6. Cost and availability; library or driver support.
Python Parsing a sensor's digital register value (BME280-style)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. How does an ultrasonic distance sensor compute distance?

    The pulse travels to the object and back, so the one-way distance is half of speed multiplied by time.
  2. 2. Why are capacitive soil-moisture probes preferred over resistive ones?

    Current through exposed metal electrodes causes corrosion; capacitive probes are sealed.
  3. 3. What does a motionless accelerometer lying flat read on its Z axis?

    Accelerometers measure gravity too, so flat on a table the Z axis reads about 1 g.
  4. 4. Which sensor type is best for confirming a door has been opened?

    A reed switch opens or closes when the magnet on the door moves away.

Summary

  • Sensor families: temperature and humidity, light, motion and distance, inertial, pressure, gas, position and more.
  • Digital sensors return calibrated numbers over a bus; analog sensors need an ADC and conversion math.
  • Distance, tilt and altitude are computed from raw readings with simple formulas.
  • Choose by quantity, accuracy, interface, voltage, power, environment and driver support.

Next: Analog-to-Digital Conversion