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

TOPIC:JOYSTICK READING AND CONTROL

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() {
12. int V1 = analogRead(VX_pin);
13. int V2 = analogRead(VY_pin);
14. int V3 = digitalRead(SW_pin);
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.

Comments

author
medwards38 (author)2016-03-23

did you use a mosfet/transistor with the motor circuit?

author
Rock Michael (author)medwards382016-03-24

Great comment, Thank you.

author
grayl (author)2016-03-22

When looking at graph paper, X is usually + ve right, and Y is usually + ve up. You seem to have that reversed in the first image at Step 2 where it appears X is + ve up and Y is + ve right. Was this your intention?

author
Rock Michael (author)grayl2016-03-23

I understand your doubt, i felt the same too. But the labellings i made on the drawing are based on readings i got during testing. You can try it yourself and see what values you will get for each of the positions.

About This Instructable

3,464views

98favorites

License:

Bio: B.sc in Electrical Engineering. Microcontrollers,PLC specialist programmer and embedded system developer.
More by Rock Michael:Relay INTRODUCTION TO (IC's) 555 TIMER BASICS (ASTABLE)INTRODUCTION TO I/O's IN ARDUINO (JOYSTICK)
Add instructable to: