Cover image
    Activity

    Code a micro:bit to Move in the Dark

    When the micro:bit detects darkness, the servo motor springs into motion.

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

    Code

    Move in the Dark

    00:20

    Continuously set the position of the Servo A to the value of the light sensor, constrained from 0 to 100 (since the light level goes up to 255).

    basic.forever(function () {
        sb.setServoPosition(sb.servo(SBServo.ServoA), Math.constrain(input.lightLevel(), 0, 100))
    })

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