Skip to content
SDB
Embedded Control and Mechatronics

2 hours

Programming and Interfacing of Sensors and Actuators

Connecting the Physical World to Embedded Control Systems

Subhendu Datta BhowmikRobotics Tutorials

The Sensor–Actuator Loop

Every robotic system fundamentally performs a sense → think → act cycle. Sensors convert physical quantities (distance, temperature, force, angle) into electrical signals that a microcontroller can read. Actuators convert electrical signals back into physical motion or effects.

Understanding how to interface sensors and actuators correctly — choosing the right protocol, setting appropriate signal levels, handling timing — is one of the most practically important skills in robotics engineering.

Common Sensor Types in Robotics

SensorMeasured QuantityInterfaceTypical Range
HC-SR04 UltrasonicDistanceGPIO (trigger/echo)2 cm – 4 m
IR Distance (GP2Y0A)DistanceAnalog (ADC)10 – 80 cm
MPU-6050 IMUAcceleration + GyroI²C (0x68)±2–16 g, ±250–2000 °/s
BMP280 BarometerPressure + TemperatureI²C or SPI300–1100 hPa
Encoder (incremental)Angular position/speedGPIO interrupt (A/B)Application dependent
Force Sensitive ResistorContact forceAnalog (ADC)0.1 N – 100 N
Hall Effect SensorMagnetic field / RPMGPIO (digital)Proximity / RPM
Lidar (RPLIDAR A1)2D distance scanUART / USB0.15 – 12 m, 360°
Camera (OV5647)Image / videoMIPI CSI / USBUp to 5 MP

Digital Interfaces: I²C, SPI, and UART

I²C (Inter-Integrated Circuit) uses two wires (SCL clock, SDA data) and supports up to 127 devices on the same bus, each with a unique 7-bit address. Data rates are 100 kHz (standard), 400 kHz (fast), or 1 MHz (fast+). Pull-up resistors (4.7 kΩ typical) are required on both lines. Good for sensors where speed is not critical: IMUs, barometers, EEPROMs.

SPI (Serial Peripheral Interface) uses four wires: SCLK (clock), MOSI (master-out), MISO (master-in), and CS (chip-select, one per device). SPI is full-duplex and typically faster than I²C (10–50 MHz). Used for displays, SD cards, high-speed ADCs/DACs, and fast sensors.

UART (Universal Asynchronous Receiver-Transmitter) is point-to-point (one TX to one RX). No clock wire — both sides agree on baud rate (9600, 115200, 1 Mbaud, etc.). Widely used for GPS modules, LIDAR sensors, Bluetooth/Wi-Fi modules, and host PC communication.

Analog Sensing: ADC Best Practices

  1. 01

    The ADC resolution determines the smallest detectable change: a 10-bit ADC with 5 V reference has 5 V / 1024 ≈ 4.9 mV per step

  2. 02

    Always decouple the analog power supply with 100 nF and 10 µF capacitors close to the ADC pin

  3. 03

    Use averaging (e.g., 16-sample moving average) to reduce noise in slowly-changing signals

  4. 04

    Never exceed the MCU's maximum analog input voltage — add a resistor divider for higher voltage signals

  5. 05

    The ADC sample-and-hold capacitor needs settling time — wait after switching multiplexer channels

Actuators: Motors and Their Drivers

DC motors require an H-bridge driver to enable bidirectional control and current amplification. Common H-bridge ICs: L298N (up to 2 A per channel), DRV8833 (up to 1.5 A), TB6612FNG (1.2 A continuous). The PWM duty cycle sent to the enable pin controls speed; the direction pins select the polarity.

Servo motors (RC servos) accept a 50 Hz PWM signal where pulse width encodes position: 1 ms → 0°, 1.5 ms → 90°, 2 ms → 180°. They contain a built-in H-bridge, gearbox, and position feedback potentiometer. Servos are ideal for steering linkages, pan-tilt camera mounts, and simple gripper mechanisms.

Stepper motors move in discrete angular steps (typically 1.8°/step = 200 steps/revolution). Drivers like the A4988 or DRV8825 accept STEP and DIR signals; the step count is tracked in software to determine absolute position without a separate encoder. Used in 3D printers, CNC machines, and precision positioning stages.

Brushless DC (BLDC) motors are driven by an ESC (Electronic Speed Controller) which handles the three-phase commutation. The ESC accepts a servo-style PWM signal (1–2 ms). BLDC motors offer high efficiency and power-to-weight ratios — used in drone propulsion and high-performance wheeled robots.

Motor Type Comparison

TypeControl SignalFeedback Needed?Best For
DC Motor (brushed)PWM via H-bridgeEncoder for position/speedWheels, conveyor, simple mechanisms
RC Servo50 Hz PWM (1–2 ms)Built-in (pot)Pan-tilt, steering, grippers
Stepper MotorSTEP/DIR pulsesOpen-loop (count steps)CNC, 3D printers, precise positioning
BLDC / PMSMPWM to ESC or FOC driverEncoder or Hall sensorsDrones, high-speed wheels, actuators
Linear ActuatorPWM via H-bridgeOptional limit switchesLifting, clamping, pressing

Embedded Control and Mechatronics