Arduino / learning · August 27, 2022 0

Servo Motors

In aruduino, servo motors are motors that can rotate a total of 160 degrees. Using gears and something on top of the motor, servo motors can rotate around at mildly fast speeds.

The yellow wire goes to a GPIO pin. The red wire goes to 5v. The black wire goes to GND.

#include <Servo.h>

Servo myservo;

int pos = 0, pin = 9;  // The pin variable should be the GPIO pin your servo motor is connected to.

void setup() {
  myservo.attach(pin);
}

void loop() {
  for (pos = 0; pos <= 180; pos += 1) {
    myservo.write(pos);
    delay(15);
  }
  for (pos = 180; pos >= 0; pos -= 1) {
    myservo.write(pos);
    delay(15);
  }
}