Devices & Components
Arduino Uno Rev3
Servo Motor (SG90)
Analog Joystick Module
2S LiPo Battery (7.4V) or External Power Supply
Servo Motor MG995
Breadboard-
Jumper-Wires
Software & Tools
Webotricks
Project description
Code
Arduino Code for Controlling the Servo Motor
1#include <Servo.h> 2 3Servo myServo; // Create a servo object 4int xPin = A0; // Joystick X-axis 5int yPin = A1; // Joystick Y-axis (Optional for dual-axis control) 6int servoPin = 9; 7 8void setup() { 9 myServo.attach(servoPin); 10 pinMode(xPin, INPUT); 11 pinMode(yPin, INPUT); 12} 13 14void loop() { 15 int xValue = analogRead(xPin); // Read joystick X-axis value 16 int angle = map(xValue, 0, 1023, 0, 180); // Convert to servo angle 17 myServo.write(angle); // Move servo to the mapped position 18 delay(15); // Small delay for smooth movement 19}
Comments
Only logged in users can leave comments