Last active
December 14, 2024 14:29
-
-
Save codelectron/aa35d514ecc2a43c5b57aa1912f8530c to your computer and use it in GitHub Desktop.
This gist is part of the article http://codelectron.com/rotary-encoder-with-raspberry-pi/
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from RPi import GPIO | |
| from time import sleep | |
| clk = 17 | |
| dt = 18 | |
| 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) | |
| counter = 0 | |
| clkLastState = GPIO.input(clk) | |
| try: | |
| while True: | |
| clkState = GPIO.input(clk) | |
| if clkState != clkLastState: | |
| dtState = GPIO.input(dt) | |
| if dtState != clkState: | |
| counter += 1 | |
| else: | |
| counter -= 1 | |
| print counter | |
| clkLastState = clkState | |
| sleep(0.01) | |
| finally: | |
| GPIO.cleanup() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment