Servo Motors

Servo Motors



  1. Discuss servos
  2. Difference between servos and DC motors
  3. Implement a servos
  4. Combine DC motors and servos


  • Short for “servomechanism
    • A servomotor is a type of servomechanism
  • Uses feedback mechanism to precisely control the position or effect of a mechanical device

Servomotor Applications

  • Hobby servos typically control angles
    • Some servos are continuous and are essentially precisely controlled DC motors with a feedback mechanism
  • Service range of typical servos are between 0 and 180 degrees
    • However, rotating to the full range (0 and 180) can damage so often we use a smaller range such as 15-165

Inside Servo Motors

alt text

Quick Note on Wire Colors

bg opacity:.75image-20210310115209181

  • The wiring colors vary with different servos so look carefully at your documentation
  • Many servos will require 5V not 3.3V so use VUSB

Wiring Diagram

Screenshot 2024-06-19 at 1.40.40 PM

Servo Class

  • The servo class is already part of the Particle OS, meaning we don’t need any special libraries to run it

  • The servo class has a constructor for a Servo object and methods to interact with the servo

Servo Code

  1. Create the Servo object

  2. Attach the Servo object to a pin

  3. Write to the Servo object

Servo Code - Creating the Servo Object

const int SERVO_PIN = D2;
//Create the servo object
Servo servoObj;
void setup(){
    //attach the servo object to the servo pin 

Servo Code - Turning the Physical Servo

void loop(){
    servoObj.write(15); //write the servo to position 15 degrees
    delay(1000); //wait one second
    servoObj.write(90); //write the servo to position 90 degrees
    servoObj.write(165); //write servo to position 165 degrees

Servo Limitations

  • Small hobby servos usually have a small plastic tab that keeps the armature from rotating past a certain point
  • Rotating beyond this point usually breaks the small plastic tab
  • This creates a continuous servo
  • Important
    • Our servos require 5v so they will not work with a LiPo battery (3.7v).
    • If you want to use a servo with a LiPo battery, see these instructions

Controlling a Servo

  • We can use a potentiometer to control a servo’s positioning
  • Potentiometers have values 0 – 4095

  • Have to scale that value to be 15 – 165

  • Use the map() function

map() Function

map(value, fromLow, fromHigh, toLow, toHigh)

// value – value we want to convert
// fromLow – input low value
// fromHigh – input high value
// toLow – output low value
// toHigh – output high value

map() Function

int potVal = analogRead(POTPIN);

int angleVal = map(potVal, 0, 4095, 15, 165);

Reminder: Photon 2 PWM Pins

  • All PWM pins on the Photon 2 are assigned to the same timer, meaning you can only generate ONE frequency at a time (you cannot make different servos move in different directions at the same time)
    • PWM pins: A2, A5, D15 (MO), D16 (MI), D1 (SCL)

Fixing Servo Jittering

  • In some cases, the servo may start to make noise, stutter, or become hot when not it use
  • This can be due to a variety of factors such as unstable current supply or interrupts in the Photon 2 execution
  • IF this happens, one simple solution is to attach before using the servo, and then detach after

Fixing Servo Jittering - Code

const int SERVO_PIN = D2;
Servo servoObj;
void setup(){ /*no attach code */ }
void loop(){
    servoObj.write(15); //write the servo to position 15 degrees
    delay(1000); //wait one second
    servoObj.write(90); //write the servo to position 90 degrees


  1. Connect a servo and write code to sweep through the entire range of value (Remember: use 15 and 165 instead of 0 and 180 to avoid damaging servo)
  2. Connect a potentiometer to control the position of the servo
  3. Using tape, connect your DC motor (with fan blade) to the servo. With the fan spinning, use the pot to control the position of the fan
  4. Now use the pot to control the speed of the fan while the servo rotates continuosly


Screenshot 2024-06-19 at 1.52.50 PM

Motor Controller Wiring Guide

Motor Controller Photon 2 DC Motor
AI2 D4 -
AI1 D3 -
AO1 - Motor wire (either)
AO2 - Motor wire (either)
VCC 3v3 -
VM 3v3 -
STBY 3v3 -