IR sensor (HW-201) counts way too high


This is my first post. I’m making a device that needs to count rotations. Previously I’ve used a rotary encoder, but those wear out too frequently and I don’t like that the rotation isn’t perfectly smooth.

I switched to a IR sensor to track the rotations. Right now I’m just putting my hand in front of it to simulate one rotation of the wheel, but instead of counting 1,2,3… it counts randomly high numbers, increasing upwards of hundreds of thousands each time. I’d like to keep the interrupt because the device will also be using a timer to stop and start and I don’t want to slow down that loop.

Thanks for the help!

volatile int IRsensor = 7; //on the teensy all digital pins are interrupt 
int LED = 13; //LED to indicate interrupt is being turned on 

long int count ;

void setup() {
pinMode(LED, OUTPUT);
pinMode(IRsensor, INPUT);
attachInterrupt(digitalPinToInterrupt(7), IRbeam, LOW);
count = 0;

void loop() { //loop just prints out my count value and turns the interrupt LED off
digitalWrite(LED, LOW);

void IRbeam(){
count++; //every time the beam is broken add to count
digitalWrite(LED, HIGH); //every time beam is broken turn LED on 


The variable that is affected inside the ISR (count) needs to be volatile, not the pin number.

Why use the pin number there? You gave it a name?

Post a schematic of the wiring.

Can you provide a link to the datasheet of the sensor and a schematic of your circuit?
Your digital input pin requires sharp transitions between HIGH and LOW. If the signal from the sensor is analog and stays inside the forbidden voltage range (see data sheet input voltage HIGH and LOW) the pin can behave unpredictably e.g. trigger hundreds of interrupts.

Thanks for your response. I can’t find the data sheet but this is the amazon link

Thank you. I found a similar module in my collection. It uses a different circuit but would create a similar issue. I looked at the signal with an oscilloscope. The issue is not the voltage at the Arduino pin. That stays within the boundaries of the datasheet and create fast transitions between the levels.

However, the circuit is lightning fast, and the issue is the same but instead of voltage it is the distance and reflection. When I just handle the module and distance and angle to the surfaces changes, the circuit sometimes creates multiple pulses in the 1 to 2 kHz range on my module. Your module could be even faster.

When the mechanical part of your system is setup correctly this will become less of an issue. However, you probably should investigate some filtering/de-bouncing techniques. Basically, figure out what the fastest pulse interval for your use case is and then ignore every pulse within that time.

That makes sense. Thanks for checking out the hardware for me!

When you talk about debouncing, would you put that into the interrupt or into the main loop? Would it just be a delay so that it only reads once?

attachInterrupt(digitalPinToInterrupt(7), IRbeam, LOW);

Try RISING or FALLING as the interrupt mode. LOW will constantly retrigger through the period when the signal is low.

Try getting a clean signal first.
Filtering always depends on the speed of the signal. If the signal is slow you can do some filtering in software. The faster the signals get the more you might need to do in hardware.

No delay(). Using delay is a bad software practice. It wastes processor cycles and stops other parts of your code from running.

Hi Klaus,

I think I have a cleaner signal using this setup I printed (one of my trainees asked if it was a mini-golf course) . It isn’t perfectly clean but only goes up by a few thousand instead of 100,000, which is an improvement. I think I’m going to try filtering in software. Where should I start? Do I make a loop that says if the change in value between the previous and new number is greater than X, count as 1?

Sorry for the basic questions - coding isn’t my expertise. I’m an immunologist trying to come up with a mouse device for my research.

I also dropped the CPU speed down from 600 mhz to 24 mhz. That deceased the change between values to a couple hundred.

This solved one of my issues - thanks! I was wondering why it would go up much higher values when my hand stayed in front.

It looks like your design is reflective. Allowing for more random reflections hitting the sensor.

Did you look at mechanical mouse hardware designs? The ones I have seen use wheels with slits and the IR beam is send from one side to the other. This allows the beam to be very narrow. Reflections are limited on both sides by enclosing the LED and the receiver, keeping only the front open.

You can buy complete IR barrier sensors with different sizes and distances.

Thanks for sharing, it’s really helpful.

Hi Klaus,

I’m going to rearrange it with the pillar off to the side and more of a fan blade so that the base itself doesn’t reflect. Alternatively I was thinking about your mouse hardware. The point of this IR sensor is to count the amount of times the device spins around. Do you think a photo interrupter or a magnet sensor would be a better solution?

If you have the ability to mount one of the small and strong [neodymium magnets] ( where it passes by a hall effect sensor with digital output you will have a simple and robust solution. There is much less to disturb it than with an optical solution.

Yes, both a photo interrupter and a hall sensor will be better than a reflective solution in your case. The hall sensor has advantages in environment where the sensor can get dirty.
The optical solution has advantage when it comes to size. You can build systems of almost any size, from small to large.
In a professional environments with more engineering resources this could be different. e.g. hard drive sensors can measure very small magnetic fields.

Hey Klaus,

Thanks for the tip. I ended up going with the hall sensor and it works perfectly this time. Doesn’t need any debouncing and fits nicely into my interrupt. Since the device is going to be in the cage with the mice, it was definitely a better choice than the photo interrupter.

Your project seems very similar to one of the first customers my former company had. They had us construct a reed switch assembly for the outside of the cage that could detect a magnet that was attached to the exercise wheel for the rat/mouse. We also supplied the magnet. I still have a box of perhaps a hundred magnets. To my knowledge, they are still producing the whole assembly.

I would guarantee they are selling the assembly for hundreds of dollars more than what it cost to make it. Scientific equipment is ridiculously overpriced! For example, this mouse treadmill (one with five lanes) cost our lab something like $5500. We then lent it to a collaborator for their half of the experiment.

So when our lab realized we needed a way to monitor mouse exercise I refused to buy another overpriced machine…which landed me here! We looked into buying the wheels that track revolutions, but we also want to stop the mice from moving the wheel as to limit their exercise. Those suckers will run up to 20km in a night if you let them. The one I’ve made has a little pin and servo that locks the wheel after X revolutions or Y amount of time.

The best part of the project is that it justified getting a 3d printer for the lab!

Do not be too harsh about the manufacturer. They are not overcharging. We just got used to the economy of scale for consumer electronics products. When you buy any modern gadget the development and production setup costs are divided by millions if not hundred millions of customers. When you buy scientific equipment, you must pay all these costs with a few other scientists.
If you calculate all the hidden costs your solution is likely in a similar price range. Most of these costs go to different budgets so you and your manager probably do not care. :slight_smile:
Thanks for telling us about the mice. Learned something new and interesting. Who would have thought there are gym machines for mice?
Have fun with your 3D printer and your Arduinos. See you in the forum.

klaus, My customer told us the animal they made the runway for was a very special animal the never stops running. They eat, poop, breed, have babies, feed them, all while running. So the study they designed for was to learn about their metabolism and how they never needed to stop and sleep.