Analog electronics, Electronics components, Hobby, Microcontrollers, Projects

Arduino tutorial (Part 13, joystick)

In this tutorial, it is shown how to use joystick module in Arduino to make projects. This module is similar to the model found in Playstation controls.

How the joystick works?

The shield has two potentiometers, these give information about joystick’s position.

Also has a button, to be pressed must press the top of joystick without be inclined.

Taking out the pin. Note that one potentiometer is connected to white stem, it stays in the middle of two arcs linked to other potentiometer. This is the 2 axes gimbal mechanism, which allows two potentiometers to move at the same time.

The pins VRx and VRy are analog outputs of potentiometers and SW is button’s output.

Teste with joystick module

The module’s connection with Arduino.

This is the code, when move the joystick analogue values “xValue” and “yValue” must change in Serial window.

The “xValue” and “yValue” values change from 0 to 1023 depending on joystick’s position, represent x and y axes. These are x and y coordinates which appears in Serial screen in relation to position. In practice, there is some variation.

Controlling LEDs and servomotors

In this project, joystick controls 4 LEDs of different colors and a servomotor. Each direction for which it is inclined, a LED turns on and servomotor turns a step, for each diagonal direction, 2 LEDs turn on.

To shorten the code, pins for LEDs don’t have names, were simply called 5, 7, 8 and 11. Note the declarations like “pinMode(11,OUTPUT);” and commands like “digitalWrite(7,HIGH);”.

Using libraries

This is an example of program using a library for the module, here is the link. This example comes from the source to show how the library works.

This declaration define pins of x, y axes and button respectively. Here were defined as analog pins 0 and 1, the button’s input is digital 4.

This command print values of x and y axes. The z value shows if button is pressed, in my case, false when pressed and true when released. The 0 and 100 are minimum and maximum values of coordinates of axes.


Liked it? Take a second to support Electrical e-Library on Patreon!

About Pedro Ney Stroski

Leave a Reply

Your email address will not be published. Required fields are marked *