Note
Hello, welcome to the SunFounder Raspberry Pi & Arduino & ESP32 Enthusiasts Community on Facebook! Dive deeper into Raspberry Pi, Arduino, and ESP32 with fellow enthusiasts.
Why Join?
Expert Support: Solve post-sale issues and technical challenges with help from our community and team.
Learn & Share: Exchange tips and tutorials to enhance your skills.
Exclusive Previews: Get early access to new product announcements and sneak peeks.
Special Discounts: Enjoy exclusive discounts on our newest products.
Festive Promotions and Giveaways: Take part in giveaways and holiday promotions.
👉 Ready to explore and create with us? Click [here] and join today!
Lesson 09: Joystick Module
In this lesson, you will learn how to read values from a joystick module using the ESP32 Development Board. We’ll cover measuring the X and Y axis movements of the joystick and interpreting the switch position. By integrating these inputs with the ESP32, you’ll gain insights into handling analog and digital signals. This project is perfect for beginners, providing hands-on experience in reading and processing data from interactive hardware components.
Required Components
In this project, we need the following components.
It’s definitely convenient to buy a whole kit, here’s the link:
Name |
ITEMS IN THIS KIT |
LINK |
|---|---|---|
Universal Maker Sensor Kit |
94 |
You can also buy them separately from the links below.
Component Introduction |
Purchase Link |
|---|---|
ESP32 & Development Board (ESP32 Board) |
|
Wiring
Code
Code Analysis
Pin Definitions:
const int xPin = 27; //the VRX attach to const int yPin = 26; //the VRY attach to const int swPin = 25; //the SW attach to
Constants for the joystick pins are defined.
xPinandyPinare analog pins for the joystick’s X and Y axes.swPinis a digital pin for the joystick’s switch.Setup Function:
void setup() { pinMode(swPin, INPUT_PULLUP); Serial.begin(9600); }
Initializes
swPinas an input with a pull-up resistor, essential for the switch’s functionality. Starts serial communication at 9600 baud.Main Loop:
void loop() { Serial.print("X: "); Serial.print(analogRead(xPin)); // print the value of VRX Serial.print("|Y: "); Serial.print(analogRead(yPin)); // print the value of VRY Serial.print("|Z: "); Serial.println(digitalRead(swPin)); // print the value of SW delay(50); }
Continuously reads and prints the values from the joystick’s axes and switch to the Serial Monitor, with a delay of 50 ms between readings.