Overview
In this guide, we will learn the usage of Joystick with Raspberry Pi Pico & Servo Motor. By turning the PS2 joystick left (right), the servo is controlled to rotate clockwise (counterclockwise), simulating the rotation effect of the mechanical arm.
The joystick is similar to two potentiometers connected together, one for the vertical movement (Y-axis) and the other for the horizontal movement (X-axis). The potentiometers are variable resistances and, in a way, they act as sensors that provide us with varying voltage depending on their rotation. Generally, joysticks are used in the military, leisure, and aviation sectors.
Components Required
In this guide, I used Elecrow Raspberry Pi Pico Starter Kit to test different Modules. You can buy the kit and perform some other operations as well. From this kit, you can use the following components.
1. Raspberry Pi Pico Board – 1
2. Joystick Module – 1
3. Servo Motor – 1
4. Breadboard – 1
5. Jumper Wires – 4
6. Micro-USB Cable – 1
PS2 Joystick
The PS2 joystick module is also called a dual-axis button rocker. It is mainly composed of two potentiometers and a button switch. The two potentiometers output the corresponding voltage values on the X and Y axes respectively with the twist angle of the rocker. Pressing the joystick in the Z-axis direction can trigger the touch button. Under the action of the supporting mechanical structure, in the initial state of the rocker without external force twisting, the two potentiometers are in the middle of the range.
Joysticks are generally widely used in drones, video games, remote control cars, PTZs, and other devices in model aircraft. Many devices with screens also often use joysticks as input controls for menu selection.
Control Servo Motor with PS2 Joystick & Raspberry Pi Pico
Let us learn how we can interface the PS2 Joystick with Raspberry Pi Pico to control the rotation of the SG-90 Servo Motor. Connect the Joystick & Servo with Raspberry Pi Pico as shown in the circuit diagram below.
Connect the VCC, GND, VRx, VRy, and SW pin of PS2 Joystick to VSYS, GND, GP26, GP27, and GP28 of Raspberry Pi Pico respectively. Similarly, connect the VCC, GND & Signal Pin of the SG-90 Servo Motor to the 3.3V, GND & GP0 pin of the Raspberry Pi Pico.
MicroPython Code/Program
The code is divided into two parts. One is servo.py & the other is main.py. The servo.py is the library required by Servo Motor.
servo.py
Copy the following code and save it as servo.py in Raspberry Pi Pico.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
from machine import Pin, PWM class Servo: """ A simple class for controlling a 9g servo with the Raspberry Pi Pico. Attributes: minVal: An integer denoting the minimum duty value for the servo motor. maxVal: An integer denoting the maximum duty value for the servo motor. """ def __init__(self, pin: int or Pin or PWM, minVal=2500, maxVal=7500): """ Creates a new Servo Object. args: pin (int or machine.Pin or machine.PWM): Either an integer denoting the number of the GPIO pin or an already constructed Pin or PWM object that is connected to the servo. minVal (int): Optional, denotes the minimum duty value to be used for this servo. maxVal (int): Optional, denotes the maximum duty value to be used for this servo. """ if isinstance(pin, int): pin = Pin(pin, Pin.OUT) if isinstance(pin, Pin): self.__pwm = PWM(pin) if isinstance(pin, PWM): self.__pwm = pin self.__pwm.freq(50) self.minVal = minVal self.maxVal = maxVal def deinit(self): """ Deinitializes the underlying PWM object. """ self.__pwm.deinit() def goto(self, value: int): """ Moves the servo to the specified position. args: value (int): The position to move to, represented by a value from 0 to 1024 (inclusive). """ if value < 0: value = 0 if value > 1024: value = 1024 delta = self.maxVal-self.minVal target = int(self.minVal + ((value / 1024) * delta)) self.__pwm.duty_u16(target) def middle(self): """ Moves the servo to the middle. """ self.goto(512) def free(self): """ Allows the servo to be moved freely. """ self.__pwm.duty_u16(0) |
main.py
Copy the following code and save it as main.py in the Raspberry Pi Pico.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
from machine import Pin,ADC from time import sleep from servo import Servo s1 = Servo(0) VRX = ADC(0) # ADC0 multiplexing pin is GP26 VRY = ADC(1) # ADC1 multiplexing pin is GP27 SW = Pin(28, Pin.IN, Pin.PULL_UP) # ADC2 multiplexing pin is GP28 def Map(x, in_min, in_max, out_min, out_max): return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min def servo_Angle(angle): s1.goto(round(Map(angle,0,180,0,1024))) def direction(): global i i = 0 adc_X=round(Map(VRX.read_u16(),0,65535,0,255)) adc_Y=round(Map(VRY.read_u16(),0,65535,0,255)) Switch = SW.value() if adc_X <= 30: i = 1 # Define up direction elif adc_X >= 255: i = 2 # Define down direction elif adc_Y >= 255: i = 3 # Define left direction elif adc_Y <= 30: i = 4 # Define right direction elif Switch == 0:#and adc_Y ==128: i = 5 # Define Button pressed elif adc_X - 125 < 15 and adc_X - 125 > -15 and adc_Y -125 < 15 and adc_Y -125 > -15 and Switch == 1: i = 0 # Define home location def loop(): num = 90 while True: direction() # Call the direction judgment function servo_Angle(num) sleep(0.01) if i == 1: num = 0 if i == 2: num = 180 if i == 3: num = num - 1 if num < 0: num = 0 if i == 4: num = num + 1 if num > 180: num = 180 if i==5: num = 90 if __name__ == '__main__': loop() # call the loop function |
Now click on the Run button to run the library and main file.
By turning the PS2 joystick left & right, the servo is controlled to rotate clockwise & counterclockwise.










