Raspberry Pi Pico -- Dual Axis XY Joystick Interface

by PugazhM in Circuits > Raspberry Pi

318 Views, 0 Favorites, 0 Comments

Raspberry Pi Pico -- Dual Axis XY Joystick Interface

Circuit_V01.jpeg

This experimentation is about interfacing Dual Axis XY Analog Joystick and16X2 LCD with Raspberry Pi Pico, and using Python for programming the LCD.

Visit the "VESZLE - The Art Of Electronics" you tube channel for further information and videos.

https://www.youtube.com/channel/UCY4mekPLfeFinbQHp...

Dual Axis Matrix Keypad Video Link

Abstract

The Raspberry Pi Pico is a tiny, fast, and versatile board built using RP2040 features a dual-core Arm Cortex-M0+ processor with 264KB internal RAM and support for up to 16MB of off-chip Flash. It provides wide range of flexible I/O options includes I2C, SPI, and uniquely Programmable I/O (GPIO) pins.

In Embedded system design, character LCD display [16x2, 16x4, 20x2 or 20x4 LCDs] is used for display the system status, menu navigation and configuring system parameters.

A joystick is an input device consisting of a stick that pivots on a base and reports its angle or direction for controlling the connected device. X and Y positions of the joystick can be measured as an analog voltage. The joystick analog voltage is connected to, ADC input of a uC. Joysticks are mainly used for controlling, navigating the movements of robotics, aviation and industrial machines.

This experimentation is about interfacing Dual Axis XY Analog Joystick and16X2 LCD with Raspberry Pi Pico, and using Python for programming the LCD.

Reference

“Raspberry Pi Pico, Getting Started on Board Blink LED” Instruct-able / You-tube by PugazhM https://www.instructables.com/Raspberry-Pi-Pico-G...

“Raspberry Pi Pico -- 16x2 LCD, 4Bit Mode Interface, BCD Counter” Instruct-able / You-tube by PugazhM https://www.instructables.com/Raspberry-Pi-Pico-1...

https://www.instructables.com/Raspberry-Pi-Pico-1...

The “lcd_4bit_mode.py” driver library

Components

Component.jpg

Raspberry Pi Pico

Micro USB Cable

16x2 LCD Keypad Shield

Dual Axis XY Joystick Shield

Schematic -- 4Bit Mode - 16x2 LCD Connection

Circuit_V01.jpeg
LCD_Connection.jpg

The 16x2 is very common type LCD, with two rows.

The “16x2 LCD Keypad shield” can be connected to Raspberry Pi Pico GPIO pins (GPIO0--GPIO6). LCD operates at 4-bit data mode as given in above circuit.

If the display is not visible, adjust the Contrast pot (1K), to make it visible.

The above table describes the 16x2 LCD -- Shield – RPi Pico pin
connections.

Dual Axis XY Joystick Connection

Joystick_Connection.jpg

The dual axis analog Joystick module can be used for sensing the directional movements (XY axis direction), and inbuild press-able switch is used for generating the key pressed event.

Embedded system design, the XY directional movements with activation switch is mainly used for navigating system menu or changing / updating the variables values.

The X and Y axis movement uses two 10K potentiometer, which controls 2D movements, and then generates the analog signal voltage at VRX and VRY pins.

When the module is in working mode, it will output two analog values, representing two directions.

The dual axis analog Joystick module can be powered by either 5VDC or 3.3VDC. Since it is connected to RPi Pico, it should be powered by 2.2VDC. The RPi Pico ADCs pins are 3.3VDC tolerant.

The Above table describes the Joystick -- Shield – RPi Pico pin connections

Python Program -- Joystick Polling Interface

TaskEvent_V01.jpeg

As referenced above, download “lcd_4bit_mode.py” driver library.

Open the RPi Pico as drive, and then copy the library into root directory

LCD is initialized and welcome screen of “RPi Pico, 2 AXIS JOYSTICK'” is displayed in two rows, for about 5 seconds.

The timer_one is initialized and callbacks the “BlinkLED” functionality for toggling on board LED at 200mS duration. (frequency = 5)

The timer_two is initialized and callbacks the “PollJoystick” functionality for polling the joystick at 500mS duration. (frequency = 2)

The “PollJoystick” functions reads the ADC value for sensing the XY direction selection of the Joystick and reads the GPIO for detecting the button actions.

If the joy stick actions are detected then, in accordance with the direction sense, the X or Y counter will be updated and corresponding values are displayed at the LCD screen.

X LEFT -- Decrement the x_count

X RIGHT -- Increment the x_count

Y DOWN -- Decrement the y_count

Y UP -- Increment the y_count

BUTTON PRESSED – Reset x_count and y_count to zero

'''<br> Demonstrates the use of 1602 LCD and Dual axis analog Joystick shield with RPi Pico.
 
 4Bit mode LCD interface on Instructable and Youtube:
 https://www.instructables.com/Raspberry-Pi-Pico-16x2-LCD-4Bit-Mode-Interface-BCD/
 https://www.youtube.com/channel/UCY4mekPLfeFinbQHpf9QcHg
   
 * The Raspberry Pi Pico pin connections for 16x2 LCD, for 4Bit mode are given below:
 
 # LCD Power Pins
 * 16x2 LCD VCC pin to VBUS
 * 16x2 LCD GND pin to GND
 
 # LCD Data Pins
 * 16x2 LCD D4 pin to GPIO0
 * 16x2 LCD D5 pin to GPIO1
 * 16x2 LCD D6 pin to GPIO2
 * 16x2 LCD D7 pin to GPIO3
 
 # LCD Control Pins
 * 16x2 LCD RS pin to GPIO4
 * 16x2 LCD ENABLE pin to GPIO5
 * 16x2 LCD BACK LIGHT pin to GPIO6
 * The Raspberry Pi Pico pin connections for Dual axis analog Joystick shield are given below:
 
 # Joystick Power Pins
 * GND pin of Joystick to GND pin of RPi Pico
 * +5VDC pin of Joystick to 3V3(OUT) pin of RPi Pico
 * VRX pin of Joystick to GP26\ADC0 pin of RPi Pico
 * VRY pin of Joystick to GP27\ADC1 pin of RPi Pico
 * SW pin of Joystick to GP22 pin of RPi Pico
  
 Name:- M.Pugazhendi
 Date:-  14thJul2021
 Version:- V0.1
 e-mail:- muthuswamy.pugazhendi@gmail.com
'''
import machine
# Import lcd_4bit_mode
import lcd_4bit_mode
# Import time
import time
# Initialize LCD pins
RS = machine.Pin(4,machine.Pin.OUT)
ENABLE = machine.Pin(5,machine.Pin.OUT)
BACK_LIGHT = machine.Pin(6,machine.Pin.OUT)
D4 = machine.Pin(0,machine.Pin.OUT)
D5 = machine.Pin(1,machine.Pin.OUT)
D6 = machine.Pin(2,machine.Pin.OUT)
D7 = machine.Pin(3,machine.Pin.OUT)
# Initialize Joystick pins
X_AXIS = machine.ADC(26)
Y_AXIS = machine.ADC(27)
JOY_BUTTON = machine.Pin(22,machine.Pin.IN,machine.Pin.PULL_UP)
#Initialize the onboard LED as output
led = machine.Pin(25,machine.Pin.OUT)
# 1 = NONE, 2 = X_LEFT, 3 = X_RIGHT
x_status = 1
x_count = 0
# 1 = NONE, 2 = Y_UP, 3 = Y_DOWN
y_status = 1
y_count = 0
# 1 = BUTTON_OFF, 2 = BUTTON_ON
button_status = 1
# Toggle LED funtionality
def BlinkLED(timer_one):
    led.toggle()
# Poll JoyStick functionality
def PollJoystick(timer_two):
    global x_status
    global y_status
    global button_status
    
    #X_AXIS Reading
    adc_value = X_AXIS.read_u16()    
    if adc_value < 1000:
        x_status = 2      #X_LEFT
    elif adc_value > 50000:
        x_status = 3      #X_RIGHT
    else:
        x_status = 1      #NONE
    #Y_AXIS Reading
    adc_value = Y_AXIS.read_u16()    
    if adc_value < 1000:
        y_status = 2      #Y_UP
    elif adc_value > 50000:
        y_status = 3      #Y_DOWN
    else:
        y_status = 1      #NONE
        
    #Button Reading
    button_status = JOY_BUTTON.value()
    
    #If Joystick event is detected, then update the screen
    if x_status != 1 or y_status != 1 or button_status != 1:
        ProcessJoystickEventTask()
def ProcessJoystickEventTask():
    global x_status
    global y_status
    global button_status
    global x_count
    global y_count
    
    if x_status == 2:
        x_count = x_count - 1
        display.WriteLine('                ',1)
        display.WriteLine('X_L COUNT  ' + str(x_count),1)
    if x_status == 3:
        x_count = x_count + 1
        display.WriteLine('                ',1)
        display.WriteLine('X_R COUNT  ' + str(x_count),1)
    if y_status == 2:
        y_count = y_count - 1
        display.WriteLine('                ',2)
        display.WriteLine('Y_L COUNT  ' + str(y_count),2)
    if y_status == 3:
        y_count = y_count + 1
        display.WriteLine('                ',2)
        display.WriteLine('Y_R COUNT  ' + str(y_count),2)
    if button_status == 0:
        x_count = 0
        y_count = 0
        display.ClearScreenCursorHome()
        display.WriteLine('X  COUNT   ' + str(x_count),1)
        display.WriteLine('Y  COUNT   ' + str(y_count),2)
 
#Initialize timer_one. Used for toggling the on board LED
timer_one = machine.Timer()
#Initialize timer_two. Used for poling the Joystick
timer_two = machine.Timer()
display = lcd_4bit_mode.LCD16x2(RS,ENABLE,BACK_LIGHT,D4,D5,D6,D7)
# Turnon Back light
display.BackLightOn()
# Welcome string
display.WriteLine('    RPi PICO',1)
display.WriteLine('2 AXIS JOYSTICK',2)
# Wait for five seconds
time.sleep(5)
#Timer one initialization for on board blinking LED at 200mS interval
timer_one.init(freq=5, mode=machine.Timer.PERIODIC, callback=BlinkLED)
#Timer two initialization for on polling Joystick at 500 mSec interval
timer_two.init(freq=2, mode=machine.Timer.PERIODIC, callback=PollJoystick)
# Clear LCD
display.ClearScreenCursorHome()
display.WriteLine(' PRESS JOYSTICK',1)

Conclusion

Conclusion.jpg

The project is successfully completed with Raspberry Pi Pico and Dual Axis Joystick shield

The Joystick can be used for many embedded projects for controlling industrial machines, navigation system and robotics.

Video Links

Result_Video.mp4

Visit "VESZLE - The Art Of Electronics" you tube channel for further information and videos. https://www.youtube.com/channel/UCY4mekPLfeFinbQH...

If you enjoyed this instruct-able, then make sure you subscribe

Dual Axis Matrix Keypad Video Link