Webotricks
Published

How to control servo motor using analog joystick and Arduino

This project demonstrates how to control a servo motor using an analog joystick and an Arduino Uno.

BeginnerProtip2 hours56
How to control servo motor using analog joystick and Arduino

Things used in this project

Hardware components

Arduino Uno
×1
Jumper wires
×1
2S LiPo Battery (7.4V) or External Power Supply
×1
Analog Joystick Module
×1
BreadBoard
×1
Servo Motor (SG90)
×1
Servo Motor MG995
×1

Story

Read more

Code

Code

Arduino
#include <Servo.h>
 
Servo myServo;  // Create a servo object
int xPin = A0;  // Joystick X-axis
int yPin = A1;  // Joystick Y-axis (Optional for dual-axis control)
int servoPin = 9;
 
void setup() {
    myServo.attach(servoPin);
    pinMode(xPin, INPUT);
    pinMode(yPin, INPUT);
}
 
void loop() {
    int xValue = analogRead(xPin);  // Read joystick X-axis value
    int angle = map(xValue, 0, 1023, 0, 180);  // Convert to servo angle
    myServo.write(angle);  // Move servo to the mapped position
    delay(15);  // Small delay for smooth movement
}

Credits

Webotricks
24 projects • 9 followers
Contact

Comments

Please log in or sign up to comment.