Note
Hello, welcome to the SunFounder Raspberry Pi & Arduino & ESP32 Enthusiasts Community on Facebook! Dive deeper into Raspberry Pi, Arduino, and ESP32 with fellow enthusiasts.
Why Join?
Expert Support: Solve post-sale issues and technical challenges with help from our community and team.
Learn & Share: Exchange tips and tutorials to enhance your skills.
Exclusive Previews: Get early access to new product announcements and sneak peeks.
Special Discounts: Enjoy exclusive discounts on our newest products.
Festive Promotions and Giveaways: Take part in giveaways and holiday promotions.
π Ready to explore and create with us? Click [here] and join today!
2.1.6 Rotary Encoder ModuleΒΆ
IntroductionΒΆ
In this project, you will learn about Rotary Encoder. A rotary encoder is an electronic switch with a set of regular pulses in strictly timing sequence. When used with IC, it can achieve increment, decrement, page turning and other operations such as mouse scrolling, menu selection, and so on.
Required ComponentsΒΆ
In this project, we need the following components.
Itβs definitely convenient to buy a whole kit, hereβs the link:
Name |
ITEMS IN THIS KIT |
LINK |
---|---|---|
Raphael Kit |
337 |
You can also buy them separately from the links below.
COMPONENT INTRODUCTION |
PURCHASE LINK |
---|---|
Schematic DiagramΒΆ
Experimental ProceduresΒΆ
Step 1: Build the circuit.
In this example, we can connect the Rotary Encoder pin directly to the Raspberry Pi using a breadboard and 40-pin Cable, connect the GND of the Rotary Encoder to GND, γ+γto 5V, SW to digital GPIO27, DT to digital GPIO18, and CLK to digital GPIO 17.
Step 2: Open the code file.
cd ~/raphael-kit/python/
Step 3: Run.
sudo python3 2.1.6_RotaryEncoder.py
You will see the count on the shell. When you turn the rotary encoder clockwise, the count is increased; when turn it counterclockwise, the count is decreased. If you press the switch on the rotary encoder, the readings will return to zero.
Code
Note
You can Modify/Reset/Copy/Run/Stop the code below. But before that, you need to go to source code path like raphael-kit/python
. After modifying the code, you can run it directly to see the effect.
#!/usr/bin/env python3
import RPi.GPIO as GPIO
import time
clkPin = 17 # CLK Pin
dtPin = 18 # DT Pin
swPin = 27 # Button Pin
globalCounter = 0
flag = 0
Last_dt_Status = 0
Current_dt_Status = 0
def setup():
GPIO.setmode(GPIO.BCM) # Numbers GPIOs by physical location
GPIO.setup(clkPin, GPIO.IN) # input mode
GPIO.setup(dtPin, GPIO.IN)
GPIO.setup(swPin, GPIO.IN, pull_up_down=GPIO.PUD_UP)
def rotaryDeal():
global flag
global Last_dt_Status
global Current_dt_Status
global globalCounter
Last_dt_Status = GPIO.input(dtPin)
while(not GPIO.input(clkPin)):
Current_dt_Status = GPIO.input(dtPin)
flag = 1
if flag == 1:
flag = 0
if (Last_dt_Status == 0) and (Current_dt_Status == 1):
globalCounter = globalCounter - 1
if (Last_dt_Status == 1) and (Current_dt_Status == 0):
globalCounter = globalCounter + 1
def swISR(channel):
global globalCounter
globalCounter = 0
def loop():
global globalCounter
tmp = 0 # Rotary Temperary
GPIO.add_event_detect(swPin, GPIO.FALLING, callback=swISR)
while True:
rotaryDeal()
if tmp != globalCounter:
print ('globalCounter = %d' % globalCounter)
tmp = globalCounter
def destroy():
GPIO.cleanup() # Release resource
if __name__ == '__main__': # Program start from here
setup()
try:
loop()
except KeyboardInterrupt: # When 'Ctrl+C' is pressed, the child program destroy() will be executed.
destroy()
Code Analysis
Read dtPin value when clkPin is low.
When clkPin is high, if dtPin goes from low to high, the count decreases, otherwise the count increases.
swPin will output low when the shaft is pressed.
From this, the program flow is shown below: