Pin ID on pyboard

The official pyboard running MicroPython.
This is the reference design and main target board for MicroPython.
You can buy one at the store.
Target audience: Users with a pyboard.
Andrew1234
Posts: 20
Joined: Sat Oct 16, 2021 3:58 pm

Pin ID on pyboard

Post by Andrew1234 » Mon Feb 14, 2022 10:34 pm

Hello:
I want to use a rotary encoder with the pyboard using the following module:
https://github.com/miketeachman/micropython-rotary

From documentation, the class RotaryIRQ has arguments pin_num_clk and pin_num_dt that have an integer value. These arguments specify the GPIOs on the microcontroller that are connected to the rotary encoder.

I am trying to figure out how I can use an integer with the pyboard to indicate the specific GPIOs to be connected to the encoder. The pyboard quick reference has a class indicating an 'id' parameter, but no description of what it is.
class pyb.Pin(id, ...)

Can 'id' be an integer that I define? If yes, how to map it to a particular pin (for example, pin 'X1')? Do I need to use Pin.dict or Pin.mapper class methods to do this?

From the quick reference for the ESP32, I see that 'id' is described and can be an integer:
id is mandatory and can be an arbitrary object. Among possible value types are: int (an internal Pin identifier), str (a Pin name), and tuple (pair of [port, pin]).
Probably it is the same as for the pyboard, but I still am not sure how this integer maps to a particular pin.

Can anyone help? An example is appreciated.
Regards
Andy

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: Pin ID on pyboard

Post by Roberthh » Tue Feb 15, 2022 7:12 am

the value and type used for id is specific to the respective board. For ESP32 or Pico, it is for instance an integer, for Pyboard, it it is a string like "X1". It can also be member from the Pin.board or Pin.cpu classes. Look into the machine.Pin documentation and the board specific quick reference for a detailed explanation.

Andrew1234
Posts: 20
Joined: Sat Oct 16, 2021 3:58 pm

Re: Pin ID on pyboard

Post by Andrew1234 » Tue Feb 15, 2022 12:51 pm

for Pyboard, it it is a string like "X1"

Yes, this is the what I haven't figured out. The rotary encoder module requires an integer, but the pyboard pin name is a string. The encoder module documentation example is for ESP32, but the documentation also says that the module was tested with a pyboard. So there is still something missing here. I'll study the module code more closely to see if I can find the answer. Thanks for your response.

Regards
Andy

OutoftheBOTS_
Posts: 847
Joined: Mon Nov 20, 2017 10:18 am

Re: Pin ID on pyboard

Post by OutoftheBOTS_ » Tue Feb 15, 2022 7:50 pm

Andrew1234 wrote:
Tue Feb 15, 2022 12:51 pm
for Pyboard, it it is a string like "X1"

Yes, this is the what I haven't figured out. The rotary encoder module requires an integer, but the pyboard pin name is a string. The encoder module documentation example is for ESP32, but the documentation also says that the module was tested with a pyboard. So there is still something missing here. I'll study the module code more closely to see if I can find the answer. Thanks for your response.

Regards
Andy
i had a quick look at the code on the github. First notice that there is a different files for diffderent boards, there is 1 for pyboard.
Capture.PNG
Capture.PNG (12.98 KiB) Viewed 3759 times
Don't worry that it says it needs an integer just pass the string for the pin for the pyboard and it will work looking at the code

User avatar
Mike Teachman
Posts: 155
Joined: Mon Jun 13, 2016 3:19 pm
Location: Victoria, BC, Canada

Re: Pin ID on pyboard

Post by Mike Teachman » Wed Feb 16, 2022 2:19 pm

Andrew1234 wrote:
Tue Feb 15, 2022 12:51 pm
for Pyboard, it it is a string like "X1"
Yes, this is the what I haven't figured out. The rotary encoder module requires an integer, but the pyboard pin name is a string. The encoder module documentation example is for ESP32, but the documentation also says that the module was tested with a pyboard. So there is still something missing here. I'll study the module code more closely to see if I can find the answer. Thanks for your response.
I am the author of this repo. Sorry that the example is confusing. I see that other forum members have given some help for the pyboard pin references. In the next module update, I will add pin examples for the pyboards. Any other feedback on this module is always appreciated. Thanks.

note: I also plan to allow Pin class objects to be passed into the initializer (that is a best practice in MicroPython and I should have implemented this long ago).

OutoftheBOTS_
Posts: 847
Joined: Mon Nov 20, 2017 10:18 am

Re: Pin ID on pyboard

Post by OutoftheBOTS_ » Wed Feb 16, 2022 9:51 pm

Mike Teachman wrote:
Wed Feb 16, 2022 2:19 pm
note: I also plan to allow Pin class objects to be passed into the initializer (that is a best practice in MicroPython and I should have implemented this long ago).
Yer I like it like that and the same if the driver needs I2C that you pass an object rather than the pin numbers :)

Andrew1234
Posts: 20
Joined: Sat Oct 16, 2021 3:58 pm

Re: Pin ID on pyboard

Post by Andrew1234 » Thu Feb 17, 2022 1:21 am

Don't worry that it says it needs an integer just pass the string for the pin for the pyboard and it will work looking at the code

Yes thank you OutoftheBOTS_, you are right. I passed the string for the pin name and I now get a response from the encoder.

And thanks Mike for your response. Generally I find the document and code for your repo to be very clear. I was hung up on where the document indicates pin_num_clk and pin_num_dat should be integers, which isn't how the pyboard pins are defined.

I'm trying your code with a more expensive encoder that claims 600 P/R. I'll report on any issue or other feedback in the forum.

Thanks
Andy

OutoftheBOTS_
Posts: 847
Joined: Mon Nov 20, 2017 10:18 am

Re: Pin ID on pyboard

Post by OutoftheBOTS_ » Thu Feb 17, 2022 10:05 am

Using software python interrupts to count encoders works fine as long as the freq at which the encoder tick at is low. You will notice that at the repo it has a pic of a dial that just gets turned by hand and will only have low tick freq.

If you want to count a fast encoder you will need to use the hardware encoder counters that are on the pyboards timers. I have never used these in python but have used them in a C project. They are 16 bit counters so get wrap around easy. How I used them was to set the count register to the middle of the count so no matter whether it is counting up or down it is a log way from wrap around. I made a 64 bit signed integer to store the current count in to. The had a timer interrupt at a regular freq read then hardware counter then set the count back to the center of the count then I would update my 64 bit signed int based upon how far the read encoder had strayed from the center of the count. I could also calculate the speed of the encoder based upon for far it had changed from the center of the count and the freq of the interupt.

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Pin ID on pyboard

Post by pythoncoder » Thu Feb 17, 2022 10:27 am

@andrew1234 You might like to read my encoders FAQ.
Peter Hinch
Index to my micropython libraries.

Andrew1234
Posts: 20
Joined: Sat Oct 16, 2021 3:58 pm

Re: Pin ID on pyboard

Post by Andrew1234 » Thu Feb 17, 2022 11:19 pm

Okay, I was hoping that the examples in Mike's repo that use uasynchio would give good results even with a high P/R rating. I still plan to give that a try to compare versus the example_simple.py in the documentation. After that, I'll look into the timer based approaches. I don't have experience with rotary encoders, so everyone's input, advice, and code is a great help -- thank you!

Regards
Andy

Post Reply