import machine
import utime as time
from machine import Pin
# Rotary encoder input pins
clk = Pin(26, Pin.IN, Pin.PULL_UP) # Using the CLK (A) pin, connected to GPIO 26
dt = Pin(27, Pin.IN, Pin.PULL_UP) # Using the DT (B) pin, connected to GPIO 27
# Creating a list to store all the LED pins
leds = []
# The GPIO pins connected to the LEDs:
ledpins = [22, 21, 19, 18] # Orange, Green, Blue, and Yellow, respectively
# Adding the LED pins to the list
for led_num in ledpins:
leds.append(Pin(led_num, Pin.OUT))
angle = 0
# Since this is a 20-step-per-revolution rotary encoder, one step equals 360/20 degrees
incrementation = 360 / 20
def rotary_change(pin):
global last_state_clk, angle, incrementation
# Read the current state of CLK
current_state_clk = clk.value()
# When this function is triggered, check whether CLK (A) and DT (B) match
if dt.value() != current_state_clk:
print("Clockwise")
# If (A) and (B) don't match, the rotary encoder is moving clockwise, increasing the angle
angle += incrementation
else:
print("Counterclockwise")
# If (A) and (B) match, the rotary encoder is moving counterclockwise, decreasing the angle
angle -= incrementation
# Use the modulo operator to keep the angle within the range of 0–360 degrees
angle = angle % 360
print(angle)
# The angle required for the rotary encoder to light up one LED
change_angle = 80
# Number of LEDs to turn on based on the rotary encoder's angle
num_of_led_on = angle // change_angle
# Turn off all LEDs before lighting the required ones
for i in range(len(ledpins)):
leds[i].value(0)
# Turn on the LEDs based on the calculated angle
for needed_led in range(num_of_led_on):
leds[needed_led].value(1)
# Add interrupt handling, triggered by a FALLING edge
clk.irq(handler=rotary_change, trigger=Pin.IRQ_FALLING) # A
# Print to indicate the program is loaded and ready to use
print("The manual for this program is written at the end of the code.")
while True:
time.sleep(0.1)
'''
This program detects the rotary encoder's direction, displays the current angle,
and lights up LEDs on the breadboard based on the rotary encoder's movement.
The rotary encoder is the small black chip with a rotatable knob.
To rotate it, click on one of the arrows. The terminal (the black window
displaying text) will show the direction and the current angle. Based on
the angle, one or more LEDs will light up. Here's a table showing which
LEDs light up depending on the angle:
0° ≤ angle < 80°: No LEDs will light up.
80° ≤ angle < 160°: LED 1 (orange) will light up.
160° ≤ angle < 240°: LED 1 and 2 (orange and green) will light up.
240° ≤ angle < 320°: LED 1, 2, and 3 (orange, green, and blue)
will light up.
320° ≤ angle < 360°: All LEDs will light up.
**Components used:**
- Rotary Encoder (KY-040)
- ESP32 Microcontroller
- LEDs (various colors)
- Resistors (four-color bands, placed under the LEDs)
- Half Breadboard (white board with holes)
- Wires (red wires for power, black wires for ground, other colors for clarity)
Thank you for reviewing my project! This activity was part of Level 4
Embedded Systems, Week 4. The project was adapted by Kenzy Alaa with the help
of session lead Hassan Saied and colleagues from the same activity.
Date: 12/6/2024
'''