Chapter 14 Joystick

In the previous chapter, we have learned how to use rotary potentiometer. Now, let’s learn a new electronic module joystick which working on the same principle as rotary potentiometer.

Project 14.1 Joystick

In this project, we will read the output data of a joystick and display it to the Terminal screen.

Component List

ESP32-WROVER x1

Chapter01_00

GPIO Extension Board x1

Chapter01_01

Breadboard x1

Chapter01_02

Joystick x1

Chapter14_00

Jumper F/M x5

Chapter14_08

Component knowledge

Joystick

A joystick is a kind of input sensor used with your fingers. You should be familiar with this concept already as they are widely used in gamepads and remote controls. It can receive input on two axes (Y and or X) at the same time (usually used to control direction on a two dimensional plane). And it also has a third direction capability by pressing down (Z axis/direction).

../../../_images/Chapter14_01.png

This is accomplished by incorporating two rotary potentiometers inside the joystick Module at 90 degrees of each other, placed in such a manner as to detect shifts in direction in two directions simultaneously and with a push button switch in the “vertical” axis, which can detect when a User presses on the Joystick.

../../../_images/Chapter14_02.png

When the joystick data is read, there are some differences between the axes: data of X and Y axes is analog, which needs to use the ADC. The data of the Z axis is digital, so you can directly use the GPIO to read this data or you have the option to use the ADC to read this.

Circuit

Schematic diagram

Chapter14_03

Hardware connection

If you need any support, please contact us via: support@freenove.com

Chapter14_04

Sketch

In this project’s code, we will read the ADC values of X and Y axes of the joystick, and read digital quality of the Z axis, then display these out in terminal.

Sketch_14.1_Joystick

../../../_images/Chapter14_09.png

Download the code to ESP32-WROVER, open the serial port monitor, the baud rate is 115200, as shown in the figure below, shift (moving) the joystick or pressing it down will make the data change.

../../../_images/Chapter14_05.png

The following is the code:

 1/**********************************************************************
 2  Filename    : Joystick
 3  Description : Read data from joystick.
 4  Auther      : www.freenove.com
 5  Modification: 2024/06/19
 6**********************************************************************/
 7int xyzPins[] = {13, 12, 14};   //x,y,z pins
 8void setup() {
 9  Serial.begin(115200);
10  pinMode(xyzPins[2], INPUT_PULLUP);  //z axis is a button.
11}
12
13void loop() {
14  int xVal = analogRead(xyzPins[0]);
15  int yVal = analogRead(xyzPins[1]);
16  int zVal = digitalRead(xyzPins[2]);
17  Serial.printf("X,Y,Z: %d,\t%d,\t%d\n", xVal, yVal, zVal);
18  delay(500);
19}

In the code, configure xyzPins[2] to pull-up input mode. In loop(), use analogRead () to read the value of axes X and Y and use digitalRead () to read the value of axis Z, then display them.

1int xVal = analogRead(xyzPins[0]);
2int yVal = analogRead(xyzPins[1]);
3int zVal = digitalRead(xyzPins[2]);
4Serial.printf("X,Y,Z: %d,\t%d,\t%d\n", xVal, yVal, zVal);
5delay(500);