Base Input

๐ŸŽฎ Joystick Shield V1.A

๐Ÿ“Œ Description

The joystick shield is like a game controller for the robot. It has a small stick that can move in different directions (X and Y) and several buttons that can be pressed.

๐ŸŽฏ Use

In our projects, the joystick is used to:

  • Control movement (forward, backward, left, right).
  • Send signals to start or stop actions.
  • Combine with buttons to give more commands.

๐Ÿ‘‰ Think of the joystick as the robotโ€™s remote control โ€” you move the stick or press buttons, and the robot reacts.

ย Module parameters

Pin name Description
G GND (Power Input Negative)
V VCC (Power Input Cathode)
X Analog signal pins
Y Analog signal pins

๐Ÿ–ฅ๏ธ Code Explanation โ€“ Joystick Only

import machine
import time

adc2 = machine.ADC(machine.Pin(2))   # Joystick X-axis
adc2.atten(machine.ADC.ATTN_11DB)
adc2.width(machine.ADC.WIDTH_12BIT)

adc4 = machine.ADC(machine.Pin(4))   # Joystick Y-axis
adc4.atten(machine.ADC.ATTN_11DB)
adc4.width(machine.ADC.WIDTH_12BIT)

while True:
    print('x ', adc2.read(), 'Y ', adc4.read())   # Show joystick position
    time.sleep_ms(100)

Step by Step

  1. adc2 and adc4 โ†’ These read the joystickโ€™s X and Y positions.
  2. atten and width โ†’ Settings to make the readings more accurate.
  3. while True: โ†’ Loop that repeats forever.
  4. print('x ', adc2.read(), 'Y ', adc4.read()) โ†’ Shows the joystickโ€™s position on the screen.
    ๐Ÿ‘‰ Moving the joystick changes the numbers for X and Y.

๐Ÿ–ฅ๏ธ Code Explanation โ€“ Joystick + Buttons

import machine

# Buttons
pin26 = machine.Pin(26, machine.Pin.IN, machine.Pin.PULL_DOWN)  # A
pin25 = machine.Pin(25, machine.Pin.IN, machine.Pin.PULL_DOWN)  # B
pin17 = machine.Pin(17, machine.Pin.IN, machine.Pin.PULL_DOWN)  # C
pin16 = machine.Pin(16, machine.Pin.IN, machine.Pin.PULL_DOWN)  # D
pin27 = machine.Pin(27, machine.Pin.IN, machine.Pin.PULL_DOWN)  # E
pin14 = machine.Pin(14, machine.Pin.IN, machine.Pin.PULL_DOWN)  # F

# Joystick
adc2 = machine.ADC(machine.Pin(2))   # X-axis
adc2.atten(machine.ADC.ATTN_11DB)
adc2.width(machine.ADC.WIDTH_12BIT)

adc4 = machine.ADC(machine.Pin(4))   # Y-axis
adc4.atten(machine.ADC.ATTN_11DB)
adc4.width(machine.ADC.WIDTH_12BIT)

while True:
    if pin26.value() == 0:
        print('A')
    elif pin25.value() == 0:
        print('B')
    elif pin17.value() == 0:
        print('C')
    elif pin16.value() == 0:
        print('D')
    elif pin27.value() == 0:
        print('E')
    elif pin14.value() == 0:
        print('F')
    else:
        print('x ', adc2.read(), 'Y ', adc4.read())

Step by Step

  1. Pins 26โ€“14 โ†’ Each button is connected to a pin.
  2. Joystick X and Y โ†’ Read the stickโ€™s position.
  3. while True: โ†’ Loop that repeats forever.
  4. If button pressed โ†’ Prints the buttonโ€™s name (Aโ€“F).
  5. Else โ†’ Prints joystick position.
    ๐Ÿ‘‰ This way, the program shows either the button pressed or the joystickโ€™s movement.

โœ… Conclusion of the Test

  • If moving the joystick changes the X and Y numbers, the joystick works.
  • If pressing a button prints its letter (Aโ€“F), the buttons work.
  • This test shows how the shield can be used as a controller: joystick for direction, buttons for actions.
  • Later, instead of just printing, the robot can use these inputs to move motors, turn lights on, or trigger sounds.
On this page