Obviously these are Made in China and costs around $2. Here is a getting started guide around connecting Arduino with M029 JoyStick and testing with basic code. It is a useful thing for robotics. These analog, has 2 axis, PS2 compatible. Exactly like Nokia’s old Symbian mobile phones, we can use this joystick to control a menu, centre click to select or control servo motors. But before doing advanced projects, we need to test with basic code and circuit.
Connecting Arduino With M029 JoyStick : Getting Started
These joysticks are nothing but 2 potentiometer and one push button. There are 5 connections to the joystick. Two are for electrical connections – one to +5V of Arduino and another to GND. Other three pins are for Key, Y and X.
The key is digital, Y and X are analog. For basic testing, frankly you do not need the Key – the digital one. The names are variables depending on China company manufactured it. Usually the name of the pins are :
1 2 3 4 5 | GND +5V VRx VRy SW |
The VRx
and VRy
are analog and will connect to Arduino’s A0, A1 pin. Obviously, GND
and +5V
will connect to respective same named connection. Rest is SW, which we said as Key, that is digital and for our this guide we connected to Pin 2. This is the circuit diagram :
---
You can upload this code :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | int ledPin = 13; int UD = 0; int LR = 0; void setup() { pinMode(ledPin, OUTPUT); Serial.begin(9600); } void loop() { UD = analogRead(A0); LR = analogRead(A1); //Map the values char x_translate = map(LR, 1021, 0, 7, 0); char y_translate = map(UD, 1021, 0, 0, 7); //Serial.print("UD = "); //Serial.print(UD, DEC); //Serial.print(", LR = "); //Serial.print(LR, DEC); Serial.print(" x = "); Serial.print(x_translate, DEC); Serial.print(" y = "); Serial.println(y_translate, DEC); delay(150); //this to delay correctly digitalWrite(ledPin, HIGH); delay(UD); digitalWrite(ledPin, LOW); delay(LR); } |
and open the Serial Monitor on Arduino IDE/Software on computer. You’ll get this kind of reading :
1 2 3 4 5 6 7 | ... x = 4, Y = 3 x = 4, Y = 3 x = 4, Y = 3 x = 4, Y = 3 x = 4, Y = 3 ... |
If you move the joystick, you’ll understand that values of X and Y are changing.
There are few lines which are commented out :
1 2 3 4 | //Serial.print("UD = "); //Serial.print(UD, DEC); //Serial.print(", LR = "); //Serial.print(LR, DEC); |
If you make all the lines active, you’ll get raw value too. More easy code I ever found is this :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | const int SW_pin = 2; const int X_pin = 0; const int Y_pin = 1; void setup() { pinMode(SW_pin, INPUT); digitalWrite(SW_pin, HIGH); Serial.begin(9600); } void loop() { Serial.print("Switch: "); Serial.print(digitalRead(SW_pin)); Serial.print("n"); Serial.print("X-axis: "); Serial.print(analogRead(X_pin)); Serial.print("n"); Serial.print("Y-axis: "); Serial.println(analogRead(Y_pin)); Serial.print("nn"); delay(500); } |
There is a JoyStick module with stick, I saw them to use this code which somewhat works as the hardware is same :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 | const byte PIN_ANALOG_X = 0; const byte PIN_ANALOG_Y = 1; const int X_THRESHOLD_LOW = 300; const int X_THRESHOLD_HIGH = 380; const int Y_THRESHOLD_LOW = 300; const int Y_THRESHOLD_HIGH = 380; int x_position; int y_position; int x_direction; int y_direction; void setup() { Serial.begin(9600); } void loop () { x_direction = 0; y_direction = 0; x_position = analogRead(PIN_ANALOG_X); y_position = analogRead(PIN_ANALOG_Y); if (x_position > X_THRESHOLD_HIGH) { x_direction = 1; } else if (x_position < X_THRESHOLD_LOW) { x_direction = -1; } if (y_position > Y_THRESHOLD_HIGH) { y_direction = 1; } else if (y_position < Y_THRESHOLD_LOW) { y_direction = -1; } if (x_direction == -1) { if (y_direction == -1) { Serial.println("left-down"); } else if (y_direction == 0) { Serial.println("left"); } else { // y_direction == 1 Serial.println("left-up"); } } else if (x_direction == 0) { if (y_direction == -1) { Serial.println("down"); } else if (y_direction == 0) { Serial.println("centered"); } else { // y_direction == 1 Serial.println("up"); } } else { // x_direction == 1 if (y_direction == -1) { Serial.println("right-down"); } else if (y_direction == 0) { Serial.println("right"); } else { // y_direction == 1 Serial.println("right-up"); } } } |
Everything needs some adjustment of values.
Probably you want to move a dot on 8×8 LED Dot Matrix Display With MAX7219 with the joystick.
You can try this kind of logic, I found it on Github :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | int UD = 0; int LR = 0; //Setting up controller// #include "LedControl.h" // need the library LedControl lc=LedControl(8,10,9,1); //10 is to CLOCK, 9 = CS, 8=DIN// void setup() { Serial.begin(9600); lc.shutdown(0,false);// turn off power saving, enables display lc.setIntensity(0,8);// sets brightness (0~15 possible values) lc.clearDisplay(0);// clear screen } void loop() { UD = analogRead(A0); LR = analogRead(A1); char x_translate = map(LR, 1021, 0, 7, 0); //This maps the values// char y_translate = map(UD, 1021, 0, 0, 7); Serial.print("UD = "); Serial.print(UD, DEC); Serial.print(", LR = "); Serial.print(LR, DEC); Serial.print(", x = "); Serial.print(x_translate, DEC); Serial.print(", y = "); Serial.println(y_translate, DEC); // not in shutdown mode lc.clearDisplay(0); lc.setLed(0,x_translate,y_translate,true); delay(150); //Mess with this delay to get your joystick correct// } |