Joystick Shield V1.A for Arduino

FunduinoPublished on

Sechs Taster und einen Joystick auf einem Shield auslesen

Das Joystick-Shield V1.A wird einfach auf den Arduino gesteckt und ist sofort einsatzbereit – kein Steckbrett, keine Verkabelung. An Bord sind ein analoger Joystick mit Druckfunktion und sechs Taster.

In dieser Anleitung liest du beides aus und lässt dir die Werte im Seriellen Monitor anzeigen. Damit hast du die Grundlage für jede Fernsteuerung, jedes Menü und jedes Spiel.

Joystick Shield V1.A with joystick and six buttons
The Joystick Shield V1.A

You need this

Joystick Shield V1.A - 2 axes, 2 buttons, for Arduino UNO Joystick Shield V1.A - 2 axes, 2 buttons, for Arduino UNO 3,44 € *Item no. F23107178About the product — Joystick Shield V1.A - 2 axes, 2 buttons, for Arduino UNO
Funduino UNO R3 Microcontroller - ATmega328P, 16 MHz, for Arduino Funduino UNO R3 Microcontroller - ATmega328P, 16 MHz, for Arduino 11,12 € *Item no. F23107480About the product — Funduino UNO R3 Microcontroller - ATmega328P, 16 MHz, for Arduino

All 2 parts together 14,56 € *

* All prices include statutory VAT, plus shipping costs.

The Joystick Shield V1.A

The joystick shield is a plug-in module: plug it onto the Arduino and it’s ready to use – no wiring, no breadboard.

It features an analog joystick with a push function and six buttons labeled A to F.

Joystick Shield V1.A with joystick and six buttons
The Joystick Shield V1.A

Operating voltage and value range

There is a switch on the shield that lets you switch between 3.3 V and 5 V. This changes the value range provided by the joystick:

  • at 5 V: 0 to 1023
  • at 3.3 V: a correspondingly smaller range

This applies equally to the X and Y axes. At rest, the joystick sits roughly in the middle of the range.

Joystick values in the four directions of movement
The values at a 5 V operating voltage

The pinout

  • Buttons A to F – D2, D3, D4, D5, D6, D7
  • Joystick button K (press from above) – D8
  • Joystick X-axis – A0
  • Joystick Y-axis – A1

For advanced users, the shield also provides connections for I²C (SDA on A4, SCL on A5) and a Bluetooth module.

The program

The sketch outputs which button was pressed and the values the joystick reports on both axes in the Serial Monitor. The output repeats once per second.

The setup is missing the setting for KnopfK. Add pinMode(KnopfK, INPUT_PULLUP);, otherwise the joystick button will return random values.

Button states and joystick values in the Serial Monitor
This is what the output looks like.
Display the button and joystick position in the Serial Monitor
const byte KnopfA = 2;     // The variables are declared
const byte KnopfB = 3;     // We use the "const byte" declaration because each button is connected to its respective pin constantly ("const")
const byte KnopfC = 4;
const byte KnopfD = 5;
const byte KnopfE = 6;
const byte KnopfF = 7;
const byte KnopfK = 8;
const byte PinJoystickX = 0;
const byte PinJoystickY = 1;
int JoystickX;
int JoystickY;
boolean Agedruckt = false;  // Set the variables to the default state "false" – the corresponding button is not pressed in the default state
boolean Bgedruckt = false;
boolean Cgedruckt = false;
boolean Dgedruckt = false;
boolean Egedruckt = false;
boolean Fgedruckt = false;
boolean Kgedruckt = false;
void setup() // Setup begins here
{
Serial.begin(9600);
pinMode(KnopfA, INPUT);   // Button A now serves as an input and …
digitalWrite(KnopfA, HIGH);  // … is set to the "HIGH" state
pinMode(KnopfB, INPUT);
digitalWrite(KnopfB, HIGH);
pinMode(KnopfC, INPUT);
digitalWrite(KnopfC, HIGH);
pinMode(KnopfD, INPUT);
digitalWrite(KnopfD, HIGH);
pinMode(KnopfE, INPUT);
digitalWrite(KnopfE, HIGH);
pinMode(KnopfF, INPUT);
digitalWrite(KnopfF, HIGH);
}
void loop() // The loop begins here
{
      if(Agedruckt == digitalRead(KnopfA))  // When the button is pressed… (read using the "digitalRead()" command)
          {
          Serial.print("Button A pressed"); // …"Button A pressed" should be output
          Serial.println();
          }
      if(Bgedruckt == digitalRead(KnopfB))
          {
          Serial.print("Button B pressed");
          Serial.println();
          }
      if(Cgedruckt == digitalRead(KnopfC))
          {
          Serial.print("Button C pressed");
          Serial.println();
          }
      if(Dgedruckt == digitalRead(KnopfD))
          {
          Serial.print("Button D pressed");
          Serial.println();
          }
      if(Egedruckt == digitalRead(KnopfE))
          {
          Serial.print("Button E pressed");
          Serial.println();
          }
      if(Fgedruckt == digitalRead(KnopfF))
          {
          Serial.print("Button F pressed");
          Serial.println();
          }
      if(Kgedruckt == digitalRead(KnopfK))
          {
          Serial.print("Joystick button K pressed");
          Serial.println();
          }
      // Next, the joystick position is read
          JoystickX = analogRead(PinJoystickX);    // The JoystickX variable stores the value of the X-axis
          Serial.print("Joystick X-axis value is    ");
          Serial.println(JoystickX);
          JoystickY = analogRead(PinJoystickY);    // The JoystickY variable stores the value of the Y-axis
          Serial.print("Joystick Y-axis value is    ");
          Serial.println(JoystickY);
Serial.println();   // Add blank lines so the output values are easy to read
Serial.println();
delay(1000);  // The values (or button presses) should be output with a delay of 1000 ms (1 second)
}
Rate the instructions

Comments

Ask a question or share an experience

Contact us if something doesn’t work. Please include your board and the error message.

We review every comment before publication. Your email address remains with us.