Skip to main content

Mission8_Servo_Motor

In this mission, you will use another kind of motor, the servo motor. The angle of its arm will change with the potentiometer.

What you need

The parts you will need are all included in the Maker kit.

  • SwiftIO board
  • Shield
  • Servo
  • Potentiometer module
  • 4-pin cable

Circuit

  1. Plug the shield on top of your SwiftIO board.
  2. Connect the potentiometer module to pin A0 using a 4-pin cable.
  3. Connect the servo to the pin PWM4A as below. The servo has three wires: the ground wire is brown, the power wire is red, and the signal wire is orange.
Circuit diagram

Example code

Open the project Mission8_Servo_Motors in the folder MadExamples/Examples/MakerKit if you downloaded the folder.

Mission8_Servo_Motors.swift
import SwiftIO
import MadBoard

@main
public struct Mission8_Servo_Motor {
public static func main() {
let a0 = AnalogIn(Id.A0) // Initialize the analog pin.

// Each cycle of the signal lasts for 20 milliseconds.
// The pulse should last between 0.5 and 2.5 milliseconds to activate the servo.
// With a 0.5ms pulse, the servo will turn to 0 degrees and with a 2.5ms pulse, it will at 180 degrees.
// In between, it is at an angle between 0-180.
let servo = PWMOut(Id.PWM4A)

while true {
let value = a0.readPercentage() // Read the analog value and return a value between 0.0 and 1.0.
let pulse = Int(500 + 2000 * value) // Calculate the value to get the pulse duration.
servo.set(period: 20000, pulse: pulse) // Set the servo position according to the scaled value.

sleep(ms: 20)
}
}
}

Background

Servo motor

The servo motor can adjust its arm's position according to the signal. You can set PWM output to control it. This servo requires a pulse every 20ms. And the duration of each pulse determines the position of the arm:

How PWM drives the servo
  • If the high voltage lasts for 0.5ms, the arm will be at 0 degrees.
  • If the high voltage lasts for 1.5ms, the arm will be at 90 degrees.
  • If the high voltage lasts for 2.5ms, the arm will be at 180 degrees.
note

This servo needs the pulse to be in the range of 0.5 to 2.5ms. You may also meet other servos that need a pulse in 1 to 2ms.

Code analysis

import SwiftIO
import MadBoard

First, import the two libraries: SwiftIO and MadBoard. SwiftIO is used to control the input and output of all boards. MadBoard defines the pin name of the board.

let a0 = AnalogIn(Id.A0)
let servo = PWMOut(Id.PWM4A)

Initialize the analog pin A0 for the potentiometer and the PWM pin PWM4A for the servo.

let value = a0.readPercentage() 
let pulse = Int(500 + 2000 * value)
servo.set(period: 20000, pulse: pulse)
sleep(ms: 20)

In the dead loop, read the input value in percentage, so you get a value between 0 and 1.

You will use this method set(period:pulse:) to set the PWM.

  • The period is in microseconds, so it is 20000us.
  • The pulse should be the number between 0.5ms to 2.5ms. So the value will be matched into this range by doing some calculations.

Reference

PWMOut - set the PWM signal.

AnalogIn - read the voltage from an analog pin.

MadBoard - find the corresponding pin ids of your board.