The next module as part of the Geekcreit sensor kit which we are going to see is the 2 axis joystick.

Hardware required:

https://www.banggood.com/Geekcreit-45-In-1-Sensor-Module-Board-Kit-Upgrade-Version-For-Arduino-p-1137050.html?p=W214159476515201703B

The module consists of three parts the joystick, a soft touch mushroom rubber hat and a breakout board. It is a 2-axis joystick which means, it can record movements in 2 directions, horizontal or vertical. It is primarily a potentiometer working on horizontal and vertical directions. The potentiometers are variable resistors which generate a variable voltage, depending on the rotation of the device around its shaft. When the joystick knob is pressed the push button switch sends a low signal.

The breakout board consist of 5 pins namely GND, VCC, VRx, VRy, S. THe VCC and GND are for power supply. VRx pin gives a value of the joystick in the horizontal direction, VRy pin gives a value of the joystick in the vertical direction. The S pin is used for selection, you can consider it like a push button.

Connection :

Code :

const int S_pin = 2; const int X_pin = 0; const int Y_pin = 1; void setup() { pinMode(S_pin, INPUT_PULLUP); Serial.begin(9600); } void loop() { Serial.print("S Pin: "); Serial.print(digitalRead(S_pin)); Serial.print("

"); Serial.print("X-axis: "); Serial.print(analogRead(X_pin)); Serial.print("

"); Serial.print("Y-axis: "); Serial.println(analogRead(Y_pin)); Serial.print("

"); delay(200); }

The above code will print the values of the

S Pin: 0 X-axis: 514 Y-axis: 518 S Pin: 1 X-axis: 514 Y-axis: 518 S Pin: 1 X-axis: 514 Y-axis: 518 S Pin: 1 X-axis: 514 Y-axis: 518

Joystick can be used for robot control, gaming and many other such applications. An innovative way of using a joystick is to use it as a mouse.

I got the code from the following link, https://www.arduino.cc/en/Tutorial/JoystickMouseControl. Since the code required small changes to get it working, I will putout the code below for reference.

Arduino Code:

/* JoystickMouseControl Controls the mouse from a joystick on an Arduino Leonardo, Micro or Due. Uses a push button to turn on and off mouse control, and a second push button to click the left mouse button Hardware: * 2-axis joystick connected to pins A0 and A1 * push buttons connected to pin D2 and D3 The mouse movement is always relative. This sketch reads two analog inputs that range from 0 to 1023 (or less on either end) and translates them into ranges of -6 to 6. The sketch assumes that the joystick resting values are around the middle of the range, but that they vary within a threshold. WARNING: When you use the Mouse.move() command, the Arduino takes over your mouse! Make sure you have control before you use the command. This sketch includes a push button to toggle the mouse control state, so you can turn on and off mouse control. created 15 Sept 2011 updated 28 Mar 2012 by Tom Igoe this code is in the public domain */ &amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;nbsp; // set pin numbers for switch, joystick axes, and LED: const int switchPin = 2; // switch to turn on and off mouse control const int mouseButton = 3; // input pin for the mouse pushButton const int xAxis = A0; // joystick X axis const int yAxis = A1; // joystick Y axis const int ledPin = 5; // Mouse control LED // parameters for reading the joystick: int range = 12; // output range of X or Y movement int responseDelay = 5; // response delay of the mouse, in ms int threshold = range / 4; // resting threshold int center = range / 2; // resting position value boolean mouseIsActive = false; // whether or not to control the mouse int lastSwitchState = LOW; // previous switch state void setup() { pinMode(switchPin, INPUT); // the switch pin pinMode(ledPin, OUTPUT); // the LED pin // take control of the mouse: Mouse.begin(); } void loop() { // read the switch: int switchState = digitalRead(switchPin); // if it's changed and it's high, toggle the mouse state: if (switchState != lastSwitchState) { if (switchState == HIGH) { mouseIsActive = !mouseIsActive; // turn on LED to indicate mouse state: digitalWrite(ledPin, mouseIsActive); } } // save switch state for next comparison: lastSwitchState = switchState; // read and scale the two axes: int xReading = readAxis(A0); int yReading = readAxis(A1); // if the mouse control state is active, move the mouse: if (mouseIsActive) { Mouse.move(xReading, yReading, 0); } // read the mouse button and click or not click: // if the mouse button is pressed: if (digitalRead(mouseButton) == HIGH) { // if the mouse is not pressed, press it: if (!Mouse.isPressed(MOUSE_LEFT)) { Mouse.press(MOUSE_LEFT); } } // else the mouse button is not pressed: else { // if the mouse is pressed, release it: if (Mouse.isPressed(MOUSE_LEFT)) { Mouse.release(MOUSE_LEFT); } } delay(responseDelay); } /* reads an axis (0 or 1 for x or y) and scales the analog input range to a range from 0 to &amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;lt;range&amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;gt; */ int readAxis(int thisAxis) { // read the analog input: int reading = analogRead(thisAxis); // map the reading from the analog input range to the output range: reading = map(reading, 0, 1023, 0, range); // if the output reading is outside from the // rest position threshold, use it: int distance = reading - center; if (abs(distance) &amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;lt; threshold) { distance = 0; } // return the distance for this axis: return distance; }

When the above code is flashed in to an Arduino Leonardo and plugged in to a PC (preferably a Linux running machine) then it will look like this.

Video :