## Introduction: INTRODUCTION TO I/O's IN ARDUINO (JOYSTICK)

DATE:3/22/2016

DIFFICULTY: EASY

Hello everyone, i have come with another easy tutorial on inputs, this time on joystick reading and control. The arduino.cc website explains the joystick as a 2D potentiometer and also gave examples on how to treat the values and a code that blinks the LED depending on the value of the 2 potentiometers. Here i decided to make a simple example on how to turn ON and OFF a LED using the joystick and i have left the output values untreated. so without further ado lets begin.

## Step 1: MATERIALS NEEDED/ CONNECTION

• Platform: Arduino

• Input device: Joystick

• Output device: LED

• Power supply: Arduino USB/9V battery

• Connectors/Jumpers

JOYSTICK __________ ARDUINO

GND ----------------------------- GND

+5V ----------------------------- VCC

VRx ----------------------------- A0

VRy ----------------------------- A1

SW ----------------------------- Pin2

LED

long leg __________________ Pin5

short leg __________________ GND

## Step 2: ADDITIONAL INFO AND CODE

I thought it may be helpful to some people, when you setup the circuit, upload the code below you can try for yourself and see the different readings in each direction, in the picture above i provided 5 positions of the joystick and their corresponding values. Have fun with the code, play around with it and you can see from the schematic that no Resistor was attached to the LED cathode, this is because the output voltage is greatly affected, since the joystick itself is already a resistor device (potentiometer). The code below is a very easy one to understand, it turns ON the LED at X:1023,Y:1023 and turns it OFF at X:0,Y:0, and you can also monitor the values from the Serial background.

1. // POrt initialization
2. int SW_pin = 2; // digital pin connected to switch output
3. int VX_pin = 0; // analog pin connected to X output
4. int VY_pin = 1; // analog pin connected to Y output
5. int LED = 5;
6. void setup() {
7. pinMode(SW_pin, INPUT);
8. pinMode(LED, OUTPUT);
9. Serial.begin(9600);
10. }
11. void loop() {
15. if(V1 == 1023 && V2 == 1023){
16. delay(100);
17. digitalWrite(LED,HIGH);
18. }
19. else if (V1 == 0 && V2 == 0){
20. digitalWrite(LED,LOW);
21. }
22. Serial.print("Switch: ");
23. Serial.print(V3);
24. Serial.print("\n");
25. Serial.print("VX-axis: ");
26. Serial.print(V1);
27. Serial.print("\n");
28. Serial.print("VY-axis: ");
29. Serial.print(V2);
30. Serial.print("\n\n");
31. delay(500);
32. }

## Step 3: MOTOR CONTROL

I promised to edit this work and add new information should i succeed in a finding a solution to motor control. That is what i am doing in this step, thanks to medwards38 for his comment, it reminded me of what i failed to do earlier.

So if you would like to control a motor instead of a led with this solution, you will have to use the setup in the image.

things you will need include:

• 10kohms resistor
• NPN transistor, i used(BC639)
• Diode, i used(BY299)

The connection is easy, when you look at the schematic

Arduino Pin5 ----- 10kohm resistor ----- Transistor Base

Emitter ----- GND

+5V ---- Motor(positive) and Diode(+) junction

Motor(negative) ------- Collector and Diode(-) junction.

if anybody has an idea on how to make this project more fun or have any questions please comment.Thank you.