Hi everybody,
after playng around with some stepper motors and a DIY car chassis, I decided to buy a 2WD Car Kit (yep, chinese ones..) to let me "play" with "moving things". I previously used a pair of stepper motors, they were easy to control but too slow, so I switched to such Car Kit that includes two DC motors and two wheel discs. I also bought an L298N motor driver, and two FC-03 IR speed sensors like this:

I planned to use them with attachInterrupt() to count the wheel "holes" (20 per turn) so to determine the distance covered and relative speed. The problem is neither the Car Kit nor the driver, it's the FC-03: it seems to have an unexpected (for me) behavior.
To understand its usage, I found this schematics of the sensor:

The four pins are labeled VCC, GND, DO, AO, so everything was pretty clear to me. I connected DO (Digital Output) to pin 2 (interrupt 0), and obviously VCC to +5V and GND to Arduino GND, and wrote this test sketch:
#define LED1 13
#define M1_SENS 2
#define M1_INT 0
volatile int count1 = 0;
void setup() {
Serial.begin(9600);
Serial.println("Start");
pinMode(LED1, OUTPUT);
pinMode(M1_SENS, INPUT);
attachInterrupt(M1_INT, motorStep1, RISING);
}
void loop() {
if ( count1 >= 20 ) {
digitalWrite(LED1, !digitalRead(LED1));
count1 = 0;
}
}
void motorStep1()
{
count1++;
Serial.println(count1);
}
So, if I use a piece of black plastic to block IR light from the LED and the phototransistor (as an "on" event), the serial output shows me TWO events of the ISR , and TWO when I remove it (as an "off" event), e.g. 1 and 2 when I put the plastic, then 3 and 4 whan I remove it). The same if I install it over the encoding disc and move the wheel by hand, a full 360° returns a count of 80, instead of 20!!
First of all, if I set attachInterrupt() with RISING option, why on earth I have interrupts on both "on" AND "off"? I expected to have it on "off" only!
Secondly, that "double interrupt" makes me crazy. Why TWO consecutive events?
Thirdly, if I change RISING into FALLING, the output is exactly the same!!
So, why this behavior? It's not the speed of events, I used both the wheel and making single events by hand, same result. It's not the "Serial.print()" contained in the ISR, I moved to loop() and nothing changed (except it "missed" some counts, like 1 2 3 5 6 7 9..., probably due to lower response of loop() compared to interrupts.
So I replaced the FC-03 with a simple circuit, two resistors, one IR LED (TSUS4300) and an IR phototransistor (TEFT4300), and it works like a charm even if output should be analog (but it falls into digital signals range)!!! Same connections, same sketch, and one event per each "off" event, no double interrupts, no events on "on"!
You'd ask me "what's the problem? Use your components instead of FC-03!". Well, placing my sensors is critical as they should be perfectly aligned, so I'd like to use the FC-03s.
After such test, I tried to use AO (Analog Output) pin instead of DO: same Arduino connection, same sketch, but it WORKS!
So, I think I can now use AO and start working on the car, but I wonder why that DO behavior, if it depends on something wrong in my usage of the sensor, in attachInterrupt(), the schematics is wrong, or the sensors are defective (thus I'll send them back)!!!
What do you think?
