Pin ID on pyboard
-
- Posts: 20
- Joined: Sat Oct 16, 2021 3:58 pm
Pin ID on pyboard
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
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
Re: Pin ID on pyboard
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.
-
- Posts: 20
- Joined: Sat Oct 16, 2021 3:58 pm
Re: Pin ID on pyboard
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
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
-
- Posts: 847
- Joined: Mon Nov 20, 2017 10:18 am
Re: Pin ID on pyboard
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. 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 codeAndrew1234 wrote: ↑Tue Feb 15, 2022 12:51 pmfor 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
- Mike Teachman
- Posts: 155
- Joined: Mon Jun 13, 2016 3:19 pm
- Location: Victoria, BC, Canada
Re: Pin ID on pyboard
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.Andrew1234 wrote: ↑Tue Feb 15, 2022 12:51 pmfor 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.
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).
-
- Posts: 847
- Joined: Mon Nov 20, 2017 10:18 am
Re: Pin ID on pyboard
Yer I like it like that and the same if the driver needs I2C that you pass an object rather than the pin numbersMike Teachman wrote: ↑Wed Feb 16, 2022 2:19 pmnote: 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).
-
- Posts: 20
- Joined: Sat Oct 16, 2021 3:58 pm
Re: Pin ID on pyboard
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
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
-
- Posts: 847
- Joined: Mon Nov 20, 2017 10:18 am
Re: Pin ID on pyboard
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.
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.
- pythoncoder
- Posts: 5956
- Joined: Fri Jul 18, 2014 8:01 am
- Location: UK
- Contact:
Re: Pin ID on pyboard
@andrew1234 You might like to read my encoders FAQ.
Peter Hinch
Index to my micropython libraries.
Index to my micropython libraries.
-
- Posts: 20
- Joined: Sat Oct 16, 2021 3:58 pm
Re: Pin ID on pyboard
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
Regards
Andy