Cover image
    Activity

    Code Two micro:bits as a Servo Remote Controller

    Create a remote control to send signals to a second micro:bit, instructing a servo motor to initiate movement.

    Coding
    Get Started
    8-14+
    40:00
    micro:bit Coding Cards

    Code

    Servo Remote Controller

    00:10

    Sender

    Set the radio group to 1, to connect with the receiver. Continuously send the number value of the readings from the accelerator y axis, converted from the range -1023 to 1023 (raw data) to 0 to 100 (valid servo positions).

    let movement = 0
    radio.setGroup(1)
    basic.forever(function () {
        movement = Math.map(input.acceleration(Dimension.X), -1023, 1023, 0, 100)
        radio.sendValue("movement", movement)
    })

    Receiver

    Set the radio group to 1, to connect with the sender. Monitor the incoming radio number as receivedNumber and use it to set the Servo A position.

    radio.onReceivedValue(function (name, value) {
        if (name == "movement") {
            sb.setServoPosition(sb.servo(SBServo.ServoA), value)
        }
    })
    radio.setGroup(1)

    If using micro:bit for the first time, follow this setup.