Control Servo with Joystick & Raspberry Pi Pico
How to Control Servo Motor with PS2 Joystick Module & Raspberry Pi Pico using MicroPython code to use like a Mechanical Arm
- Suitable forB.Tech/Diploma RPi Pico students
- Build time1-2 weeks
- LevelBeginner
- Available atAjvika Labs, Kakinada
About this project
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.
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
(
)
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
<
:
value
=
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
(
)
def
free
(
self
)
:
""
" Allows the servo to be moved freely.
"
""
self
__pwm
duty_u16
(
)
main.py
Copy the following code and save it as
main.py
in the Raspberry Pi Pico.
from
machine
import
Pin
ADC
from
time
import
sleep
from
servo
import
Servo
s1
=
Servo
(
)
VRX
=
ADC
(
)
# ADC0 multiplexing pin is GP26
VRY
=
ADC
(
)
# ADC1 multiplexing pin is GP27
SW
=
Pin
(
Pin
IN
Pin
PULL_UP
)
# ADC2 multiplexing pin is GP28
def
Map
(
in_min
in_max
out_min
out_max
)
:
return
(
in_min
)
*
(
out_max
out_min
)
/
(
in_max
in_min
)
+
out_min
def
servo_Angle
(
angle
)
:
s1
goto
(
round
(
Map
(
angle
1024
)
)
)
def
direction
(
)
:
global
i
i
=
adc_X
=
round
(
Map
(
VRX
read_u16
(
)
65535
)
)
adc_Y
=
round
(
Map
(
VRY
read_u16
(
)
65535
)
)
Switch
=
SW
value
(
)
if
adc_X
<=
:
i
=
# Define up direction
elif
adc_X
>=
:
i
=
# Define down direction
elif
adc_Y
>=
:
i
=
# Define left direction
elif
adc_Y
<=
:
i
=
# Define right direction
elif
Switch
==
:
#and adc_Y ==128:
i
=
# Define Button pressed
elif
adc_X
<
and
adc_X
>
and
adc_Y
<
and
adc_Y
>
and
Switch
==
:
i
=
# Define home location
def
loop
(
)
:
num
=
while
True
:
direction
(
)
# Call the direction judgment function
servo_Angle
(
num
)
sleep
(
0.01
)
if
i
==
:
num
=
if
i
==
:
num
=
if
i
==
:
num
=
num
if
num
<
:
num
=
if
i
==
:
num
=
num
+
if
num
>
:
num
=
if
i
==
:
num
=
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.
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.
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
(
)
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
<
:
value
=
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
(
)
def
free
(
self
)
:
""
" Allows the servo to be moved freely.
"
""
self
__pwm
duty_u16
(
)
main.py
Copy the following code and save it as
main.py
in the Raspberry Pi Pico.
from
machine
import
Pin
ADC
from
time
import
sleep
from
servo
import
Servo
s1
=
Servo
(
)
VRX
=
ADC
(
)
# ADC0 multiplexing pin is GP26
VRY
=
ADC
(
)
# ADC1 multiplexing pin is GP27
SW
=
Pin
(
Pin
IN
Pin
PULL_UP
)
# ADC2 multiplexing pin is GP28
def
Map
(
in_min
in_max
out_min
out_max
)
:
return
(
in_min
)
*
(
out_max
out_min
)
/
(
in_max
in_min
)
+
out_min
def
servo_Angle
(
angle
)
:
s1
goto
(
round
(
Map
(
angle
1024
)
)
)
def
direction
(
)
:
global
i
i
=
adc_X
=
round
(
Map
(
VRX
read_u16
(
)
65535
)
)
adc_Y
=
round
(
Map
(
VRY
read_u16
(
)
65535
)
)
Switch
=
SW
value
(
)
if
adc_X
<=
:
i
=
# Define up direction
elif
adc_X
>=
:
i
=
# Define down direction
elif
adc_Y
>=
:
i
=
# Define left direction
elif
adc_Y
<=
:
i
=
# Define right direction
elif
Switch
==
:
#and adc_Y ==128:
i
=
# Define Button pressed
elif
adc_X
<
and
adc_X
>
and
adc_Y
<
and
adc_Y
>
and
Switch
==
:
i
=
# Define home location
def
loop
(
)
:
num
=
while
True
:
direction
(
)
# Call the direction judgment function
servo_Angle
(
num
)
sleep
(
0.01
)
if
i
==
:
num
=
if
i
==
:
num
=
if
i
==
:
num
=
num
if
num
<
:
num
=
if
i
==
:
num
=
num
+
if
num
>
:
num
=
if
i
==
:
num
=
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.
Technologies used
Hardware components
- Raspberry Pi
- Servo
- Servo Motor
Software & tools
- MicroPython
What you get
- Working project / hardware kit, tested before delivery
- Complete source code with explanation
- Project demo and guidance for your viva
- Project report, PPT and IEEE paper support (on request)
Delivery & Payment
Delivery takes a minimum of two weeks from the date your inquiry is confirmed.
Payment is in two parts:
• 50% advance to start the project
• Remaining 50% before delivery
Note:
• Documentation and PPT are not included by default
• If you need documentation/PPT, it can be arranged for extra time and cost
We'll confirm the exact timeline and cost with you after reviewing your requirements.