Site Guide

Find your way around the Engineering Club.

Know what you need? Search for a component, concept or project.

Around the site

Start

Labs

TestingExperimental

Build

Club

LearnExperimental

Go a little deeper

Entry points to larger collections. Use Search for individual records.

Learning paths & reference

Inside PiCrawlerExperimental

Inside Inventory

← Mega Components

ELEGOO MEGA · LESSON 13

Analog Joystick Module

Print the joystick’s X, Y and pushbutton readings.

Read two analog channels and an active-low switch.

Parts List

  • ELEGOO Mega 2560 R3 is a 5 V ATmega2560 AVR board with many I/O pins and four hardware UARTs for control systems with several peripherals. Long ELEGOO-branded Mega PCB with USB-B and barrel connector, ATmega2560 marking, extended side headers and a large double-row digital header at the end. Read printed Mega labels; its SPI hardware placement is not the same numeric pins as an Uno.
  • A USB-A to USB-B cable carries USB power and, when its data conductors are present, a host/device data link. USB-A plug and square USB-B plug. Inspect the plug shape; micro-USB is not USB-C, and USB-B is the squarer device connector.
  • A two-axis thumb joystick converts lever position into two analog voltages and usually includes a push-down switch. Thumb cap above a metal/plastic gimbal on a small PCB; headers commonly separate X, Y and switch signals.
  • Male-to-female jumper leads make removable point-to-point connections for low-current prototype signals. Pin at one end and socket at the other. Check intact insulation and snug contacts before use.

Assembly

  1. Unplug USB and follow the joystick’s printed labels.
  2. Reconnect, move each axis and press the stick to check all three readings.
Illustration © ELEGOO. All rights reserved.
Illustration © ELEGOO. All rights reserved. Open full-size ↗
Reviewed pin-to-pin connections
FromToPurpose / notes
Mega: 5VJoystick: +5V / VCC
Mega: GNDJoystick: GND
Mega: A0Joystick: VRx
Mega: A1Joystick: VRy
Mega: D2Joystick: SW
Open vector connection diagram ↗

Code

PlatformIO configuration
platformio.ini
[env:mega]
platform = atmelavr@5.3.0
board = megaatmega2560
framework = arduino
monitor_speed = 9600

Open PlatformIO Monitor at 9600 baud after Upload. Close Monitor before another Upload if the port is busy.

main.cpp
#include <Arduino.h>

// The joystick has a pushbutton plus two analog axes.
const uint8_t SWITCH_PIN = 2;
// A0 reads the X-axis voltage; A1 below reads the Y-axis voltage.
const uint8_t X_PIN = A0;
const uint8_t Y_PIN = A1;

// Enable the switch pull-up and open Serial Monitor.
void setup() {
  pinMode(SWITCH_PIN, INPUT_PULLUP);
  // Use 9600 baud in PlatformIO Monitor to read the text.
  Serial.begin(9600);
}

// Print one snapshot of the switch state and both analog axes.
void loop() {
  Serial.print("Switch: ");
  // Switch = HIGH when released, LOW when pressed because of INPUT_PULLUP.
  Serial.println(digitalRead(SWITCH_PIN));
  Serial.print("X-axis: ");
  // On a Mega, analogRead() normally returns 0–1023 for the input voltage.
  Serial.println(analogRead(X_PIN));
  Serial.print("Y-axis: ");
  Serial.println(analogRead(Y_PIN));
  // Print a blank line to separate each group of readings.
  Serial.println();
  delay(500);
}

Expected behavior

The monitor shows X/Y values0–1023, usually near512 at center, and switch1 released/0 pressed.

Experiments & Challenges

  • Choose a center deadband and print a direction only outside it.

Troubleshooting

  • Reversed X/Y names: check VRx→A0 and VRy→A1; a drifting center is not exactly512 on every unit.