# Using analog joystick boards to control components of electronic circuits

Check out the new site at https://rkblog.dev.

To control robots, or some electronic values we often need some potentiometers. One potentiometer is just one value. To make things easier for controlling multiple parameters at once joystick were invented. Nowadays you can get cheap "analog" joystick for your microcontroller that allows you to control signal from two axis - X and Y. That can be used for example to control where and how fast a robot-car is going. Such joystick has two potentiometers moving in perpendicular axis.

In this article I'll cover the basic 5-pin joystick boards. There are also Arduino shields or more advanced joystick boards that may have more pins and more features.

### Connecting joystick to a mictrocontroller

The board will have 5 pins: GND (-), VCC (+), one for X axis, one for Y axis, and one for button hidden on the side of the joystick mechanics. Depending on model the order may differ and the labels may be also different. If there is a manual for your board it's good to check it. I have two boards, and one has GNC, +5V, VRx, VRy, SW while the other +, X, R, Y, G (where G is GND and R is for the button).

X, Y pins need to be connected to analog pins, while button pin to a digital pin.

### Reading position of joystick axis

Potentiometers of the X and Y axis will give a value depending on the resistance they are current set to. It will change when we move the joystick knob. The button can be pressed when pressing the joystick knob. The digital pin must be set to high and when the button will be pressed it will fall to low state.

Here is an example code for pyMCU that will read all the values:
```from time import sleep

import pymcu

BUTTON_DIGITAL_PIN = 1
X_AXIS_ANALOG_PIN = 1
Y_AXIS_ANALOG_PIN = 2

mb = pymcu.mcuModule()
mb.digitalState(BUTTON_DIGITAL_PIN, 'input')
mb.pinHigh(BUTTON_DIGITAL_PIN)

while True: