Raspberry Pi Pico and Rotary Encoder
by Ramatronics Laboratory in Circuits > Sensors
3329 Views, 6 Favorites, 0 Comments
Raspberry Pi Pico and Rotary Encoder
INTRODUCTION:
Today in this instructables, I am going to show you how we can interface a rotary encoder to raspberry pi pico. Rotary Encoders are sensors that detect position and speed by converting rotational mechanical displacements into electrical signals. So let's get started.
Supplies
Hardware Requirements:
https://quartzcomponents.com?sca_ref=3671286.DzEptz4I3w
Raspberry pi pico
https://quartzcomponents.com/products/raspberry-pi-pico?_pos=3&_sid=ed21c1573&_ss=r
Rotary encoder
https://quartzcomponents.com/products/rotary-encoder-module-ky-040?_pos=1&_sid=dfdb41663&_ss=r
Bread board
Jumper wires
https://quartzcomponents.com/products/65pcs-breadboard-jumper-cable?_pos=7&_sid=a2a84167d&_ss=r
Micro-B USB cable
https://quartzcomponents.com/products/raspberry-pi-cable-for-charging?_pos=3&_sid=46e090303&_ss=r
Downloading the Micro Python Modules
You need to download two micro python modules rotary.py and rotary_irq_rp2.py. You can download them from GitHub. The link is given below:
https://github.com/ramjipatel041/Rotary-encoder-and-Raspberry-pi-pico
Making a Prototype Circuit
Fist of all insert the raspberry pi pico and rotary encoder into your bread board as shown in the schematic diagram. Now Connect the pins of the rotary encoder to the raspberry pi pico, according to following wiring map.
GND(Encoder)----------->GND(Pico)
V+(Encoder)-------------->VBUS(Pico)
SW(Encoder)-------------->GPIO13(Pico)
DT(Encoder)--------------->GPIO14(Pico)
CLK(Encoder)------------->GPIO15(Pico)
After making all wire connections, recheck all your connections then connect your pico to computer using USB cable.
Writing the Micro Python Program
Write the following program on the Thonny IDE and and save it on Raspberry pi pico with name as main.py.
import time
from rotary_irq_rp2 import RotaryIRQ
from machine import Pin
SW=Pin(13,Pin.IN,Pin.PULL_UP)
r = RotaryIRQ(pin_num_dt=15,
pin_num_clk=14,
min_val=0,
reverse=False,
range_mode=RotaryIRQ.RANGE_UNBOUNDED)
val_old = r.value()
while True:
try:
val_new = r.value()
if SW.value()==0 and n==0:
print("Button Pressed")
print("Selected Number is : ",val_new)
n=1
while SW.value()==0:
continue
n=0
if val_old != val_new:
val_old = val_new
print('result =', val_new)
time.sleep_ms(50)
except KeyboardInterrupt:
break
Run the Program
After writing the main program file save it on your pico as main.py. Now click on run option and rotate your encoder shaft either clockwise or counter clockwise. If we rotate the encoder shaft one step in clockwise direction than we see the the number 1 printed on the shell area and if I further rotate the shaft in the same direction then we shall again see the new value on the shell area. If I rotate the encoder shaft clockwise again and again then the values continuously prints on the shell area as shown below:
1
2
3
4
5
If I rotate the encoder shaft in counter clockwise direction then it starts decreasing the values. If the value just before rotating in counter clockwise direction is 0 then we see the value -1 printed on the shell area. If I rotate the shaft in counter clockwise direction again and again the we shall see the decreasing value printed on the shell area as shown below:
-1
-2
-3
-4
-5
Now press the shaft of the rotary encoder and now you will see "button pressed" on the shell area. when we press the button, the value at that time is also prints on the shell area.
If you want to plot the graph using these values then click on view option in Thonny and a list of different options will appear now click on Plotter option and now you will be able to see the graph of the values as shown in the above image.