Using a Joystick With Your Arduino
24953 Views, 73 Favorites, 0 Comments
Using a Joystick With Your Arduino
Joysticks are a great source of input for a project. Especially for robotics. As hardware hackers most of us have always loved these sorts of things. Except the new guys to this might find it hard to grasp the concept during coding and such. That is what this instructable is for. Knowledge is a tool all should have, so in this I will try to give others some additions to their tools of knowledge. Let's begin!
Stuff Required
Required:
1. Arduino(I have an UNO R3)
2. Joystick(You can buy these from places like Adafruit and MakerShed)
3. Jumper Cables
4. USB Programming Cable for Arduino
Optional:
1. A coconut
2. A little bit of time
Make the Circuit
As you can see from the picture it is a simple circuit. On my joystick X,Y, VCC, and GND were labeled. X and Y go to an analog port. VCC goes to power. At first I tried 5 volts, but the results weren't the best so I changed it to 3.3 volts. After that the results were perfectly fine. Then, you hook GND to GND on the Arduino.
The Code(dun Dun Dunnnn)
The code is pretty straightforward. It reads the values and prints them to the serial monitor.
/*
Using a joystick with Arduino
By: RobotsWillRule
*/
int xVal; //X values from joystick
int yVal; //Y values from joystick
void setup() {
Serial.begin(9600); //Starts serial at 9600 baud
pinMode(A0, INPUT); //Sets the analog ports used to an input
pinMode(A2, INPUT);
}
void loop() {
xVal = analogRead(A0); //sets the X value
yVal = analogRead(A2); //sets the Y value
Serial.print(" Y is...");
Serial.print(yVal);} //prints Y values
Serial.print(" X is...");
Serial.print(xVal);}} //prints X values
Ideas
1. Using If statements you can use the joystick as input to turn on lights or motors
2. Make a video game with Arduino using a joystick
3. If you're not afraid to mod some stuff control your room fan with a joystick(I am not to be held accountable for the stupid acts you may do)
4. Be creative. Make your own!
Troubleshooting
1. Check your connections! I don't know how many times something didn't work and it turns out it wasn't connected right.
2. Check you code! Code is very picky about stuff. Make sure it does exactly what you want. Simple code that works is better than complicated code that doesn't!
3. When in doubt ask questions. It can be on forums or on this instructable.
Thanks!
By now you should know the basics of using a joystick with your Arduino because you can just check for certain values of the joystick's potentiometers. I am planning on more projects(I've got some Xbees i want to use) so please give some suggestions.