Reading a rotary encoder

If doesn't fit into any other category ....
Eloclegin
Posts: 33
Joined: Fri Dec 09, 2022 5:04 pm
Has thanked: 17 times
Been thanked: 7 times

Reading a rotary encoder

Post by Eloclegin »

Hi
Can i just check… there are no commands in Annex that allow easy reading of a rotary encoder, right? I searched and I see nothing… but I am often blind! One has to write it from scratch. If anyone can confirm back it would be great. I dont want to write a sub from scratch only to discover that I missed an easy way to do it! There are so many amazing Commands in Annex that one starts to expect it to do everything!
Best wishes to all..
User avatar
PeterN
Posts: 385
Joined: Mon Feb 08, 2021 7:56 pm
Location: Krefeld, Germany
Has thanked: 179 times
Been thanked: 214 times
Contact:

Re: Reading a rotary encoder

Post by PeterN »

Our friend Fernando seems to have some experience on this topic:
[Local Link Removed for Guests]

Good luck!
User avatar
cicciocb
Site Admin
Posts: 1989
Joined: Mon Feb 03, 2020 1:15 pm
Location: Toulouse
Has thanked: 426 times
Been thanked: 1329 times
Contact:

Re: Reading a rotary encoder

Post by cicciocb »

[Local Link Removed for Guests] wrote: [Local Link Removed for Guests]Mon Oct 02, 2023 2:51 pm Hi
Can i just check… there are no commands in Annex that allow easy reading of a rotary encoder, right? I searched and I see nothing… but I am often blind! One has to write it from scratch. If anyone can confirm back it would be great. I dont want to write a sub from scratch only to discover that I missed an easy way to do it! There are so many amazing Commands in Annex that one starts to expect it to do everything!
Best wishes to all..
If I remember well there is a "not documented" feature that supports the optical encoders using the chip H/W counters.

I just need to look inside the code .....
User avatar
cicciocb
Site Admin
Posts: 1989
Joined: Mon Feb 03, 2020 1:15 pm
Location: Toulouse
Has thanked: 426 times
Been thanked: 1329 times
Contact:

Re: Reading a rotary encoder

Post by cicciocb »

Yes, I confirm that there is a "forgotten" feature that use the internal PCNT counters to read the encoders. (ESP32)

The functions are the following :

ret = ENCODER.SETUP pin1, pin2 [,mode]
where mode can be 0-> single, 1->half, 2->full
set the pins and the mode; returns always 0

ret = ENCODER.COUNT
returns the number of impulses counted

ret = ENCODER.RESET
reset the counter and returns 0

ret = ENCODER.SETVALUE value
set the the counter at a given value, return always 0


this is a very simple example using the pins 18 and 19

Code: [Local Link Removed for Guests]

wlog encoder.setup 19,18

timer0 1000, count
wait

count:
wlog encoder.count
return

Eloclegin
Posts: 33
Joined: Fri Dec 09, 2022 5:04 pm
Has thanked: 17 times
Been thanked: 7 times

Re: Reading a rotary encoder

Post by Eloclegin »

Fabulous. One really does start to believe that Annex32 can do everything!
Thank you!
User avatar
Fernando Perez
Posts: 378
Joined: Mon Feb 15, 2021 10:09 pm
Location: Santander (Spain)
Has thanked: 195 times
Been thanked: 267 times

Re: Reading a rotary encoder

Post by Fernando Perez »

Pedro, thanks for remembering, but it seems that both Eloclegin and Cicciocb are referring to rotary optical encoders, which are used to control the speed and, eventually, the position of a motor that rotates in a single direction.
My experiments were with mechanical encoders. Which has much more merit :lol: because the difficult thing is determining the direction of rotation and acting accordingly.
User avatar
cicciocb
Site Admin
Posts: 1989
Joined: Mon Feb 03, 2020 1:15 pm
Location: Toulouse
Has thanked: 426 times
Been thanked: 1329 times
Contact:

Re: Reading a rotary encoder

Post by cicciocb »

No, the function decodes also the direction and can be used with mechanical encoders. The advantage is that it can increase the resolution of the pulses detecting edges (using the mode option)
User avatar
Fernando Perez
Posts: 378
Joined: Mon Feb 15, 2021 10:09 pm
Location: Santander (Spain)
Has thanked: 195 times
Been thanked: 267 times

Re: Reading a rotary encoder

Post by Fernando Perez »

It would be spectacular. Can you give us some example?

Edit: It seems to work. I have connected a KY-040 to pins 18 and 19 and the counter increments and decrements depending on the direction of rotation.
Do you have more things hidden inside Annex? :shock:
User avatar
cicciocb
Site Admin
Posts: 1989
Joined: Mon Feb 03, 2020 1:15 pm
Location: Toulouse
Has thanked: 426 times
Been thanked: 1329 times
Contact:

Re: Reading a rotary encoder

Post by cicciocb »

[Local Link Removed for Guests] wrote: [Local Link Removed for Guests]Mon Oct 02, 2023 7:04 pm It would be spectacular. Can you give us some example?

Edit: It seems to work. I have connected a KY-040 to pins 18 and 19 and the counter increments and decrements depending on the direction of rotation.
Do you have more things hidden inside Annex? :shock:
:D Yes, there are some more
lignum
Posts: 3
Joined: Fri Aug 27, 2021 3:03 pm
Been thanked: 1 time

Re: Reading a rotary encoder

Post by lignum »

Searching for something else this topic surprised me. An undocumented, even hidden feature!
My favourite Annex as an adventure...

Had ever written a routine for this function but tried this and it works fine. easy piece now.
Don't forget the capacitors on the ports when using a cheap mechanical encoder.

Any other surprises?
From profession to hobby. Micro-computers still my challenge!
Post Reply