Let's Make Robots!

Homemade wheel encoder

Aniss1001's picture

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.
--I understand the schematic above but don’t quite follow how you have this hooked up--

I came across another site that had the same schematic as you have listed above. The site also had some instruction like what I have listed below, Please read...

 

////Don't use this code it is from another web site. I'm using it as an example... 

Connect analog pin 0 to G via a 0.1uF capacitor

Connect the white wire (collector) from the sensor to pin 0, 10K resistor to V

Connect the blue wire(emitter) to G

Connect the green wire(cathode) to G

Connect the orange wire(anode) to V via a 220ohms resistor

///End of example.

Looking at the above code he has the white wire on line 0. I believe you have it connected to analog line 3. but what i don't understand is how you are reading digital line 14. 

As you can see this example is using analog. It looks like you are using a mix of analog and digital.

I would deeply appreciate it if you could type up something like this as an example so I can use your code example.

Sorry, as I’m quite new to this…

 

pt3r's picture
Forgive me my ignorance but could you tell me what this "one of these" things is ?
Aniss1001's picture

I understand your confusion. People just call it an "encoder" which is an awful term for it, since everything that transforms information of any kind into another format is an "encoder". The term "wheel encoder" I just invented because I don't know what to call it. It doesn't qualify as a "quadrature encoder" because it has only one IR sensor, and a "rotary encoder" is (to my knowledge) synonimous with "quadrature encoder". So I'm not sure what kind of encoder I just made :)

However it works like jklug80 says: The wheel is divided into 40 steps (in my case) and the IR sensor allows me to detect whenever the wheel has turned another step. I then know that the wheel has turned 360 / 40 = 9 degrees. Asuming that both wheels of the robot are turning with the same speed, and knowing the diameter of the wheel, I can then calculate how far the robots has moved using the geometric formula: c = PI * d (c: the full circle and d: diameter).

jklug80's picture
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.
jklug80's picture
post code and a schematic on how you hooked it up please :)
Aniss1001's picture
Just posted schematic and code used ;)
jklug80's picture
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.
Aniss1001's picture

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

jklug80's picture
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
Aniss1001's picture
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...
robologist's picture
Looks great, now your robot is getting ready for closed loop speed control and mapping!
Aniss1001's picture
Step by step my ideas are becoming reality :)