• 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

This Article Has Been Shared 290 Times!

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

  • Methods and Components To Build Electronic Circuits

    We Have Discussed Some Details On Methods and Components To Build Electronic Circuits Which Are Needed To Create Ammeter Electronic Projects.

  • Arduino Temperature Humidity Sensor : New DHT11, DHT21, DHT22 Test Code

    Here is New Test Codes For Arduino Temperature Humidity Sensor DHT11, DHT21, DHT22 Test Code as Hardware (Not Shields). 2 Libraries Needed.

  • How to Increase the Number of Digital Pins in Arduino (Port Extender)

    13+6 Pins Often a Limitation of Arduino in Projects Where We Need Many Pins. Here is How to Increase the Number of Digital Pins in Arduino.

  • Arduino Fingerprint Scanner Module GT-511C3, GT-511C1R

    Here is Arduino Fingerprint Scanner Module GT-511C3 or GT-511C1R Buying Guide and Basic Details On Unknown Matters Around Fingerprint Scanner.

  • Arduino UNO Prototype Shield Tutorial

    In Arduino UNO Prototype Shield Tutorial We Have Talked How We Can Use to Convert a Project Semi-Permanent With Shield, Components & Solder.

Additionally, 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 (22.1K 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

  • Types of Blackjack Variants: Discover the Different Versions of the Game May 23, 2022
  • How Cloud, Robotics And Sensor Technologies Are Changing The Business Landscape May 23, 2022
  • Modernizing Your Business With a Hybrid Cloud Strategy May 22, 2022
  • Big Data in Sports May 20, 2022
  • FaaS Versus PaaS Deployment: What You Should Know May 18, 2022

About This Article

Cite this article as: Abhishek Ghosh, "Connecting Arduino With M029 JoyStick : Getting Started," in The Customize Windows, November 9, 2017, May 24, 2022, https://thecustomizewindows.com/2017/11/connecting-arduino-with-m029-joystick-getting-started/.

Source:The Customize Windows, JiMA.in

This website uses cookies. If you do not want to allow us to use cookies and/or non-personalized Ads, kindly clear browser cookies after closing this webpage.

Read Privacy Policy.

PC users can consult Corrine Chorney for Security.

Want to know more about us? Read Notability and Mentions & Our Setup.

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

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