Skip to content
Tutorials Center
My learning

Topics

Lesson 8 of 36 ยท Sensors and Actuators

Actuators: Relays, Motors and Displays

Learn how IoT devices act on the world: LEDs, relays, transistors, DC, servo and stepper motors, buzzers, valves, driver circuits, and safe control logic.

Sensors let a device observe. Actuators let it change the world: turn on a light, open a valve, spin a fan, lock a door, sound an alarm. A microcontroller pin can only provide a few milliamps at 3.3 V, so almost every actuator needs a driver, a component that switches a bigger current under the pin's control. This lesson shows the common actuators, the driver circuits they need, and safe control patterns.

What actuators do

Output Typical actuator Interface
Light LED, LED strip GPIO, PWM, one-wire protocol
Sound Buzzer, speaker GPIO tone, PWM, I2S
Heat or power switching Relay, solid-state relay, MOSFET GPIO
Motion (rotation) DC motor, servo, stepper PWM, driver board
Flow Solenoid valve, pump Relay or MOSFET
Display OLED, e-paper, LCD I2C, SPI
Lock Electric strike, servo latch GPIO via driver

Driving LEDs: the series resistor

A GPIO pin can power a small LED, but you must limit the current with a resistor: R = (Vsupply - Vled) / I. For a 3.3 V pin, a red LED with a forward voltage near 2 V and a target 10 mA needs about 130 ohms; 150 ohm is the nearest common value.

Python Choosing an LED resistor

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.

Rounding up keeps the current at or below the target. Never connect an LED without a resistor.

Relays and solid-state relays

A relay is an electrically controlled switch: a small coil current closes contacts that can handle mains voltage. Because the coil needs more current than a pin supplies, relay modules include a transistor and often an opto-isolator. Points to remember:

  • Choose the contact rating for the load (voltage and amps) with margin.
  • Inductive loads (motors, solenoids) cause voltage spikes; use a flyback diode on the coil side and a suppressor across heavy loads.
  • Mechanical relays click and wear out (about 100,000 or more switches), so avoid switching them rapidly. This is why thermostats use hysteresis.
  • A solid-state relay (SSR) has no moving parts and switches silently and fast, but gets warm and leaks a little current.

Mains voltage can kill. Use certified, enclosed modules, keep low-voltage wiring separate, and have a qualified person check anything that connects to the wall socket. For learning, switch low-voltage loads only.

Transistors and MOSFETs

For DC loads, a logic-level N-channel MOSFET is the standard electronic switch: the pin sets the gate voltage, and the load current flows through the MOSFET from drain to source. Add a pull-down resistor on the gate so the load is off while the microcontroller boots, and a flyback diode across inductive loads.

DC motors, PWM and H-bridges

A DC motor's speed depends on its average voltage. You cannot lower it with a pin directly, but PWM (pulse-width modulation) switches the supply on and off thousands of times a second; the duty cycle (percentage of time on) sets the average. An H-bridge driver (L298N, TB6612, DRV8833) also reverses direction.

Python PWM duty cycle to average voltage and motor speed

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.

Real motors do not start below a minimum duty (they stall) and speed falls with load; closed-loop control with an encoder fixes this, as a later project shows.

Servo motors

A hobby servo contains a motor, gears and a position controller. You send a pulse every 20 ms; a width of about 1 ms means one end, 1.5 ms the middle and 2 ms the other end, usually covering 0 to 180 degrees. Servos draw large current spikes, so power them from a separate supply, sharing ground with the board.

Python Servo angle to pulse width

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.

The listing for Arduino is short:

C++
#include <Servo.h>
Servo door;

void setup() { door.attach(9); }

void loop() {
  door.write(0);    delay(1000);    // closed
  door.write(90);   delay(1000);    // open
}

Stepper motors

A stepper moves in fixed steps (for example 200 per revolution, or 1.8 degrees each), so it can position accurately without feedback, such as in 3D printers and blinds. A driver (A4988, TMC2209) turns step and direction pulses into coil currents. Micro-stepping divides each step finely.

Valves, pumps, buzzers and displays

Solenoid valves and small pumps are inductive DC loads: MOSFET or relay plus a flyback diode. Passive buzzers need a frequency (a tone), active buzzers just need power. Small OLED and e-paper displays show status; e-paper keeps its image with zero power, ideal for battery devices.

Safe actuator control patterns

Actuators can cause harm and damage, so build in protections:

  • Safe default state: on power loss or crash, the output should go to a safe state (valve closed, heater off).
  • Limits: enforce a maximum run time and range in the device, not just in the app.
  • Watchdog: restart if the program hangs.
  • Rate limit commands so a message flood cannot wear out a relay.
  • Confirm state: read back a limit switch or current to verify the action happened.
Python A guarded heater controller with a time limit and rate limiter

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. Why can't a GPIO pin usually drive a motor or relay coil directly?

    Pins source tiny currents, so a transistor, MOSFET or driver board must switch the load.
  2. 2. What does PWM duty cycle control?

    Switching fast with a variable on-time changes the average voltage the load sees.
  3. 3. What is the purpose of a flyback diode across a relay coil or motor?

    Collapsing magnetic fields create voltage spikes that can destroy the driver transistor.
  4. 4. Which design rule makes actuators safer?

    Safety must work even when the network or the program fails.

Summary

  • Actuators turn commands into light, sound, heat, flow or motion, usually through a driver.
  • LEDs need a resistor; relays and MOSFETs switch loads; inductive loads need flyback protection.
  • PWM sets average power; H-bridges reverse motors; servos use pulse widths; steppers move in steps.
  • Always design a safe default state, device-side limits and rate limits.

Next: Signal Conditioning and Filtering