Arduino Nano 33 IoT - Joystick

In this guide, you'll learn how to use a joystick with the Arduino Nano 33 IoT. We will cover the following topics:

Arduino Nano 33 IoT Joystick

Hardware Preparation

1×Arduino Nano 33 IoT
1×Micro USB Cable
1×Joystick
1×Breadboard
1×Jumper Wires
1×Optionally, DC Power Jack
1×Recommended: Screw Terminal Expansion Board for Arduino Nano
1×Recommended: Breakout Expansion Board for Arduino Nano
1×Recommended: Power Splitter for Arduino Nano

Or you can buy the following sensor kits:

1×DIYables Sensor Kit (30 sensors/displays)
1×DIYables Sensor Kit (18 sensors/displays)
Disclosure: Some of the links provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you.
Additionally, some of these links are for products from our own brand, DIYables .

Overview of Joystick Sensor

You have probably seen a joystick before. It might be on a video game controller, a toy controller, or even on a large machine like the one used to control an excavator.

The joystick has two potentiometers arranged at right angles to each other and one push button. This means it provides the following outputs:

  • An analog number between 0 and 4095 showing the left-right position (called the X-coordinate).
  • An analog number between 0 and 4095 showing the up-down position (called the Y-coordinate).
  • A digital reading from a pushbutton, which can be either HIGH or LOW.

Using two analog values together creates 2-D coordinates, with the center point being the value when the joystick is at rest. The exact direction of these coordinates can be easily found by running a test program in the next section.

Some programs might use all three outputs, while others might only use a few of them.

Pinout

A joystick has five pins:

  • GND pin: Connect this to GND (0V).
  • VCC pin: Connect this to VCC (3.3V).
  • VRX pin: This gives an analog signal that shows the horizontal (X-axis) position.
  • VRY pin: This gives an analog signal that shows the vertical (Y-axis) position.
  • SW pin: This comes from the joystick’s button. It is usually not connected. When you add a pull-up resistor, the SW pin will be HIGH (on) when the button is not pressed and LOW (off) when it is pressed.
Joystick Pinout

How It Works

  • When you move the joystick left or right, the voltage on the VRX pin changes between 0 and 3.3V. Pushing it fully left gives 0V and fully right gives 3.3V. The Arduino Nano 33 IoT reads a value between 0 and 4095 on its analog pin depending on the position.
  • When you move the joystick up or down, the voltage on the VRY pin also changes between 0 and 3.3V. Fully up is 0V and fully down is 3.3V. The Arduino Nano 33 IoT reads a value between 0 and 4095 on its analog pin based on how far you push it.
  • When you push the joystick in any direction, the voltages on both the VRX and VRY pins change in proportion to how much you move along each axis.
  • When you press down on the joystick, the built-in pushbutton is activated. If you use a pull-up resistor on the SW pin, the voltage on that pin will change from 3.3V to 0V, so the Arduino Nano 33 IoT’s digital pin will detect a change from HIGH to LOW.

Wiring Diagram

The wiring diagram between Arduino Nano and 33 IoT Joystick

This image is created using Fritzing. Click to enlarge image

※ NOTE THAT:

Please note that the Arduino Nano 33 IoT pins A4 and A5 have built-in pull-up resistors for I2C communication. Although these pins can be used as digital input pins, it is recommended to avoid using them for digital input. If you must use them, do not use internal or external pull-down resistors for these pins

How To Program For Joystick

The joystick has two parts: an analog stick that moves in two directions (X and Y) and a digital button you press.

  • For the analog parts (X and Y directions), you only need to read the value from the analog input pin by using the analogRead() function.
int valueX = analogRead(A0); int valueY = analogRead(A1);
  • For the digital section (pushbutton): This is a button. The easiest and most convenient way is to use the ezButton library. This library takes care of button bouncing and also turns on an internal pull-up resistor. You can find more information about the button in the Arduino Nano 33 IoT - Button tutorial. The code will be shown in the next part of this guide.

After reading numbers from analog pins, we might have to change them into values we can control. The next part will show sample code for this.

Arduino Nano 33 IoT Code

In this section, you'll see these sample codes for the Arduino Nano 33 IoT.

  • Example code: reads number values from a joystick
  • Example code: reads number values and checks the joystick button state
  • Example code: changes number values into commands like MOVE_LEFT, MOVE_RIGHT, MOVE_UP, and MOVE_DOWN
  • Example code: changes number values to angles to control two servo motors (for example, in a pan-tilt camera)

Reads analog values from joystick

/* * This Arduino Nano 33 IoT code was developed by newbiely.com * * This Arduino Nano 33 IoT code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-nano-iot/arduino-nano-33-iot-joystick */ #define VRX_PIN A0 // The Arduino Nano 33 IoT pin connected to VRX pin #define VRY_PIN A1 // The Arduino Nano 33 IoT pin connected to VRY pin int valueX = 0; // to store the X-axis value int valueY = 0; // to store the Y-axis value void setup() { Serial.begin(9600); } void loop() { // read X and Y analog values valueX = analogRead(VRX_PIN); valueY = analogRead(VRY_PIN); // print data to Serial Monitor on Arduino IDE Serial.print("x = "); Serial.print(valueX); Serial.print(", y = "); Serial.println(valueY); delay(200); }

Detailed Instructions

If you are new to the Arduino Nano 33 IoT, be sure to check out our Getting Started with Arduino Nano 33 IoT tutorial. Then, follow these steps:

  • Connect the components to the Arduino Nano 33 IoT board as depicted in the diagram.
  • Use a USB cable to connect the Arduino Nano 33 IoT board to your computer.
  • Launch the Arduino IDE on your computer.
  • Select the Arduino Nano 33 IoT board and choose its corresponding COM port.
  • Copy the given code and open it in the Arduino IDE.
  • Press the Upload button in the Arduino IDE to send the code to your Arduino Nano 33 IoT.
  • Push the joystick all the way in one direction, then move it in a circle (either clockwise or anticlockwise).
  • Look at the Serial Monitor to see the results.
COM6
Send
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  
  • While moving the joystick with your thumb, keep an eye on the Serial Monitor.
  • If the X value is 0, note that spot as the left side (the opposite side is right).
  • If the Y value is 0, note that spot as the top (the opposite side is down).

You might see that the value does not match how you move the joystick. The problem is not the joystick, but the ADC on the Arduino Nano 33 IoT. We will explain why this happens at the end of this guide.

Reads analog values and reads the button state from a joystick

/* * This Arduino Nano 33 IoT code was developed by newbiely.com * * This Arduino Nano 33 IoT code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-nano-iot/arduino-nano-33-iot-joystick */ #include <ezButton.h> #define VRX_PIN A0 // The Arduino Nano 33 IoT pin connected to VRX pin #define VRY_PIN A1 // The Arduino Nano 33 IoT pin connected to VRY pin #define SW_PIN 2 // The Arduino Nano 33 IoT pin connected to SW pin ezButton button(SW_PIN); int valueX = 0; // to store the X-axis value int valueY = 0; // to store the Y-axis value int bValue = 0; // To store value of the button void setup() { Serial.begin(9600); button.setDebounceTime(50); // set debounce time to 50 milliseconds } void loop() { button.loop(); // MUST call the loop() function first // read X and Y analog values valueX = analogRead(VRX_PIN); valueY = analogRead(VRY_PIN); // Read the button value bValue = button.getState(); if (button.isPressed()) { Serial.println("The button is pressed"); // TODO do something here } if (button.isReleased()) { Serial.println("The button is released"); // TODO do something here } // print data to Serial Monitor on Arduino IDE Serial.print("x = "); Serial.print(valueX); Serial.print(", y = "); Serial.print(valueY); Serial.print(" : button = "); Serial.println(bValue); }

Detailed Instructions

  • Open the Library Manager by clicking the Library Manager icon on the left side of the Arduino IDE.
  • Type ezButton into the search box, then look for the button library by ArduinoGetStarted.com.
  • Click the Install button to add the ezButton library.
Arduino Nano 33 IoT button library
  • Copy the code above and open it in the Arduino IDE.
  • Click the Upload button in the Arduino IDE to send the code to your Arduino Nano 33 IoT.
  • Move the joystick to the left, right, up, or down.
  • Press the top of the joystick.
  • See the results in the Serial Monitor.
COM6
Send
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

Converts analog value to MOVE LEFT/RIGHT/UP/DOWN commands

/* * This Arduino Nano 33 IoT code was developed by newbiely.com * * This Arduino Nano 33 IoT code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-nano-iot/arduino-nano-33-iot-joystick */ #define VRX_PIN A0 // The Arduino Nano 33 IoT pin connected to VRX pin #define VRY_PIN A1 // The Arduino Nano 33 IoT pin connected to VRY pin #define LEFT_THRESHOLD 1000 #define RIGHT_THRESHOLD 3000 #define UP_THRESHOLD 1000 #define DOWN_THRESHOLD 3000 #define COMMAND_NO 0x00 #define COMMAND_LEFT 0x01 #define COMMAND_RIGHT 0x02 #define COMMAND_UP 0x04 #define COMMAND_DOWN 0x08 int valueX = 0 ; // to store the X-axis value int valueY = 0 ; // to store the Y-axis value int command = COMMAND_NO; void setup() { Serial.begin(9600); } void loop() { // read X and Y analog values valueX = analogRead(VRX_PIN); valueY = analogRead(VRY_PIN); // converts the analog value to commands // reset commands command = COMMAND_NO; // check left/right commands if (valueX < LEFT_THRESHOLD) command = command | COMMAND_LEFT; else if (valueX > RIGHT_THRESHOLD) command = command | COMMAND_RIGHT; // check up/down commands if (valueY < UP_THRESHOLD) command = command | COMMAND_UP; else if (valueY > DOWN_THRESHOLD) command = command | COMMAND_DOWN; // NOTE: AT A TIME, THERE MAY BE NO COMMAND, ONE COMMAND OR TWO COMMANDS // print command to serial and process command if (command & COMMAND_LEFT) { Serial.println("COMMAND LEFT"); // TODO: add your task here } if (command & COMMAND_RIGHT) { Serial.println("COMMAND RIGHT"); // TODO: add your task here } if (command & COMMAND_UP) { Serial.println("COMMAND UP"); // TODO: add your task here } if (command & COMMAND_DOWN) { Serial.println("COMMAND DOWN"); // TODO: add your task here } }

Detailed Instructions

  • Copy the code above and open it in the Arduino IDE
  • Click the Upload button in the Arduino IDE to send the code to the Arduino Nano 33 IoT
  • Move the joystick in any direction: left, right, up, down, or any angle
  • See the result in the Serial Monitor
COM6
Send
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

※ NOTE THAT:

Sometimes, there might be no command, one command, or two commands at the same time (for example, UP and LEFT together).

Converts analog values to angles to control two servo motors

More information is available in the Arduino Nano 33 IoT - Joystick Controls Servo Motor tutotial.

Video Tutorial

※ OUR MESSAGES

  • As freelancers, We are AVAILABLE for HIRE. See how to outsource your project to us
  • Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!