No. 13 – Control a servo

Task: A servo has to turn to three different positions. Between the movements there should be a short break.

Required equipment: Arduino / one servo / three cables

Setup:

Ser1

Code:


#include <Servo.h> //Include the servo library

Servo servoblue; //The servo gets the name “servoblue”

void setup()

{

servoblue.attach(8); //The signal line of the servo is on pin 8

}

void loop()

{

servoblue.write(0); //Position 1 with an angle of 0°

delay(3000); //Wait 3 seconds

servoblue.write(90); //Position 2 with an angle of 90°

delay(3000); //Wait 3 seconds

servoblue.write(180); //Position 3 with an angle of 180°

delay(3000); //Wait 3 seconds

servoblue.write(20); //Position 4 with an angle of 20°

delay(3000); //Wait 3 seconds

}