Clock with encoder

All ESP32 boards running MicroPython.
Target audience: MicroPython users with an ESP32 board.
Post Reply
Num
Posts: 3
Joined: Mon Aug 12, 2019 7:58 am

Clock with encoder

Post by Num » Mon Aug 12, 2019 8:03 am

Hello,
I am a beginner and French so sorry for the Google translation.
I realize a thermostat project and I need to be able to adjust the time manually with buttons or a rotary encoder but I can not find information on the web. can you help me please.

User avatar
jimmo
Posts: 892
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia

Re: Clock with encoder

Post by jimmo » Mon Aug 12, 2019 12:54 pm

Hi,

For a rotary encoder, you might find this library useful -- https://github.com/miketeachman/micropython-rotary

For a button, you need to connect the button to a pin on the ESP32 (with the other side of the button connected to ground). Then enable a pull-up on the pin, and you can read the value

Code: Select all

import machine
p = machine.Pin(26, mode=machine.Pin.IN, pull=machine.Pin.PULL_UP)

...

if p.value() == 0:
  # button is pressed
You can enable a falling edge irq on the pin to have a function called when the pin is pressed. http://docs.micropython.org/en/latest/l ... e.Pin.html

You might want to read about "button debouncing" though.

Num
Posts: 3
Joined: Mon Aug 12, 2019 7:58 am

Re: Clock with encoder

Post by Num » Mon Aug 12, 2019 2:41 pm

Thanks

for the physical part and gpio it goes.
I have trouble converting the encoder signal into time.
I thought to make a script to change each value one after the other.
00:00 I change the first value I valid changes the second ... to get the current time.

fdufnews
Posts: 29
Joined: Mon Jul 25, 2016 11:31 am

Re: Clock with encoder

Post by fdufnews » Mon Aug 12, 2019 3:17 pm

Providing you are using the library proposed by jimmo you need to declare a rotaryIRQ() object.
This object handle all the interaction with the encoder you just have to read the value method.
You define the min and max value. When you turn the knob the value is increased/decreased.
You should look at the example. If you put 0 as min, 59 as max and replacing the print function to a call to your own display function you'll have the minutes adjustment. When you detect a push on the button you switch to hour adjustment by changing max to 23 instead of 59.

Num
Posts: 3
Joined: Mon Aug 12, 2019 7:58 am

Re: Clock with encoder

Post by Num » Mon Aug 12, 2019 3:23 pm

thank you very much I will try this

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

Re: Clock with encoder

Post by pythoncoder » Tue Aug 13, 2019 7:57 am

There is also an encoder solution in this repo (encoder_portable.py).
Peter Hinch

Post Reply