"""
┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
┃ Interfacing A Joystick Module On The Raspberry Pi Using ┃
┃ An Mcp3008 (MicroPython) ┃
┃ ┃
┃ Utilize Wokwi simulation tool. Use a joystick module to ┃
┃ control an LED selection and use the potentiometer to ┃
┃ adjust the brightness. ┃
┃ ┃
┃ Copyright (c) 2024 CPE4B - Group 1 ┃
┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛
"""
from machine import Pin, PWM, ADC
import utime
# Joystick setup
xAxis = ADC(Pin(27)) # Joystick x-axis input on GPIO 27
yAxis = ADC(Pin(26)) # Joystick y-axis input on GPIO 26
button = Pin(16, Pin.IN, Pin.PULL_UP) # Joystick button input with pull-up resistor on GPIO 16
# Potentiometer setup for brightness control
potentiometer = ADC(Pin(28)) # Potentiometer input on GPIO 28 to adjust LED brightness
# Simulated Hall effect sensor on GPIO 29
hall_effect_sensor = ADC(Pin(29)) # Analog input for the hall effect sensor on GPIO 29
# LED setup
led_red = PWM(Pin(15)) # Red LED connected to GPIO 15, using PWM for brightness control
led_green = PWM(Pin(14)) # Green LED connected to GPIO 14
led_blue = PWM(Pin(13)) # Blue LED connected to GPIO 13
led_red.freq(1000) # Set PWM frequency for Red LED
led_green.freq(1000) # Set PWM frequency for Green LED
led_blue.freq(1000) # Set PWM frequency for Blue LED
# Track selected LED and LED state (on/off)
selected_led = led_red # Default selected LED is Red
led_on = False # LED starts in the off state
def display_status():
"""
Display the current status of sensors and voltage:
- Hall effect sensor value
- Potentiometer ADC value and calculated voltage
"""
hall_value = hall_effect_sensor.read_u16() # Read hall effect sensor value (0-65535)
pot_value = potentiometer.read_u16() # Read potentiometer value (0-65535)
# Calculate the voltage from ADC reading (assuming 3.3V reference voltage)
voltage = pot_value * 3.3 / 65535
# Print sensor and voltage status to the console
print(f"Hall Effect = {hall_value} | Potentiometer = {pot_value} | Voltage = {voltage:.2f}V")
while True:
# Joystick readings
xValue = xAxis.read_u16() # Read joystick x-axis position (0-65535)
yValue = yAxis.read_u16() # Read joystick y-axis position (0-65535)
button_pressed = button.value() == 0 # Check if joystick button is pressed (active low)
# Joystick controls LED selection
if xValue <= 600: # Move joystick left to select Red LED
selected_led = led_red
print("Selected LED: Red")
elif xValue >= 60000: # Move joystick right to select Green LED
selected_led = led_green
print("Selected LED: Green")
elif yValue <= 600: # Move joystick up to select Blue LED
selected_led = led_blue
print("Selected LED: Blue")
# Toggle the selected LED on/off when the button is pressed
if button_pressed:
led_on = not led_on # Change LED state (on/off)
utime.sleep(0.3) # Debounce delay to avoid multiple toggles
# Adjust brightness if LED is on
if led_on:
brightness = potentiometer.read_u16() # Read potentiometer for brightness level (0-65535)
selected_led.duty_u16(brightness) # Set PWM duty cycle to adjust brightness
else:
# If LED is off, set brightness to 0 (turn off the LED)
selected_led.duty_u16(0)
# Display sensor and potentiometer status
display_status()
# Delay for stability
utime.sleep(0.1)