Controlling a servo using the original Netduino. Repository created for educational purposes.
Software:
Parts:
- Netduino 1
- One (Two For This Tutorial) TowerPro SG51R Servos
- One Standard Breadboard
- Eight Wires
Servo API:
public class Servo
{
protected PWM pwmServo { get; set; }
protected uint Position { get; set; }
public Servo(Cpu.PWMChannel pin)
{
pwmServo = new PWM(pin, 20000, Position, PWM.ScaleFactor.Microseconds, false);
SetPositionStraight();
MoveServoLeft();
}
public void SetPositionStraight()
{
Position = 1500;
}
public void Start()
{
pwmServo.Start();
}
public void MoveServoLeft()
{
pwmServo.Duration = 750;
}
public void MoveServoRight()
{
pwmServo.Duration = 2250;
}
}
Program:
Servo servo = new Servo(PWMChannels.PWM_PIN_D6);
servo.Start();
Servo servo2 = new Servo(PWMChannels.PWM_PIN_D9);
servo2.Start();
InputPort button = new InputPort(Pins.ONBOARD_BTN, false, Port.ResistorMode.Disabled);
int pos = 0;
while (true)
{
if (button.Read())
{
if (pos == 0)
{
servo.MoveServoLeft();
servo2.MoveServoLeft();
pos = 1;
}
else if (pos == 1)
{
servo.MoveServoRight();
servo2.MoveServoRight();
pos = 0;
}
}
}