Controlling a standard hobby servo using an Arduino is one of the most beginner-friendly and versatile intro projects in electronics. The guide walks you through wiring a servo and, optionally, integrating an IR remote control receiver to set angles via remote commands
What You’ll Need
Components Required for Arduino with Servo Control system
A list of the required components is given below. Make sure that the components are working properly and of the correct values.
- Arduino UNO
- TSOP IR reciever
- Servo motor
- IR remote
- LEDs of three different colors
- 16×2 LCD
- 10K potentiometer
- Jumper wires and a breadboard
- 220 ohms resistors
- USB cable for uploading the code
- Connect the signal wire (usually orange or yellow) to any PWM-capable digital pin, like D3
- Servo Motor:Connect the servo’s red wire (VCC) to Arduino 5 VConnect the brown/black wire (GND) to Arduino GNDConnect the signal wire (usually orange or yellow) to any PWM-capable digital pin, like D
IR Receiver (if using):
Connect its VCC to 5 V, GND to GND, and output to Arduino D4
- Connect its VCC to 5 V, GND to GND, and output to Arduino D4
- IR Receiver (if using):Connect its VCC to 5 V, GND to GND, and output to Arduino D4
cpp
CopyEdit
#include <Servo.h>
Servo myServo;
void setup() {
myServo.attach(3); // Attach servo signal to pin 3
}
void loop(){
myServo.write(0);
delay(2000);
myServo.write(90);
delay(2000);
myServo.write(180);
delay(2000);
myServo.write(90);
delay(2000);
}
cpp
CopyEdit
#include <Servo.h>
Servo myServo;
void setup() {
myServo.attach(3); // Attach servo signal to pin 3
}
void loop(){
myServo.write(0);
delay(2000);
myServo.write(90);
delay(2000);
myServo.write(180);
delay(2000);
myServo.write(90);
delay(2000);
}
This code moves the servo sequentially between 0°, 90°, and 180° with 2-second pauses, demonstrating full-range movement
Adding IR Remote ControlUse the IRremote.h
library to decode signal values and map them to servo positions. In the Techatronic tutorial, specific remote codes trigger servo angles like 0°, 60°, 90°, 120°, or 180°.
The Arduino’s Servo
library generates PWM signals (~50 Hz, repeating every ~20 ms), where the pulse width (typically 1 ms = 0° to 2 ms = 180°) determines the servo arm’s position. The neutral (90°) position is around 1.5 ms
Use a Stable Power Source: Servos often draw up to 700 mA under stall conditions. If powering from Arduino, issues like jitter can appear. It’s safest to use a dedicated 5 V supply and ensure shared ground between servo and Arduino
- Use a Stable Power Source: Servos often draw up to 700 mA under stall conditions. If powering from Arduino, issues like jitter can appear. It’s safest to use a dedicated 5 V supply and ensure shared ground between servo and Arduino
Reduce Jitter with Delay Adjustment: The typical sweep code includes a delay(15)
per angle step. Many users found that reducing it to ~5 ms improved smoothness—and that shorter delays than the servo can handle can cause pauses
- Reduce Jitter with Delay Adjustment: The typical sweep code includes a
delay(15)
per angle step. Many users found that reducing it to ~5 ms improved smoothness—and that shorter delays than the servo can handle can cause pauses
Detach Signal When Idle: To stop unnecessary jitter, use servo.detach()
once movement is complete, then reattach before the next motion. This is especially helpful if the servo isn’t loaded
- Detach Signal When Idle: To stop unnecessary jitter, use
servo.detach()
once movement is complete, then reattach before the next motion. This is especially helpful if the servo isn’t loaded
Robotics arms, pan/tilt camera mounts, simple RC vehicles, automation labs
- Robotics arms, pan/tilt camera mounts, simple RC vehicles, automation labs
Combine with sensors for servo-based feedback (e.g. pot to analogRead → servo angle mapping)
- Combine with sensors for servo-based feedback (e.g. pot to analogRead → servo angle mapping)
Controlling a servo with Arduino involves just three wires and the built-in Servo library. You can sweep, hold, or position the servo precisely in the 0–180° range using write()
or writeMicroseconds()
. Adding an optional IR remote allows for interactive control. Key considerations: proper power supply, shared ground, and delay tuning help ensure smooth and reliable operation.
Comments