ESP8266 - Joystick
Hardware Preparation
1 | × | ESP8266 NodeMCU | |
1 | × | Micro USB Cable | |
1 | × | Joystick | |
1 | × | Jumper Wires | |
1 | × | Breadboard | |
1 | × | (Optional) 5V Power Adapter for ESP8266 | |
1 | × | (Optional) Screw Terminal Expansion Board for ESP8266 |
Or you can buy the following sensor kits:
1 | × | DIYables Sensor Kit (30 sensors/displays) | |
1 | × | DIYables Sensor Kit (18 sensors/displays) |
Additionally, some of these links are for products from our own brand, DIYables.
Overview of 2-axis Joystick
You may have encountered a Joystick in various places, like a game controller, a toy controller, or even a large machine like an excavator controller.
The joystick is made up of two potentiometers arranged perpendicular to one another and a push button. Thus, it produces the following outputs:
- An analog value ranging from 0 to 1023 that corresponds to the horizontal position (known as the X-coordinate)
- An analog value ranging from 0 to 1023 that corresponds to the vertical position (known as the Y-coordinate)
- A digital value of the push button (either HIGH or LOW)
Therefore:
- Two analog values can be merged to form 2-D coordinates
- Center of the coordinates is the resting position of the joystick
- A test code (presented in the next section) can determine the actual direction of the coordinates
Some applications may use all three outputs, while others may only use a subset of them.
ESP8266 and Joystick
Because the ESP8266 only has one analog input pin, and the joystick needs two, there are two ways to tackle this:
- Analog to Digital Module:
- You can add an external gadget called an Analog to Digital Converter (ADC) module, like the ADS1115 ADC Module.
- This nifty device gives you more analog input pins, letting you connect multiple analog devices, including the joystick.
- Multiplexing Solution:
- Another option is to use a multiplexing solution, where you take turns reading the X and Y axes of the joystick using that single analog input pin.
- This involves switching between the X and Y axes at different times, using a technique called time-division multiplexing.
However, both of these options can be a bit pricey and a tad complicated. Instead, we recommend going for the ESP32 instead of the ESP8266. Check out the ESP8266 - Joystick tutorial for a simpler and more effective way to go about it.