Arduino Giga R1 WiFi IR Slotted Optical Speed Sensor

In this guide, we will learn how to use the IR slotted optical speed sensor with the Arduino Giga R1 WiFi. In detail, we will learn:

Arduino Giga R1 WiFi and IR slotted optical speed sensor

This page uses one sensor and one wheel. The Giga R1 WiFi has a large number of pins, though, so later you can add more sensors and watch the speed of several wheels with one board.

Hardware Preparation

1×Arduino Giga R1 WiFi
1×USB Cable Type-A to Type-C (for USB-A PC)
1×USB Cable Type-C to Type-C (for USB-C PC)
1×IR Slotted Optical Speed Sensor
1×Jumper Wires
1×Optionally, Breadboard
1×Recommended: Screw Terminal Block Shield for Arduino Uno/Mega/Giga
1×Recommended: Sensors/Servo Expansion Shield for Arduino Mega/Giga
1×Recommended: Breadboard Shield for Arduino Mega/Giga
1×Recommended: Enclosure for Arduino Giga
1×Recommended: Power Splitter for Arduino Giga

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

The sensor is a small fork with two arms and a narrow gap of 5.9 mm between them. An infrared LED is built into one arm. An NPN phototransistor is built into the opposite arm, so it looks straight at the LED. You may also see this part sold as a photo interrupter, an opto interrupter or a U-shaped IR sensor. All of these names mean the same module. Two M3 screw holes, one at each end, let you bolt it to a robot frame or a motor bracket.

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)

How It Works

How the IR slotted optical speed sensor works

The LED sends an invisible beam across the gap, and the phototransistor waits on the other side. The OUT pin simply reports whether the beam gets through:

What is in the slot Beam OUT pin
Nothing reaches the phototransistor HIGH
An opaque object (card, finger, disc tooth) is cut off LOW

Between the phototransistor and the OUT pin, the module has a Schmitt trigger. Its job is to give a firm HIGH or a firm LOW, never a shaky value in between. That is why the code on this page has no debounce part, unlike a button.

The beam is infrared light, so a see-through object such as clear plastic may not stop it. For a reliable result, pass something solid and not transparent through the slot.

Pinout

IR slotted optical speed sensor pinout
  • GND pin: connect to GND (0V) of the Arduino Giga R1 WiFi.
  • VCC pin: connect to the 3.3V pin of the Arduino Giga R1 WiFi (the module takes 3.3V to 5V).
  • OUT pin: sends the digital result, connect to pin 2 of the Arduino Giga R1 WiFi.

Not every batch puts the three pins in the same order. Before you wire, look at the small labels on your own module.

Wiring Diagram

All Giga R1 WiFi pins work with 3.3V logic. The voltage on the OUT pin can go as high as the voltage you give to VCC. If you power the sensor from 3.3V, the signal on pin 2 stays at 3.3V or lower, which is safe for the board. So use the 3.3V header pin for VCC, not the 5V one.

The wiring diagram between Arduino Giga R1 WiFi IR slotted optical speed sensor

This image is created using Fritzing. Click to enlarge image

Pin 2 is our choice because it works with attachInterrupt(), and the speed program depends on that.

How To Program For IR Slotted Optical Speed Sensor

The two programs on this page read the sensor in two different ways.

Program 1 - check the pin yourself, inside loop():

  • Set pin 2 as an input:
pinMode(SENSOR_PIN, INPUT);
  • Read the OUT signal. LOW tells you the beam is cut:
int state = digitalRead(SENSOR_PIN);
  • React only to a change. When the new value is LOW, add one to the count:
if (state != lastState) { if (state == LOW) { objectCount++; } lastState = state; }

Program 2 - let the board count for you, with an interrupt:

  • Declare the pulse counter as volatile, because a function outside loop() changes it:
volatile unsigned long pulseCount = 0;
  • Create the function that adds one pulse:
void countPulse() { pulseCount++; }
  • Ask the board to call that function each time OUT goes from HIGH to LOW. This is the FALLING edge. It happens once for every tooth that enters the slot:
attachInterrupt(digitalPinToInterrupt(SENSOR_PIN), countPulse, FALLING);
  • Every 1000 ms, pause interrupts for a moment, copy the counter, set it to zero, and turn the interrupts back on. Pausing makes sure the counter does not change while you copy it:
noInterrupts(); unsigned long pulses = pulseCount; pulseCount = 0; interrupts();
  • Change pulses into RPM:
float rpm = pulses * 60.0 / SLOTS_PER_TURN;

The formula in plain words: a disc with 20 slots gives 20 pulses in one turn. The pulses from one second, times 60, are the pulses in one minute. Those divided by 20 are the turns in one minute, which is what RPM means.

Arduino Giga R1 WiFi Code - Object Counter

This program looks at pin 2 all the time, but it stays silent until the slot changes. When an object goes in, it adds one to the count and prints the new total. When the object leaves, it prints that the slot is clear.

/* * This Arduino Giga R1 WiFi code was developed by newbiely.com * * This Arduino Giga R1 WiFi code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-giga/arduino-giga-r1-wifi-ir-slotted-optical-speed-sensor */ const int SENSOR_PIN = 2; // Arduino Giga R1 WiFi 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

Follow these instructions step by step:

  • Using the Giga R1 WiFi for the first time? Start with the Arduino Giga R1 WiFi getting started guide.
  • Wire the components according to the provided diagram. VCC must go to 3.3V.
  • Connect the Giga R1 WiFi to your computer through the USB Type-C cable.
  • Launch the Arduino IDE on your computer.
  • Select the appropriate Arduino Giga R1 WiFi board (e.g., Arduino Giga R1 WiFi) and COM port.
  • Copy the code and put it into a new sketch.
  • Hit the Upload button and wait until it finishes.
  • Open the Serial Monitor and pick 9600 baud.
  • Move a piece of thick paper down into the slot and lift it out again. Repeat this three times.
  • See the output:
∞
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
Arduino Giga R1
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'Arduino Giga R1' 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 Giga R1 on COM15
2

Do you see "blocked" while the slot is empty? Your module probably comes from another maker and uses the reverse logic. Swap LOW and HIGH in this program and it will be fine.

Prepare the Encoder Disc

The next program needs something that spins through the slot. An encoder disc is a thin wheel with a ring of holes, so its edge looks like a row of teeth.

  • Slide the disc onto the motor shaft, or fix it to the wheel you want to measure.
  • Hold the sensor so the toothed edge of the disc sits inside the slot.
  • Give the disc a slow turn and check that it moves freely, without hitting either arm.
  • Spin the disc by hand, or let the motor drive it. Motor wiring is not part of this page. For one way to run a motor from this board, see the Arduino Giga R1 WiFi - DC Motor Shield tutorial.

Arduino Giga R1 WiFi Code - Measure Rotation Speed (RPM)

When the disc spins fast, many teeth can go past while loop() is busy with other work. Checking the pin in loop() could lose some of them. So here the counting happens in an interrupt function, which runs right at the FALLING edge of each tooth. Once a second, the main program takes the count, shows it, and works out the RPM.

Near the top of the code you will find SLOTS_PER_TURN = 20. Twenty slots is what the usual encoder disc for TT gear motors has. If your disc has another number of holes, write that number here.

/* * This Arduino Giga R1 WiFi code was developed by newbiely.com * * This Arduino Giga R1 WiFi code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-giga/arduino-giga-r1-wifi-ir-slotted-optical-speed-sensor */ const int SENSOR_PIN = 2; // Arduino Giga R1 WiFi 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"); } }

Detailed Instructions

  • Leave the sensor wired to 3.3V, GND and pin 2.
  • Put the RPM code into the Arduino IDE.
  • Click Upload.
  • Open the Serial Monitor at 9600 baud.
  • Start the disc, keep it turning for a few seconds, then stop it.
  • A new line shows up every second. This test ran close to 300 RPM:
∞
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
Arduino Giga R1
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'Arduino Giga R1' 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 Giga R1 on COM15
2

Check the second line by hand: 98 × 60 ÷ 20 = 294 RPM. The 0 lines at the start and the end are the moments when the disc was still. Because this program counts one edge per tooth, it gives the right speed with both normal and reverse-logic modules.

Troubleshooting

Problem What to check
The count never changes Is OUT on pin 2? Is the object solid, not clear plastic?
The board says "blocked" with an empty slot Your module uses reverse logic. Swap LOW and HIGH in the object counter.
RPM looks too high or too low Does SLOTS_PER_TURN match the holes on your disc?

Two rules for the interrupt function: keep it very short, and never put Serial.print() inside it. Also, any variable it changes needs the volatile keyword.

Reading Several Wheels with the Giga R1 WiFi

A robot often has two or four wheels. With the many pins of the Giga R1 WiFi, you can give each wheel its own sensor:

  • Wire each extra sensor the same way: VCC to 3.3V, GND to GND, OUT to a free pin. Make sure that pin works with attachInterrupt().
  • For each sensor, make its own volatile counter and its own small counting function.
  • Call attachInterrupt() once for each sensor in setup().
  • In loop(), read and reset every counter inside the same noInterrupts() / interrupts() block, then work out the RPM of each wheel.

Project Ideas

  • A tachometer that shows the RPM of a motor.
  • Speed feedback for each wheel of a robot.
  • A counter for parts, pulses or items that pass the slot.
  • A distance meter: turns × wheel circumference (π × wheel diameter) gives how far a wheel has rolled.
  • An end-stop or home-position check with no contact, as an option next to a limit switch.

To find an object at some distance instead of inside a narrow slot, try the ultrasonic sensor.

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!