Key Takeaways
- Sensors convert physical quantities (temperature, pressure, distance) into electrical signals a microcontroller can read
- The three main serial interfaces: I2C (multi-device on two wires), SPI (high-speed, four wires), UART (point-to-point serial)
- Analog sensors output variable voltage read by an ADC; digital sensors output discrete signals or use serial protocols
- Actuators (motors, servos, relays) usually need a driver circuit between the microcontroller and the load
- PWM duty cycle controls motor speed and servo position
Sensors Convert the Physical World to Data
A sensor is a transducer — it converts one form of energy into another. Temperature sensors convert thermal energy to voltage. Accelerometers convert acceleration forces to capacitance changes. Microphones convert sound pressure to voltage. All sensors ultimately produce an electrical signal that a microcontroller can read.
Two broad categories:
- Analog sensors — Output a continuous voltage proportional to the measured quantity. Require an Analog-to-Digital Converter (ADC) to read. Example: LM35 temperature sensor (10mV per °C).
- Digital sensors — Output a digital signal. Either discrete (GPIO high/low) or serial protocol (I2C/SPI data). Example: DHT22 temperature/humidity sensor (custom serial protocol).
Interface Protocols: How Sensors Talk to Microcontrollers
| Protocol | Wires | Speed | Devices | Common Uses |
|---|---|---|---|---|
| I2C | SDA + SCL (+ power) | 100kHz–1MHz | Up to 127 on one bus | IMUs, displays, EEPROMs, temp sensors |
| SPI | MOSI+MISO+CLK+CS (+ power) | Up to 50MHz+ | One CS pin per device | SD cards, displays, DACs, ADCs |
| UART | TX + RX (+ power) | 9600–921600 baud | Point-to-point | GPS, Bluetooth, debug, ESP modules |
| 1-Wire | Single data wire | Slow | Multiple on one pin | DS18B20 temperature sensors |
| Analog (ADC) | Single wire to ADC pin | N/A | One per ADC pin | Potentiometers, LDRs, simple sensors |
I2C example with Arduino:
#include <Wire.h>
#include <Adafruit_BME280.h> // I2C temperature/humidity/pressure
Adafruit_BME280 bme;
void setup() {
Serial.begin(9600);
if (!bme.begin(0x76)) { // 0x76 is the I2C address
Serial.println("Could not find BME280 sensor!");
while(1);
}
}
void loop() {
Serial.print("Temperature: ");
Serial.print(bme.readTemperature());
Serial.println(" °C");
delay(2000);
}
Common Sensor Types and When to Use Them
Temperature & Humidity:
- DHT22 — Digital, ±0.5°C accuracy, 0-100% humidity. Easy to use, single-wire protocol. Good for home automation.
- BME280 — I2C, adds barometric pressure. ±1°C, ±3% RH. Better accuracy and additional pressure reading for weather monitoring.
- DS18B20 — 1-Wire waterproof temperature sensor. Multiple sensors on one wire. Great for liquid temperature or outdoor use.
Distance & Proximity:
- HC-SR04 — Ultrasonic, 2cm-400cm range. Two GPIO pins (trigger and echo). Works by measuring time-of-flight of sound. Good for 5-300cm.
- VL53L1X — ToF (Time-of-Flight) laser, I2C, 4m range, ±1mm accuracy. Compact, more accurate than ultrasonic.
- IR proximity sensor (GP2Y0A21YK) — Analog output, 10-80cm. Affected by object color and surface reflectivity.
Motion & Orientation:
- MPU-6050 — 6-DOF IMU (3-axis gyroscope + 3-axis accelerometer), I2C. Used in drones, robots, wearables.
- BNO055 — 9-DOF IMU with integrated sensor fusion. Outputs Euler angles directly — saves you the math.
- PIR sensor (HC-SR501) — Passive infrared motion detection. Digital output, adjustable sensitivity and timeout. Simple security and occupancy sensing.
Actuators: Converting Electrical Signals to Physical Action
Actuators do the opposite of sensors — they take electrical input and produce physical output: movement, light, sound, heat.
- LED — Light emitting diode. GPIO high/low for on/off; PWM for brightness control.
- Buzzer — Piezoelectric: digital signal (tone() in Arduino). Electromagnetic: DC voltage for constant tone.
- Relay — Electrically controlled switch. A 5mA GPIO signal switches a circuit carrying amps (motor, light, appliance). Always use a flyback diode with relay coils.
- Solenoid — Electromagnetic plunger. Linear motion (lock mechanisms, valve actuators). Needs a transistor driver and flyback diode.
Motor Types and Control
| Motor Type | Control | Driver | Best For |
|---|---|---|---|
| DC Motor | Voltage + PWM speed + H-bridge direction | L298N, DRV8833 | Wheels, fans, continuous rotation |
| Servo Motor | PWM pulse width (500μs-2500μs) | Direct from MCU | Precise angle positioning (0-180°) |
| Stepper Motor | Step pulses, direction, enable | A4988, DRV8825 | CNC, 3D printers, precise rotation |
| BLDC Motor | Three-phase, ESC for speed | ESC (Electronic Speed Controller) | Drones, high-speed applications |
Servo control example:
#include <Servo.h>
Servo myServo;
void setup() {
myServo.attach(9); // Servo on pin 9
}
void loop() {
myServo.write(0); // 0 degrees
delay(1000);
myServo.write(90); // 90 degrees (center)
delay(1000);
myServo.write(180); // 180 degrees
delay(1000);
}
Integrating Sensors with Cloud and AI
The real power of sensor data comes from processing it at scale. Common integration patterns:
- MQTT to cloud — Publish sensor readings to an MQTT broker (Mosquitto, HiveMQ, AWS IoT Core). Subscribe and process on the backend.
- Edge inference — Run a TensorFlow Lite model on the microcontroller itself. Classify sensor data locally without a network. Good for latency-sensitive or offline applications.
- REST API — Send sensor data via HTTP POST to a cloud endpoint. Simple, works everywhere, not real-time.
- InfluxDB + Grafana — Time-series database for sensor data. Grafana for visualization. Standard IoT monitoring stack.
Learn Hardware Integration at Precision AI Academy
Our bootcamp covers sensors, microcontrollers, IoT protocols, and edge AI — giving you the skills to build smart hardware systems. Five cities, October 2026.
Frequently Asked Questions
What is the difference between I2C, SPI, and UART?
I2C: two wires, up to 127 devices, 100kHz-1MHz, good for slow sensors. SPI: four wires, one CS per device, very fast, for displays/SD cards. UART: two wires, point-to-point, for GPS, Bluetooth serial, debug.
How do you control motor speed with PWM?
PWM duty cycle (0-100% on-time) controls average power to the motor. Higher duty cycle = faster speed. You need an H-bridge driver for DC motors to also control direction. Arduino's analogWrite() outputs PWM on supported pins.
What is ADC resolution and why does it matter for sensors?
ADC resolution is the number of bits used to represent the analog range. 10-bit = 1024 steps over the voltage range (~4.9mV steps for 5V range). 12-bit = 4096 steps (~1.2mV). Higher resolution enables finer measurement. Most Arduino boards are 10-bit; ESP32 is 12-bit.