Hackster is hosting Hackster Holidays, Ep. 6: Livestream & Giveaway Drawing. Watch previous episodes or stream live on Monday!Stream Hackster Holidays, Ep. 6 on Monday!
Philipp Henkel
Published © MIT

Lego® Power Functions IR Sender For Micro:bit

Control your Lego® Power Functions motors using your micro:bit, an infrared LED and MakeCode.

BeginnerFull instructions provided2 hours16,509
Lego® Power Functions IR Sender For Micro:bit

Things used in this project

Hardware components

BBC micro:bit board
BBC micro:bit board
×1
Infrared LED
High Power Infrared LED, 940 nm, e.g. SFH 4546 or TSAL 6200
×1
LEGO® 8884 - Power Function IR Receiver
×1

Software apps and online services

MakeCode
Microsoft MakeCode
pxt-powerfunctions
MakeCode package that provides Power Functions functionality on micro:bit devices
MakeCode Project
MakeCode project template with integrated Power Functions package

Story

Read more

Code

JavaScript code example MakeCode

JavaScript
Use IR LED on P0.
Change direction of the motor on output blue on channel 1.
Plot light level
Stop train for 5 seconds if the light level is above a threshold.
Move forward for 3 seconds till next light level check.
powerfunctions.useIrLedPin(AnalogPin.P0)

powerfunctions.setMotorDirection(
  PowerFunctionsMotor.Blue1, 
  PowerFunctionsDirection.Backward
)
  
input.onButtonPressed(Button.A, () => {
    powerfunctions.moveForward(PowerFunctionsMotor.Blue1)
})

input.onButtonPressed(Button.B, () => {
    powerfunctions.float(PowerFunctionsMotor.Blue1)
})

basic.forever(() => {
    led.plotBarGraph(input.lightLevel(), 255)
    
    if (input.lightLevel() > 180) {
        powerfunctions.float(PowerFunctionsMotor.Blue1)
        basic.pause(5000)
        powerfunctions.setSpeed(PowerFunctionsMotor.Blue1, 2)
        basic.pause(3000)
    }
})

Credits

Philipp Henkel

Philipp Henkel

1 project • 10 followers

Comments