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

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
23 hours ago
Raghuvir G posted discussions
yesterday
Jagajeet K posted a discussion

Digital IO – LED Blink – Arduino for Beginners

I have a doubt in the below link  …in that I used 220 ohm resistor for LED …Is Resistor have any…See More
yesterday
Profile IconRalph Wade, Jagajeet K, Raghuvir G and 1 more joined GarageLab (arduino, electronics, robotics, hacking)
yesterday
GarageLab posted blog posts
Tuesday
Enrico M. posted a status
"Building the Apollo console version I"
Monday
Lena Cook is now a member of GarageLab (arduino, electronics, robotics, hacking)
Monday
Paul Denton updated their profile
Nov 21
GarageLab posted a blog post
Nov 21
Profile IconRichard Coplan and Juan V. Sanchez-Andres joined GarageLab (arduino, electronics, robotics, hacking)
Nov 21
Kshitij Dadhekar commented on GarageLab's blog post Tutorial: How to use the Cellular Shield (SM5100B) with Arduino
"Hello,       Can you tell me what is the resistance of the speaker to be…"
Nov 19
GarageLab posted a blog post
Nov 19
Profile IconKshitij Dadhekar, Marvellous Mark and Enrico M. joined GarageLab (arduino, electronics, robotics, hacking)
Nov 19
david chan commented on GarageLab's blog post Tutorial: Humidity and Temperature Sensor with Arduino
"How do you see the output "
Nov 18
Enrico M. posted a discussion

Power supply circuit

Hi guys,I'm building some sort of wood case to arduino mega with LCD, keypad, LED's ...I also want…See More
Nov 18
GarageLab posted blog posts
Nov 18
Profile Icondavid chan, uday harihar, CVassiliou and 4 more joined GarageLab (arduino, electronics, robotics, hacking)
Nov 18
Jeff Bowen updated their profile
Nov 17
Michael Shaw replied to Jean-Guillaume LALANNE's discussion Smallest Arduino with BLE stack and push button integrated
"Check out Tiny circuits for open source: https://tiny-circuits.com/"
Nov 16
rozzers commented on rozzers's video
Nov 14

© 2014   Created by Marcelo Rodrigues.

Badges  |  Report an Issue  |  Terms of Service