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

Comment

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

Join GarageLab (arduino, electronics, robotics, hacking)

Latest Activity

Dmitry Slepov posted a blog post

Control smart RGBW LEDs from your Tibbo Project System

Test application for the FPGA Tibbit (#57) in the smart LED controller configuration…See More
1 hour ago
Profile IconAUGUTIN AZIAKOU, FRANK KWABENA and RossBurk joined GarageLab (arduino, electronics, robotics, hacking)
1 hour ago
Profile IconOjasvi Gupta, Simran Singh, ebraam yousif doud and 6 more joined GarageLab (arduino, electronics, robotics, hacking)
Monday
Richard Reubenstone left a comment for David
"Hi, David. Please see the friends invite I sent you recently. I wanted to introduce you to…"
Friday
GarageLab posted a blog post
Sep 19
Faiyaz Ahamed is now a member of GarageLab (arduino, electronics, robotics, hacking)
Sep 16
Sylvain Monette is now a member of GarageLab (arduino, electronics, robotics, hacking)
Sep 15
vivid posted blog posts
Sep 14
Profile IconAutha Rachman, Nguyễn Anh Tú and mohammad nabe joined GarageLab (arduino, electronics, robotics, hacking)
Sep 14
vivid and Richard Reubenstone are now friends
Sep 9
vivid joined Marcelo Rodrigues's group
Thumbnail

Arduino

Group for the Arduino communitySee More
Sep 9
Anatolio Filho is now a member of GarageLab (arduino, electronics, robotics, hacking)
Sep 5
Richard Reubenstone added a discussion to the group Arduino
Thumbnail

RF Controlled Robot Using Arduino

I just joined GarageLab and the Arduino group. My son, Steven, is the Founder & CEO of…See More
Aug 30
Richard Reubenstone joined Marcelo Rodrigues's group
Thumbnail

Arduino

Group for the Arduino communitySee More
Aug 30
Richard Reubenstone joined KK. sai subramanyam's group
Thumbnail

Atmel Microcontrollers

 Sharing Ideas and information about microcontrollers.See More
Aug 29
Profile IconSmith King and Richard Reubenstone joined GarageLab (arduino, electronics, robotics, hacking)
Aug 29
Profile IconJon danks and amit sulu joined GarageLab (arduino, electronics, robotics, hacking)
Aug 26
Profile IconMario, Riyadi raiku and Luu Duc joined GarageLab (arduino, electronics, robotics, hacking)
Aug 24
Profile Iconks kr, mashkoor and Mert joined GarageLab (arduino, electronics, robotics, hacking)
Aug 22
Profile IconSastri Yanamandra and Omar Stanley joined GarageLab (arduino, electronics, robotics, hacking)
Aug 17

© 2016   Created by Marcelo Rodrigues.   Powered by

Badges  |  Report an Issue  |  Terms of Service