Learn / Sensors and actuators / Relays and servos

Relays and Servos for Beginners

ReviewedBeginnerFreeAbout 6 min
BME280
In short

Relays and Servos for Beginners: the short answer

A relay is an electrically controlled switch. A small coil current pulls a metal contact across, so a low-voltage signal can switch a completely separate circuit. Example: Separate servo power with a shared ground stops the resets. The signal wire is the only connection the servo needs from the Arduino.

Level
Beginner
Time
About 6 min
Topic
Sensors and actuators
You will learn to

Switch a separate circuit with a relay module and position a hobby servo, powering each correctly.

Before this: Digital Inputs and Outputs, Using a Transistor as a Switch

A relay is an electrically controlled switch. A small coil current pulls a metal contact across, so a low-voltage signal can switch a completely separate circuit. Relay modules for Arduino include the transistor, flyback diode and often an optocoupler, so you only connect VCC, GND and IN. The output side has three screw terminals: COM (common), NO (normally open, connected to COM only when the relay is on) and NC (normally closed, connected when it is off). Check whether your module is active-low (it switches on when IN is LOW), which is common.

Relay contacts have ratings such as 10 A at 250 V AC, but cheap modules and their PCB tracks are not reliably safe for mains, and mains wiring must be done by a trained person in a proper enclosure. For learning, switch a 12 V lamp, fan or pump. A relay clicks and wears out after many switching cycles, so for frequent switching or PWM use a MOSFET instead.

A hobby servo such as the SG90 contains a small motor, a gearbox and a position sensor. It turns to an angle (about 0 to 180°) set by a pulse sent every 20 ms: around 1 ms for one end, 1.5 ms for the middle and 2 ms for the other end. The Arduino Servo library handles this with myservo.write(90). Servos draw sudden current spikes of several hundred milliamps when moving, more for the MG996R, so power them from a separate 5 V supply rated at least 1 A per servo, and connect that supply's ground to the microcontroller's ground.

What is inside a relay module

A bare relay has a coil that needs 30 to 90 mA at 5 V, far more than a pin can supply. The module adds a transistor to switch the coil, a flyback diode across it, an LED, and often an optocoupler that separates the microcontroller from the coil supply. Many modules have a jumper to share or split the supply (JD-VCC); remove it and give the relay its own 5 V when you want full isolation.

Choose NO (normally open) for something that should be off when the controller is off or crashes, and NC when it should be on by default. Think about the safe state before wiring, for example a heater must be off if the board resets.

Limits of relay contacts

A contact rating of 10 A at 250 V AC is for a resistive load. A motor, a lamp or a transformer can draw several times that when switching on, and an inductive load arcs across the contacts when it opens. Derate to about half, and add a snubber or a diode for DC coils and motors. Solid-state relays have no moving parts and switch silently, but some leak a little current when off.

Relay switching is slow, around 5 to 10 ms, and wears with use, typically 100,000 operations under load. Do not use them for PWM; use a MOSFET or a motor driver.

How a servo is controlled

A servo compares the pulse it receives with its position sensor and drives its motor until they agree. The pulse repeats about every 20 ms; the pulse width of 1 to 2 ms sets the angle. Real servos differ: many SG90 clones range from 0.5 to 2.4 ms for a bigger swing, so calibrate with the Servo library's attach(pin, min, max) rather than trusting 0 to 180.

A servo holding a load draws current all the time and can buzz or get hot. Detach it when it does not need to hold position, or use a servo with a metal gear for loads.

Power and wiring

A servo's stall current can reach 700 mA for an SG90 and over 2 A for an MG996R. A USB port or the board's 5 V pin cannot give that, and the voltage dip resets the microcontroller. Use a separate 5 V supply, add a 470 to 1000 µF capacitor across the servo supply, and join the grounds.

On an ESP32 the servo signal is 3.3 V, which most servos accept. Use the ESP32Servo library, and choose pins that are not input-only.

Common mistakes
  • Powering a servo from the board's 5 V pin and getting random resets.
  • Wiring a load to NC and NO the wrong way round, so it is on when it should be off.
  • Switching mains with a cheap relay module in an open box.
Worked example

The Arduino resets every time the servo moves

  1. The SG90 is powered from the Arduino's 5 V pin, which comes from USB.
  2. A moving servo can draw spikes of several hundred milliamps, and more when it stalls.
  3. USB voltage dips and the Arduino browns out and resets.
  4. Fix: power the servo from a 5 V 2 A supply, join the grounds, and add a 470 µF capacitor across the servo supply.

Result. Separate servo power with a shared ground stops the resets. The signal wire is the only connection the servo needs from the Arduino.

Try it

Use a relay module to switch a 12 V DC fan on when a button is pressed. Then attach an SG90 servo to a potentiometer so that turning the knob moves the servo (the Knob example), powering the servo from a separate 5 V supply.

Check yourself

3 quick questions

  1. Which relay terminal is connected to COM when the relay is off?

  2. What pulse width centres a typical servo?

  3. Why power servos from a separate supply?

Common questions

What will I learn in "Relays and Servos for Beginners"?

Switch a separate circuit with a relay module and position a hobby servo, powering each correctly.

How do I the Arduino resets every time the servo moves?

The SG90 is powered from the Arduino's 5 V pin, which comes from USB. A moving servo can draw spikes of several hundred milliamps, and more when it stalls. USB voltage dips and the Arduino browns out and resets. Fix: power the servo from a 5 V 2 A supply, join the grounds, and add a 470 µF capacitor across the servo supply. Separate servo power with a shared ground stops the resets. The signal wire is the only connection the servo needs from the Arduino.

Which relay terminal is connected to COM when the relay is off?

NC. NC means normally closed: connected when the coil is off.

What pulse width centres a typical servo?

About 1.5 ms. 1.5 ms is the middle; 20 ms is the repeat period.

Why power servos from a separate supply?

Their current spikes can brown out the microcontroller. Moving servos draw large, sudden currents.