Controlling a servo

A servo is a small rotary motor. From a neutral (0) position it can rotate +90 and -90 degrees. You can connect things to the servo in order to make them move.

Parts

  • SG90 Micro-Servo

  • 3 jumper wires

Wire the circuit

The servo has a connector to which you will connect three jumper wires. The connectors are for power, ground and signal. This chart shows how the wires on the servo connector map to GPIO pins:

Create the Xojo app

You'll use the open-source Xojo GPIO library to control the servo. This library has the GPIO.Servo module which contains methods you can call to easily set the servo positions. This code moves the servo to its neutral and the left (-90) and right (90) positions:

GPIO.SetupGPIO

Const kServoPin = 6 ' Change to the pin you used
Var servo As New GPIO.Servo(kServoPin)

If servo.ErrorCode = 0 Then
  servo.Neutral
  App.DoEvents(500)
  servo.Left
  App.DoEvents(500)
  servo.Right
  App.DoEvents(500)
  servo.Cleanup
End If

Transfer the built app to the Pi to test it out.