I have recently found a motor in a box of bits I was given.
It looks similar to the robot motors we use but only has a geared output on one side.
It also has a piece of circuit board on the end of the motor with a magnet wheel and two hall effect sensors.
If I use the button sketch from the examples and add a serial.println line into it so I can monitor one or the other sensors it just goes crazy when I turn the motor by hand, I assume this is because of bounce.
If I use the debounce program and lower the debounceDelay to 1 millisecond it works fine by turning the motor by hand but only sees a few of the signals when I apply 5v to the motor. The input is pin 2 which I understand is a PWM pin.
Is there another way of reading the signal or is the motor just too fast to monitor on an arduino?
Speeding up the display on the debounce sketch to 115200 and 230400 doesn't seem to make much difference with a debounceDelay of 1, I can see the number rising but it looks to speed up quite a bit when I turn the power off and the motor is slowing down.
On the scope If I slow the trace down so there is only 1 cycle on the screen it is wavering a tiny bit at the edges but it is still a pretty good square wave.
The solution to reading these signals lies in using interrupts, basically doing the counting in the background and independent from the rest of your code.
The signal is far from being too fast for an Arduino to read (it's only about 1 kHz, it can happily handle three orders of magnitude faster), but it is too fast for it to read while you're doing many other things, such as trying to do Serial communication for every one pulse. Actually a 1 kHz signal can be handled without interrupts but it's getting rather tricky to ensure you don't miss pulses.
Thanks Guys that is brilliant and thank you jCA34F the sketch works well. I can alter the voltage with my bench power supply to the motor and the speed follows. Around 3000 pps at 5v.
Yes there are two hall effect devices and on the cct board it says 5v, gnd, A & B so I suppose it could act as a rotary encoder.