INTRODUCTION TO I/O's IN ARDUINO (JOYSTICK)
Intro: 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
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
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.
4 Comments
Maker19854 8 years ago
Rock Michael 8 years ago
grayl 8 years ago
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?
Rock Michael 8 years ago
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.