Servo Motors SG90 - johnosbb/Automation GitHub Wiki
Servo motors generally have three pins/wires, this includes the VCC, GND, and the Signal pin. The Signal pin is the one used to feed the control signal from the microcontroller to the servo, to get the servo rotate to a particular angle.
Wiring
- VCC(Red wire) - 5V
- SIG(yellow/orange) - D8
- GND(Black/Brown) - GND
Servo directions are sent from the microcontroller to the servo motor as PWM pulses.
#include <Servo.h>
Servo servo;
int angle = 10;
void setup() {
servo.attach(8); // Use servo.attach(pin, min, max, channel); to explicitly set the PWM channel. min & max – Pulse width range in microseconds (default: 1000 to 2000).
servo.write(angle);
}
void loop()
{
// scan from 0 to 180 degrees
for(angle = 10; angle < 180; angle++)
{
servo.write(angle);
delay(15);
}
// now scan back from 180 to 0 degrees
for(angle = 180; angle > 10; angle--)
{
servo.write(angle);
delay(15);
}
}