Raspberry Pi Pico and Rotary Encoder

by Ramatronics Laboratory in Circuits > Sensors

3329 Views, 6 Favorites, 0 Comments

Raspberry Pi Pico and Rotary Encoder

WhatsApp Image 2023-05-04 at 1.07.38 PM.jpeg
WhatsApp Image 2023-05-04 at 1.07.34 PM.jpeg

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

WhatsApp Image 2023-05-04 at 1.02.20 PM (1).jpeg
WhatsApp Image 2023-05-04 at 1.02.20 PM.jpeg
WhatsApp Image 2023-05-04 at 1.02.18 PM.jpeg
WhatsApp Image 2023-05-04 at 1.06.09 PM.jpeg

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

Raspberry Pi Pico and Rotary Encoder_page-0001.jpg
WhatsApp Image 2023-05-04 at 1.07.38 PM.jpeg
WhatsApp Image 2023-05-04 at 1.07.37 PM (1).jpeg
WhatsApp Image 2023-05-04 at 1.07.37 PM.jpeg
WhatsApp Image 2023-05-04 at 1.07.36 PM (2).jpeg

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

WhatsApp Image 2023-05-04 at 1.07.34 PM.jpeg
WhatsApp Image 2023-05-04 at 1.07.29 PM.jpeg
WhatsApp Image 2023-05-04 at 1.07.32 PM.jpeg
Screenshot 2023-05-04 131611.png

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.