FREE SHIPPING ON ALL ORDERS OVER £20

60 DAY HASSLE-FREE RETURNS

Gamit ang isang rotary encoder na may Raspberry Pi

Using a Rotary Encoder with the Raspberry Pi

A rotary encoder ay isang sensor na nagko-convert ng rotational motion sa mga digital signal, na nagbibigay-daan sa tumpak na kontrol sa posisyon, bilis, at direksyon. Karaniwan itong ginagamit sa volume controls, robotics, CNC machines, at menu navigation. Ipakikita ng gabay na ito kung paano i-set up at gamitin ang isang rotary encoder kasama ang Raspberry Pi gamit ang Python.


Mga Kakailanganin

  1. Raspberry Pi (anumang model na may GPIO support, hal. Pi 3, Pi 4)
  2. Rotary Encoder (hal. KY-040)
  3. Breadboard at Jumper Wires
  4. Python na naka-install sa Raspberry Pi

Hakbang 1: Pag-unawa sa kung paano gumagana ang Rotary Encoder

Ang isang rotary encoder ay may dalawang pangunahing output:

  • CLK (A) – Clock pulse signal
  • DT (B) – Direction signal
  • SW (opsyonal) – Push button (ginagamit sa pagpili ng mga opsyon)

Kapag pinihit, ang CLK at DT pins ay bumubuo ng mga pulse. Ang pagkakasunod-sunod ng mga pulse na ito ang tumutukoy sa direksyon ng pag-ikot.


Hakbang 2: Pag-wire ng Rotary Encoder sa Raspberry Pi

Rotary Encoder Pin Raspberry Pi Pin Function
VCC 3.3V (Pin 1) Power Supply
GND Ground (Pin 6) Ground
CLK (A) GPIO17 (Pin 11) Clock Pulse
DT (B) GPIO27 (Pin 13) Direction Signal
SW (Button) GPIO22 (Pin 15) Button Press

Hakbang 3: I-install ang mga Kinakailangang Library

I-update ang iyong Raspberry Pi at i-install ang RPi.GPIO library para sa paghawak ng mga GPIO interrupt.

sudo apt update && sudo apt upgrade -y
sudo apt install python3-rpi.gpio

Hakbang 4: Python Code para Basahin ang Input ng Rotary Encoder

Basic na Python Code para Basahin ang Pag-ikot

import RPi.GPIO as GPIO
import time

# I-define ang mga GPIO pin
CLK = 17
DT = 27
counter = 0
last_state = None

# Setup ng GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setup(CLK, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(DT, GPIO.IN, pull_up_down=GPIO.PUD_UP)

# Basahin ang initial state
last_state = GPIO.input(CLK)

try:
    while True:
        current_state = GPIO.input(CLK)
        if current_state != last_state:
            if GPIO.input(DT) != current_state:
                counter += 1
                direction = "Clockwise"
            else:
                counter -= 1
                direction = "Counterclockwise"
            print(f"Position: {counter}, Direction: {direction}")
        last_state = current_state
        time.sleep(0.01)  # Debounce delay
except KeyboardInterrupt:
    print("Exiting...")
finally:
    GPIO.cleanup()

Pagdagdag ng Button Press Detection

# I-define ang button pin
SW = 22
GPIO.setup(SW, GPIO.IN, pull_up_down=GPIO.PUD_UP)

def button_pressed(channel):
    print("Button Pressed!")

# Magdagdag ng event detection para sa button press
GPIO.add_event_detect(SW, GPIO.FALLING, callback=button_pressed, bouncetime=300)

Hakbang 5: Mga Aplikasyon ng Rotary Encoder sa Raspberry Pi

  1. Menu Navigation – Mag-scroll sa mga LCD o OLED menu.
  2. Volume Control – I-adjust ang volume sa mga media application.
  3. CNC Machines – Tumpak na kontrol sa paggalaw sa mga stepper motor application.
  4. Robotics – Tukuyin ang pag-ikot ng gulong para sa pag-track ng bilis at posisyon.
  5. Smart Home Automation – Gamitin bilang selector para sa iba't ibang function sa bahay.

Troubleshooting

  1. Hindi tumutugon ang Rotary Encoder

    • Suriin ang wiring at GPIO pin assignment.
    • Siguraduhin na ang pull-up resistors ay naka-enable (PUD_UP).
  2. Paglaktaw ng mga Hakbang o Maling Paggalaw

    • Magdagdag ng debounce delay (time.sleep(0.01)).
    • Gumamit ng GPIO interrupts sa halip na polling (GPIO.add_event_detect).
  3. Hindi ma-detect ang Button Press

    • Siguraduhin na ang button pin ay maayos na nakakonekta sa GND.
    • Magdagdag ng debounce time na 300ms sa GPIO.add_event_detect.

Konklusyon

Ang isang rotary encoder ay isang versatile na input device para sa menu navigation, motor control, at position tracking. Sa pagsunod sa gabay na ito, madali mong maisasama ang isang rotary encoder sa iyong Raspberry Pi para sa mga interactive na proyekto. 🚀

Notice an Issue? Have a Suggestion?
If you encounter a problem or have an idea for a new feature, let us know! Report a problem or request a feature here.

Mag -iwan ng komento