How to use the IR Speed Sensor with Microbit | RPM Detection system with Microbit

Hello and welcome back. In this project, we will learn how to make a Speed Detection System using the Microbit board. For this, I have mainly used an IR speed detection sensor, which helps to measure the rotation speed of a wheel or motor shaft. I used the Microbit V2 board for this project, but you can also use the V1 board without any problem.
To display the speed (in RPM), I connected an LCD screen, which updates the values in real time. I also added an LED bulb to detect when the RPM goes above 300. You can change this value as you like. If you want to try this project using Arduino instead of Microbit, please use the link. I used the MakeCode IDE for programming, which allows us to create code easily using drag-and-drop code blocks. It’s a very beginner-friendly IDE. You can also see the auto-generated Python and Java scripts in this IDE. If you want to know more details about how the IR Speed Sensor works and how to connect it with Arduino, please use this link.
- What is the Micro:bit? — Click on me
Ok, let’s do this project step by step. The required components are given below.
- Microbit board — Our store / Amazon
- IR infrared speed sensor x 1 — Our Store / Amazon
- LCD x 1 — Our Store / Amazon
- I2C module x 1 — Our Store / Amazon
- LED bulb — Our store / Amazon
- Microbit sensor shield — Our store / Amazon
- Jumper wires — Our store / Amazon
- Keyestudio BBC Micro: bit starter kit — Click on me
Disclosure: These Amazon links are Affiliate links. As an Amazon Associate, I earn from qualifying purchases.
Step 1
Firstly, identify these components.








Step 2
Secondly, connect the Microbit board to the sensor shield.

Step 3
Next, connect the LCD, IR speed sensor, and LED module to the sensor shield. For this, follow the circuit diagram given below.







Step 4
Now, connect the Microbit board to your computer and open the MakeCode IDE.


Step 5
Afterward, create a new project. Then you will see the MakeCode IDE interface. Now, add the LCD extension to start using the display.



Step 6
Next, click on the Python tab and paste the following script. Then, switch back to the Blocks tab, and you’ll see the code blocks automatically generated from the script.
- Python Script and Circuit Diagram — Download
SLOTS_PER_ROTATION = 20 # Set according to your encoder
LED_PIN = DigitalPin.P2
LED_RPM_THRESHOLD = 300
# Initialize LCD
I2C_LCD1602.lcd_init(0)
# Loading animation
loading_messages = ["System Loading", "System Loading.", "System Loading.."]
for message in loading_messages:
I2C_LCD1602.show_string(message, 0, 0)
basic.pause(500)
# Blink P11 during loading
for i in range(3):
pins.digital_write_pin(DigitalPin.P11, 1)
basic.pause(300)
pins.digital_write_pin(DigitalPin.P11, 0)
basic.pause(300)
# Ready message
I2C_LCD1602.show_string("Ready!", 4, 1)
basic.pause(1000)
I2C_LCD1602.clear()
# Static label (first line)
I2C_LCD1602.show_string("Speed(RPM):", 0, 0)
# Variables
pulse_count = 0
last_state = 0
rpm = 0
# Pulse detection
def count_pulses():
global pulse_count, last_state
current_state = pins.digital_read_pin(DigitalPin.P1)
if last_state == 0 and current_state == 1:
pulse_count += 1
last_state = current_state
# Main loop
def on_forever():
global pulse_count, rpm
pulse_count = 0
start_time = input.running_time()
# Count for 1 second
while input.running_time() - start_time < 1000:
count_pulses()
rpm = int((pulse_count * 60) / SLOTS_PER_ROTATION)
# Display RPM (overwrite old number)
I2C_LCD1602.show_string(" ", 11, 0) # Clear digits
I2C_LCD1602.show_number(rpm, 11, 0)
# Buzzer alert
if rpm > LED_RPM_THRESHOLD:
pins.digital_write_pin(LED_PIN, 1)
else:
pins.digital_write_pin(LED_PIN, 0)
basic.forever(on_forever)





Step 7
Now, click the “Connect to Device” button and select your Microbit board. Once connected, the IDE will recognize the board. Finally, click the “Download” button to upload the code.







Step 8
Finally, remove the USB cable and connect an external power supply to the sensor shield. For that, I used two Li-ion batteries, but you can also use a 9V battery or any other suitable power source.



Now you can test this project using an encoder wheel with a gear motor. Enjoy your project! The full video guide is below. We hope to see you in the next project. Have a great day!


Troubleshooting Tips
- Check all connections.
- Check the power supply.
- Include the LCD extension
- Check the USB cable.
How to use the IR Speed Sensor with Microbit | RPM Detection system with Microbit