Homemade wheel encoder
- Aniss1001's blog
- Login or register to post comments
Basically I just printed out one of these (laser printer recommended):

...And attached it to a wheel (double sided tape recommended).
Then I hooked up one of these (a 2$ IR sensor: QRB1134):

...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):


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

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 :)


@ Sun, 2009-11-01 14:54
Looks great, now your robot
@ Sun, 2009-11-01 22:11
Ya bet ya
@ Sun, 2009-11-01 15:02
post code and a schematic on
@ Sun, 2009-11-01 22:10
Done
@ Sun, 2009-11-01 22:15
Looks good but when you
@ Sun, 2009-11-01 22:38
D'OH!
@ Sun, 2009-11-01 23:07
Oh and...
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...
@ Sun, 2009-11-01 23:15
It is going to be a while it
@ Sun, 2009-11-01 23:53
Actually, any pin can be
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
@ Sun, 2009-11-01 21:47
Forgive me my ignorance but
@ Sun, 2009-11-01 21:55
It is an encoder. It counts
@ Sun, 2009-11-01 22:56
I understand
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).
@ Wed, 2009-12-02 15:57
Need Assistance...
////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 capacitorConnect the white wire (collector) from the sensor to pin 0, 10K resistor to VConnect the blue wire(emitter) to GConnect the green wire(cathode) to GConnect 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…