Raspberry Pi Pico - IR Slotted Optical Speed Sensor

In this guide, we will learn to set up and use the IR slotted optical speed sensor with the Raspberry Pi Pico. We will cover the following details:

Raspberry Pi Pico and IR slotted optical speed sensor

Hardware Preparation

1×Raspberry Pi Pico W
1×Raspberry Pi Pico Alternatively,
1×Micro USB Cable
1×IR Slotted Optical Speed Sensor
1×Jumper Wires
1×Breadboard
1×Optionally, Breadboard
1×Recommended: Screw Terminal Expansion Board for Raspberry Pi Pico

Or you can buy the following kits:

1×DIYables Sensor Kit (18 sensors/displays)
Disclosure: Some of the links provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you.
Additionally, some of these links are for products from our own brand, DIYables .

Overview of IR Slotted Optical Speed Sensor

Look at this module from the side and you see the letter U. People call it by many names: photo interrupter, opto interrupter, slot sensor or U-shaped IR sensor. The job is always the same. It tells the Raspberry Pi Pico if something is sitting in the gap between its two arms.

With an encoder disc, the sensor becomes a tachometer. You can read the speed of a motor or a robot wheel, count turns to find a distance, count parts on a small belt, or use it as a limit switch that never touches anything.

IR Slotted Optical Speed Sensor
Sensor type slot-type photoelectric (IR LED + NPN phototransistor)
Slot width 5.9 mm
Operating voltage 3.3V to 5V DC
Output digital (TTL), LOW when blocked, HIGH when clear
Signal conditioning built-in Schmitt trigger
Mounting M3 screw holes at both ends
Pins 3 (VCC, GND, OUT)

Pinout

IR Slotted Optical Speed Sensor Pinout

There are only three pins on the module:

  • VCC pin: connect to the 3V3(OUT) pin of the Raspberry Pi Pico (3.3 volts). The module accepts 3.3 volts to 5 volts.
  • GND pin: connect to any GND pin of the Raspberry Pi Pico (0 volts).
  • OUT pin: connect to a GPIO pin of the Raspberry Pi Pico (GP15 in this guide). It gives a digital signal.

The pin order can change from one batch to the next. Read the small labels on your own module before you wire it.

Why not 5 volts? The OUT pin goes as high as the VCC voltage. The Pico is a 3.3 volts board, and its GPIO pins do not like 5 volts. So, feed the sensor from 3V3(OUT), and the signal will never go over 3.3 volts.

How It Works

How the IR slotted optical speed sensor works

One arm of the U holds an infrared LED. The other arm holds an NPN phototransistor. They face each other across the 5.9 mm slot.

  • Slot empty: the infrared light gets to the phototransistor, and the OUT pin is HIGH (value 1 in MicroPython).
  • Slot blocked: a card or a tooth of a disc stops the light, and the OUT pin is LOW (value 0).

A Schmitt trigger chip on the board shapes the signal. The Pico gets a sharp 0 or 1, with no shaky in-between values, so the code needs no debounce.

Keep two things in mind:

  • Use an opaque object. A clear piece of plastic can let the infrared light pass, and the sensor will not see it.
  • A few modules from other brands work the other way round. If the Shell says "blocked" while the slot is empty, swap the 0 and 1 checks in the first script. The RPM script counts one edge per tooth, so it works with both types.

Wiring Diagram

The built-in Schmitt trigger already gives a clean signal, so you need no extra resistor; just take power from the 3.3 volts pin of the Pico.

The wiring diagram between Raspberry Pi and Pico IR Slotted Optical Speed Sensor

This image is created using Fritzing. Click to enlarge image

Raspberry Pi Pico Code - Count Objects in the Slot

The first script checks the OUT pin again and again. It prints a line only when the state changes. Each time the slot goes from clear to blocked, the counter goes up by one.

""" This Raspberry Pi Pico MicroPython code was developed by newbiely.com This Raspberry Pi Pico code is made available for public use without any restriction For comprehensive instructions and wiring diagrams, please visit: https://newbiely.com/tutorials/raspberry-pico/raspberry-pi-pico-ir-slotted-optical-speed-sensor """ from machine import Pin import time SENSOR_PIN = 15 # Pico GP15 connected to the OUT pin of the speed sensor sensor = Pin(SENSOR_PIN, Pin.IN) last_state = sensor.value() object_count = 0 # how many times the slot was blocked while True: state = sensor.value() if state != last_state: # the state has changed if state == 0: # LOW = something is in the slot object_count += 1 print("The slot is blocked - count: {}".format(object_count)) else: # HIGH = the slot is clear print("The slot is clear") last_state = state time.sleep_ms(1)

Detailed Instructions

Please follow these instructions step by step:

  • Ensure that Thonny IDE is installed on your computer.
  • Ensure that MicroPython firmware is installed on your Raspberry Pi Pico.
  • If this is your first time using a Raspberry Pi Pico, refer to the Raspberry Pi Pico - Getting Started tutorial for detailed instructions.
  • Wire the speed sensor to the Raspberry Pi Pico according to the provided diagram. Double-check that VCC goes to 3V3(OUT), not to VBUS.
  • Connect the Raspberry Pi Pico to your computer using a Micro USB cable.
  • Launch the Thonny IDE on your computer.
  • On Thonny IDE, select MicroPython (Raspberry Pi Pico) Interpreter by navigating to Tools Options.
  • In the Interpreter tab, select MicroPython (Raspberry Pi Pico) from the drop-down menu.
  • Ensure the correct port is selected. Thonny IDE should automatically detect the port.
  • Copy the above code and paste it to the Thonny IDE's editor.
  • Save the script to your Raspberry Pi Pico by:
    • Click the Save button, or use Ctrl+S keys.
    • In the save dialog, select Raspberry Pi Pico.
    • Save the file as main.py
  • Click the green Run button (or press F5) to run the script.
  • Slide a piece of card down into the slot, then pull it out. Do it three times. Your finger works too.
  • Check out the message in the Shell at the bottom of Thonny.
Shell x
>>> %Run -c $EDITOR_CONTENT
MPY: soft reboot The slot is blocked - count: 1 The slot is clear The slot is blocked - count: 2 The slot is clear The slot is blocked - count: 3 The slot is clear
MicroPython (Raspberry Pi Pico) • Board CDC @ COM29 ≡

If you name your script main.py and save it to the root directory of the Raspberry Pi Pico, it will automatically run each time the Pico is powered on or reset.

Raspberry Pi Pico Code - Measure Motor Speed (RPM)

Now, let's measure how fast something spins. For this, you need an encoder disc: a thin wheel with slots cut around its edge. Fix the disc on the motor shaft or on the wheel. Its teeth must pass through the slot of the sensor without rubbing on it.

A spinning disc makes pulses very fast. A simple loop that reads the pin could miss some of them. So this script uses an interrupt. With Pin.irq() and Pin.IRQ_FALLING, MicroPython calls a small function at the moment the signal drops from HIGH to LOW. That happens once for each tooth that enters the slot, so one tooth gives exactly one count.

Once per second, the main loop takes the count and sets it back to zero. Then it does the math:

  • The disc in this example has 20 slots, so one full turn gives 20 pulses.
  • Pulses in 1 second × 60 = pulses in 1 minute.
  • Pulses in 1 minute ÷ 20 = turns in 1 minute, which is the RPM.

The 20-slot disc is the one that often comes with TT gear motors. If your disc has a different number of slots, change SLOTS_PER_TURN at the top of the script.

""" This Raspberry Pi Pico MicroPython code was developed by newbiely.com This Raspberry Pi Pico code is made available for public use without any restriction For comprehensive instructions and wiring diagrams, please visit: https://newbiely.com/tutorials/raspberry-pico/raspberry-pi-pico-ir-slotted-optical-speed-sensor """ from machine import Pin import time SENSOR_PIN = 15 # Pico GP15 connected to the OUT pin of the speed sensor SLOTS_PER_TURN = 20 # number of slots (holes) in the encoder disc pulse_count = 0 # runs every time a tooth of the disc blocks the slot def count_pulse(pin): global pulse_count pulse_count += 1 sensor = Pin(SENSOR_PIN, Pin.IN) sensor.irq(trigger=Pin.IRQ_FALLING, handler=count_pulse) while True: time.sleep(1) # count pulses for 1 second pulses = pulse_count # read and reset the counter pulse_count = 0 rpm = pulses * 60 / SLOTS_PER_TURN # pulses per second -> turns per minute print("Pulses: {} | Speed: {:.2f} RPM".format(pulses, rpm))

Detailed Instructions

The wiring stays the same. Only the script changes:

  • Copy the code above into a new tab in Thonny.
  • Save it to the Raspberry Pi Pico as main.py. This replaces the first script.
  • Click the green Run button (or press F5).
  • Spin the disc with your hand, or let the motor turn it. To drive a motor from the Pico, see the Raspberry Pi Pico - DC Motor tutorial.
  • Check out the message in the Shell at the bottom of Thonny. A new line shows up every second.
Shell x
>>> %Run -c $EDITOR_CONTENT
MPY: soft reboot Pulses: 0 | Speed: 0.00 RPM Pulses: 98 | Speed: 294.00 RPM Pulses: 101 | Speed: 303.00 RPM Pulses: 100 | Speed: 300.00 RPM Pulses: 99 | Speed: 297.00 RPM Pulses: 0 | Speed: 0.00 RPM
MicroPython (Raspberry Pi Pico) • Board CDC @ COM29 ≡

In this run, the motor turned at about 300 RPM, and then it stopped. The value moves up and down a little because the count window is one second long. A wheel can also tell you distance: one turn moves the robot by the wheel's circumference (π × diameter), so total distance = number of turns × π × diameter.

Do you want to sense an object from a few centimeters away, not inside a slot? Try the Raspberry Pi Pico - Obstacle Avoidance Sensor tutorial. For a knob that also tells the turning direction, see the Raspberry Pi Pico - Rotary Encoder tutorial.

Video Tutorial

※ OUR MESSAGES

  • As freelancers, We are AVAILABLE for HIRE. See how to outsource your project to us
  • Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!