How to use linear encoders with Arduino. hardware, code, and demo.

Поделиться
HTML-код
  • Опубликовано: 22 янв 2025

Комментарии • 109

  • @josegeraldoordones160
    @josegeraldoordones160 9 месяцев назад +1

    Thank you very much! This is the easiest and most useful code I found in 2024!

  • @josephcperry
    @josephcperry 11 лет назад +2

    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!

  • @thehangmansaxe
    @thehangmansaxe 8 лет назад +7

    I can't tell you how many printers you've saved from the dump with this video! Excellent work and thanks for the code!!!

  • @jimjones1652
    @jimjones1652 8 месяцев назад +1

    I was taking a few printers to bist today and thought about using the encoder strips this answeres my questions thanks.

  • @ragnarmarnikulasson3626
    @ragnarmarnikulasson3626 5 лет назад +2

    You sir are a gentleman and a scholar! Fantastic demenstrayion, been wondering about these for a while now

  • @asiszka
    @asiszka 8 лет назад +2

    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!

    • @TheRainHarvester
      @TheRainHarvester 4 года назад

      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.

  • @scottprints
    @scottprints 2 года назад +2

    That was awesome! Thanks so much for the down-to-earth and very useful video! Now I need to go find an encoder somewhere...

  • @MrCTNalves
    @MrCTNalves 6 лет назад +2

    After googling for days looking for some really helpful tutorial I finaly found! Thank you very much!

  • @rbaleksandar
    @rbaleksandar 9 лет назад +2

    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!

  • @tnop2002
    @tnop2002 11 лет назад +2

    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.

  • @bena2.0
    @bena2.0 2 года назад +2

    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.

  • @ErnstsBlog
    @ErnstsBlog 9 лет назад +2

    Very good explanation! The best video about the topic I found so far.

  • @darkshadowsx5949
    @darkshadowsx5949 3 года назад +2

    i salvaged the linear and rotary encoder parts from a paper printer and this would be very helpful in using them.
    thanks.

  • @tmcr3318
    @tmcr3318 3 года назад +2

    Excellent work!
    Concise.
    Thank You!

  • @WhoWantsToKnow81
    @WhoWantsToKnow81 9 лет назад +3

    Thanks for the great tutorial! I love reusing valuable parts from printers.

  • @houstonfirefox
    @houstonfirefox 9 лет назад +3

    Very nicely done! Would love to see something similar with the circular encoder. Great video!

    • @rklauco
      @rklauco 9 лет назад +1

      +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 :(

    • @jamesvirtudazo
      @jamesvirtudazo 6 лет назад +1

      Robert Klauco Please tell me how you identified the pinouts. I have fried 3 encoders already.

  • @TheRainHarvester
    @TheRainHarvester 4 года назад +1

    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.

  • @jorgecarlosalpirepedraza9548
    @jorgecarlosalpirepedraza9548 6 лет назад +1

    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.

  • @leonardochile8735
    @leonardochile8735 8 лет назад +2

    Excelente explicación. Logre hacerlo funcionar gracias a tu video. Gracias. Saludos desde Chile.

  • @ganes93
    @ganes93 12 лет назад +1

    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?

  • @yssing
    @yssing 5 лет назад +2

    I think this is exactly what I am looking for!

  • @manuelbergmann9222
    @manuelbergmann9222 11 лет назад +1

    Thank you very much for this! I use that in my current project (embroidery machine) - especially the code emphasing triggers is quite helpful

  • @tomjensen1289
    @tomjensen1289 10 лет назад +3

    Thank a million for a very informative video and a super perfect code
    kind regards Tom Jensen

  • @aeropau
    @aeropau 9 лет назад +1

    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

    • @TheRainHarvester
      @TheRainHarvester 4 года назад +1

      Were you able to control for only 1 step at a time?

  • @dattasaurabh82
    @dattasaurabh82 10 лет назад +6

    This video saved my life..

  • @juanandersondanielcamilode8793
    @juanandersondanielcamilode8793 5 лет назад +1

    How do you get to the screen where you get all your data point? The code works but I can't see those values...

  • @bryanrolfe5617
    @bryanrolfe5617 11 лет назад +2

    Fantastic work sir, keep it up.

  • @tomzh8627
    @tomzh8627 3 года назад +2

    Thank you for showing this!

  • @das250250
    @das250250 8 лет назад +2

    Very well explained

  • @nesscamelo
    @nesscamelo 6 лет назад +1

    Can I do the same using and optical switches reflective sensor?

  • @bergweg
    @bergweg 12 лет назад +1

    would this code and Arduino pin connection setup work with a mouse wheel encoder?

  • @riqued.7074
    @riqued.7074 3 года назад +1

    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?

  • @chiragcomputers3410
    @chiragcomputers3410 6 лет назад +2

    very helpful video

  • @thephatnguyen
    @thephatnguyen 10 лет назад +1

    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?

  • @1VERTIGO59
    @1VERTIGO59 11 лет назад +1

    Great job in detail of explaining. Thank you so much.

  • @smartdustbin7646
    @smartdustbin7646 6 лет назад +2

    many thanks man you are awesome
    this is so helpful

  • @Assakshay584
    @Assakshay584 6 лет назад +2

    Thank you so much for the video! :)

  • @tomaswilhelmsson5621
    @tomaswilhelmsson5621 10 лет назад +2

    Super nice video thanks!

  • @ELJORGE1
    @ELJORGE1 11 лет назад +1

    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

  • @sebascarra
    @sebascarra 9 лет назад +1

    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.

    • @sebascarra
      @sebascarra 9 лет назад +1

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

    • @sebascarra
      @sebascarra 9 лет назад

      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.

    • @kbruin79
      @kbruin79 7 лет назад

      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.

  • @filipstojanovicmechanicale9265
    @filipstojanovicmechanicale9265 2 года назад

    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?

  • @chrisgavin
    @chrisgavin 11 лет назад +1

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

  • @kbruin79
    @kbruin79 7 лет назад +2

    Great video, thank you for this.

  • @chesspro13
    @chesspro13 8 лет назад +1

    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.

    • @chesspro13
      @chesspro13 8 лет назад

      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.

    • @PiefacePete46
      @PiefacePete46 7 лет назад

      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.

  • @ghostrider090
    @ghostrider090 10 лет назад +3

    Very useful, thanks a lot man!

  • @btomas225
    @btomas225 4 года назад +1

    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--;
    }

    • @scottrharris
      @scottrharris  4 года назад

      lower case “if” not “If”

  • @frankydebruyne9842
    @frankydebruyne9842 9 лет назад +4

    Great video ... It certainly helped me !

  • @spilperson
    @spilperson 11 лет назад +1

    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!

  • @ekaggrat
    @ekaggrat 7 лет назад

    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

  • @JacobClarke
    @JacobClarke 10 лет назад +1

    This has been really helpful. Thanks!

  • @plaije
    @plaije 9 лет назад +2

    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.

    • @kbruin79
      @kbruin79 7 лет назад +1

      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.

  • @alexisantoniocruzcerda4661
    @alexisantoniocruzcerda4661 5 лет назад +1

    you can used rotary encoder librery, linear enconder and rotary econder work same.

  • @HalfLife2Beta
    @HalfLife2Beta 8 лет назад

    Any tips to get the optical encoder wires on a printer without removing the board off the printing head ?

    • @thehangmansaxe
      @thehangmansaxe 7 лет назад

      www.lemilica.com/kako-iskoristiti-stari-tintni-pisac/

  • @jorloujr
    @jorloujr 12 лет назад

    I could not find the Arduinosketch anywhere, is it available ? Thanks !

  • @fadelmuhammad-grafisium1898
    @fadelmuhammad-grafisium1898 3 года назад

    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?

  • @3dprintwiz378
    @3dprintwiz378 8 лет назад +2

    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.

    • @jegorbessonov8417
      @jegorbessonov8417 8 лет назад

      I am also interested in that. A reply would be very helpful :)

    • @PiefacePete46
      @PiefacePete46 7 лет назад

      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)

    • @Enigma758
      @Enigma758 7 лет назад +1

      If you're polling, and doing other things in your code, you could miss transitions.

  • @scottrharris
    @scottrharris  12 лет назад

    I don't have the Arduino motor shield.

  • @chychywoohoo
    @chychywoohoo 7 лет назад

    So at what frequency does the arduino become inaccurate?

  • @MickLumsdon
    @MickLumsdon 11 лет назад +2

    Great video! What kind of printer did it come from, and what level of resolution does it provide?

  • @danielmcnamara_
    @danielmcnamara_ 11 лет назад +1

    Very cool video is there a way to improve the resolution?

  • @HalfLife2Beta
    @HalfLife2Beta 8 лет назад

    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

    • @thehangmansaxe
      @thehangmansaxe 7 лет назад

      www.lemilica.com/kako-iskoristiti-stari-tintni-pisac/

  • @Enigma758
    @Enigma758 7 лет назад +1

    I think you could just assign count into another variable and display the other variable. The assignment should be atomic.

  • @AhPiaoGor
    @AhPiaoGor 10 лет назад +1

    thx bro.. this has been really helpful to me !

    • @lucasmun1066
      @lucasmun1066 10 лет назад

      How many pins did your encoder have? 4 or 6? I having though luck working this thing for couple of weeks.

    • @AhPiaoGor
      @AhPiaoGor 10 лет назад

      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.

  • @josemakera2820
    @josemakera2820 6 лет назад +1

    excelente explicación y muy buen aporte , lastima que no este en español...

  • @ganes93
    @ganes93 12 лет назад

    please if you could write a sketch for L298 Board or any stepper motor controller to arduino.Thanks.

  • @mr_mun5034
    @mr_mun5034 10 лет назад +1

    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

  • @amikhimji
    @amikhimji 11 лет назад +1

    Great video thanks

  • @elsayedosama8244
    @elsayedosama8244 8 лет назад

    please the same function code but for rotary encoder

  • @thai19995
    @thai19995 7 лет назад

    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

    • @brubilucoteclado
      @brubilucoteclado 7 лет назад

      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!

    • @thai19995
      @thai19995 7 лет назад

      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.

  • @pinaut4803
    @pinaut4803 9 лет назад +1

    nice tutorial. thx!

  • @davidcircuity6473
    @davidcircuity6473 11 лет назад

    how accurate are these e.g. 0.001in?

  • @lucasmun1066
    @lucasmun1066 10 лет назад

    What if there is only 4 pins.... When I do this, it just goes one way and not the other way.

  • @scottrharris
    @scottrharris  12 лет назад

    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.

  • @scottrharris
    @scottrharris  12 лет назад

    The sketch is shown at 5:30, but I'll add it to the description. It's trivial and short.

  • @byugrad1024
    @byugrad1024 11 лет назад

    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?

  • @emadgkamel
    @emadgkamel 9 лет назад

    man i love it
    thank you very much

  • @supertruckertom
    @supertruckertom 12 лет назад

    I bought my first Arduino Friday night.

  • @Deshbhakt_Indian229
    @Deshbhakt_Indian229 5 лет назад

    Can i use Arduino Uno ?

  • @ArcAiN6
    @ArcAiN6 8 лет назад

    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

    • @sahildwivedi4802
      @sahildwivedi4802 8 лет назад +2

      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.

    • @kbruin79
      @kbruin79 7 лет назад

      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.

  • @markomalapitan3696
    @markomalapitan3696 7 лет назад

    i love it
    very nice
    commeded

  • @emreberat8712
    @emreberat8712 6 лет назад

    i got a hp printer , so i can use these for my future project , thanks!

  • @TheCode-X
    @TheCode-X 10 лет назад

    Lexmark x3350 Printer, i have the SAME PIECES from a recycled inkjet printer.

  • @arduinomaquinas
    @arduinomaquinas Год назад +1

    Excelent job❤thanks for video😉👍🇧🇷🇧🇷awesome lesson 👏👏👏 @arduinomaquinas thanks you and like 972