Let's Make Robots!

Homemade wheel encoder

Basically I just printed out one of these (laser printer recommended):

encoder_40_s.gif

...And attached it to a wheel (double sided tape recommended).

Then I hooked up one of these (a 2$ IR sensor: QRB1134):

qrb.jpg

...And attached it to the motor pointing towards the wheel.

I've been testing it a bit with a few lines of Arduino code and damnit it works :D I'm now able to measure how much the wheel is rotating and therefore calculate how far a robot is moving. I just love it when these cheap lowtech solutions work.

Here are some photos (sorry about the bad quality):

encoder2_s.jpg

encoder1_s.jpg

 

UPDATE: 

Here is the schematic I used to hook up the QRB1134 sensor:

qrbsetup.jpg

I used a 0.1uF ceramic cap. Don't know if that's what was intended? It does have a + indicating a polarized cap, but I dunno? Man I wish people would write the kind of cap you're supposed to use, but apparently that's obvious to everyone but me :/ If anyone has a suggestion of what to use I'm open?

Here is the Arduino code I used for testing:

#define IOP 14
#define PWM 3

int val_new;
int val_old;
int clicks = 0;
int turns = 0;

void setup() {
    Serial.begin(115200);
    pinMode(IOP, INPUT);
    val_new = digitalRead(IOP);
    val_old = val_new;
}

void loop() {
    analogWrite(PWM, 80);
    val_new = digitalRead(IOP);
   
    if(val_new != val_old) {
        if(clicks == 40) {
            clicks = 1;
            turns++;
            Serial.print("TURNS: ");
            Serial.println(turns);  
        }
        else clicks++;
       
        Serial.print("CLICKS: ");
        Serial.println(clicks);

        val_old = val_new;
    }
}

Basically I just add 1 to the variable click every time the color in front of the sensor changes. When I reach 40 clicks I reset the variable click and add 1 to the variable turns. And off course I'm logging everthing through serial for testing. That's it :)

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
It is an encoder. It counts every time it transitions from black to white and white to black. If you know that you have 16 transitions and your wheel has a 6 inch circumference then you know that every 16 encoder ticks you drove 6 inches. VERY useful if you want your robot to map a room or drive a certain distance.
post code and a schematic on how you hooked it up please :)
Just posted schematic and code used ;)
Looks good but when you reset clicks to 1 you are outputting the clicks variable when you want turns. Minor thing to fix and really only affects debugging ;) Good job I will use this when I make my XMOS and Arduino tutorials using encoders to give me a leg up on doing the code.

You may wanna use interrupts for this sorta thing. I looked into it and found out that this can only be done with pin 2 and 3. Unfortunately my homemade motor shield allready uses pin 3 for motor PWM, so I thought I would try it this way to avoid resoldering my motor shield...

Just thought I'd let you know ;)

PS: Very much looking forward to your X-mos tutorial...

Actually, any pin can be used as an interrupt -- just not using the "attachInterrupt" function. The newer AVRs support "pin-change" interrupts on any pin. You have to end up delving a bit deeper into understanding the AVR architecture, but there's some sample code in a tutorial I wrote over at TRC: http://forums.trossenrobotics.com/tutorials/how-to-diy-128/an-introduction-to-interrupts-3248/

-Fergs

It is going to be a while it will be one of the last turorials and the easy ones wont start for a couple weeks. I want to finish my dagu contest bot first
Sorry! It's because I changed the variable names from counter1 and counter2 into something more descriptive just before I posted the code. It has been corrected...
Looks great, now your robot is getting ready for closed loop speed control and mapping!
Step by step my ideas are becoming reality :)