Read a joystick module with Arduino

FunduinoPublished on

Zwei Achsen und den Taster auslesen, LEDs und einen Ton ansteuern

Ein Joystick-Modul ist im Grunde nichts anderes als zwei Potentiometer in einem Gehäuse – eines für die x-Achse, eines für die y-Achse – plus ein Taster, den du durch Drücken auf den Joystick auslöst.

In dieser Anleitung liest du zuerst die Position des Joysticks aus und machst die vier Endanschläge mit je einer LED sichtbar. Danach kommt der Taster dazu: Drückst du ihn, gibt ein Piezo-Speaker einen Ton aus.

Schematic: Joystick Module and Four LEDs on Arduino UNO
Circuit diagram for task 1: Joystick module and four LEDs

You need this

KY-023 joystick module - 2-axis with push button, 3.3-5 V KY-023 joystick module - 2-axis with push button, 3.3-5 V 1,29 € *Item no. F23107469About the product — KY-023 joystick module - 2-axis with push button, 3.3-5 V
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
830-point Breadboard - solderless breadboard for Arduino 830-point Breadboard - solderless breadboard for Arduino 2,90 € *Item no. FUN-1010553About the product — 830-point Breadboard - solderless breadboard for Arduino
Breadboard Cables Male-to-Male - 40 Pieces, 20 cm Breadboard Cables Male-to-Male - 40 Pieces, 20 cm 1,74 € *Item no. FUN-1010558About the product — Breadboard Cables Male-to-Male - 40 Pieces, 20 cm
Breadboard cables male-female - 40 pieces, 20 cm Breadboard cables male-female - 40 pieces, 20 cm 1,74 € *Item no. FUN-1010554About the product — Breadboard cables male-female - 40 pieces, 20 cm
LED 5 mm - 100 Pieces, 5 Colors, 2 V LED 5 mm - 100 Pieces, 5 Colors, 2 V 1,97 € *Item no. F23107316About the product — LED 5 mm - 100 Pieces, 5 Colors, 2 V
Resistor Assortment 100 Pieces - 5 Values 100 Ω to 10 kΩ, 0.5 W Resistor Assortment 100 Pieces - 5 Values 100 Ω to 10 kΩ, 0.5 W 1,72 € *Item no. F23107216About the product — Resistor Assortment 100 Pieces - 5 Values 100 Ω to 10 kΩ, 0.5 W

All 7 parts together 22,48 € *Ready to ship immediately · Delivery time: 1-3 business days

* All prices include statutory VAT, plus shipping costs.

How the joystick module works

The module has five connections (pins):

  • GND – Ground, to the Arduino’s GND pin
  • +5V – Power supply, to the Arduino’s 5V pin
  • VRx – x-axis, to pin A0
  • VRy – y-axis, to pin A1
  • SW – the “Switch.” The module has a push button, which you activate by pressing down on the joystick. In Task 1, this pin remains unused.

There is a standard potentiometer behind each of the x- and y-axes. Depending on which way you move the joystick, each axis outputs a voltage between 0 V and 5 V at its pin. The Arduino reads this voltage through its analog inputs and stores the joystick’s position as a value between 0 and 1023.

If you move the joystick all the way to the left, the microcontroller registers a value of 1023 on the x-axis. At the opposite end, the value is 0. The same applies to the y-axis: 1023 at the top, 0 at the bottom.

You could simply display the x and y values in the Serial Monitor. In this guide, instead, you’ll make the end positions directly visible: a separate LED lights up for each direction.

Task 1: Read position and control LEDs

Build the circuit according to the schematic. The four LEDs are assigned as follows in the sketch:

  • LED for the “down” limit switch on pin 6
  • LED for the “up” limit switch on pin 7
  • LED for the “left” limit switch on pin 8
  • LED for the “right” limit switch on pin 9

Each LED has a 220-ohm series resistor. The sketch reads both axes one after the other, outputs the values to the serial monitor, and switches on the corresponding LED as soon as an end stop is reached.

Check the LEDs’ pin numbers against your own setup. If you swap two LEDs, they’ll light up in the wrong direction—nothing can break.

Schematic: Joystick Module and Four LEDs on Arduino UNO
Circuit diagram for task 1: Joystick module and four LEDs
Task 1: Read the joystick and control the LEDs
int ledUnten = 6;
int ledOben = 7;
int ledLinks = 8;
int ledRechts = 9;
int SensorWert = 0;              // Festlegen der Variablen

void setup()
{
  pinMode(ledUnten, OUTPUT);     // Definieren der LEDs
  pinMode(ledOben, OUTPUT);
  pinMode(ledLinks, OUTPUT);
  pinMode(ledRechts, OUTPUT);
  Serial.begin(9600);
}

void loop()
{
  SensorWert = analogRead(A0);   // Auslesen des Wertes an A0. Hier: VRx, X-Achse
  Serial.print("X:");            // Darstellung des Wertes im seriellen Monitor
  Serial.print(SensorWert, DEC); // Darstellung des Wertes als Dezimalzahl

  if (SensorWert > 1010)         // Wenn der Wert größer als 1010 ist, dann ...
  {
    digitalWrite(ledLinks, HIGH);  // ... soll ledLinks leuchten
  }
  if (SensorWert < 1010)         // Wenn der Wert kleiner als 1010 ist, dann ...
  {
    digitalWrite(ledLinks, LOW);   // ... soll ledLinks nicht mehr leuchten
  }
  if (SensorWert < 20)           // Wenn der Wert kleiner als 20 ist, dann ...
  {
    digitalWrite(ledRechts, HIGH); // ... soll ledRechts leuchten
  }
  if (SensorWert > 20)           // Wenn der Wert größer als 20 ist, dann ...
  {
    digitalWrite(ledRechts, LOW);  // ... soll ledRechts nicht mehr leuchten
  }

  SensorWert = analogRead(A1);   // Auslesen des Wertes an A1. Hier: VRy, Y-Achse
  Serial.print(" |Y:");
  Serial.println(SensorWert, DEC);

  if (SensorWert > 1010)         // Wenn der Wert größer als 1010 ist, dann ...
  {
    digitalWrite(ledOben, HIGH);   // ... soll ledOben leuchten
  }
  if (SensorWert < 1010)         // Wenn der Wert kleiner als 1010 ist, dann ...
  {
    digitalWrite(ledOben, LOW);    // ... soll ledOben nicht mehr leuchten
  }
  if (SensorWert > 20)           // Wenn der Wert größer als 20 ist, dann ...
  {
    digitalWrite(ledUnten, LOW);   // ... soll ledUnten nicht leuchten
  }
  if (SensorWert < 20)           // Wenn der Wert kleiner als 20 ist, dann ...
  {
    digitalWrite(ledUnten, HIGH);  // ... soll ledUnten leuchten
  }
}

Task 2: Read the push button and output a tone

The joystick module has a built-in button that you activate by pressing down on the joystick. You’ll now read its state and output it as a tone. To do this, also connect a piezo speaker to pin 5.

One special feature of the module: Pin SW continuously carries about 2 volts. As soon as you press the button, there’s no voltage there (GND). That’s why you can’t read the button on a digital pin - 2 volts aren’t enough for the Arduino to register a clear HIGH signal when the button isn’t pressed.

So connect pin SW to analog pin A2. When you press the button, the microcontroller registers a value of 0. When the button isn’t pressed, the value is about 400 - corresponding to around 2 volts.

To do this, extend the sketch from Exercise 1 with an if/else condition (if … then … else …).

Schematic: Joystick module, four LEDs, and piezo speaker connected to the Arduino UNO
Circuit diagram for task 2: additionally with a piezo speaker on pin 5
Task 2: additionally read the button and play a tone
int piezo = 5;                   // Der Piezo-Speaker wird an Pin 5 angeschlossen
int ledUnten = 6;
int ledOben = 7;
int ledLinks = 8;
int ledRechts = 9;
int SensorWert = 0;              // Festlegen der Variablen

void setup()
{
  pinMode(piezo, OUTPUT);        // Definieren des Speakers als Ausgang
  pinMode(ledUnten, OUTPUT);     // Definieren der LEDs
  pinMode(ledOben, OUTPUT);
  pinMode(ledLinks, OUTPUT);
  pinMode(ledRechts, OUTPUT);
  Serial.begin(9600);
}

void loop()
{
  SensorWert = analogRead(A0);   // Auslesen des Wertes an A0. Hier: VRx, X-Achse
  Serial.print("X:");            // Darstellung des Wertes im seriellen Monitor
  Serial.print(SensorWert, DEC); // Darstellung des Wertes als Dezimalzahl

  if (SensorWert > 1010)         // Wenn der Wert größer als 1010 ist, dann ...
  {
    digitalWrite(ledLinks, HIGH);  // ... soll ledLinks leuchten
  }
  if (SensorWert < 1010)         // Wenn der Wert kleiner als 1010 ist, dann ...
  {
    digitalWrite(ledLinks, LOW);   // ... soll ledLinks nicht mehr leuchten
  }
  if (SensorWert < 20)           // Wenn der Wert kleiner als 20 ist, dann ...
  {
    digitalWrite(ledRechts, HIGH); // ... soll ledRechts leuchten
  }
  if (SensorWert > 20)           // Wenn der Wert größer als 20 ist, dann ...
  {
    digitalWrite(ledRechts, LOW);  // ... soll ledRechts nicht mehr leuchten
  }

  SensorWert = analogRead(A1);   // Auslesen des Wertes an A1. Hier: VRy, Y-Achse
  Serial.print(" |Y:");
  Serial.print(SensorWert, DEC);

  if (SensorWert > 1010)         // Wenn der Wert größer als 1010 ist, dann ...
  {
    digitalWrite(ledOben, HIGH);   // ... soll ledOben leuchten
  }
  if (SensorWert < 1010)         // Wenn der Wert kleiner als 1010 ist, dann ...
  {
    digitalWrite(ledOben, LOW);    // ... soll ledOben nicht mehr leuchten
  }
  if (SensorWert > 20)           // Wenn der Wert größer als 20 ist, dann ...
  {
    digitalWrite(ledUnten, LOW);   // ... soll ledUnten nicht leuchten
  }
  if (SensorWert < 20)           // Wenn der Wert kleiner als 20 ist, dann ...
  {
    digitalWrite(ledUnten, HIGH);  // ... soll ledUnten leuchten
  }

  SensorWert = analogRead(A2);   // Auslesen des Wertes an A2. Hier: SW, der Taster
  Serial.print(" |Taster:");
  Serial.println(SensorWert, DEC);

  if (SensorWert < 20)           // Wenn der Wert kleiner als 20 ist, dann ...
  {
    digitalWrite(piezo, HIGH);     // ... soll der Speaker angesteuert werden ...
  }
  else                           // ... ist die Bedingung nicht erfüllt, ...
  {
    digitalWrite(piezo, LOW);      // ... soll der Speaker wieder ausgeschaltet werden
  }
}
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.