English
English
简体中文
日本語

Arduino Quick Start

2. Devices & Examples

5. Extensions

6. Applications

Joystick Input Module

The Joystick is an input module with two potentiometers and a pushbutton switch. As the joystick moves, the divided outputs of the two potentiometers change with its position. Connect X_ADC and Y_ADC to ADC-capable pins on the host to read their raw ADC values. When the joystick is pressed, the internal switch closes and the BUTTON pin goes LOW; when released, it returns HIGH through a pull-up resistor.

Hardware Connection

Connect the Joystick module's X_ADC and Y_ADC outputs to ADC-capable pins on the Basic (this example uses PIN35 and PIN36), and connect BUTTON to PIN2.

Example Program

This example reads the joystick position and button state and displays them on the screen and serial monitor.

cpp
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
#include "M5Unified.h"
#include <M5GFX.h>

void setup() {
  M5.begin();
  pinMode(2, INPUT_PULLUP);

  pinMode(35, INPUT);
  pinMode(36, INPUT);
  dacWrite(25, 0);
  M5.Lcd.setCursor(100, 0, 4);
  M5.Lcd.print("JOYSTICK");
}
uint16_t x_data;
uint16_t y_data;
uint8_t button_data;
void loop() {
  // put your main code here, to run repeatedly:
  x_data = analogRead(35);
  y_data = analogRead(36);
  button_data = digitalRead(2);
  Serial.printf("x:%0d y:%0d button:%d\n", x_data, y_data, button_data);

  M5.Lcd.setCursor(0, 120, 4);
  M5.Lcd.printf("X:%04d   Y:%04d\n", x_data, y_data);
  M5.Lcd.printf("Button:%d\n", button_data);
  delay(200);
}

After uploading the program, you should see the following result.

Page Tools
PDF
On This Page