pxt-calliope/docs/reference/pins/servo-write-pin.md
2016-04-13 08:27:45 -07:00

1.3 KiB

Servo Write Pin

Writes a value to the servo on to the specified pin (P0, P1, P2), controlling the shaft accordingly.

  • on a standard servo, this will set the angle of the shaft (in degrees), moving the shaft to that orientation.
  • on a continuous rotation servo, this will set the speed of the servo (with 0 being full-speed in one direction, 180 being full speed in the other, and a value near 90 being no movement).
pins.servoWritePin(AnalogPin.P0, 180)

Parameters

  • name - String; the pin name ("P0", "P1", or "P2")
  • value - a Number between 0 and 180 included

Examples

  • setting the shaft angle to mid point on a servo
pins.servoWritePin(AnalogPin.P0, 90)
  • control the shaft by using the tilt information of the accelerometer
basic.forever(() => {
    let millig = input.acceleration(Dimensions.X)
    // map accelerometer readings to angle
    let angle = pins.map(millig, -1023, 1023, 0, 180)
    pins.servoWritePin(AnalogPin.P0, angle)
})
  • setting the full speed on a continuous servo
pins.servoWritePin(AnalogPin.P0, 0)

See also

BBC micro:bit pins, servo set pulse