Servo Motors

Servo Motors

image-20200323184116654

Goals

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

Servo

  • 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

servo_bb

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 
    servoObj.attach(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
    delay(1000);
    servoObj.write(165); //write servo to position 165 degrees
    delay(1000);
}

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: Argon PWM Pin Groups

  • PWM pins are assigned to one of three groups
  • Each group can have different PWM values (duty cycles), but must share the same frequency and resolution
    • Pins D4, D5, D6, D8
    • Pins A0, A1, A2, A3
    • Pins D2, D3, A4, A5

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 Argon 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.attach(SERVO_PIN);
    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.detach();
}

Exercise

  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

Wiring

servo_dc_fan_controller_bb

Motor Controller Wiring Guide

Motor Controller Argon DC Motor
PWMA D5 -
AI2 D4 -
AI1 D3 -
AO1 - Motor wire (either)
AO2 - Motor wire (either)
VCC 3v3 -
GND GND -
VM 3v3 -
STBY 3v3 -

Credits

Updated: