Joystick With Raspberry Pi Pico is a great way to control your next project. You can use the joystick to control anything from a simple LED to a complex robotic arm. The joystick is easy to use and can be interfaced with various electronics. This essay will explore how to interface a Raspberry Pi Pico with a Joystick.
We’ll also look at some example projects that you can use to get started with your own Raspberry Pi Pico and Joystick project.
Required Components
- Raspberry Pi Pico (Amazon.com)
- Joystick Module (Amazon.com)
- LEDs (Amazon.com)
- Jumper wire (Amazon.com)
What is a Raspberry pi pico board?
Raspberry Pi Pico is a new microcontroller board from Raspberry Pi. It’s their smallest and most affordable board yet, at only $4. It’s also their most powerful microcontroller board, with a Cortex-M0+ processor and support for high-speed GPIO, PWM, and ADC.
It is designed for use in a wide range of applications, from simple electronics projects to complex system-on-chip designs.
Analog Joystick Module
A joystick is an input device that consists of a stick that can be tilted in multiple directions to control an on-screen pointer. A joystick module is used to control a computer or video game character by moving a lever in different directions. It is also used to control other devices such as cameras and robots. Joystick modules come in different sizes and shapes. The module can be used to measure the tilt of a joystick on the X and Y axes.
There are two main types of joysticks:
- Analog – Analog joysticks use potentiometers to measure the position of the stick
- Digital – Digital joysticks use switches to determine the direction the stick is moved.
Joystick module pinout
This joystick module has five pins which are numbered from left to right for reference:
- GND: Pin connected to ground.
- +5V: power pin(5v).
- VRx: potentiometer reading pin for the x’s axis.
- VRy: potentiometer reading pin for the y’s axis.
- SW: is an additional pin used for a push button at the bottom.
Connections Joystick With Raspberry Pi Pico
Raspberry pi pico | Joystick | |
---|---|---|
GND |
➡️ | GND |
5V |
➡️ | +5V |
GP27 (PIN 32) | ➡️ | VRx |
GP25 (PIN 31) |
➡️ | VRy |
GP17 (PIN 22) |
➡️ | SW |
Programming – Joystick With Raspberry Pi Pico
Also, Check This- Getting Started With Raspberry Pi Pico With Thonny IDE
MicroPython Sketch: Reading analog input from Joystick Module
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
from machine import Pin, ADC import utime xAxis = ADC(Pin(27)) yAxis = ADC(Pin(26)) button = Pin(17,Pin.IN, Pin.PULL_UP) while True: xValue = xAxis.read_u16() yValue = yAxis.read_u16() buttonValue = button.value() buttonStatus = "not pressed" if buttonValue == 0: buttonStatus = "pressed" print("X: " + str(xValue) + ", Y: " + str(yValue) + " -- button value: " + str(buttonValue) + " button status: " + buttonStatus) utime.sleep(0.2) |
Open Serial Monitor and see the value
Example 2. Schematic Diagram for Controlling LEDs using Joystick with Raspberry Pi pico
- xAxis = ADC(Pin(27))
- yAxis = ADC(Pin(26))
- button = Pin(17,Pin.IN, Pin.PULL_UP)
- led_left = Pin(14, Pin.OUT)
- led_middle = Pin(15, Pin.OUT)
- led_right = Pin(12, Pin.OUT)
- led_up = Pin(18, Pin.OUT)
- led_down = Pin(13, Pin.OUT)
Code 2 – Controlling LEDs using Joystick with Raspberry Pi pico
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
from machine import Pin, ADC import utime xAxis = ADC(Pin(27)) yAxis = ADC(Pin(26)) button = Pin(17,Pin.IN, Pin.PULL_UP) led_left = Pin(14, Pin.OUT) led_middle = Pin(15, Pin.OUT) led_right = Pin(12, Pin.OUT) led_up = Pin(18, Pin.OUT) led_down = Pin(13, Pin.OUT) while True: xValue = xAxis.read_u16() yValue = yAxis.read_u16() buttonValue = button.value() xStatus = "middle" yStatus = "middle" buttonStatus = "not pressed" led_left.value(0) led_down.value(0) led_up.value(0) led_right.value(0) led_middle.value(1) # Check the x and y Value to determine the status of the joystick if buttonValue == 0: buttonStatus = "pressed" led_middle.value(0) if xValue <= 600: xStatus = "left" led_left.value(1) led_middle.value(0) if xValue >= 60000: xStatus = "right" led_right.value(1) led_middle.value(0) if yValue <= 600: yStatus = "up" led_up.value(1) led_middle.value(0) if yValue >= 60000: yStatus = "down" led_down.value(1) led_middle.value(0) print("X: " + xStatus + ", Y: " + yStatus + " button status: " + buttonStatus) utime.sleep(0.2) |
Controlling LEDs using Joystick with Raspberry Pi pico
After uploading the code to the Raspberry pi pico and connecting components according to the circuit diagram, you can now control the LEDs with Joystick.
You can turn ON the four LEDs in each direction according to the Joystick shaft movement. The Joystick is having two potentiometers inside it, one is for X-axis movement and another is for Y-axis movement. Each potentiometer is getting 5v from the Raspberry Pi pico.
By moving the joystick, you can see the voltage value will change and the analog value at Analog pins will also change. From the Pico, you can read the analog value for the X and Y axis and turn the LEDs ON according to the axis movement of the Joystick.
A push button switch on the Joystick module is used to control the single LED in the circuit as shown in the video below.
Move the joystick in different directions and you will be able to view the LED
- Getting Started Raspberry Pi Pico – Pinout, Specs – Beginner Guide
- Interfacing PIR Motion Sensor with Raspberry Pi Pico
- Raspberry Pi Pico Home Automation System
- Interface Servo Motor With Raspberry Pi Pico
- Interface 0.96″ OLED Display with Raspberry Pi Pico
- Raspberry Pi Pico Weather Station Using Dht11 Sensor
- Interface 16*2 LCD Display With Raspberry Pi Pico
- Flame Sensor With Raspberry Pi Pico Tutorial