Hackster is hosting Hackster Holidays, Ep. 7: Livestream & Giveaway Drawing. Watch previous episodes or stream live on Friday!Stream Hackster Holidays, Ep. 7 on Friday!
MohammadReza Sharifi
Published © MIT

Control Raspberry Pi Pico W Robot Using PS5 Controller

In this project, we are going to control a 4WD robot based on the Raspberry Pi Pico W microcontroller using the PlayStation 5 Controller.

IntermediateFull instructions provided4 hours260
Control Raspberry Pi Pico W Robot Using PS5 Controller

Things used in this project

Story

Read more

Schematics

circuit diagram

Code

main script on raspberry pi pico w

MicroPython
import socket
import network
from time import sleep
from machine import Pin as pin
 
ssid = 'Mrsh77'
password = '1m77n2299215r77#'
 
in1 = pin(16,pin.OUT)
in2 = pin(17,pin.OUT)
in3 = pin(18,pin.OUT)
in4 = pin(19,pin.OUT)

ENA = pin(20,pin.OUT)
ENB = pin(21,pin.OUT)

ENA.value(1)
ENB.value(1)


#This Function moves the Robot forward
def forward():
    in1.value(1)
    in2.value(0)
    in3.value(1)
    in4.value(0)

#This Function moves the Robot backward
def backward():
    in1.value(0)
    in2.value(1)
    in3.value(0)
    in4.value(1)

    
#This Function moves the Robot right
def right():
    in1.value(1)
    in2.value(0)
    in3.value(0)
    in4.value(0)

#This Function moves the Robot left
def left():
    in1.value(0)
    in2.value(0)
    in3.value(1)
    in4.value(0)

#This Function stops the Robot
def stopfcn():
    in1.value(0)
    in2.value(0)
    in3.value(0)
    in4.value(0)

 
def connect():
    #Connect to WLAN
    wlan = network.WLAN(network.STA_IF)
    wlan.active(True)
    wlan.connect(ssid, password)
    while wlan.isconnected() == False:
        print('Waiting for connection...')
        sleep(1)
    ip = wlan.ifconfig()[0]
    print(f'Connected on {ip}')
    return ip
 
def open_socket(ip):
    address = (ip,80)
    connection = socket.socket()
    connection.bind(address)
    connection.listen(1)
    #print(connection)
    return connection
 
 
try:
    ip = connect()
    connection = open_socket(ip)
    while True:
        # Accept a connection from a client
        client, addr = connection.accept()
        print(f'Connected to {addr}')
        while True:
            # Receive data from the client
            data = client.recv(1024)
            if data:
                # Print the data to the console
                #print(data)
                 
                if data == b'forward':
                    forward()
                     
                elif data == b'back':
                    backward()
                          
                elif data == b'right':
                    right()
                    
                elif data == b'left':
                    left()
                    
                elif data == b'stop':
                    stopfcn()
                    
except KeyboardInterrupt:
    # Close the server socket
    connection.close()

main python script

Python
from pydualsense import pydualsense
import socket
from time import sleep

# Set up the client socket
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client_socket.connect(('192.168.1.233', 80))  # Connect to the server

def button_pressed(cmd):
    #print(f"{cmd} button pressed")
    client_socket.sendall(cmd.encode('utf-8'))

def button_released(cmd):
    #print(f"{cmd} button released")
    client_socket.sendall(cmd.encode('utf-8'))

def main():
    # Initialize the DualSense controller
    ds = pydualsense()
    ds.init()

    # Set up event handlers for buttons
    ds.cross_pressed += lambda state: button_pressed("back") if state else button_released("stop")
    ds.circle_pressed += lambda state: button_pressed("right") if state else button_released("stop")
    ds.square_pressed += lambda state: button_pressed("left") if state else button_released("stop")
    ds.triangle_pressed += lambda state: button_pressed("forward") if state else button_released("stop")

    try:
        print("Press any button to see the output...")
        while True:
            pass  # Keep the script running to listen for events
    except KeyboardInterrupt:
        pass
    finally:
        ds.close()  # Close the controller connection

if __name__ == "__main__":
    main()

Source Code on my Github page

Credits

MohammadReza Sharifi

MohammadReza Sharifi

13 projects • 7 followers
I'm an Electrical Engineer and Maker.

Comments