Arduino MKR WiFi 1010 - IR Slotted Optical Speed Sensor

In this tutorial, we are going to learn:

Arduino MKR WiFi 1010 IR Slotted Optical Speed Sensor

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)
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. 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

IR Slotted Optical Speed Sensor 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

How IR slotted optical speed sensor 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.

The wiring diagram between Arduino MKR WiFi 1010 IR Slotted Optical Speed Sensor

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.

/* * This Arduino MKR WiFi 1010 code was developed by newbiely.com * * This Arduino MKR WiFi 1010 code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-mkr/arduino-mkr-wifi-1010-ir-slotted-optical-speed-sensor */ const int SENSOR_PIN = 4; // Arduino MKR WiFi 1010 pin connected to the OUT pin of the speed sensor int lastState; // the previous state of the sensor int objectCount = 0; // how many times the slot was blocked void setup() { Serial.begin(9600); pinMode(SENSOR_PIN, INPUT); lastState = digitalRead(SENSOR_PIN); } void loop() { int state = digitalRead(SENSOR_PIN); if (state != lastState) { // the state has changed if (state == LOW) { // LOW = something is in the slot objectCount++; Serial.print("The slot is blocked - count: "); Serial.println(objectCount); } else { // HIGH = the slot is clear Serial.println("The slot is clear"); } lastState = state; } }

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.
∞
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
Arduino MKR WiFi 1010
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'Arduino MKR WiFi 1010' on 'COM15')
New Line
9600 baud
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
Ln 11, Col 1
Arduino MKR WiFi 1010 on COM15
2

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.

/* * This Arduino MKR WiFi 1010 code was developed by newbiely.com * * This Arduino MKR WiFi 1010 code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-mkr/arduino-mkr-wifi-1010-ir-slotted-optical-speed-sensor */ const int SENSOR_PIN = 4; // Arduino MKR WiFi 1010 pin connected to the OUT pin of the speed sensor const int SLOTS_PER_TURN = 20; // number of slots (holes) in the encoder disc volatile unsigned long pulseCount = 0; // changed inside the interrupt unsigned long lastTime = 0; // runs every time a tooth of the disc blocks the slot void countPulse() { pulseCount++; } void setup() { Serial.begin(9600); pinMode(SENSOR_PIN, INPUT); attachInterrupt(digitalPinToInterrupt(SENSOR_PIN), countPulse, FALLING); } void loop() { if (millis() - lastTime >= 1000) { // every 1 second lastTime = millis(); noInterrupts(); // read and reset the counter safely unsigned long pulses = pulseCount; pulseCount = 0; interrupts(); float rpm = pulses * 60.0 / SLOTS_PER_TURN; // pulses per second -> turns per minute Serial.print("Pulses: "); Serial.print(pulses); Serial.print(" | Speed: "); Serial.print(rpm); Serial.println(" RPM"); } }

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.
∞
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
Arduino MKR WiFi 1010
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'Arduino MKR WiFi 1010' on 'COM15')
New Line
9600 baud
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
Ln 11, Col 1
Arduino MKR WiFi 1010 on COM15
2

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:

Video Tutorial

Learn More

※ 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!