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

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

Ali Kazemian replied to Ali Kazemian's discussion Help! Reading AC Voltage with Arduino
"Here is an update: I repeated the test and this used an oscilliscope as well to read voltage from…"
17 hours ago
Edward Kendall updated their profile
21 hours ago
Edward Kendall joined KK. sai subramanyam's group
Thumbnail

SOLAR [ Green Earth ]

The growing environmental concerns and demand for alternatives to fossil fuel-based energy…See More
21 hours ago
Ali Kazemian posted a discussion

Help! Reading AC Voltage with Arduino

Hi guys! I'm trying to build a device to measure the electrical resistivity of a paste…See More
Wednesday
Profile IconAli Kazemian, Daniel , ABHILASH A and 5 more joined GarageLab (arduino, electronics, robotics, hacking)
Wednesday
Profile IconElectric and Adderall, Ishmael Muhammad, Yuval kob and 2 more joined GarageLab (arduino, electronics, robotics, hacking)
Feb 3
Jesse Howards updated their profile
Jan 29
Profile Iconsuneet, Augustin Aziankou and Ayinde Adetunji joined GarageLab (arduino, electronics, robotics, hacking)
Jan 29
Augustin Aziankou updated their profile
Jan 28
Profile IconTom Evans, said, Ghenam houssam and 15 more joined GarageLab (arduino, electronics, robotics, hacking)
Jan 25
Kenny Sims is now a member of GarageLab (arduino, electronics, robotics, hacking)
Jan 11
Paulo Trindade is now a member of GarageLab (arduino, electronics, robotics, hacking)
Jan 7
Profile Iconyashwita suvarna, António Gonçalves, Thurman Gillespy and 6 more joined GarageLab (arduino, electronics, robotics, hacking)
Jan 4
Profile IconMehmet Emin Örs, Andrew Myers, Ruban Venkatesh and 1 more joined GarageLab (arduino, electronics, robotics, hacking)
Dec 28, 2015
stephen smith is now a member of GarageLab (arduino, electronics, robotics, hacking)
Dec 22, 2015
Profile Iconlantoniaina bruce, Manoel Pereira, Ben and 1 more joined GarageLab (arduino, electronics, robotics, hacking)
Dec 21, 2015
GarageLab replied to GarageLab's discussion Serious claims against BeagleBone's manufacturer CircuitCo
"Hello guys, New facts to the CircuitCo case. But until now, the didn't refund us or send the…"
Dec 21, 2015
michae coley left a comment for RITIU TRAIAN
"what project have you done?"
Dec 15, 2015
Profile Iconshalini amarnani, Philipp Schmid, Jesse Howards and 4 more joined GarageLab (arduino, electronics, robotics, hacking)
Dec 14, 2015
Profile IconAbador and Dara joined GarageLab (arduino, electronics, robotics, hacking)
Dec 9, 2015

© 2016   Created by Marcelo Rodrigues.   Powered by

Badges  |  Report an Issue  |  Terms of Service