Tutorial: Joystick shield with Arduino

The Joystick Shield is an Arduino add-on that has a 2 axis joystick and 5 buttons (one in the joystick). We can use it to control a lot of things that needs analog and digital commands. Here we have the circuit extracted from the datasheet):

Here we have a sample code, modified from here.

//Create variables for each button on the Joystick Shield to assign the pin numbers

char button0=3, button1=4, button2=5, button3=6;
char sel=2;

void setup(void)
{
pinMode(sel, INPUT); //Set the Joystick 'Select'button as an input
digitalWrite(sel, HIGH); //Enable the pull-up resistor on the select button

pinMode(button0, INPUT); //Set the Joystick button 0 as an input
digitalWrite(button0, HIGH); //Enable the pull-up resistor on button 0

pinMode(button1, INPUT); //Set the Joystick button 1 as an input
digitalWrite(button1, HIGH); //Enable the pull-up resistor on button 1

pinMode(button2, INPUT); //Set the Joystick button 2 as an input
digitalWrite(button2, HIGH); //Enable the pull-up resistor on button 2

pinMode(button3, INPUT); //Set the Joystick button 3 as an input
digitalWrite(button3, HIGH); //Enable the pull-up resistor on button 3

Serial.begin(9600); //Turn on the Serial Port at 9600 bps
}

void loop(void)
{
Serial.print(analogRead(0)); //Read the position of the joysticks X axis and print it on the serial port.
Serial.print(",");
Serial.print(analogRead(1)); //Read the position of the joysticks Y axis and print it on the serial port.
Serial.print(",");
Serial.print(digitalRead(sel)); //Read the value of the select button and print it on the serial port.
Serial.print(",");
Serial.print(digitalRead(button0)); //Read the value of the button 0 and print it on the serial port.
Serial.print(",");
Serial.print(digitalRead(button1)); //Read the value of the button 1 and print it on the serial port.
Serial.print(",");
Serial.print(digitalRead(button2)); //Read the value of the button 2 and print it on the serial port.
Serial.print(",");
Serial.println(digitalRead(button3)); //Read the value of the button 3 and print it on the serial port.

//Wait for 100 ms, then go back to the beginning of 'loop' and repeat.
delay(100);
}

Upload the code to your board and open the serial monitor. You will see the numbers as below:

Move the joystick around and watch the first 2 values changing. The following numbers represents the buttons. Push one of them and the value will become zero.

References:

http://www.sparkfun.com/products/9760

http://dlnmh9ip6v2uc.cloudfront.net/datasheets/Dev/Arduino/Shields/...

http://www.sparkfun.com/Code/Joystick_Shield_Example.zip

Views: 3909

Tags: arduino, joystick, shield, tutorial

Comment

You need to be a member of GarageLab (arduino, electronics, robotics, hacking) to add comments!

Join GarageLab (arduino, electronics, robotics, hacking)

Latest Activity

GarageLab posted blog posts
Friday
Profile IconSheila Garland and petran747 . joined GarageLab (arduino, electronics, robotics, hacking)
Thursday
Antonio Turner posted a status
Thursday
GarageLab posted blog posts
Wednesday
Profile Iconleon crist, Tim Owen and Mishca de Costa joined GarageLab (arduino, electronics, robotics, hacking)
Sep 15
GarageLab posted a blog post

Arduino-Powered Turntables That Turn DJing Into Child’s Play

Per Holmquist has always had a finely tuned ear for music. “When I hear a song for the first time I…See More
Sep 15
GarageLab posted a blog post
Sep 12
Jean-Guillaume LALANNE posted a discussion

Smallest Arduino with BLE stack and push button integrated

Hi,I am a newbie in the maker world. I have bought a RFDUINO some month ago to play with.I did find…See More
Sep 12
Profile IconDavid bolles and Abhisht Yadav joined GarageLab (arduino, electronics, robotics, hacking)
Sep 11
GarageLab posted blog posts
Sep 11
chuby okafor updated their profile
Sep 10
maurice patrick commented on GarageLab's blog post 3D printer uses Arduino controller and makes food in unusual ways
"Sweet!!!!"
Sep 10
Stephen Wizowski left a comment for Stephen Wizowski
"NO... do not like this approach... AT ALL!"
Sep 10
Profile IconBernie Whalen, FRANK KWABENA, mikedaniels and 11 more joined GarageLab (arduino, electronics, robotics, hacking)
Sep 10
GarageLab posted a blog post

This Robot Crawls Through Acid, Ice and Fire

This robot, developed at Professor George M. Whitesides’ lab at Harvard University has recently…See More
Sep 10
GarageLab posted a blog post
Sep 8
Antonio Turner posted a status
Sep 8
GarageLab posted blog posts
Sep 5
Profile IconWilliam Ritchie, Chuck Untulis, David Perez and 3 more joined GarageLab (arduino, electronics, robotics, hacking)
Sep 5
GarageLab posted blog posts
Sep 3

© 2014   Created by Marcelo Rodrigues.

Badges  |  Report an Issue  |  Terms of Service