Skip to content

rollyceballos/rpi_joystick

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

RaspberryPi Zero W as a Virtual joystick

This project was developed to turn a RaspberryPi Zero into a virtual joystick

Hardware requirements

  1. Raspberry Pi zero W x 1
  2. Micro SD card x 1
  3. Raspberry Pi OS Lite

Note: You can use the original Zero and configure the USB port to act as a network adaptor but that is out of scope for this project

Note2: You can use one of the other OS packages if you wish and skip the install of the GPIO Python package install

Software installation

  1. Image the SD card with the image of the OS. There are many instructions on how to do this but my go to program is balenaEtcher
  2. Download the GPIO Python package:
    sudo apt-get update
    sudo apt-get -y install rpi.gpio python3-gpiozero
    

Configure the Zero as a USB device

  1. Enable libcomposite and other necessary modules and drivers

    sudo echo "dtoverlay=dwc2" | sudo tee -a /boot/config.txt
    sudo echo "dwc2" | sudo tee -a /etc/modules
    sudo echo "libcomposite" | sudo tee -a /etc/modules
    
  2. Copy the USB device creation script to /usr/bin and make it executable

    sudo cp joystick.sh /usr/bin
    sudo chmod +x /usr/bin/joystick.sh
    
  3. The RaspberryPi uses dynamic device creation so the creation script needs to be run every time the Pi boots. To configure the Zero to run the USB joystick device creation on boot add the following line above 'exit 0' to the file /etc/rc.local

    /usr/bin/joystick.sh
    
  4. Reboot the Zero

    sudo reboot
    
  5. Connect the Zero to your computer, you should now see the virtual joystick in your devices

    • Control Panel -> Devices and Printers

    Joystick device

Testing the virtual joystick

  1. Right click the joystick and select Joystick device

    • Game controller settings
    • Properties button
    • Test tab

    Joystick device

  2. On the Zero run the command:

    sudo python3 joystick.py
    
  3. On the GPIO pins of the Zero connect the following pins to pin 6 (third from the top on the right) and this is the result you should see:

    1. Pin 3 (second left) - Button 1
    2. Pin 5 (third left) - Button 10
    3. Pin 7 (fourth left) - Button 18
    4. Pin 40 (last right) - Button 26
  4. To exit the script use Ctrl+C

  5. Copy and paste the top 30+ lines of the test script to your own project

  6. Configure the GPIO pins to trigger whichever joystick button you need

If you want to know all I have learned while creating this project so you can customise or change the configuration then read the 'About HID joysticks' file

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published