Control servo motors with a joystick (Thumbstick) | Arduino


Today, I will show you the control of two servo motors with a joystick. Actually this is a simple project, but many followers wanted me to share this project. So how does it work? When the joystick module moves horizontally or vertically, it gives values from 0 to 1023. It's like a potentiometer. We will scale the servo motor positions to the joystick values using the map function. Thus, the servo motors will move according to the scaled value. Required Hardware: Arduino Board - https://goo.gl/RcRmi1 Servo MG90S - https://goo.gl/aRPE6D Joystick Module - https://goo.gl/qupktG Breadboard - https://goo.gl/ExxNWy Jumper Wires - https://goo.gl/EGTafY Optional: Arduino Starter Kit - https://goo.gl/aRPE6D Arduino Compatible SCM - https://goo.gl/yi21yB Electronics - https://goo.gl/covX9J ------------------------------------------------------------------------ Studio Equipment: Studio Lighting Softbox - https://goo.gl/PM3xTM Adjustable SoftBox Tripod - https://goo.gl/j1N2UP Zhiyun Smooth Q Gimbal - https://goo.gl/wcNqB8 Xiaomi Mi6 - https://goo.gl/EkiV7i Magnetic Blue Mat - https://goo.gl/5j4Q7y ------------------------------------------------------------------------ Social Media: Subscribe to Channel - http://bit.ly/MertArduino Facebook - http://bit.ly/MertArduinoFB Instructables - http://bit.ly/Mertduino

------------------------------------------------------------------------

Source Code:
#include <Servo.h>

//Servo objects created to control the servos
Servo myServo1;
Servo myServo2;

int servo1 = 3; //Digital PWM pin used by the servo 1
int servo2 = 5; //Digital PWM pin used by the servo 2
int joyX = 0;   //Analog pin to which the joystick (X) is connected
int joyY = 1;   //Analog pin to which the joystick (Y) is connected

void setup(){
  myServo1.attach(servo1);
  myServo2.attach(servo2);
}

void loop(){
  
  int valX = analogRead(joyX); //Read the joystick X value (value between 0 and 1023)
  int valY = analogRead(joyY); //Read the joystick Y value (value between 0 and 1023)

  valX = map(valX, 0, 1023, 10, 170); //Scale the joystick X value to use it with the servo
  valY = map(valY, 0, 1023, 10, 170); //Scale the joystick X value to use it with the servo

  //Sets the servo position according to the scaled values.
  myServo1.write(valX);   
  myServo2.write(valY);

  delay(5);
}