I'm amazed at how little google comes up with on this subject. This is the most information I've been able to find in weeks. Thanks so much for sharing this!
Working perfectly with both linear and rotary encoders pulled out of an old HP printer. Will be very helpful for my cnc machine. Thank you very much for this!
Awesome. Just dissected a printer and one of the treasures I got from it were one linear encoder (for the carriage) and one circular encoder (for the control of the paper during printing). This is a very helpful video!
Thanks for the demo. I was able to make this work with the adafruit motor shield and arduino to move a printer head back and forth. While it will move to a particular value, it tends to hunt back and forth a bit first. Your example made it all possible and I will continue to work on it to perfect it. I am trying to use it with an XY plotter.
Do you have any idea why this is code is not working on an arduino pro micro? My board has an ATmega32U4 and I modified the line: attachInterrupt(0 , handleEncoder, CHANGE); to: attachInterrupt(digitalPinToInterrupt(encoderI), handleEncoder, CHANGE); I've also tried the code on an arduino nano and it works without a problem, but in the micro I get a lot of random jumps on the count value.
+Houston Firefox I used the same steps for circular encoder and it worked just fine. Took me a little while to identify the pins, though, as the part had no signature and therefore no specs :(
I'm going to make a 3d printer with these sleds. (Stay tuned!) I wonder if I can make one of the lines on the encoder, near the end, wider so I can notice when/where the print carriage is. The trick will be knowing the waveform is longer on one photo receiver but not the other. But the speed could change to make this happen too. So it will only be noticed during a homing sequence with constant pwm. Great video! There aren't a lot on the subject. Subscribed.
Excellent explanation and video. Many thanks and blessings for sharing your knowledge, I am 100% sure that it has been very useful for people like me who were searching the internet for information that you have given in a simple way and very easy to understand. Greetings from Santa Cruz - Bolivia. Excelente explicacion y video. Mil gracias y bendiciones por compartir tus conocimientos, estoy seguro 100% que ha sido de muchisima utilidad para personas que como yo estuvo buscando en internet información que has dado de forma sencilla y muy fácil de entender. Saludos desde Santa Cruz - Bolivia.
thanks you very much for uploading your explanation of this way to control the position of a motor. I really apreciate it because I 'm trying to control a old printer motor and you give me some light about it
Hello. I tried to read the signal directlly from the rotary encoder of a working HP inkjet printer using your code. The signal in this printer encoder varies between 0.6V to 2.5V (aproximated values). I believe that, specifically in this case, 0.5v should be interpreted as Low level and 2.5V high level. However, when I simply touch the shaft, the counter changes suddenlly to high values as 1,000 counts. I´m not sure if 2.5V is the treshold between low and high, and due to that, the interrupt pin count many transistion around this voltage value. Would you have any tip to make possible correctlly count the number of transitions? I was going in the direction to do a voltage follower or something like this. What do you think?
I am quite curious about the pullups, I happened to de-solder the optical sensor, and I was wondering if the pull ups resistance should be 300-400 ohms like in the beginning of the video? Does it matters if I go for a 10k pullups?
hey thanks a lot for the info in this sensor! i was looking for it. I am new to your channel and i see you have some nice videos so i will subscribe :) Thanks a lot
Hey, I tried doing this but whenever i slide the strip through the sensor it only moves from 0 to 1 or -1 back and forth! It's almost impossible to see another number. I think that I got the connections right because the sensor is lit up (pins for power are right) and I assumed the other two pins to be for the I and Q signals. So in summary, nothing "blew up" but the sensor doesn't work as intended either.
Experimenting a little with the code, I found out that the counter goes down by 1 almost every time it goes up!! If I comment the count-- line, the counter goes up every time, regardless of the direction of motion. I am going to check the I and Q connections for shorts, otherwise I have no idea of what's going on...
I was connecting the encoder in a wrong manner. You should recheck your connections, the code works perfectly. Pay attention to the numbers the pins are assigned. The code provided by this guy is not very clear in that sense.
I had the same issue using pin 2 & 3 however in the Pin define I &Q should be pin connected to 2 & 4. Once you connect Q to pin 4 instead of 3 then it works like a charm.
I found a pcb with the same encoder. I just can't figure out the pins on the receiver side. I found somewhere that there is signal 1 and signal 2 pins and other two pins which are Vcc and ground. I just can't thrust that the connections are exactly the same on that encoder and mine. Any tips and advices?
Really interesting and thorough, thanks. I've just started trying to use Arduino for my DIY cine film-scanning system... I've been thinking about these optical encoders for a while, as a possible means to identify the linear position of the film passing through the unit. Could I use these encoders I wonder to detect the leading and trailing edges of film sprocket holes passing? If so, I wonder what the accuracy of such a system might be? Many thanks if you've got any hints or ideas on this...
What about a 6 pin? I am assuming that I would just need to provide the LED it's own pos/ground (with a resistor) and wire everything the same as you do, but I don't want to ruin the encoder.
I just got it working, and if anyone else has this question, yes. You do power the LED with its own separate Pos/Ground. I used a 200ohm resistor with 3.3 volts to power the LED. Great tutorial! Thank you very much! Almost a month of head scratching is over.
I have been experimenting with a 6 pin encoder from a Canon printer. It was mounted on a small PCB (which I left attached). The PCB incorporated the resistor and capacitor for the LED, and it only had 4 wires to it... Ground, +5v, output from sensor 1, and output from sensor 2. This made connecting it up much simpler.
I know this is old but I tried this code and can only get it to compile using a while in place of the if statement. I get an error (if not declared in this scope) with the if statement but not when using the while: If (digitalRead (encIn_P_pin) == digitalRead(encQIn_P_pin)) { countEnc++; } else { countEnc--; }
Great explanation. I did quadrature measurement a long time ago, this was a good refresher for me, so thanks for that. Can you tell me what the resolution of that code strip? I am looking for something I can use for position indication on a small milling machine, maybe future control with a motor. Thanks!
i am looking at making a diy absolute linear encoders.. Do you have any sources that you have seen before. one idea is to use a cheap ir reflective sensor and a gradient color strip... any more ways of doing it.. i cant seam to find a good source
I'm getting 0 and 1 with the 2 and 3 connections one way, then when swapped I get 0 to -2. It appears I have the same equipment, brand anyway(Lexmark). I used the wheel that was matched to the encoder and a strip from another printer with the same results. To start I copied and pasted your code. Any suggestions? Thank you for getting me started regardless, I've been wanting to figure these out for some time.
I had the same issue using pin 2 & 3 however in the Pin define I &Q should be pin connected to 2 & 4. Once you connect Q to pin 4 instead of 3 then it works like a charm.
I want to ask, why my sensor printer while display value read data in serial monitor never decrement? The value data in serial monitor always increment?
In this example I don't think it is essential to use interrupts, because the Arduino is doing nothing but monitoring the two inputs. As soon as you add code to perform other chores, it is likely that a change in logic level on either input will be missed, and the variable "count" will not be updated. There would be a noticeable slowdown as well. The interrupt looks after the job of watching the input for a logic level change. This allows the processor to carry on with other tasks until the logic level changes... when it is interrupted by the interrupt, which is probably where it got its name! :o)
I succeeded int ogetting the wires and printhead assembly intact but how did you figure the pinout (ground, vcc, a and b) ? did you use a logic analyser? thanks in advance
Hi, my encoder have 4 pin, if you look from top , you will get ___________ | | __| Encoder |__ |_______________| | | | | GND A VCC B connect power supply from arduino to GND and VCC 5V+ connect A to arduino pin 2 connect B to arduino pin 4 then use code from this video , then you make it. if you already connect VCC to pin A or B , the encoder will not function anymore. you have to find other encoder.
Hello, I tried this with a linear encoder with 4 pins with out 5 pins that I believe is what you are using. I just couldn't get to work. I mean it shows count but it only goes increment in positive, not negative when I move the strip the opposite way. Any thing you have in mind? Using Arduino Mega by the way and exact same codes that you are using :D
Hi, which printer did you get that part from ? I've got one from my HP printer and it seems like the pinout is different from yours. So, i just put 5V to Vcc, and 0V to pin GND then watch the pulse in the Osciiloscope ? Do i need to connect anything else, ie. the 2 pins of the led ? (Mine is a 6-pin version) Thanks
Hey! still need the info? I was trying to reuse a 6-pin HP linear encoder sensor too and it worked by following this diagram: reprap.org/wiki/File:FD_opto_sensor_pinout_02.JPG Cheers!
By doing it the way you have with triggering on the "I" line, you sacrifice half of your available encoder resolution, and you aren't really running in quadrature. Also, if you used a polling approach instead of an interrupt approach, I think you'll be better off. Don't you think?
Correct me if i'm wrong, but doesn't delay cause the entire program to halt for the defined delay time? Wouldn't that cause the program to miss counts, leading to an inaccurate count? The only reason i bring it up, is people needing an encoder like this for their project, only really need it for accuracy, over time, the delay would cause this count to be so far off, that all accuracy is gone. That being said. Very well described operation of an optical linear encoder :D
The delay only causes a delay in the loop. The values of the encoders are independent of the code and will change accordingly. Only the time it waits to print the value changes, the encoder values vary in real time.
Thank you very much! This is the easiest and most useful code I found in 2024!
I'm amazed at how little google comes up with on this subject. This is the most information I've been able to find in weeks. Thanks so much for sharing this!
I can't tell you how many printers you've saved from the dump with this video! Excellent work and thanks for the code!!!
I was taking a few printers to bist today and thought about using the encoder strips this answeres my questions thanks.
You sir are a gentleman and a scholar! Fantastic demenstrayion, been wondering about these for a while now
Working perfectly with both linear and rotary encoders pulled out of an old HP printer. Will be very helpful for my cnc machine. Thank you very much for this!
Did you get it working? I find stepping only 1 step is difficult because the motor overshoots / or has to move really slow to not overshoot.
That was awesome! Thanks so much for the down-to-earth and very useful video! Now I need to go find an encoder somewhere...
After googling for days looking for some really helpful tutorial I finaly found! Thank you very much!
Awesome. Just dissected a printer and one of the treasures I got from it were one linear encoder (for the carriage) and one circular encoder (for the control of the paper during printing). This is a very helpful video!
Thanks for the demo. I was able to make this work with the adafruit motor shield and arduino to move a printer head back and forth. While it will move to a particular value, it tends to hunt back and forth a bit first. Your example made it all possible and I will continue to work on it to perfect it. I am trying to use it with an XY plotter.
Do you have any idea why this is code is not working on an arduino pro micro?
My board has an ATmega32U4 and I modified the line:
attachInterrupt(0 , handleEncoder, CHANGE);
to:
attachInterrupt(digitalPinToInterrupt(encoderI), handleEncoder, CHANGE);
I've also tried the code on an arduino nano and it works without a problem, but in the micro I get a lot of random jumps on the count value.
Very good explanation! The best video about the topic I found so far.
i salvaged the linear and rotary encoder parts from a paper printer and this would be very helpful in using them.
thanks.
Excellent work!
Concise.
Thank You!
Thanks for the great tutorial! I love reusing valuable parts from printers.
Very nicely done! Would love to see something similar with the circular encoder. Great video!
+Houston Firefox I used the same steps for circular encoder and it worked just fine. Took me a little while to identify the pins, though, as the part had no signature and therefore no specs :(
Robert Klauco Please tell me how you identified the pinouts. I have fried 3 encoders already.
I'm going to make a 3d printer with these sleds. (Stay tuned!)
I wonder if I can make one of the lines on the encoder, near the end, wider so I can notice when/where the print carriage is. The trick will be knowing the waveform is longer on one photo receiver but not the other.
But the speed could change to make this happen too. So it will only be noticed during a homing sequence with constant pwm.
Great video! There aren't a lot on the subject. Subscribed.
Excellent explanation and video. Many thanks and blessings for sharing your knowledge, I am 100% sure that it has been very useful for people like me who were searching the internet for information that you have given in a simple way and very easy to understand. Greetings from Santa Cruz - Bolivia.
Excelente explicacion y video. Mil gracias y bendiciones por compartir tus conocimientos, estoy seguro 100% que ha sido de muchisima utilidad para personas que como yo estuvo buscando en internet información que has dado de forma sencilla y muy fácil de entender. Saludos desde Santa Cruz - Bolivia.
Excelente explicación. Logre hacerlo funcionar gracias a tu video. Gracias. Saludos desde Chile.
very well but how to drive a stepper motor as you slide the linear encoder ?could you please tell me whats the output pins for arduino motor shield?
I think this is exactly what I am looking for!
Thank you very much for this! I use that in my current project (embroidery machine) - especially the code emphasing triggers is quite helpful
Thank a million for a very informative video and a super perfect code
kind regards Tom Jensen
thanks you very much for uploading your explanation of this way to control the position of a motor. I really apreciate it because I 'm trying to control a old printer motor and you give me some light about it
Were you able to control for only 1 step at a time?
This video saved my life..
How do you get to the screen where you get all your data point? The code works but I can't see those values...
Fantastic work sir, keep it up.
Thank you for showing this!
Very well explained
Can I do the same using and optical switches reflective sensor?
would this code and Arduino pin connection setup work with a mouse wheel encoder?
Hello. I tried to read the signal directlly from the rotary encoder of a working HP inkjet printer using your code. The signal in this printer encoder varies between 0.6V to 2.5V (aproximated values). I believe that, specifically in this case, 0.5v should be interpreted as Low level and 2.5V high level. However, when I simply touch the shaft, the counter changes suddenlly to high values as 1,000 counts. I´m not sure if 2.5V is the treshold between low and high, and due to that, the interrupt pin count many transistion around this voltage value. Would you have any tip to make possible correctlly count the number of transitions? I was going in the direction to do a voltage follower or something like this. What do you think?
very helpful video
I am quite curious about the pullups, I happened to de-solder the optical sensor, and I was wondering if the pull ups resistance should be 300-400 ohms like in the beginning of the video? Does it matters if I go for a 10k pullups?
Great job in detail of explaining. Thank you so much.
many thanks man you are awesome
this is so helpful
Thank you so much for the video! :)
Super nice video thanks!
hey thanks a lot for the info in this sensor! i was looking for it. I am new to your channel and i see you have some nice videos so i will subscribe :) Thanks a lot
Hey, I tried doing this but whenever i slide the strip through the sensor it only moves from 0 to 1 or -1 back and forth! It's almost impossible to see another number. I think that I got the connections right because the sensor is lit up (pins for power are right) and I assumed the other two pins to be for the I and Q signals. So in summary, nothing "blew up" but the sensor doesn't work as intended either.
Experimenting a little with the code, I found out that the counter goes down by 1 almost every time it goes up!! If I comment the count-- line, the counter goes up every time, regardless of the direction of motion. I am going to check the I and Q connections for shorts, otherwise I have no idea of what's going on...
I was connecting the encoder in a wrong manner. You should recheck your connections, the code works perfectly. Pay attention to the numbers the pins are assigned. The code provided by this guy is not very clear in that sense.
I had the same issue using pin 2 & 3 however in the Pin define I &Q should be pin connected to 2 & 4. Once you connect Q to pin 4 instead of 3 then it works like a charm.
I found a pcb with the same encoder. I just can't figure out the pins on the receiver side. I found somewhere that there is signal 1 and signal 2 pins and other two pins which are Vcc and ground. I just can't thrust that the connections are exactly the same on that encoder and mine. Any tips and advices?
Really interesting and thorough, thanks. I've just started trying to use Arduino for my DIY cine film-scanning system... I've been thinking about these optical encoders for a while, as a possible means to identify the linear position of the film passing through the unit. Could I use these encoders I wonder to detect the leading and trailing edges of film sprocket holes passing? If so, I wonder what the accuracy of such a system might be? Many thanks if you've got any hints or ideas on this...
Great video, thank you for this.
What about a 6 pin? I am assuming that I would just need to provide the LED it's own pos/ground (with a resistor) and wire everything the same as you do, but I don't want to ruin the encoder.
I just got it working, and if anyone else has this question, yes. You do power the LED with its own separate Pos/Ground. I used a 200ohm resistor with 3.3 volts to power the LED. Great tutorial! Thank you very much! Almost a month of head scratching is over.
I have been experimenting with a 6 pin encoder from a Canon printer. It was mounted on a small PCB (which I left attached). The PCB incorporated the resistor and capacitor for the LED, and it only had 4 wires to it... Ground, +5v, output from sensor 1, and output from sensor 2. This made connecting it up much simpler.
Very useful, thanks a lot man!
I know this is old but I tried this code and can only get it to compile using a while in place of the if statement. I get an error (if not declared in this scope) with the if statement but not when using the while:
If (digitalRead (encIn_P_pin) == digitalRead(encQIn_P_pin))
{
countEnc++;
}
else
{
countEnc--;
}
lower case “if” not “If”
Great video ... It certainly helped me !
Great explanation. I did quadrature measurement a long time ago, this was a good refresher for me, so thanks for that. Can you tell me what the resolution of that code strip? I am looking for something I can use for position indication on a small milling machine, maybe future control with a motor. Thanks!
i am looking at making a diy absolute linear encoders.. Do you have any sources that you have seen before. one idea is to use a cheap ir reflective sensor and a gradient color strip... any more ways of doing it.. i cant seam to find a good source
This has been really helpful. Thanks!
I'm getting 0 and 1 with the 2 and 3 connections one way, then when swapped I get 0 to -2.
It appears I have the same equipment, brand anyway(Lexmark).
I used the wheel that was matched to the encoder and a strip from another printer with the same results. To start I copied and pasted your code.
Any suggestions?
Thank you for getting me started regardless, I've been wanting to figure these out for some time.
I had the same issue using pin 2 & 3 however in the Pin define I &Q should be pin connected to 2 & 4. Once you connect Q to pin 4 instead of 3 then it works like a charm.
you can used rotary encoder librery, linear enconder and rotary econder work same.
Any tips to get the optical encoder wires on a printer without removing the board off the printing head ?
www.lemilica.com/kako-iskoristiti-stari-tintni-pisac/
I could not find the Arduinosketch anywhere, is it available ? Thanks !
I want to ask, why my sensor printer while display value read data in serial monitor never decrement? The value data in serial monitor always increment?
The sensor same like the tutorial , the wiring, the pin
and i connect to arduino nano.
Hi thanks for the informative vid. Just wanted to know though, what is the reason of using interrupts instead of just reading digital pins from loop.
I am also interested in that. A reply would be very helpful :)
In this example I don't think it is essential to use interrupts, because the Arduino is doing nothing but monitoring the two inputs. As soon as you add code to perform other chores, it is likely that a change in logic level on either input will be missed, and the variable "count" will not be updated. There would be a noticeable slowdown as well.
The interrupt looks after the job of watching the input for a logic level change. This allows the processor to carry on with other tasks until the logic level changes... when it is interrupted by the interrupt, which is probably where it got its name! :o)
If you're polling, and doing other things in your code, you could miss transitions.
I don't have the Arduino motor shield.
So at what frequency does the arduino become inaccurate?
Great video! What kind of printer did it come from, and what level of resolution does it provide?
Very cool video is there a way to improve the resolution?
I succeeded int ogetting the wires and printhead assembly intact but how did you figure the pinout (ground, vcc, a and b) ? did you use a logic analyser? thanks in advance
www.lemilica.com/kako-iskoristiti-stari-tintni-pisac/
I think you could just assign count into another variable and display the other variable. The assignment should be atomic.
thx bro.. this has been really helpful to me !
How many pins did your encoder have? 4 or 6? I having though luck working this thing for couple of weeks.
Hi, my encoder have 4 pin, if you look from top , you will get
___________
| |
__| Encoder |__
|_______________|
| | | |
GND A VCC B
connect power supply from arduino to GND and VCC 5V+
connect A to arduino pin 2
connect B to arduino pin 4
then use code from this video , then you make it.
if you already connect VCC to pin A or B , the encoder will not function anymore. you have to find other encoder.
excelente explicación y muy buen aporte , lastima que no este en español...
please if you could write a sketch for L298 Board or any stepper motor controller to arduino.Thanks.
Hello, I tried this with a linear encoder with 4 pins with out 5 pins that I believe is what you are using. I just couldn't get to work. I mean it shows count but it only goes increment in positive, not negative when I move the strip the opposite way. Any thing you have in mind? Using Arduino Mega by the way and exact same codes that you are using :D
Great video thanks
please the same function code but for rotary encoder
Hi, which printer did you get that part from ?
I've got one from my HP printer and it seems like the pinout is different from yours.
So, i just put 5V to Vcc, and 0V to pin GND then watch the pulse in the Osciiloscope ?
Do i need to connect anything else, ie. the 2 pins of the led ? (Mine is a 6-pin version)
Thanks
Hey! still need the info?
I was trying to reuse a 6-pin HP linear encoder sensor too and it worked by following this diagram:
reprap.org/wiki/File:FD_opto_sensor_pinout_02.JPG
Cheers!
I followed that one too and it worked perfectly.
There's one more thing, do you know the resolution of that linear strip ?
Thanks for your concern.
nice tutorial. thx!
how accurate are these e.g. 0.001in?
What if there is only 4 pins.... When I do this, it just goes one way and not the other way.
Google: " L298 arduino stepper example" seems to bring up tons of information and a few videos. I don't have any plans to use that chip.
The sketch is shown at 5:30, but I'll add it to the description. It's trivial and short.
By doing it the way you have with triggering on the "I" line, you sacrifice half of your available encoder resolution, and you aren't really running in quadrature. Also, if you used a polling approach instead of an interrupt approach, I think you'll be better off. Don't you think?
man i love it
thank you very much
I bought my first Arduino Friday night.
Can i use Arduino Uno ?
Correct me if i'm wrong, but doesn't delay cause the entire program to halt for the defined delay time? Wouldn't that cause the program to miss counts, leading to an inaccurate count?
The only reason i bring it up, is people needing an encoder like this for their project, only really need it for accuracy, over time, the delay would cause this count to be so far off, that all accuracy is gone.
That being said. Very well described operation of an optical linear encoder :D
The delay only causes a delay in the loop. The values of the encoders are independent of the code and will change accordingly.
Only the time it waits to print the value changes, the encoder values vary in real time.
The delay will not affect the accuracy because it’s only in the main loop. The delay will not affect the interrupt which happen on pin level change.
i love it
very nice
commeded
i got a hp printer , so i can use these for my future project , thanks!
Lexmark x3350 Printer, i have the SAME PIECES from a recycled inkjet printer.
Excelent job❤thanks for video😉👍🇧🇷🇧🇷awesome lesson 👏👏👏 @arduinomaquinas thanks you and like 972