Approved content

The content below is provided by a partner.

jdarling/pxt-pca9685 0.0.5 GitHub

micro:bit PCA9685 driver

This is an extremely generic PCA9685 driver for the BBC micro:bit and Microsoft MakeCode Editor.

Supported LED, Servo, Continuous Rotation Servo and direct manipulation of the PCA9685.

pca9685_01

Adding to Project

From within MakeCode select the “Advanced” section. Click on “+ Extensions” in the “Search or enter project URL…” section enter “jdarling/pxt-pca9685” or simply “PCA9685”.

002 003

001

Constants

PinNum

ServoNum

LEDNum

Servos

setServoPosition(servoNum, degrees, chipAddress)

Options:

setCRServoPosition(servoNum, speed, chipAddress)

Options:

setServoLimits(servoNum, minTimeCs, maxTimeCs, midTimeCs, chipAddress)

NOTE: If you read online you will most likely find that your servos specifications rate the min and max as milliseconds and this library requires centiseconds. To go from milliseconds to centiseconds multiple by 10. So 0.5 milliseconds equals 5 centiseconds, 2.5 milliseconds equals 25 centiseconds.

Options:

LED’s

setLedDutyCycle(ledNum, dutyCycle, chipAddress)

Options:

Direct Pin

setPinPulseRange(pinNum, onStep, offStep, chipAddress)

NOTE: Direct Pin control is done using index’s 0-15 NOT 1-16 like the LED and Servo helpers above.

Options:

General

init(chipAddress, frequency)

Changes the chip frequency to the value passed in between 40Hz and 1000Hz then performs a soft reset on the PCA9685 and sets the output states to all of the outputs to off. You will need to set your output values again if you decide to initialize the chip after you have set a particular output value.

reset(chipAddress)

Maintains the existing chip frequency setup and performs a chip init, see init(chipAddress, frequency) for more details.

Example Usage

microbit-screenshot

let controller = 0
input.onButtonPressed(Button.A, () => {
    PCA9685.setServoPosition(PCA9685.ServoNum.Servo1, 0, controller)
    basic.showString("A")
})
input.onButtonPressed(Button.B, () => {
    PCA9685.setServoPosition(PCA9685.ServoNum.Servo1, 180, controller)
    basic.showString("B")
})
input.onButtonPressed(Button.AB, () => {
    PCA9685.setServoPosition(PCA9685.ServoNum.Servo1, 90, controller)
    basic.showString("C")
})
controller = PCA9685.chipAddress("0x40")
basic.showNumber(controller)
PCA9685.init(controller, 60)

License

MIT

Supported targets

pxt-pca9685=github:jdarling/pxt-pca9685
pca9685=github:jdarling/pxt-pca9685#v0.0.5