Arduino101 ServoControl

From Twin Cities Maker wiki
Jump to: navigation, search

The servo is a (somewhat) precisely positionable motor. Pulses sent from the Arduino down the control wire will swing the servo's arm to a set position, depending on the pulse width. Sounds messy, right? Actually it's pretty simple, thanks to the Servo library, which does all the heavy lifting for you!

Note: The wire colors in the diagram don't match the actual servo in your kit. The wires map out like so:

  • Diagram black = servo brown = ground
  • Diagram red = servo red = +5V
  • Diagram yellow = servo orange = control signal

Breadboard layout

Servo Control bb.png


#include <Servo.h>

int ledPin = 11;
int potPin = 0;

int brt = 0;
int angle = 0;
Servo myservo;

void setup() {
  // put your setup code here, to run once:
  pinMode(ledPin, OUTPUT);

void loop() {
  // put your main code here, to run repeatedly: 
  brt = map(analogRead(potPin), 0, 1023, 0, 255);
  angle = map(analogRead(potPin), 0, 1023, 0, 180);
  brt = constrain(brt, 0, 255);
  angle = constrain(angle, 0, 180);
  analogWrite(ledPin, brt);