Control a servo connected to a Raspberry Pi using an Xbox controller

https://github.com/dcs-ink/raspberry_servo_controller

raspberry_servo_controller

Control a servo connected to a Raspberry Pi using an Xbox controller. Easy setup for testing.

Parts list:

raspberry pi 4
xbox controller
power b…


This content originally appeared on DEV Community and was authored by Dan

https://github.com/dcs-ink/raspberry_servo_controller

raspberry_servo_controller

output4

Control a servo connected to a Raspberry Pi using an Xbox controller. Easy setup for testing.

Parts list:

  • raspberry pi 4
  • xbox controller
  • power bank
  • breadboard
  • 100 µF capacitor
  • servo
  • jumper wires

Diagram

wire_diagram

Instructions

  1. Prep Raspberry Pi (I used Raspberry OS Lite but I imagine most OS's should work)
  2. Install dependancies
   sudo apt-get install -y evtest evdev
  1. Pair controller
   bluetoothctl
   scan on
   pair XX:XX:XX:XX:XX:XX
   trust XX:XX:XX:XX:XX:XX
   connect XX:XX:XX:XX:XX:XX

Make note of the ID; eventX

  1. Look at controller output
   sudo evtest /dev/input/eventX
  1. Create python script
   vim ServoController.py

copy paste (be sure to change 'eventX' to your controller id

   import evdev
   import RPi.GPIO as GPIO
   import time

   GPIO.setmode(GPIO.BOARD)
   GPIO.setup(11,GPIO.OUT)
   servo = GPIO.PWM(11,50)
   servo.start(0)

   controllerInput = evdev.InputDevice("/dev/input/event0")

    for event in controllerInput.read_loop():
      if event.type == evdev.ecodes.EV_ABS:
        if event.code == evdev.ecodes.ABS_X:
          angle = 2 + (event.value / 6553.5)
          servo.ChangeDutyCycle(angle)
          print(angle)
  1. Run script
   python3 ServoController.py

Thanks to Berardinux for the code and guide! XboxController_Servo_RaspberryPi
https://youtu.be/3Z6Bf0uV7tc?si=rzX2nBYM4o1anVEc


This content originally appeared on DEV Community and was authored by Dan


Print Share Comment Cite Upload Translate Updates
APA

Dan | Sciencx (2024-07-22T03:52:28+00:00) Control a servo connected to a Raspberry Pi using an Xbox controller. Retrieved from https://www.scien.cx/2024/07/22/control-a-servo-connected-to-a-raspberry-pi-using-an-xbox-controller/

MLA
" » Control a servo connected to a Raspberry Pi using an Xbox controller." Dan | Sciencx - Monday July 22, 2024, https://www.scien.cx/2024/07/22/control-a-servo-connected-to-a-raspberry-pi-using-an-xbox-controller/
HARVARD
Dan | Sciencx Monday July 22, 2024 » Control a servo connected to a Raspberry Pi using an Xbox controller., viewed ,<https://www.scien.cx/2024/07/22/control-a-servo-connected-to-a-raspberry-pi-using-an-xbox-controller/>
VANCOUVER
Dan | Sciencx - » Control a servo connected to a Raspberry Pi using an Xbox controller. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/07/22/control-a-servo-connected-to-a-raspberry-pi-using-an-xbox-controller/
CHICAGO
" » Control a servo connected to a Raspberry Pi using an Xbox controller." Dan | Sciencx - Accessed . https://www.scien.cx/2024/07/22/control-a-servo-connected-to-a-raspberry-pi-using-an-xbox-controller/
IEEE
" » Control a servo connected to a Raspberry Pi using an Xbox controller." Dan | Sciencx [Online]. Available: https://www.scien.cx/2024/07/22/control-a-servo-connected-to-a-raspberry-pi-using-an-xbox-controller/. [Accessed: ]
rf:citation
» Control a servo connected to a Raspberry Pi using an Xbox controller | Dan | Sciencx | https://www.scien.cx/2024/07/22/control-a-servo-connected-to-a-raspberry-pi-using-an-xbox-controller/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.