How to Control a Servo Motor with Arduino

How to Control a Servo Motor with Arduino

Servo motors are widely used in robotics, remote control models, and automation projects due to their ability to move to precise positions. This guide explains how to control a servo motor using an Arduino, covering connections, code, and important considerations.

What You’ll Need

  • An Arduino board (e.g., Arduino Uno)
  • A servo motor (e.g., SG90 or MG996R)
  • Jumper wires
  • (Optional) An external power supply for high-power or multiple servos

 

Connecting the Servo to Arduino

Servo motors typically have three wires: signal, power, and ground. Here’s how to connect them:

For Small Servos (e.g. our 9g micro servo NP09P, compatible with SG90)

  • Signal Wire (usually yellow or orange): Connect to a digital pin on the Arduino, such as pin 9.
  • Power Wire (usually red): Connect to the Arduino’s 5V pin.
  • Ground Wire (usually black or brown): Connect to the Arduino’s GND pin.

 

Small servos like the SG90 can be powered directly from the Arduino’s 5V output, which provides up to 500mA via USB or 1A via a barrel jack.

For High-Power or Multiple Servos (e.g., our mental servos 9kgs NP900M or 15kgs NP150M, compatible MG996R)

  • Signal Wire: Connect to a digital pin (e.g., pin 9).
  • Power Wire: Connect to an external 5V–6V power supply (check your servo’s specifications).
  • Ground Wire: Connect to both the external power supply’s ground and the Arduino’s GND to ensure a common ground.

High-power servos draw more current (e.g., 650mA at stall for MG996R), so an external power supply is essential to avoid damaging the Arduino.

Servo Specifications

Here’s a quick reference for two common servo motors:

Model

Stall Torque

Speed

Size (LxWxH)

Motor Type

Gear

Weight

9g micro servo NP09P

1.6kg-cm/4.8V  1.8kg-cm/6.0V 

0.10sec/60°/4.8V

0.09sec/60°/6.0V

22.4mm*12.5mm*23mm

DC Motor

POM 9g

9kg standard servo NP900M

9.6kg-cm/4.8V

10.5kg-cm/6.0V

0.14sec/60°/4.8V

0.13sec/60°/6.0V

240*20*37.5mm

DC Motor

Mental 58g

 

Writing the Control Code

Arduino uses PWM (Pulse Width Modulation) signals to control servo motors, where the pulse width (typically 0.5ms to 2.5ms) determines the position (0° to 180°). The easiest way to generate these signals is with the built-in Servo library.

 

Basic Sweep Example

This code makes the servo sweep from 0° to 180° and back:

  • #include <Servo.h>: Imports the Servo library.
  • myservo.attach(9): Links the servo to pin 9.
  • myservo.write(angle): Sets the servo to a specific angle (0° to 180°).
  • delay(15): Controls the speed of movement; adjust for faster or slower sweeps.

Key Considerations

  1. Power Supply:
    • Small servos work fine with Arduino’s 5V pin.
    • For high-power servos or multiple servos, use an external power supply to handle the current demand.
  2. Pulse Width Adjustment:
    • Some servos may need custom pulse widths for full range. Use myservo.attach(pin, minPulse, maxPulse) (e.g., 600µs for 0°, 2300µs for 180°) if the default 0.5ms–2.5ms range isn’t accurate.
  3. Controlling Multiple Servos:
    • The Arduino Uno can control up to 12 servos using the Servo library. For more, consider a PCA9685 PWM driver, which supports 16 servos per board via I2C.

 

Advanced Example: Potentiometer Control

You can use a potentiometer to adjust the servo position interactively:

  • Connect the potentiometer’s ends to 5V and GND, and the middle pin to A0.

Conclusion

Controlling a servo motor with Arduino is straightforward with the Servo library and proper connections. Whether you’re sweeping a single servo or building a multi-servo project, ensure your power supply matches your servo’s needs. Experiment with the code examples above to get started!

 

Back to blog

Leave a comment