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 an Arduino Uno. We will explore connecting the joystick’s X and Y axes to the Arduino and how to display their values on the serial monitor. Additionally, we’ll cover the usage of a switch button on the joystick. This project is perfect for beginners, offering hands-on experience with analog and digital inputs on the Arduino platform.

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

Universal Maker Sensor Kit

You can also buy them separately from the links below.

Component Introduction

Purchase Link

Arduino UNO R3 or R4

BUY

Joystick Module

BUY

Wiring

../_images/Lesson_09_joystick_module_uno_bb.png

Code

Code Analysis

  1. Pin Definitions:

    const int xPin = A0;  //the VRX attach to
    const int yPin = A1;  //the VRY attach to
    const int swPin = 8;  //the SW attach to
    

    Constants for the joystick pins are defined. xPin and yPin are analog pins for the joystick’s X and Y axes. swPin is a digital pin for the joystick’s switch.

  2. Setup Function:

    void setup() {
      pinMode(swPin, INPUT_PULLUP);
      Serial.begin(9600);
    }
    

    Initializes swPin as an input with a pull-up resistor, essential for the switch’s functionality. Starts serial communication at 9600 baud.

  3. 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.