Raspberry Pi Pico W Controlled RGB LED with Joystick Interaction
View and Edit Project In Cirkit Designer
Circuit Documentation
Summary
The circuit in question is designed to interface a Raspberry Pi Pico W with a KY-023 Dual Axis Joystick Module and a multi-color LED with four pins (common anode configuration). The Raspberry Pi Pico W reads the joystick position and controls the color of the LED based on the joystick's position. The LED's individual color channels (red, green, and blue) are connected through resistors to the Raspberry Pi Pico W's PWM-capable pins, allowing for color mixing and brightness control.
Component List
Raspberry Pi Pico W
- Description: A microcontroller board based on the RP2040 microcontroller, featuring Wi-Fi connectivity.
- Pins: 40 GPIO pins, SWCLK, GND, SWDIO.
KY-023 Dual Axis Joystick Module
- Description: A module that provides two analog outputs representing the X and Y positions of the joystick, and a digital output that represents the push-button state.
- Pins: GND, +5V, VRx, VRy, SW.
LED: Four Pin
- Description: A multi-color LED with a common anode and three cathodes for red, green, and blue channels.
- Pins: red cathode, common anode, green cathode, blue cathode.
Resistor (3x)
- Description: A passive two-terminal electrical component that implements electrical resistance as a circuit element.
- Value: 330 Ohms.
Wiring Details
Raspberry Pi Pico W
- Pin 36: Connected to the common anode of the LED.
- Pin 35: Supplies +5V to the KY-023 Joystick Module.
- Pin 32: Reads the VRx (X-axis) analog output from the KY-023 Joystick Module.
- Pin 31: Reads the VRy (Y-axis) analog output from the KY-023 Joystick Module.
- Pin 28: Connected to GND on the KY-023 Joystick Module.
- Pin 24: Connected to a 330 Ohm resistor, which is then connected to the red cathode of the LED.
- Pin 22: Connected to a 330 Ohm resistor, which is then connected to the green cathode of the LED.
- Pin 21: Connected to a 330 Ohm resistor, which is then connected to the blue cathode of the LED.
KY-023 Dual Axis Joystick Module
- +5V: Receives power from Raspberry Pi Pico W (Pin 35).
- VRx: X-axis analog output connected to Raspberry Pi Pico W (Pin 32).
- VRy: Y-axis analog output connected to Raspberry Pi Pico W (Pin 31).
- GND: Ground connected to Raspberry Pi Pico W (Pin 28).
LED: Four Pin
- Common Anode: Connected to Raspberry Pi Pico W (Pin 36).
- Red Cathode: Connected through a 330 Ohm resistor to Raspberry Pi Pico W (Pin 24).
- Green Cathode: Connected through a 330 Ohm resistor to Raspberry Pi Pico W (Pin 22).
- Blue Cathode: Connected through a 330 Ohm resistor to Raspberry Pi Pico W (Pin 21).
Resistor (3x)
- 330 Ohm: Each resistor is connected between the Raspberry Pi Pico W's PWM pins and the corresponding LED cathode.
Documented Code
main.py
## main.py
#include modules
from machine import Timer, ADC, PWM
from utime import sleep
import rainbow
# declaring objects from PWM and ADC classes
joystickY = ADC(26)
redLED = PWM(18, freq = 100, duty_u16 = 0, invert = 1)
greenLED = PWM(17, freq = 100, duty_u16 = 0, invert = 1)
blueLED = PWM(16, freq = 100, duty_u16 = 0, invert = 1)
while True:
valueY = joystickY.read_u16()
rainbow.RGB(valueY, redLED, greenLED, blueLED, joystickY)
sleep(.01) # in seconds
rainbow.py
# rainbow.py contains the RGB function used to control the LED color based on the joystick position.
# The actual content of rainbow.py is not provided here, but it should be saved with a .py extension.
Documentation Note
main.ino is the main.py program
rainbow.py.txt is the rainbow.py program
save these as .py extensions only
sketch.ino
// sketch.ino contains helper functions for the main.py program.
// It defines the forward_sequence, backward_sequence, and RGB functions.
// These functions are used to create a rainbow effect on the LED based on the joystick's Y-axis position.
// The actual content of sketch.ino is not provided here, but it should be saved with a .py extension.
Note: The code provided is in Python, and the file extensions mentioned as .ino are typically associated with Arduino sketches. The documentation note suggests saving the files with .py extensions, which is consistent with Python code for the Raspberry Pi Pico W.