This content originally appeared on DEV Community and was authored by Dan
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 bank
- breadboard
- 100 µF capacitor
- servo
- jumper wires
Diagram
Instructions
- Prep Raspberry Pi (I used Raspberry OS Lite but I imagine most OS's should work)
- Install dependancies
sudo apt-get install -y evtest evdev
- 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
- Look at controller output
sudo evtest /dev/input/eventX
- 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)
- 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
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/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.