This simple tutorial for arduino will show you how to read the values from a thumb joystick. Due to the nature of ADC (Analogue to Digital Conversion) and depending upon the sensitivity of your device, the values may be seeen to be constantly changing. This tutorial includes solution to 'smooth' those readings to make the joystick more friendly to our projects.
These instructions will get you a copy of the project up and running on your local machine for development and testing purposes.
You will need:
- An arduino device with at least 2 analogue inputs and 1 digital input
- A thumb joystick for arduino. Many are available online
- A 10kΩ resistor (if you want to use the joystick button)
- Connecting wires
The ESP8266 only has a single ADC input and is therefore not suitable for this demo. See our multiplexing tutorial for extending the capabilities of your device.
You will already know:
- How to use the Arduino IDE (or IDE of your choice)
- How to upload sketches / code to your device
- How to open the serial monitor
Many of the cheaper thumb sticks available are also not particularly good quality and we find that the reisitance across the aces are not always linear and also reach min / max values a long way before the end of the stick travel. We have also seen that some have a pin labelled '5v', however, some devices, such as the ESP8366 allow a maximum of 3.3v on the ADC pin and voltages above this will cause unexpected results and may damage the device. Please connect the '5v' pin to the appropriate voltage for your ADC device. On the ESP32, we use the 3.3v line.
The button on our stick connects the output pin to GND when pressed and leaves the pin floating when not pressed which resulted in constantly changing values on the digital pin. For this reason we recommend a 10kΩ resistor from the button pin to +ve (we used the 3.3v line again).
- Clone the repo to your local computer, or copy the .ino file.
- Connect the x & y axes of your joystick to the ADC pins on your arduino
- Connect the button to a free digital pin
- Set the pin numbers
- Compile and upload to your device.
- Open the serial monitor to view the values
- Wiggle your stick and see the values change
ESP32 dev kit
Please let us know if you test this on other devices so we can add to the list and acknowledge your contribution!
- Are the correct pin numbers in the code? (be careful not to mix up GPIO numbers and physical pin numbers)
- Is the +ve pin on the joystick connected to the correct voltage to match the analogue pins in use?
- Is the pull up resistor connected to the switch pin? The values are output if the switch is pressed even if the stick hasn't moved far enough
- Is the device connected to a 'noisy' power supply which is causing votlage fluctuations, or are other nearby devices possibly causing interferrence
- Joystick quality or even simple loose connections can also cause fluctuationsin the votlage reaching the ADC pins
This project is licensed under the MIT License - see the LICENSE.md file for details