# Step-by-step Python tutorial : Use a joystick with your Gamebuino Meta

**URL:** <https://community.gamebuino.com/t/step-by-step-python-tutorial-use-a-joystick-with-your-gamebuino-meta/320>\
**Category:** \[EN\] Programming in Python\
**Created:** [6 September 2021 16:47 UTC](https://community.gamebuino.com/t/step-by-step-python-tutorial-use-a-joystick-with-your-gamebuino-meta/320 "2021-09-06T16:47:51Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tombuino](https://community.gamebuino.com/user_avatar/community.gamebuino.com/tombuino/32/64_2.png) [@Tombuino](https://community.gamebuino.com/u/Tombuino)\
**Post date:** [6 September 2021 16:47 UTC](https://community.gamebuino.com/t/step-by-step-python-tutorial-use-a-joystick-with-your-gamebuino-meta/320/1 "2021-09-06T16:47:51Z")

</div>

In this tutorial we will see how to simply use the joystick from the [accessory pack](https://shop.gamebuino.com/en/accessoires/73-pack-accessoires-pour-gamebuino.html) with CircuitPython.

![hline_orange](https://community.gamebuino.com/uploads/default/original/1X/c338b375ef4f0c5b6fd0ee77e3b4da6b90ef4e26.png)

### Getting started : Tools requirement

This tutorial is made with Mu IDE for CircuitPython.  
If you haven’t installed those yet please refer to the [installation guide](https://gamebuino.com/en/academy/workshop/discover-python-programming/installation-and-prerequisites) on our website.

![hline_orange](https://community.gamebuino.com/uploads/default/original/1X/c338b375ef4f0c5b6fd0ee77e3b4da6b90ef4e26.png)

### Wiring :

 ![dessin_joystick](https://community.gamebuino.com/uploads/default/original/1X/1076b3ffd65362e1acc2560242662eb9d8fdf755.png)

| Joystick | Backpack | Color | Description |
| --- | --- | --- | --- |
| GND | GND | BROWN | Ground |
| 5V | VBAT | RED | Power input |
| VRX | A1 | GREEN | X Axis |
| VRY | A2 | BLUE | Y Axis |
| SW | 7 | YELLOW | ISwitch |

![hline_orange](https://community.gamebuino.com/uploads/default/original/1X/c338b375ef4f0c5b6fd0ee77e3b4da6b90ef4e26.png)

## Step 1 : Program’s Basis

Begin by opening Mu and creating a new file

 ![capture_mu](https://community.gamebuino.com/uploads/default/original/1X/1cf1d93b94fdace88fdccad9924f61002e488158.png)

Copy the following code :

```python
from gamebuino_meta import begin, waitForUpdate, display, buttons, color
import board
from analogio import AnalogIn

while True:
    waitForUpdate()
    display.clear()

```

This will import the Gamebuino libraries needed for the program to work, as well as a “board” and an “AnalogIn” libraries we will use to handle the joystick.

The part begining by “while True:” is our main loop, without it nothing would happen at all.

![hline_orange](https://community.gamebuino.com/uploads/default/original/1X/c338b375ef4f0c5b6fd0ee77e3b4da6b90ef4e26.png)

## Step 2 : Configure the joystick

To be able to use the joystick we will need to read the values sent by the two pins corresponding to the X and Y axis. Since those are analog pins we will use the “AnalogIn” function to communicate with the stick.

For that purpose we define the attach pins of our two axis as follows :

```python
x_pin = analogio.AnalogIn(board.A1)
y_pin = analogio.AnalogIn(board.A2)

```

We assign the A1 pin to the X axis and the A2 pin to the Y axis.

![hline_orange](https://community.gamebuino.com/uploads/default/original/1X/c338b375ef4f0c5b6fd0ee77e3b4da6b90ef4e26.png)

## Steap 3 : Read and display

We can now proceed with the reading itself.

For that we use a small function called “read\_input()” such as :

```python
def read_input(pin):
    return pin.value

```

Calling this function and specifying which pin we want to read from will give us a value between 0 and 65536 (16 bits base)

To monitor the behavior of our setup we will display those values on screen.  
Nothing complicated here, we just add a few calls the the print function in our main loop :

```python
while True:
    waitForUpdate()
    display.clear()
    
    display.print("X = ")
    display.print(read_input(x_pin))
    display.print("\n")
    display.print("Y = ")
    display.print(read_input(y_pin))

```

A bit convoluted but since we don’t have access to a proper printf function we can’t do complex formating so we need to make do.

We will now create a specific function that will transform those values into a visual representation of our joystick.  
This function will be called “draw\_interface” and is declared as such :

```python
def draw_interface():

```

It doesn"t do anything yet but we can already add a call to it in our main loop :

```python
while True:
    waitForUpdate()
    display.clear()
    
    display.print("X = ")
    display.print(read_input(x_pin))
    display.print("\n")
    display.print("Y = ")
    display.print(read_input(y_pin))

    draw_interface()

```

We will represent the movement of our joystick in the form of a point moving inside a circle. So we will display those two elements :

```python
def draw_interface():
    display.drawCircle(display.width()//2, display.height()//2, 20)
    
    dot_x = (read_input(x_pin) - 32768) * 20 // 65536
    dot_y = (read_input(y_pin) - 32768) * 20 // 65536
    
    display.setColor(color.RED)
    display.fillCircle(dot_x + display.width()//2, dot_y + display.height()//2, 5)

```

We start by drawing a circle with the function “drawCircle”, we place it at the center of the screen with a radius of 20 units.

We then calculate the coordinates of the point to display by reporting the values read from our two axis X and Y on this 20 units circle.

The we change the display color to red before using “fillCircle” to draw a red dot of 5 units radius that moves accordingly with the movement of our two axis.

![hline_orange](https://community.gamebuino.com/uploads/default/original/1X/c338b375ef4f0c5b6fd0ee77e3b4da6b90ef4e26.png)

You just went through another tutorial ! Now it’s your time to shine.
