How to use Joysticks with your Arduino Projects
Using joysticks with Arduino is a common way to add manual control to your Arduino projects. Joysticks are input devices that allow users to provide analog input in two dimensions (X and Y axes) or even more with additional buttons. They are often used in applications like robotics, gaming, remote control systems, and more. This guide will show you exactly how to get started programming joysticks with your Arduino.
Components You’ll Need:
- Arduino board (e.g., Arduino Uno)
- Joystick module (usually includes an XY-axis potentiometer and sometimes additional buttons)
- Breadboard and jumper wires
Here’s a Step-by-Step Guide
Connect the Joystick:
Most joystick modules come with three pins: VCC, GND, and Signal (sometimes labeled X and Y). Connect these pins as follows:
-
- Connect VCC to 5V on the Arduino.
- Connect GND to GND on the Arduino.
- Connect the X-axis (or Y-axis) signal pin to an analog input pin on the Arduino, such as A0 or A1.
Upload the Code:
You’ll need to write a simple Arduino sketch to read the analog values from the joystick’s X and Y axes. Here’s a basic example:
void setup() { Serial.begin(9600); } void loop() { int xAxisValue = analogRead(A0); // Read X-axis value int yAxisValue = analogRead(A1); // Read Y-axis value Serial.print("X-Axis: "); Serial.println(xAxisValue); Serial.print("Y-Axis: "); Serial.println(yAxisValue); delay(100); // Optional delay to slow down serial output }
This code reads the analog values from the X and Y axes of the joystick and prints them to the serial monitor.
Open the Serial Monitor:
- After uploading the code to your Arduino, open the Arduino IDE’s Serial Monitor (Tools -> Serial Monitor) to view the analog values being read from the joystick. You should see values changing as you move the joystick.
Interpret the Values:
- As you move the joystick, the analogRead() function will return values between 0 and 1023 (for a 10-bit analog input). The center position of the joystick typically reads around 512 (or close to half of the range), and the values increase or decrease as you move the joystick in different directions.
Customize Your Project:
- You can use these analog values to control various aspects of your project, such as controlling the movement of a robot, adjusting the speed of a motor, or changing the position of a servo.
Remember that the exact pin connections and code may vary depending on the specific joystick module you have. Be sure to check the datasheet or documentation that comes with your joystick for precise wiring instructions.
Let's Build!
Basic Example
The Y Axis is a vertical line, and the X Axis is a horizontal line. So, the X Axis on a joystick would be left and right, and the Y Axis would be up and down. Most Joysticks also have a push button functionality.
The Code:
int yAxis=A1; int xAxis=A0; int Button=2; int xVal; int yVal; int ButtonVal; void setup() { Serial.begin(9600); pinMode(yAxis,INPUT); pinMode(xAxis,INPUT); pinMode(Button,INPUT); digitalWrite(Button,HIGH); } void loop() { xVal=analogRead(xAxis); yVal=analogRead(yAxis); ButtonVal=digitalRead(Button); delay(200); Serial.print("X-Axis Value = "); Serial.println(xVal); Serial.print(" Y-Axis Value = "); Serial.println(yVal); Serial.print(" Push Button state is "); Serial.println(ButtonVal); delay(1000); }
The Circuit:
Assemble your components in the exact same way as the image below.
Upload the Code:
When you’re ready, upload the code to your Arduino board. Once the code has finished uploading, open the serial monitor in the IDE and play around with the joystick. You should see some data being returned from the joystick.
Controlling an RGB LED with the Joystick
The Code:
int RED=3; int GREEN=5; int BLUE=6; int xAxis=A0; int yAxis=A1; int xVal; int yVal; int Button=2; int ButtonVal; int tm=100; void setup() { Serial.begin(9600); pinMode(xAxis,INPUT); pinMode(yAxis,INPUT); pinMode(RED,OUTPUT); pinMode(GREEN,OUTPUT); pinMode(BLUE,OUTPUT); pinMode(Button,INPUT); digitalWrite(Button,HIGH); } void loop() { xVal=analogRead(xAxis); yVal=analogRead(yAxis); ButtonVal=digitalRead(Button); delay(tm); Serial.print("X Value = "); Serial.println(xVal); Serial.print(" Y Value = "); Serial.println(yVal); Serial.println(ButtonVal); if (xVal>=1000){ //red digitalWrite(RED,HIGH); digitalWrite(GREEN,LOW); digitalWrite(BLUE,LOW); } else{ digitalWrite(RED,LOW); } if (xVal<=200){ //green digitalWrite(RED,LOW); digitalWrite(GREEN,HIGH); digitalWrite(BLUE,LOW); } else{ digitalWrite(GREEN,LOW); } if (yVal<=200){ //blue digitalWrite(RED,LOW); digitalWrite(GREEN,LOW); digitalWrite(BLUE,HIGH); } else{ digitalWrite(BLUE,LOW); } if(yVal>1000){ digitalWrite(RED,HIGH); digitalWrite(GREEN,HIGH); digitalWrite(BLUE,HIGH); } if(ButtonVal==0){ int disco=0; while(disco<10){ digitalWrite(RED,HIGH); digitalWrite(GREEN,LOW); digitalWrite(BLUE,LOW); delay(tm); digitalWrite(RED,LOW); digitalWrite(GREEN,HIGH); digitalWrite(BLUE,LOW); delay(tm); digitalWrite(RED,LOW); digitalWrite(GREEN,LOW); digitalWrite(BLUE,HIGH); delay(tm); digitalWrite(RED,HIGH); digitalWrite(GREEN,HIGH); digitalWrite(BLUE,HIGH); delay(tm); disco=disco+1; } } }
The Circuit:
Assemble your components in the exact same way as in the image below. The joystick in the image may differ from yours, don’t panic, VCC = 5v, we can assume the next 2 pins are VRx and VRy and then SW, you know GND already.
Upload the Code:
Study the code. There is nothing here that we haven’t covered so far, so I hope you understand what’s going on. When you’re ready upload the code to your Arduino board.
Moving the joystick in 1 direction will emit red light, in another, green, in another, blue and finally in the last direction it will emit white light. We have added a surprise, which I hope you noticed when studying the code, so not much of a surprise after all, press down on the joystick to start the Disco lights.
Controlling an LED with the joystick may not seem that impressive, but I needed you to understand how the joystick works before we move onto the next part.
Converting Joystick Data with the Map Function
Study the code below. First, we need to create 2 new variables to store the mapped data. Next, we convert 0-1023 to 0-255 using the map function. As you can see below, the max value is now 255. This doesn’t mean the power supplied at 255 is any less than 1023, it will be the same. This data will now be useful for analogWrite.
The Code:
int yAxis=A1; int xAxis=A0; int xVal; int yVal; int xValMapped; int yValMapped; void setup() { Serial.begin(9600); pinMode(yAxis,INPUT); pinMode(xAxis,INPUT); } void loop() { xVal=analogRead(xAxis); xValMapped=map(xVal,0,1023,0,255); yVal=analogRead(yAxis); yValMapped=map(yVal,0,1023,0,255); delay(20); Serial.print("X-Axis Value = "); Serial.println(xValMapped); Serial.print(" Y-Axis Value = "); Serial.println(yValMapped); delay(1000); }
Upload the Code:
We don’t need to alter the circuit, so when you’re, ready upload the code to your Arduino board. Once the code has uploaded, open the serial monitor and play around with the joystick. You will now see the joystick values returned are within the range we created using the map function.
Conclusion
Integrating joysticks with Arduino opens up a world of possibilities for creating interactive and responsive projects. Joysticks provide a versatile means of user input, allowing for precise control in two dimensions, and even additional buttons for added functionality. Moreover, by incorporating the map()
function, we can seamlessly translate joystick input values into meaningful output, enabling us to control motors, servos, or other devices with ease.
This combination of joystick input and the map()
function empowers Arduino enthusiasts to craft a wide range of applications, from remote-controlled vehicles and gaming controllers to interactive art installations and educational tools. The ability to remap and scale input values ensures that your projects respond intuitively to user actions, offering a more engaging and immersive experience.
So, whether you’re navigating a robot through intricate paths or fine-tuning the lighting in your DIY home automation system, the synergy between joysticks and the map()
function is a powerful tool in your Arduino toolkit, enabling you to bring your creative ideas to life with precision and control.
Happy Tinkering!
Recommendations:
If you don’t already own any Arduino hardware, we highly recommend purchasing the Elegoo Super Starter Kit. This kit has everything you need to start programming with Arduino.
You can find out more about this kit here: Elegoo Super Starter Kit