Rotary encoder.

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
AJB2K3
Posts: 13
Joined: Wed Mar 06, 2019 5:20 pm

Rotary encoder.

Post by AJB2K3 » Sat Feb 01, 2020 7:56 pm

Warning Newbie alert!

Hi guys,
I want to use and ESP 32 as a wireless controller for film editing. I have micropython reading the states of the two pins but I lack the understanding of working out the logic of getting micropython to tell me which way the rotary encoder is turning.
I know there are 2 libraries available but I'm wondering if the authors are on this forum to explain to me how they work?
Please and thank you in advanced.

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

Re: Rotary encoder.

Post by OutoftheBOTS_ » Sat Feb 01, 2020 10:07 pm

I made a video to explain encoders and also in the second half of the video explain how code works to read them. https://www.youtube.com/watch?v=p4BCFhIuC88&t=6s

Software encoder counters usually peform baldy because of the speed the code needs to run at. I have used an external hardware counter chip with the ESP32 see https://www.youtube.com/watch?v=41ogu0UlwCc&t=78s

The STM32 MCUs have a hardware encoder counter as part of their timer peripherals

AJB2K3
Posts: 13
Joined: Wed Mar 06, 2019 5:20 pm

Re: Rotary encoder.

Post by AJB2K3 » Fri Feb 07, 2020 8:57 pm

Stms?
Does that include the f103 aka the blue pill?
I'm starting to look at building around a stm

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

Re: Rotary encoder.

Post by OutoftheBOTS_ » Sat Feb 08, 2020 12:41 am

Yes bull pill does have endoser counters on their timers but the bluee pill doesn't have enough RAM or flash to run MicroPython.

If you use an official PyBoard then you will get the most amount of support.

Both the following boards are much less supported

If your super budget then these 2 new boards https://www.aliexpress.com/item/4000109 ... b201603_55 seen as the replacment for blue pill now known as the back pill have enough RAM and flash to run MP

This board https://www.aliexpress.com/item/4000602 ... b201603_53 has become my favorite STM32 board due to both price and it is capable of anything that I ever do.

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

Re: Rotary encoder.

Post by pythoncoder » Sun Feb 09, 2020 3:59 pm

@AJB2K3 The encoder_portable solution should be about as fast as an interrupt driven pure MicroPython solution can be. It certainly won't match hardware solutions, but even on sluggish platforms like ESPx it should handle up to ~1K edges/sec.

Code: Select all

import time
from machine import Pin
from encoder_portable import Encoder

phase = Pin(0, Pin.IN, Pin.PULL_UP)  # Change the 1st arg to match hardware
quad = Pin(2, Pin.IN, Pin.PULL_UP)

e = Encoder(phase, quad, False, 1)
while True:
    print(e.position)
    time.sleep(1)
Peter Hinch

pidou46
Posts: 78
Joined: Sat May 28, 2016 7:01 pm

Re: Rotary encoder.

Post by pidou46 » Mon Feb 10, 2020 10:23 am

Hello,

There is a PR pending to add hardware counter device support of the ESP32 to micropython:

https://github.com/micropython/micropython/pull/5496

Your post show that there is interest in hardware counter with esp32.
nodemcu V2 (amica)
micropython firmware Daily build 05/31/2016

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

Re: Rotary encoder.

Post by pythoncoder » Tue Feb 11, 2020 1:28 pm

The docs - ESP32 notes indicate that modes capable of handling encoders exist but are unsupported in this PR.
Peter Hinch

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

Re: Rotary encoder.

Post by OutoftheBOTS_ » Tue Feb 11, 2020 9:48 pm

Has anyone here personally used the PCNT peripheral to count Quadrature encoders????

By reading the Espressif docs it is clear that the PCNT will read a single encoder but when reading the docs the second channel of the PCNT doesn't work like a quadrature encoder but rather the second channel is a direction channel and depending upon whether it it is low or high whether the counter counts up or down.

I have never used the PCNT because reading the docs it doesn't appear it can count quadrature encoders, I have used software, a hardware IC (LS7366R) and the hardware quadrature counter on the timers of the STM32 to count quadrature encoders.

I would be super interested to hear from anyone that has personally tried to use PCNT to count quadrature encoders and can confirm if it does work or doesn't work

sengp
Posts: 2
Joined: Mon Feb 03, 2020 11:16 am

Re: Rotary encoder.

Post by sengp » Fri Mar 20, 2020 10:37 pm

Hello,
think this theme is almost solved, please see:
viewtopic.php?f=18&t=5997&p=43919&hilit ... ngp#p43919
And follow the links.

Problem is: MicroPython is slow and you may not guarantee a suitable minimal cycle time which is necessary when using software solutions. So the only way is using hardware - but good news: this hardware exists on ESP32, even for multiple channels!

Best regards,
Peter

Post Reply