/ / navigation


Servos
Skeleton
Challenge 1
Challenge 2
Inspiration
NYS Standards

Servo Code



Objective:

You want to have 3 potentiometers control 3 servos.

Here is how to connect the components to an Arduino and code:

  1. Connect pin 9 to one of your servo's yellow wires.


  2. Connect a potentiometer to analog pin 0.


  3. To control a servo:
    #include <Servo.h> 
    
    Servo myservo1;  // create servo object to control a servo 
    
    int potpin1 = 0;  // analog pin used to connect the potentiometer
    
    int val1;    // variable to read the value from the analog pin 
    
    void setup() { 
      myservo1.attach(9);  // attaches the servo on pin 9 to the servo object
      
    } 
    
    void loop() { 
      val1 = analogRead(potpin1);   // reads the value of the potentiometer 
      val1 = map(val, 0, 1023, 0, 179);
      myservo1.write(val1);    // sets the servo position according to the scaled value 
      delay(15);              // waits for the servo to get there 
    } 
    
    



  4. Complete the code for three servos and upload the code.


 
 $("p:not(p:eq(2))").css("border","2px solid pink").css("padding", ".5em");