Outils pour utilisateurs

Outils du site


issue201:micro-ci

Ceci est une ancienne révision du document !


Rotary Encoders

What looks like a potentiometer, but isn’t a potentiometer? It’s a Rotary Encoder.

Think of a rotary switch with an unlimited number of switch positions. It really isn’t, but it sort of acts like that.

While there are many different models of rotary encoders, the particular encoder we will be using in this project is a KY-040. You can get them from many places on the Internet starting for about $3.00 USD. Some come in packs of 5 for $10.00. I borrowed this image from https://opencircuit.shop/product/rotary-encoder-module-ky-40 .

There is a pretty good PDF on how the KY-040 works at https://www.epitran.it/ebayDrive/datasheet/25.pdf .

The actual encoder has three pins, but the breakout board has 5. One extra feature of the KY-040 is that the shaft can be pressed to provide a momentary Normally Open switch.

The drivers that we’ll need for this project can be found at https://github.com/MikeTeachman/micropython-rotary . Go ahead and download the driver, because we’ll need some of the files in a few minutes.

For now, let’s start wiring the breadboard.

Now for the code. You need to copy from the rotary encoder driver zip the rotary.py and rotary_irq_rp2.py files to your RPi Pico. Then you need to copy the example_simple.py file from the Examples folder and save it as example_simple_rotary.py onto your Pico as well. This is the file we need to modify (top right).

Now we need to add the driver for the SSD1306 (below).

Now we need to define all the pins for the rotary encoder. Note that these are the GPIO pins, not the physical pins. I’ve included the GPIO pin numbers to the Physical pin numbers in the table above.

The first line sets the switch for the shaft push momentary switch. The next lines handle the IRQ set ups. We use max_val to define the max number of positions. By setting reverse=True means that turning the shaft clockwise the counter increments and anti-clockwise decrements the counter. Finally range_mode=RANGE_WRAP causes the counter to go from max_val to min_val (bottom right).

We keep two values to determine the current value of the encoder. Val_old is set to make sure that we don’t report the same value each time the loop is run. We’ve used the SSD1306 fill, text and show methods before, so I won’t go into depth on those (next page, top right).

If you want to use the shaft switch to exit the loop, change the while loop from

while True:

To

doloop = True

while doloop:

And then in the if button.value check, add the line

doloop = False

When you run the program each time you turn the shaft of the encoder the value printout on the OLED should change (and the REPL should show the value).

I’ve set up a repository for the project at https://github.com/gregwa1953/MTMT_201 .

That’s it for this month.

Until next time, HAPPY NEW YEAR and, as always; stay safe, healthy, positive and creative!

issue201/micro-ci.1706373200.txt.gz · Dernière modification : 2024/01/27 17:33 de auntiee