.. note::
Hello, welcome to the SunFounder Raspberry Pi & Arduino & ESP32 Enthusiasts Community on Facebook! Dive deeper into Raspberry Pi, Arduino, and ESP32 with fellow enthusiasts.
**Why Join?**
- **Expert Support**: Solve post-sale issues and technical challenges with help from our community and team.
- **Learn & Share**: Exchange tips and tutorials to enhance your skills.
- **Exclusive Previews**: Get early access to new product announcements and sneak peeks.
- **Special Discounts**: Enjoy exclusive discounts on our newest products.
- **Festive Promotions and Giveaways**: Take part in giveaways and holiday promotions.
👉 Ready to explore and create with us? Click [|link_sf_facebook|] and join today!
.. _2.1.9_py_pi5:
2.1.9 Joystick
==============
.. note::
.. image:: ../img/mcp3008_and_adc0834.jpg
:width: 25%
:align: left
Depending on your kit version, please identify whether you have **ADC0834** or **MCP3008** and proceed with the matching section.
Introduction
------------
In this project, We're going to learn how joystick works. We manipulate
the Joystick and display the results on the screen.
Required Components
------------------------------
In this project, we need the following components.
.. image:: ../python_pi5/img/2.1.9_joystick_list.png
It's definitely convenient to buy a whole kit, here's the link:
.. list-table::
:widths: 20 20 20
:header-rows: 1
* - Name
- ITEMS IN THIS KIT
- LINK
* - Raphael Kit
- 337
- |link_Raphael_kit|
You can also buy them separately from the links below.
.. list-table::
:widths: 30 20
:header-rows: 1
* - COMPONENT INTRODUCTION
- PURCHASE LINK
* - :ref:`cpn_gpio_board`
- |link_gpio_board_buy|
* - :ref:`cpn_breadboard`
- |link_breadboard_buy|
* - :ref:`cpn_wires`
- |link_wires_buy|
* - :ref:`cpn_resistor`
- |link_resistor_buy|
* - :ref:`cpn_joystick`
- \-
* - :ref:`cpn_adc0834`
- \-
Schematic Diagram
-----------------
When the data of joystick is read, there are some differents between
axis: data of X and Y axis is analog, which need to use ADC0834 to
convert the analog value to digital value. Data of Z axis is digital, so
you can directly use the GPIO to read, or you can also use ADC to read.
.. image:: ../python_pi5/img/2.1.9_joystick_schematic_1.png
.. image:: ../python_pi5/img/2.1.9_joystick_schematic_2.png
Experimental Procedures
-----------------------
**Step 1:** Build the circuit.
.. image:: ../python_pi5/img/2.1.9_Joystick_circuit.png
**Step 2:** Go to the folder of the code.
.. raw:: html
.. code-block::
cd ~/raphael-kit/python-pi5
**Step 3:** Run.
.. raw:: html
.. code-block::
sudo python3 2.1.9_Joystick_zero.py
After the code runs, turn the Joystick, then the corresponding values of
x, y, Btn are displayed on screen.
.. warning::
If there is an error prompt ``RuntimeError: Cannot determine SOC peripheral base address``, please refer to :ref:`faq_soc`
**Code**
.. note::
You can **Modify/Reset/Copy/Run/Stop** the code below. But before that, you need to go to source code path like ``raphael-kit/python-pi5``. After modifying the code, you can run it directly to see the effect.
.. raw:: html
.. code-block:: python
#!/usr/bin/env python3
from gpiozero import Button
import ADC0834
import time
# Initialize the button connected to GPIO pin 22
BtnPin = Button(22)
# Setup the ADC0834 ADC
ADC0834.setup()
try:
# Main loop to read and print ADC values and button state
while True:
# Read X and Y values from ADC channels 0 and 1
x_val = ADC0834.getResult(0)
y_val = ADC0834.getResult(1)
# Read the state of the button (pressed or not)
Btn_val = BtnPin.value
# Print the X, Y, and button values
print('X: %d Y: %d Btn: %d' % (x_val, y_val, Btn_val))
# Delay of 0.2 seconds before the next read
time.sleep(0.2)
# Gracefully handle script termination (e.g., via KeyboardInterrupt)
except KeyboardInterrupt:
pass
**Code Explanation**
#. This section imports the Button class from the ``gpiozero`` library to manage a button connected to a GPIO pin. It also imports the ``ADC0834`` library for interfacing with the ADC0834 ADC (Analog-to-Digital Converter) module.
.. code-block:: python
#!/usr/bin/env python3
from gpiozero import Button
import ADC0834
import time
#. Initializes a button connected to GPIO pin 22 and sets up the ADC0834 module for usage.
.. code-block:: python
# Initialize the button connected to GPIO pin 22
BtnPin = Button(22)
# Setup the ADC0834 ADC
ADC0834.setup()
#. The VRX and VRY connections of the joystick are linked to CH0 and CH1 of the ADC0834, respectively. This setup facilitates reading the values from CH0 and CH1, which are then saved in the ``x_val`` and ``y_val`` variables. In addition, the SW value of the joystick is read and assigned to the ``Btn_val`` variable. The retrieved values of ``x_val``, ``y_val``, and ``Btn_val`` are subsequently displayed using the ``print()`` function.
.. code-block:: python
try:
# Main loop to read and print ADC values and button state
while True:
# Read X and Y values from ADC channels 0 and 1
x_val = ADC0834.getResult(0)
y_val = ADC0834.getResult(1)
# Read the state of the button (pressed or not)
Btn_val = BtnPin.value
# Print the X, Y, and button values
print('X: %d Y: %d Btn: %d' % (x_val, y_val, Btn_val))
# Delay of 0.2 seconds before the next read
time.sleep(0.2)
# Gracefully handle script termination (e.g., via KeyboardInterrupt)
except KeyboardInterrupt:
pass