Quantcast
Viewing all articles
Browse latest Browse all 4908

MicroPython • Re: Rotary Encoder

With wiring like this:
Image may be NSFW.
Clik here to view.
mpy_encoder_pico.jpg
(CLK - GPIO 0; DT - GPIO 1; GND - GND; + - 3.3 V; SW not connected)

and my Raspberry Pi Pico attached on /dev/ttyACM1 (yours will likely be on /dev/ttyACM0), I installed the encoder library like this (don't type the $):

Code:

$ mpremote connect /dev/ttyACM1 mip install github:miketeachman/micropython-rotaryInstall github:miketeachman/micropython-rotaryInstalling github:miketeachman/micropython-rotary/package.json to /libInstalling: /lib/rotary.pyInstalling: /lib/rotary_irq_esp.py          Installing: /lib/rotary_irq_pyb.py          Installing: /lib/rotary_irq_rp2.py          Done
Then I ran the following code, which I think is practically identical to the OPs:

Code:

# https://github.com/miketeachman/micropython-rotary# MIT License (MIT)# Copyright (c) 2021 Mike Teachman# https://opensource.org/licenses/MIT# example for MicroPython rotary encoder - rp2from rotary_irq_rp2 import RotaryIRQimport timer = RotaryIRQ(    pin_num_clk=0,    pin_num_dt=1,    min_val=0,    max_val=5,    reverse=False,    range_mode=RotaryIRQ.RANGE_WRAP,    pull_up=True,)val_old = r.value()while True:    val_new = r.value()    if val_old != val_new:        val_old = val_new        print("result =", val_new)    time.sleep_ms(50)
It ran without problems, printing a new value every time I turned the encoder.

If you don't have mpremote installed, you can get it with (again, don't type the $s):

Code:

$ sudo apt install pipx$ pipx ensurepath$ pipx install mpremote

Statistics: Posted by scruss — Thu Mar 21, 2024 11:43 pm



Viewing all articles
Browse latest Browse all 4908

Trending Articles