Lesson 85: Introduction to Servo Motors | Arduino Step By Step Course
This project guide introduces you to the world of continuous rotation servo motors, also known as 360-degree servos. Unlike standard servos that move to a specific angle (like 0 to 180 degrees), these motors spin continuously in either direction. This makes them ideal for building small robots, tank treads, or any project requiring simple, affordable drive motors. This guide will show you how to control one using an Arduino and a few push buttons, giving you hands-on control over its direction and speed.
Here are a few practical applications for a 360-degree servo:
- Building a small two-wheeled robot car.
- Creating a pan-and-tilt camera mount that rotates endlessly.
- Driving a small conveyor belt or winch.
- Making a simple radar or sensor scanning platform.

Hardware and Components
For this project, you'll need the following components:
- Arduino board (e.g., Uno)
- Continuous rotation servo motor (e.g., FS90R or similar)
- Three push buttons
- Jumper wires
- Breadboard (optional, for easier wiring)
Wiring Guide
The wiring for this project is straightforward. The servo motor has three wires: typically brown (ground), red (power), and orange or yellow (signal). The push buttons are wired in a simple configuration.
Here is a breakdown of the connections:
- Servo Motor:
- Brown wire (Ground) connects to the Arduino's GND pin.
- Red wire (Power) connects to the Arduino's 5V pin.
- Orange/Yellow wire (Signal) connects to Arduino pin 9.
- Push Buttons:
- One side of each button is connected to the Arduino's GND pin.
- The other side of the "Counter-Clockwise" button connects to pin 2.
- The other side of the "Stop" button connects to pin 3.
- The other side of the "Clockwise" button connects to pin 4.
In the video, the creator emphasizes that the servo's signal wire must be connected to a PWM-capable pin on the Arduino, which is often marked with a tilde (~) symbol. Pin 9 is one of these pins. The push buttons use the Arduino's internal pull-up resistors, which simplifies the wiring by eliminating the need for external resistors (in video at 07:02).
Code Explanation
The code is designed to read the state of three push buttons and send the corresponding command to the servo motor. Let's break down the user-configurable parts.
Configuring Pins and Commands
At the top of the code, you'll find the definitions for the pins and the servo control values. This is where you can customize the behavior of your project.
const int servoPin = 9; // PWM pin for the servo signal
const int stopPin = 3; // Pin for the stop button
const int cwPin = 4; // Pin for the clockwise button
const int ccwPin = 2; // Pin for the counter-clockwise button
These constants define which Arduino pins are connected to the buttons and the servo. If you use different pins for your buttons, you would change the numbers here. The servoPin must be a PWM-capable pin.
const int csServoCommand[3] = {106, 52, 0}; // {CCW, STOP, CW}
const String csServoCommandText[3] = {"Counter Clockwise", "Stopped", "Clockwise"};
This is the most important part to understand and tune. The csServoCommand array holds the values sent to the servo. For a continuous servo, these values don't represent an angle but a speed and direction.
- 106 is the value for full-speed Counter-Clockwise rotation.
- 52 is the value for a full stop.
- 0 is the value for full-speed Clockwise rotation.
These values are not universal. Due to manufacturing differences, the "stop" value (52) may need to be adjusted. If your servo drifts slowly when you press the stop button, you'll need to fine-tune this value. The video demonstrates this process, showing how changing the value to 54 or 51 can affect the motor's behavior (in video at 13:45).
You can also adjust the values for rotation to change the motor's speed. For example, setting the counter-clockwise value to 70 will make it rotate slower than at 106 (in video at 12:23).
The csServoCommandText array holds the text strings that are printed to the Serial Monitor to show the current command state.
Custom Function: servoCommand()
The code uses a custom function to handle the command logic. This function takes a number (0, 1, or 2) as an argument, updates the status text, and sends the corresponding command to the servo.
void servoCommand(int n) {
statusText = csServoCommandText[n];
myservo.write(csServoCommand[n]);
Serial.println(csServoCommandText[n]);
Serial.println(csServoCommand[n]);
}
This function is called from the main loop when a button is pressed. It centralizes the logic for updating the display and controlling the servo, making the code cleaner and easier to modify.
Live Project and Demonstration
In the video, the creator demonstrates the project in action. Pressing the "Clockwise" button sends the value 0 to the servo, causing it to spin in one direction. Pressing the "Counter-Clockwise" button sends the value 106, making it spin the other way. The "Stop" button sends 52, which should halt the motor.
The Serial Monitor is used to display the current state and the exact value being sent to the servo. This is invaluable for debugging and for finding the perfect "stop" value for your specific motor. As noted in the video, the stop command may not be perfect due to the internal circuitry of the servo, and you might see a slow drift or vibration. This is a common characteristic of cheaper continuous servos and can be minimized by tuning the stop value in the code (in video at 01:15).
Chapters
- [00:05] Introduction to the 360-degree servo motor
- [02:02] Components and hardware overview
- [02:40] Detailed wiring diagram and connections
- [05:07] Code explanation: constants, arrays, and setup
- [09:56] Code explanation: the servoCommand function
- [10:54] Live demonstration and Serial Monitor output
- [12:23] Adjusting servo speed and stop values
/*
*
* Demonstration of Controlling Continuous Servo (360 servo)
* This code allows you to control a 360-degree servo by a command from the Serial Monitor.
* this 350 servo motor is a just for fun. it can't be used for real application where you need exact positioning. get stepper motor or expensive servo motor.
📚⬇️ Download and resource page https://robojax.com/RJT762
*
* Modified by Ahmad Shamshiri for Robojax.com
* on Sunday, July 1, 2018, at 11:09 AM in Ajax, Ontario, Canada
* Watch a video instruction of this project: https://youtu.be/b_xvu6wWafA
* Get this code from Robojax.com
*
Original code by BARRAGAN <http://barraganstudio.com>
This example code is in the public domain.
Modified 8 Nov 2013
by Scott Fitzgerald
http://www.arduino.cc/en/Tutorial/Sweep
*/
#include <Servo.h>
Servo myservo; // create servo object to control a servo
// twelve servo objects can be created on most boards
int pos = 0; // variable to store the servo position
int incomingByte = 0; // for incoming serial data
void setup() {
Serial.begin(9600);
myservo.attach(9); // attaches the servo on pin 9 to the servo object
}
void loop() {
// send data only when you receive data:
if (Serial.available() > 0) {
// read the incoming byte:
incomingByte = Serial.read();
// say what you got:
Serial.print("received: ");
Serial.print (incomingByte);
if(incomingByte == 108){
Serial.println(" sent 0 Rotating CW ");
myservo.write(0);
}else if(incomingByte == 114){
Serial.println(" sent 180 Rotating CCW ");
myservo.write(180);
}else if(incomingByte == 60){
Serial.println(" sent Stopped ");
myservo.write(60);
}else{
Serial.println(" moving Randomly");
myservo.write(incomingByte);
}
}
}
/*
*
* Demonstration of Controlling Continuous Servo (360 servo)
360 Servo -2
* This code allows you to control a 360-degree servo by a command from the Serial Monitor.
* this 350 servo motor is a just for fun. it can't be used for real application where you need exact positioning. get stepper motor or expensive servo motor.
📚⬇️ Download and resource page https://robojax.com/RJT762
*
* Modified by Ahmad Shamshiri for Robojax.com
* on Sunday, July 1, 2018, at 11:09 AM in Ajax, Ontario, Canada
* Watch a video instruction of this project: https://youtu.be/b_xvu6wWafA
* Get this code from Robojax.com
*
Original code by BARRAGAN <http://barraganstudio.com>
This example code is in the public domain.
Modified 8 Nov 2013
by Scott Fitzgerald
http://www.arduino.cc/en/Tutorial/Sweep
*/
#include <Servo.h>
Servo myservo; // create servo object to control a servo
// twelve servo objects can be created on most boards
int servoPin = 9;// this pin must be of those with PWM ~
int pos = 0; // variable to store the servo position
int incomingByte = 0; // for incoming serial data
int CWBS, CCWBS, SBS;
void setup() {
Serial.begin(9600);
pinMode(2,INPUT_PULLUP);// set pin for push button STOP
pinMode(3,INPUT_PULLUP);// set pin for push button CCW
pinMode(4,INPUT_PULLUP);// set pin for push button CW
myservo.attach(servoPin); // attaches the servo on pin 9 to the servo object
}
void loop() {
CCWBS = digitalRead(2);// read status of button CCW
SBS = digitalRead(3);// read status of button STOP
CWBS = digitalRead(4);// read status of button CW
if(CCWBS == LOW){
Serial.println(" sent 0 Rotaing CW ");
myservo.write(0);
}else if(CWBS == LOW){
Serial.println(" sent 180 Rotaing CCW ");
myservo.write(180);
}else if(SBS == LOW){
Serial.println(" sent Stopped ");
myservo.write(60);
}else{
Serial.println(" moving Random");
myservo.write(48);
}
}// loop
Things you might need
-
Amazon360 Degree Servo motor on Amazonlink.amazon
-
Amazon
-
eBay360 Degree Servo motor on eBayebay.us
-
AliExpress360 Degree Servo motor on AliExpresss.click.aliexpress.com
-
AliExpress360 Degree Servo motor on AliExpress-2s.click.aliexpress.com
Resources & references
Files📁
Other files
-
SG90 Servo motor datasheet
robojax-servo-SG90_datasheet.pdf0.12 MB