• Home
  • Archive
  • Tools
  • Contact Us

The Customize Windows

Technology Journal

  • Cloud Computing
  • Computer
  • Digital Photography
  • Windows 7
  • Archive
  • Cloud Computing
  • Virtualization
  • Computer and Internet
  • Digital Photography
  • Android
  • Sysadmin
  • Electronics
  • Big Data
  • Virtualization
  • Downloads
  • Web Development
  • Apple
  • Android
Advertisement
You are here:Home » Connecting Arduino With M029 JoyStick : Getting Started

By Abhishek Ghosh November 9, 2017 7:46 am Updated on November 9, 2017

Connecting Arduino With M029 JoyStick : Getting Started

Advertisement

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 :

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

Advertisement

---

Connecting Arduino With M029 JoyStick - Getting Started

You can upload this code :

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

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

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

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

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

Vim
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//
}

Tagged With stick analogico arduino
Facebook Twitter Pinterest

Abhishek Ghosh

About Abhishek Ghosh

Abhishek Ghosh is a Businessman, Surgeon, Author and Blogger. You can keep touch with him on Twitter - @AbhishekCTRL.

Here’s what we’ve got for you which might like :

Articles Related to Connecting Arduino With M029 JoyStick : Getting Started

  • Arduino and LED Bar Display : Circuit Diagram, Code

    Here is a Guide Explaining the Basics, Circuit Diagram, Code on Arduino and LED Bar Display. LED Bar Display is Actually Like Multiple LED.

  • How to Control Multiple Relays With Single Arduino ESP32?

    Before How to Control Multiple Relays With Single Arduino ESP32 Testing, You Need to Learn How to Create Multiple MQTT Channels & Fetch Data.

  • WROOM ESP32 Example Codes For IBM Watson IoT Platform

    Here Are Few WROOM ESP32 Example Codes For IBM Watson IoT Platform So That Anyone Can Get Started With Both of Them Without Huge Experience.

  • Detect Smartwatch With ESP32 on IBM Watson IoT Widget

    In our previous guide, we have shown that we can trigger ESP32 (with Arduino IDE) to send message to IBM Watson IoT in Presence of a Particular Samsung Galaxy Smartwatch. That process involves BLE and WiFi. In our one series of articles on Samsung Smartwatch as Proximity Switch, we triggered a local event, such as […]

performing a search on this website can help you. Also, we have YouTube Videos.

Take The Conversation Further ...

We'd love to know your thoughts on this article.
Meet the Author over on Twitter to join the conversation right now!

If you want to Advertise on our Article or want a Sponsored Article, you are invited to Contact us.

Contact Us

Subscribe To Our Free Newsletter

Get new posts by email:

Please Confirm the Subscription When Approval Email Will Arrive in Your Email Inbox as Second Step.

Search this website…

 

Popular Articles

Our Homepage is best place to find popular articles!

Here Are Some Good to Read Articles :

  • Cloud Computing Service Models
  • What is Cloud Computing?
  • Cloud Computing and Social Networks in Mobile Space
  • ARM Processor Architecture
  • What Camera Mode to Choose
  • Indispensable MySQL queries for custom fields in WordPress
  • Windows 7 Speech Recognition Scripting Related Tutorials

Social Networks

  • Pinterest (24.3K Followers)
  • Twitter (5.8k Followers)
  • Facebook (5.7k Followers)
  • LinkedIn (3.7k Followers)
  • YouTube (1.3k Followers)
  • GitHub (Repository)
  • GitHub (Gists)
Looking to publish sponsored article on our website?

Contact us

Recent Posts

  • Hybrid Multi-Cloud Environments Are Becoming UbiquitousJuly 12, 2023
  • Data Protection on the InternetJuly 12, 2023
  • Basics of BJT TransistorJuly 11, 2023
  • What is Confidential Computing?July 11, 2023
  • How a MOSFET WorksJuly 10, 2023
PC users can consult Corrine Chorney for Security.

Want to know more about us?

Read Notability and Mentions & Our Setup.

Copyright © 2023 - The Customize Windows | dESIGNed by The Customize Windows

Copyright  · Privacy Policy  · Advertising Policy  · Terms of Service  · Refund Policy