180 Degree Servo Motor Template

This example uses a potentiometer to control the rotation of a 180 degree servo.

What you will need:

  • Arduino and Breadboard (available for Checkout)
  • Potentiometer - Purchase - DM Store
  • 180 Deg Servo Motor - Purchase - DM Store
  • Two 100UF Capacitors - Purchase - DM Store

Arduino Code - Cut and Paste:

//Imports Servo library code//
#include

//Creates an object for the named servo//
Servo myServo;

//Creates a named constant for the arduino control pins//
int const potPin = A0;
int potVal; int angle;

void setup() {

//tells Arduino what pin the servo is attached to//
myServo.attach(9);

}

Serial.begin(9600);

//This allows you to read the analog input and print out the value to the serial monitor//

void loop() {
potVal = analogRead
(potPin); Serial.print(“potVal: “);
Serial.print(potVal);

//This function allows you to scale the potentiometer value of 0 to 1023 to the servo value range of 0 to 179 degrees//
angle = map(potVal, 0, 1023, 0,179);

//this code write the values to the Serial Monitor//
Serial.print(“, angle: “);
Serial.println(angle);
myServo.write(angle);
delay(15);

}