Jump to content
  • 0

Pmod Enc and Raspberry Pi not accurate


tpitman

Question

I have connected a pmod enc rotary encoder to a Raspberry Pi 3+ B through GPIO.

I was not able to fine any examples of how to decode the rotation, so I just looked at an Arduino example and also just read the hardware documentation on it.

From what I can tell I should be able to have an interrupt on both the A and B pins. Then if I set a flag when A triggers and then if B triggers check to see if the A flag is set that would tell me that button A triggered first. Then do the other way around and that should do it.

The problem is this isn't working.

The Arudino example tried a different approach. It triggered only on the falling edge of just A and then used this code:

if (digitalRead(A)==HIGH)
{
  sens = digitalRead(B); // Clockwise
}
else
{
  sens = !digitalRead(B); // anticlockwise
}

This didn't work well either.

When I do the dual interrupt method and keeping a flag to determine which one triggered first, it works better than the above Arduino code, but still not great.

When going clockwise it it pretty good. When going counter clockwise it is horrible. It triggers saying clockwise 50% of the time and counter the other 50%.

I look at it on a scope and the falling edges are all over the place. The rising edges seem to be pretty good about A following B or B following A, but they are super close together. They are about 50ns apart. That could be my whole probably. Maybe Python on the Pi just isn't fast enough.

Can someone please suggest a solution here? Does anyone have an example of using the Pmod Enc rotary encoder with the Raspberry Pi and Python?

 

Link to comment
Share on other sites

7 answers to this question

Recommended Posts

Ok, I feel really silly at this point.

For some reason I had it in my head that the encoder was set up in such a way that it was basically 2 switches to ground and that all I had to do was hook up the A and B lines and the Ground line. I figured the VCC was in case you want to have it go to VCC instead. Well, since I was going to pull my GPIO high and then compare to ground I just hooked up the A and B lines and Ground.

Turns out you have to hook up VCC as well no matter how you think you are going to use it.

Once I connected VCC to 3.3 volts everything works great now.

Sorry to bother everyone with my mistake.

Link to comment
Share on other sites

HI @tpitman,

I measured the Pmod ENC responses on a oscilloscope here and found out the following details (which I'll need to update the reference manual to properly reflect the information. When rotating clockwise, B gets a rising edge first, followed by A. Rotate counterclockwise, A gets a rising edge first, followed by B. The time difference between them was in the 5 to 8 ms range, so the Pi should be able to detect them just fine, though admittedly I wasn't rotating the knob super fast.

I have found in the past that when I rotated it too slowly, the microcontroller I was using would get confused while attempting to use an interrupt, likely because the edges were too far apart (and it's really hard to get just one "tick" rotation). While measuring with the oscilloscope this time around though, I was getting some pretty clean edges. The catch that I've seen with using a polling method with a microcontroller is that the logic states of the output pins A and B didn't always return to the same state each time, so depending on your code, that can give you some false information. I don't have any material for using the Pmod ENC with Python or the Raspberry Pi though.

Let me know if you have any questions about this.

Thanks,
JColvin

Link to comment
Share on other sites

Thank you for the reply.

First a question: When you say there is a rising edge are you talking about at the end of the pulse? As far as I can tell both A and B are normally HIGH and then go low when the internal "button" is being pressed. This implies that the rising edge you are talking about is as the "button" is being released.

If that is the case then I am not sure how you can be measuring a difference of 5-8ms. When I measure that difference it is about 50ns. If it was 5-8ms that would be awesome because I am sure the Pi could measure that no problem.

Can you shed any light on this?

Link to comment
Share on other sites

6 hours ago, JColvin said:

HI @tpitman,

I measured the Pmod ENC responses on a oscilloscope here and found out the following details (which I'll need to update the reference manual to properly reflect the information. When rotating clockwise, B gets a rising edge first, followed by A. Rotate counterclockwise, A gets a rising edge first, followed by B. The time difference between them was in the 5 to 8 ms range, so the Pi should be able to detect them just fine, though admittedly I wasn't rotating the knob super fast.

I have found in the past that when I rotated it too slowly, the microcontroller I was using would get confused while attempting to use an interrupt, likely because the edges were too far apart (and it's really hard to get just one "tick" rotation). While measuring with the oscilloscope this time around though, I was getting some pretty clean edges. The catch that I've seen with using a polling method with a microcontroller is that the logic states of the output pins A and B didn't always return to the same state each time, so depending on your code, that can give you some false information. I don't have any material for using the Pmod ENC with Python or the Raspberry Pi though.

Let me know if you have any questions about this.

Thanks,
JColvin

I think either I have this hooked up wrong or it is broken. I just looked at the documentation again and the image showing the timing of the pulses looks like they should be offset a LOT more than they are for me.

From what I can see on my scope the pulses are almost exactly right on top of each other. There is barely 10 nanoseconds (not milliseconds) between their rising pulses.

 

Link to comment
Share on other sites

I have a follow on questions about the pmodenc. I can't seem to find the full dimensions of the shaft that drives it. I have looked in several datasheets and can't find that.

I need to know the diameter of the shaft and the offset for keying dimensions.

Link to comment
Share on other sites

Hi @tpitman,

I don't have a nice datasheet to the the shaft encoder to directly link you to, but we just recently (i.e. today) added a 3D step file of the Pmod ENC to it's Resource Center, so hopefully you'll be able to get all of the dimensions you need from there.

Let me know if you have any questions about this.

Thanks,
JColvin

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...