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

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

anibal martins barbosa is now a member of GarageLab (arduino, electronics, robotics, hacking)
8 hours ago
Michael Oehlke is now a member of GarageLab (arduino, electronics, robotics, hacking)
yesterday
jerrysmith87 posted blog posts
Wednesday
GarageLab posted blog posts
Wednesday
Profile IconJanak Panchal, John Ruffing and jerrysmith87 joined GarageLab (arduino, electronics, robotics, hacking)
Wednesday
Profile IconJoshua, Mark Webber, Ronan Keets and 6 more joined GarageLab (arduino, electronics, robotics, hacking)
Monday
GarageLab posted a blog post

Sound Torch Bluetooth Speaker Spews Flames

Play Through the Fire and Flames (DragonForce) literally through fire and flames with the Sound…See More
May 22
GarageLab posted a blog post
May 20
GarageLab posted a blog post
May 18
Profile Iconmechanical engineer. R&D . and Geert hoste joined GarageLab (arduino, electronics, robotics, hacking)
May 18
Henry Chaj is now a member of GarageLab (arduino, electronics, robotics, hacking)
May 15
GarageLab posted a blog post
May 15
AnalysIR posted a blog post

Simple Infrared PWM on Arduino

We are often asked on discussion boards, about conflicts between IRremote or IRLib and other…See More
May 13
Profile Iconnareshkumar p and Ethan "Lone" Wolf joined GarageLab (arduino, electronics, robotics, hacking)
May 13
GarageLab posted a blog post

Printing photorealistic images on 3D objects

Hydrographic Printing is a technique of transferring colored inks on a film to the surface of an…See More
May 13
GarageLab posted a blog post

3D Printing and Ultrasound Help Blind Pregnant Moms Touch Their Unborn Babies

Here’s a pair of wonderful stories that were made possible with the help of 3D printing. As part of…See More
May 12
iqxca is now a member of GarageLab (arduino, electronics, robotics, hacking)
May 12
Profile IconTom Reagh and Alc joined GarageLab (arduino, electronics, robotics, hacking)
May 11
GarageLab posted a blog post
May 8
Arduino Aficionado posted photos
May 8

© 2015   Created by Marcelo Rodrigues.

Badges  |  Report an Issue  |  Terms of Service