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?
Question
tpitman
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
Archived
This topic is now archived and is closed to further replies.