๐น Components Needed
โ
Arduino (Uno/Nano/etc.)
โ
SG90 Servo Motor
โ
Joystick Module (XY type, like KY-023)
โ
Jumper Wires & Breadboard
๐น Understanding the Joystick
The joystick has two potentiometers (X & Y) and a push button:
- VRX (X-axis): Analog signal (A0)
- VRY (Y-axis): Analog signal (A1)
- SW (Button): Digital signal (D2, optional)
- VCC & GND: Power
For this setup, weโll control the servo using the X-axis (left/right movement).
๐น Circuit Connections
Joystick Pin | Arduino Pin | Servo Pin |
---|---|---|
VCC | 5V | 5V |
GND | GND | GND |
VRX (X-axis) | A0 | โ |
VRY (Y-axis) | A1 (optional) | โ |
SW (Button) | D2 (optional) | โ |
โ | โ | Pin 9 (Servo Signal) |
๐น Arduino Code (Joystick-Controlled Servo)
#include <Servo.h>
Servo myServo;
int joyX = A0; // Joystick X-axis connected to A0
int xValue; // Variable to store joystick reading
void setup() {
myServo.attach(9); // Servo on Pin 9
}
void loop() {
xValue = analogRead(joyX); // Read joystick X-axis (0-1023)
int angle = map(xValue, 0, 1023, 0, 180); // Convert to 0-180ยฐ
myServo.write(angle); // Move servo to the corresponding angle
delay(15); // Small delay for smooth movement
}
๐น How to Test It?
1๏ธโฃ Wire up the circuit as shown.
2๏ธโฃ Upload the code to your Arduino.
3๏ธโฃ Move the joystick left & right โ The servo should move accordingly! ๐ฎ๐ค
๐น Whatโs Next?
๐ฅ Add Y-axis โ Control two servos (X for one, Y for another).
๐ฅ Use the button โ Make it reset the servo to center (90ยฐ).
๐ฅ Upgrade to a robotic arm โ Control servo-based grippers or a full arm!