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: 4536

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

Profile IconDinko, Bukasa Tshilombo, simataempat and 1 more joined GarageLab (arduino, electronics, robotics, hacking)
11 hours ago
ANGEL RUIZ replied to ANGEL RUIZ's discussion Can this Xyron be used as a CNC router
"Here"
yesterday
ANGEL RUIZ posted a discussion

Can this Xyron be used as a CNC router

I don't have the money to afford a CNC router so I was wondering if I can convert this into…See More
yesterday
GarageLab posted a blog post

HAVOC: NASA’s proposed cloud city over Venus

The surface of Venus is a notorious hell-hole. If an atmospheric pressure roughly 90 times greater…See More
yesterday
roberto premoli's photo was featured

premobox25-per-web-001

premoboard+cubieboard+hdd2.5''features:-3LAN-5USB-2WIFI-500GB HDD…
Tuesday
Veritus's photo was featured

Battery Pack

Created a closed system of power in a wearable back pack. Back pack had 5 power outlets each at 220…
Tuesday
NELU NASTASOIU's photo was featured
Tuesday
wesley wortman's photo was featured
Tuesday
GarageLab posted blog posts
Tuesday
sunny lai left a comment for sunny lai
"thanks for hearing from you, it is my pleasure"
Tuesday
Enahoro Olori updated their profile
Monday
Profile IconRobert Sitton, Mr Abraham Akim and Magnus Nordin joined GarageLab (arduino, electronics, robotics, hacking)
Monday
GarageLab posted blog posts
Dec 12
Profile Icongiacomo, dan rules and Jay Thornton joined GarageLab (arduino, electronics, robotics, hacking)
Dec 11
GarageLab posted a blog post
Dec 10
GarageLab replied to Lou Liberatore's discussion LiquidCrystal_I2C library in the group Arduino
" Hi Lou Lberatone, I think you Hi Lou Lberatone, I think you probably are using the libraries…"
Dec 10
GarageLab joined Marcelo Rodrigues's group
Thumbnail

Arduino

Group for the Arduino communitySee More
Dec 10
GarageLab posted blog posts
Dec 9
Profile Iconbenjamin black and sunny lai joined GarageLab (arduino, electronics, robotics, hacking)
Dec 9
Profile IconJjdelala and Sathish GP joined GarageLab (arduino, electronics, robotics, hacking)
Dec 8

© 2014   Created by Marcelo Rodrigues.

Badges  |  Report an Issue  |  Terms of Service