An actuator is a device that converts a control signal into a physical action.
We already know how to move wheels (DC motors) and robotic arms (servos). But what if we want to turn on a 220V light bulb? Or activate an electronic lock? Or make our robot beep when it detects an obstacle?
In this post, we’re going to look at general-purpose actuators. Devices that convert our electrical control signal (3.3V) into a physical action of a different nature.
Relays for Controlling Power Loads
The Relay is the king of home automation. It is basically a mechanical switch that we activate using an electromagnet.
Its great advantage is Galvanic Isolation. The control circuit (your ESP32 at 3.3V) and the power circuit (your 220V lamp) do not touch electrically. They communicate magnetically. This makes it very safe.
Relay Modules
In the maker world, we rarely use the bare component. We use modules (those little boards with a blue block) that already include a transistor and a protection diode.
- VCC: 5V (usually).
- GND: Ground.
- IN: Control signal.
Connecting the Load
The blue block has 3 screw terminals:
- COM (Common): The live wire from the power source goes here.
- NO (Normally Open): The circuit is open (off) until we activate the relay. We’ll use this 99% of the time.
- NC (Normally Closed): The circuit is closed (on) until we activate the relay (used for safety systems that must work if the power goes out).
Code
For MicroPython, a relay is nothing more than a big LED. We just need to set the pin High or Low.
from machine import Pin
import time
# NOTE: Many relay modules are "Active LOW"
# (They activate with 0V and deactivate with 3.3V)
relay = Pin(26, Pin.OUT)
# Turn on (if Active Low, set to 0)
print("Turning on lamp...")
relay.value(0)
time.sleep(2)
# Turn off
print("Turning off...")
relay.value(1)
MOSFET as a High-Speed DC Switch
The relay is great, but it has two problems: it’s slow (makes a “click-clack” sound) and it wears out mechanically. If we want to control the brightness of a 12V LED strip or the speed of a powerful fan, we need a MOSFET transistor.
Unlike the relay, the MOSFET only works with Direct Current (DC), but it allows using PWM.
MOSFET Modules and Compatibility with 3.3V
Just like with relays, we’ll use modules that make it easy to connect thick wires.
- Signal (SIG): To the PWM pin of the microcontroller.
- VIN / GND: To the external power supply (e.g., 12V or 24V).
- V+ / V-: To the load (LED strip, water pump).
Logic Level If you buy bare transistors, make sure they are logic level and that the manufacturer specifies their on-resistance at a gate voltage of 2.5V or 3.3V. The popular IRF520 module is not a good choice for direct control from an ESP32: that transistor is designed for a higher gate voltage and can overheat. If the module includes a gate driver, check that its input does accept 3.3V.
from machine import Pin, PWM
import time
# Fan or LED strip on GPIO 15
# Using PWM to control the power
mosfet = PWM(Pin(15))
mosfet.freq(1000) # 1 kHz
# "Fading" effect (Breathing)
while True:
# Increase intensity
for i in range(0, 65535, 1000):
mosfet.duty_u16(i)
time.sleep_ms(10)
# Decrease intensity
for i in range(65535, 0, -1000):
mosfet.duty_u16(i)
time.sleep_ms(10)
Buzzers for Generating Sound
Sometimes a light isn’t enough and we need an audible alert. There are two types of buzzers:
Active Buzzer
It’s the easiest. It usually has a sticker on top that says “Remove seal”. It has an internal oscillator.
- Usage: Give it 3.3V, it beeps. Give it 0V, it’s silent.
- Code:
buzzer.value(1). It only produces a monotonous tone.
Passive Buzzer
It’s a small piezoelectric speaker. It doesn’t have an oscillator. If you give it a constant 3.3V, it will only make a “click” and then fall silent. It needs an oscillating signal (frequency) to sound.
- Advantage: We can control the pitch (musical notes).
from machine import Pin, PWM
import time
# Passive Buzzer on pin 18
buzzer = PWM(Pin(18))
# Musical notes (Frequencies in Hz)
notes = {
'C': 261, 'D': 294, 'E': 329, 'F': 349,
'G': 392, 'A': 440, 'B': 493
}
def play_tone(frequency, duration):
buzzer.freq(frequency)
buzzer.duty_u16(32768) # 50% duty cycle (max volume)
time.sleep(duration)
buzzer.duty_u16(0) # Silence
# Simple melody
melody = ['C', 'D', 'E', 'C']
for note in melody:
play_tone(notes[note], 0.3)
time.sleep(0.1) # Pause between notes
Solenoids for Generating Linear Motion
A Solenoid is a copper coil with a movable plunger inside. When current flows, the plunger shoots inwards (or outwards).
They are the basis of:
- Electronic locks.
- Water valves (solenoid valves).
- Pinball flippers.
Control and Protection
A solenoid is an inductive load (a giant coil). Never connect it directly to a pin. You need to use a Relay or a MOSFET to control it.
Furthermore, you must place a Flyback Diode in anti-parallel with the solenoid to absorb the reverse voltage spike generated when turning it off. (Most relay/MOSFET modules already include one).
from machine import Pin
import time
# Lock on GPIO 4 (via a MOSFET/Relay)
lock = Pin(4, Pin.OUT)
def open_door():
print("Opening...")
lock.value(1) # Activate the solenoid (retracts the latch)
# IMPORTANT: Solenoids heat up a lot.
# Never leave them activated for a long time.
time.sleep(2)
lock.value(0) # Release (the spring returns the latch)
print("Door closed.")
open_door()
Which Actuator to Choose
| Actuator | Main Use | Signal Type | Caution |
|---|---|---|---|
| Relay | Turn on bulbs, heaters (AC) | Digital (On/Off) | Slow. Do not use PWM. |
| MOSFET | LED strips, DC motors, fans | PWM (Variable) | DC only. Use “Logic Level”. |
| Active Buzzer | Simple alarms | Digital (On/Off) | Fixed annoying sound. |
| Passive Buzzer | Music, soft notifications | PWM (Frequency) | Requires generating the waveform. |
| Solenoid | Striking, opening locks, valves | Digital (Pulses) | Heats up quickly. Use a driver. |
With these components, your microcontroller is no longer just a brain in a jar; it now has hands and a voice to interact with the world.