Robotix92
Posts: 4
Joined: Tue Sep 11, 2018 4:02 pm

Quadrature encoder serial pins question

Tue Sep 11, 2018 4:13 pm

Hello,

I hate to just have made an account to ask a question, and i'm also not sure if this is the right section, but ... I've been following some guides online on setting up my pi and getting an lcd to work (all went well). Now i have a quadrature encoder, specifically a US Digital EM1-1000-N encoder (link below), which i want to get a reading telling me revolutions. I've been following a guide (link below), but it doesnt tell me how to wire it up. Any suggestions or ideas? Would really appreciate the help, Thanks!

https://www.usdigital.com/products/em1
https://walchko.github.io/blog/Robots/R ... oders.html

User avatar
MrYsLab
Posts: 371
Joined: Mon Dec 15, 2014 7:14 pm
Location: Noo Joysey, USA

Re: Quadrature encoder serial pins question

Tue Sep 11, 2018 5:47 pm

Since the output voltage specs show a range of 0.05 to 4.8 volts you can attach it directly to GPIO pins of your choice.

Here is a link to a simple Python program that should get you started:
http://abyz.me.uk/rpi/pigpio/examples.h ... encoder_py

User avatar
joan
Posts: 14196
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Quadrature encoder serial pins question

Tue Sep 11, 2018 5:59 pm

It has contacts marked A B I - + (not in that order).

Connect A and B to separate GPIO. Connect - to a Pi ground (say pin 6) and + to 5V (say pin 2). You may leave I unconnected. Configure the software you are using by specifying the GPIO connected to A and B.

https://pinout.xyz/

danjperron
Posts: 3393
Joined: Thu Dec 27, 2012 4:05 am
Location: Québec, Canada

Re: Quadrature encoder serial pins question

Tue Sep 11, 2018 6:07 pm

Since the output voltage specs show a range of 0.05 to 4.8 volts you can attach it directly to GPIO pins of your choice.
No you can't!
You need to use resistors divider or mosfet converter to step down output to 3.3V.

Check Sparkfun or adafruit i2c 5V to 3.3V adapter
https://www.adafruit.com/product/757

Robotix92
Posts: 4
Joined: Tue Sep 11, 2018 4:02 pm

Re: Quadrature encoder serial pins question

Tue Sep 11, 2018 6:10 pm

As far as i understand the encoder, it needs the pin needs to interrupt in order to accurately read the encoder signal (maybe?). Can i follow the code shown in the link and just define pins A, B?

And i guess i'll use some resistors to step down the voltages? I'll do a little more research on this, thanks for the replies! Any advice helps.

Code: Select all

import time
from serial import Serial

count = 0
COUNTS_TO_METERS = 0.001  # this depends on the encoder system

def main_loop():
  ser = Serial('/dev/tty.usbserial0', 115200)

  while True:
    time.sleep(1)  # time depends on speed of robot
    position += count * COUNTS_TO_METERS
    count = 0

    # a super simple serial response to report position
    if ser.read() == 'p':
      ser.write(position)

# an interrupt that gets called every time A or B changes
# you can do this with RPi.GPIO on the raspberry pi
def interrupt_AB():
  A, B = readEncoderPins()
  if A ^ B == 1:
    count += 1
  else:
    count -= 1

User avatar
MrYsLab
Posts: 371
Joined: Mon Dec 15, 2014 7:14 pm
Location: Noo Joysey, USA

Re: Quadrature encoder serial pins question

Tue Sep 11, 2018 6:17 pm

@danjperron is correct. Sorry, I have been playing with Arduinos recently and my brain was stuck on 5v GPIO. Here is a link to building a voltage divider: https://elinux.org/RPi_GPIO_Interface_Circuits

Robotix92
Posts: 4
Joined: Tue Sep 11, 2018 4:02 pm

Re: Quadrature encoder serial pins question

Tue Sep 11, 2018 6:21 pm

MrYsLab wrote:
Tue Sep 11, 2018 6:17 pm
@danjperron is correct. Sorry, I have been playing with Arduinos recently and my brain was stuck on 5v GPIO. Here is a link to building a voltage divider: https://elinux.org/RPi_GPIO_Interface_Circuits
Thank you! I actually got this same encoder working on the arduino. I'm not too familiar with the pi but i'm experimenting with it today. I want to eventually make it work with an LCD and a few other sensors

User avatar
joan
Posts: 14196
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Quadrature encoder serial pins question

Tue Sep 11, 2018 6:35 pm

You can use a pair of resistors of the same value as a resistor divider. You need one pair for A and another pair for B. That will drop the output voltage from 5V to a Pi safe 2.5V (still high enough to be seen as logic high at the Pi).

User avatar
joan
Posts: 14196
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Quadrature encoder serial pins question

Tue Sep 11, 2018 6:37 pm

I don't understand how the code you linked is relevant to the encoder. Have you got a circuit diagram to show the intended usage?

User avatar
OutoftheBOTS
Posts: 711
Joined: Tue Aug 01, 2017 10:06 am

Re: Quadrature encoder serial pins question

Tue Sep 11, 2018 7:31 pm

The code that was linked isn't complete and is just Python Pseudo Code not complete python code.

Reading quadrature encoders in python on RPi isn't very efficient to do because the fact that python on RPi isn't real time and has a lot of latency. The very easiest way to do it is with an external quadrature counter.

Here is a good video explaining the working of the encoder and best way of writing python code to count it see https://www.youtube.com/watch?v=p4BCFhIuC88&t=461s

And here is a video showing the advantage of using an external counter to do it. see https://www.youtube.com/watch?v=41ogu0UlwCc&t=12s

moderatelyfunky
Posts: 21
Joined: Mon May 07, 2018 7:12 pm
Location: Vermont, US

Re: Quadrature encoder serial pins question

Tue Sep 11, 2018 7:58 pm

I'm considering replacing the steppers in my project with dc and I wasn't sure where to start, but now I know.

Thanks Outofhebots, I really appreciate both of these videos!

Robotix92
Posts: 4
Joined: Tue Sep 11, 2018 4:02 pm

Re: Quadrature encoder serial pins question

Tue Sep 11, 2018 8:28 pm

OutoftheBOTS wrote:
Tue Sep 11, 2018 7:31 pm
The code that was linked isn't complete and is just Python Pseudo Code not complete python code.

Reading quadrature encoders in python on RPi isn't very efficient to do because the fact that python on RPi isn't real time and has a lot of latency. The very easiest way to do it is with an external quadrature counter.

Here is a good video explaining the working of the encoder and best way of writing python code to count it see https://www.youtube.com/watch?v=p4BCFhIuC88&t=461s

And here is a video showing the advantage of using an external counter to do it. see https://www.youtube.com/watch?v=41ogu0UlwCc&t=12s
Exactly what i needed !! I used a voltage divider with resistors are discussed above and it seems to work. My numbers are off though but i'll dig into this some more. My encoder has 1000 counts per rev but im getting like 3500 per rev! Maybe i can just add a divider in my code to compensate? Thanks a ton for the vids

Return to “Python”