B4R Library Lego Power Functions IR Control


for those who like to play with Lego Power Functions, find attached the rPowerFunctions Library.
The library is wrapped based upon this PowerFunctions library.
It enables to control elements connected to the Lego Power Functions IR receiver, via an Arduino with an IR Transmitter.
The library has been tested on
  1. a MEGA with a Sunfounder IR Transmitter (connected to PWM12) controlling a motor, via IR channel 4, connected to the blue side of the IR receiver.
  2. a NodeMCU ESP12 Module with a Sunfounder IR Transmitter (connected to D3=GPIO0 - only this pin can be used) (same setup as 1)
'    IRSend ---- Arduino    Wirecolor
'    GND ------- GND        black
'    Signal ---- PWM 12    yellow for Arduino
'    Signal ---- Pin D3      yellow for NodeMCU ESP12
Sub Process_Globals
   Public Serial1 As Serial
   Private pf As PowerFunctions
   Private Timer1 As Timer
   Private Cnt As UInt = 0
   Private start As Boolean = False
End Sub

Private Sub AppStart
  'Test MEGA
  pf.Initialize(12, pf.CHANNEL4)
  'Test NodeMCU
  'pf.Initialize(0, pf.CHANNEL4)
  Timer1.Initialize("Timer1_Tick", 1000)
  Timer1.Enabled = True
End Sub

Private Sub Timer1_Tick
  Log("Timer Tick: ", Cnt)
  start = Not(start)
  If start Then
    pf.singlePWM(pf.BLUE, pf.PWM_FWD2)
    pf.singlePWM(pf.BLUE, pf.PWM_BRK)
  End If
  Cnt = Cnt + 1
  If Cnt = 3 Then
     pf.singlePWM(pf.BLUE, pf.PWM_BRK)
     Timer1.Enabled = False
  End If
End Sub



  • rpowerfunctions.zip
    5.8 KB · Views: 477
Last edited:


Well-Known Member
Licensed User
Longtime User
Update 20160629
Successfully tested the library on a NodeMCU 0.9 (ESP-12 Module). Important is to use Pin D3 = GPIO0.

This is great as enables to remote control the Lego Power Functions via Network f.e. using MQTT (via of course a B4J server).