Arduino MKR WiFi 1010 - IR Slotted Optical Speed Sensor
In this tutorial, we are going to learn:
- How to connect an IR slotted optical speed sensor to Arduino MKR WiFi 1010 safely, using the 3.3V supply
- How to program Arduino MKR WiFi 1010 to know when the slot is blocked and to keep a count
- How to program Arduino MKR WiFi 1010 to read the turning speed of a motor (RPM) from an encoder disc
- Why the sensor goes on pin 4 of the MKR board, and not on pin 2

Hardware Preparation
| 1 | × | Arduino MKR WiFi 1010 | |
| 1 | × | Micro USB Cable | |
| 1 | × | IR Slotted Optical Speed Sensor | |
| 1 | × | Jumper Wires | |
| 1 | × | Optionally, Breadboard | |
| 1 | × | Optionally, DC Power Jack |
Or you can buy the following kits:
| 1 | × | DIYables Sensor Kit (18 sensors/displays) |
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. The gap in the middle is 5.9 mm wide. An infrared LED sits in one arm, and an NPN phototransistor sits in the other arm, looking straight at the LED. Because of this shape, sellers give it many names: photo interrupter, opto interrupter, IR slot sensor, or U-shaped IR sensor. They are all the same idea.
| IR Slotted Optical Speed Sensor | |
|---|---|
| Pins | 3 (VCC, GND, OUT) |
| Operating Voltage | 3.3V to 5V DC |
| Output | digital (TTL), HIGH when clear, LOW when blocked |
| Slot Width | 5.9 mm |
| Sensor Type | slot-type photoelectric (IR LED + NPN phototransistor) |
| Signal Conditioning | built-in Schmitt trigger |
| Mounting | M3 screw holes at both ends |
Typical projects with this sensor:
- A robot that checks how fast each wheel turns
- A tachometer that shows the RPM of a small motor
- A counter for pulses, or for items that drop through the slot
- An end stop that tells when a moving part reaches a limit or a position
- A trip meter that finds distance from the number of wheel turns
Pinout

- GND pin: needs to be connected to GND (0V)
- VCC pin: needs to be connected to VCC (3.3V, or 5V). On Arduino MKR WiFi 1010, use the VCC pin, which gives 3.3V.
- OUT pin: the pin sends the slot state to Arduino MKR WiFi 1010. It needs to be connected to a digital input pin.
Not every batch puts the pins in the same order. Trust the labels printed on your module, not the picture.
How It Works

The LED shines invisible infrared light across the gap all the time. What the OUT pin shows depends on whether that light gets to the other arm:
| Slot | IR light | OUT pin |
|---|---|---|
| Empty | reaches the phototransistor | HIGH |
| Something opaque inside (a card, a disc tooth) | is cut off | LOW |
Inside the module, a Schmitt trigger shapes the signal. The board gets a sharp 0 or 1 that does not shake, so the code has no debounce part.
Two things to keep in mind:
- Clear plastic or glass can let the IR light pass. Pick something you cannot see through.
- A few modules sold by other makers use reversed logic. If your Serial Monitor says the slot is blocked while it is empty, exchange LOW and HIGH in the counting code. The RPM code does not care, because it counts one edge for each slot.
Why Arduino MKR WiFi 1010 Needs Some Care
The OUT pin goes as high as the voltage on VCC. The MKR WiFi 1010 works at 3.3V, and its pins are not made for 5V. If you power the sensor from 5V, a HIGH on OUT is 5V, which is too much for the board. So feed the sensor from the MKR VCC pin (3.3V). The sensor works fine at 3.3V.
The second point is the pin choice. The RPM code uses an interrupt. On the MKR board, pin 2 cannot trigger an interrupt, so this tutorial uses pin 4, which can.
From Pulses to RPM
To measure speed, you need an encoder disc. It is a thin wheel with a ring of slots near the rim. Many TT gear motors come with a 20-slot disc. Mount it on the shaft or the wheel, and place the sensor so the rim runs through the gap without rubbing.
As the disc turns, the light is cut once by each tooth, so the OUT pin falls from HIGH to LOW once per tooth. Twenty falls mean one turn. Arduino MKR WiFi 1010 counts the falls for one second, then:
- Multiplies by 60 to get the falls per minute
- Divides by 20 (the slots in one turn) to get the turns per minute, which is the RPM
Wiring Diagram
Wire OUT to pin 4, GND to GND, and VCC to the VCC (3.3V) pin of Arduino MKR WiFi 1010, like in the picture below.

This image is created using Fritzing. Click to enlarge image
How To Program For IR Slotted Optical Speed Sensor
- Include the library: nothing to include. The sketches use only functions that come with the Arduino core.
- Define the pin: const int SENSOR_PIN = 4;
- Initialize the pin as an input in setup(): pinMode(SENSOR_PIN, INPUT);
- Read the slot: int state = digitalRead(SENSOR_PIN);. A result of LOW tells us the slot is blocked, and HIGH tells us it is open.
- Print only on a change: save the old reading in lastState and compare: if (state != lastState). This way one block gives one message, not hundreds.
- Declare the pulse counter with volatile, since an interrupt changes it: volatile unsigned long pulseCount = 0;
- Attach the interrupt so a short function runs on every HIGH-to-LOW change: attachInterrupt(digitalPinToInterrupt(SENSOR_PIN), countPulse, FALLING);
- Copy the counter safely, then set it back to zero: put the copy between noInterrupts(); and interrupts();
- Calculate the speed once per second: float rpm = pulses * 60.0 / SLOTS_PER_TURN;
A motor makes very short pulses. A plain digitalRead() in loop() can look at the pin too late and skip a few. An interrupt reacts to each one at once. FALLING is used so that each tooth adds exactly 1, not 2.
Arduino MKR WiFi 1010 Code - Count Objects in the Slot
This sketch keeps checking the OUT pin. Each time the state changes, it prints one line. When the change is from open to blocked, it adds 1 to the count first.
Detailed Instructions
- Connect Arduino MKR WiFi 1010 to PC via the micro USB cable.
- Wire the sensor to the board as in the wiring diagram. Check again that VCC goes to 3.3V, not 5V.
- Open Arduino IDE. Navigate to the Tools menu and pick Arduino MKR WiFi 1010 and its COM port.
- Copy the above code and paste it in a new sketch.
- Click the Upload button.
- Open the Serial Monitor and set it to 9600 baud.
- Slide a strip of paper card through the gap, take it out, and do it again two more times. A finger works too.
- Read the lines on the Serial Monitor.
Arduino MKR WiFi 1010 Code - Read Motor Speed in RPM
In this sketch, the interrupt on pin 4 adds 1 every time a tooth cuts the light. Once a second, loop() takes the total, sets the counter back to 0, turns the total into RPM, and prints both numbers.
Is your disc not a 20-slot one? Count its slots and put that number in SLOTS_PER_TURN at the top of the code.
Detailed Instructions
- Leave the wiring as it is from the first sketch.
- Put the encoder disc on the motor shaft or wheel. Line it up so the teeth run through the gap freely.
- Paste the above code into Arduino IDE and upload it.
- Turn the disc with your hand, or let the motor run. The motor needs its own driver circuit. See Arduino MKR WiFi 1010 - DC Motor for that part.
- Watch the Serial Monitor. Below is a motor running near 300 RPM, then stopping.
Let's check one line by hand: 99 pulses × 60 = 5940 pulses per minute, and 5940 ÷ 20 = 297 RPM. That is the same as the Serial Monitor.
Want the distance too? Take the number of turns and multiply it by the wheel circumference, which is π × the wheel diameter.
Related tutorials on this board:
- Need to see an object in front of a sensor, with no slot? Try Arduino MKR WiFi 1010 - Obstacle Sensor.
- Need a knob that also gives the turn direction? Try Arduino MKR WiFi 1010 - Rotary Encoder.