Let's Make Robots!

Interrupts and wheel encoding... a beef with...

EDIT (June, 25th 2012):

The headache just doesn't go away...

Despite the photo below now I have a tube of black heatshrink shielding the sensor. It increase the analog output, for both cases (black and white). Regardless, the behaviour remains the same as without the shield when I move unto digital pins. I've been trying to deal with debouncing for the past couple of days with no luck.

// Playing with encoder digital readings


const int sensorPin = 7;


int lastColor;

int currentColor;

int count;


long lastDebounceTime = 0;  // the last time the output pin was toggled

long debounceDelay = 2;    // the debounce time; [0,1] -> crazy bouncing, >= 2 silence!


void setup()



  //pinMode(sensorPin, INPUT);          // 7-8 initial bounces on black when lastColor = HIGH;

  //digitalWrite(sensorPin, HIGH);      // and both these lines are commented.


  lastColor = digitalRead(sensorPin);


  Serial.print("Starting color ");


  //lastColor = HIGH;     // HIGH bouncing on white stripes; LOW bouncing on black stripes




void loop()




  if (currentColor != lastColor)



    Serial.print("Click! ");




} //close loop()


void readSensor()


  int reading = digitalRead(sensorPin);


  if (reading != lastColor){

    lastDebounceTime = millis();


  if( (millis() - lastDebounceTime) > debounceDelay){

    currentColor = reading;




The major implications of this code is by varying:
long debounceDelay = 2;    // the debounce time; [0,1] -> crazy bouncing, >= 2 silence!

Also the bouncing will happen with the oposite stripe of the starting one. That is, 1st read is of a white stripe, then whenever a black stripe goes by hundreds of clicks will happen, if debounceDelay is 0 ou 1. However if it's 2 or more.... only the first read is done, then forever silence... :( 

So the time has arrived where I've realized I can't go on without the use of wheel encoders. So I've made these:

EDIT: Added TCRT5000-based encoder detector circuit below.
NEW EDIT: Replaced with corrected schematics.
NEW NEW EDIT: Yet again replaced the schematics, HOPEFULLY this time around I didn't leave any mistake on :P

I understand how they are supposed to work, and when I do turn the wheels manually I do get/capture the black stripes lack of reflective power and the otherwise reflective power of the white stripes (rock on \m/)!

I do know, and been throught a number of quadrature posts here and elsewhere, but they only make my head hurt and for the time being I have not need of the advantages they offer, plus I've already done this single channel encoder detection apparatus.

So I was trying some code I've found in the interwebs:

// sample arduino code for getting relative speed from an encoder

// this code will print the # of encoder ticks each 1/2 second

volatile int counter = 0;

void setup(){

       attachInterrupt(0, count, RISING);



void loop(){






void count(){





My beef is that when the motors and consequently the wheels spin I get values in the serial port up to the ~500 and the comment in the code says:

"the # of encoder ticks each 1/2 second"

which either I'm thinking wrong, the wheels speed way faster than I can see or my poor encoding apparatus does not provide the resolution to keep up with the speed... my guess is "all of the above" :/ 

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

I am using the TCRT5000 as a distance sensor for my R2D2 clone. The readings are best in 5 to 15mm distance. Below or above that it's messy. Try to adjust the distance to the wheel encoder disk.

A question somewhat on topic for lumi... I'm trying to use the TCRT5000 as a distance sensor too. Is your's hooked up to an analog pin or are you using it as a digital sensor? I'm lacking ADC pins so I'm looking for that sweet on/off distance as well.

Perhaps you can make more of this than I did...

I've applied as it is, and found it undersensitive also I was too angry to try and tweak it. As I type I prepare myself to try and implement an hardware solution... *fetches hammer* 

Well, I had only one pin left and this was the (sneeking in my code :-) ) analog pin 1 (leg #7). I did not try to hook it up as a digital sensore and I believe you can't without setting up a A/D circuit. 

I'm not sure what the Tiny13 can do but my PICAXE 08M2 is able to use the sensor in a digital fashion. The whole circuit uses 3.3V and with the IR LED consuming 50mA @1.25V it will trigger a digital low when my finger gets around 12mm away. I use the same circuit that kariloy posted. For me R1 is 47ohm and R2 is 10K.

good to know. Will try this with an Attiny when I have time.

adjusted it, however now my current problem as it seems is dealing with bouncing signals.. oh the pain

Based on your schematic, I think you have your detector wired incorrectly. V+ and GND should be swapped.

A quck test to see if your IR LED is emitting is to look at it with a digital camera. Most cameras will pick up IR LEDs as a blue glow.

Nevermind: Apparently you know the drawing is wrong, but you have it hooked up right.

Indeed, as it seems I'm getting quite good at doing things wrong ;-) This time around (3rd, 2nd correction) hopefully everytime is now well, in that it actually mirrors what I've done, not that it's appropriate :)

The distance between your sensor and the wheel allows ambient light (especially sunlight) to affect your sensor readings. If you are testing under a fluorecent light then the 50Hz/60Hz hum can also interfer with your readings. Put a shield around your sensor so that almost all of the light hitting the black and white disc comes from the IR led only.