How to control servo motor with joystick and arduino



Materials:-
  • Arduino uno
  • Joystick
  • Servo motor
  • Jumper wires
Diagram:-

Connections:-

Joystick VCC =  Arduino 3.5V
Joystick GND = Arduino GND
Joystick  URY(HOR) = Arduino  Analog pin 0
Servo Yellow = Arduino Digital pin 7
Servo Red = Arduino 5V
Servo Brown = Arduino GND

Code:-

#include <Servo.h>
const int Y_pin = A0; // analog pin connected to Y output

Servo myservo;  

int val;    

void setup() {
  myservo.attach(7); 
}

void loop() {
  val = analogRead(Y_pin);            
  val = map(val, 0, 1023, 0, 180);     
  myservo.write(val);                  
  delay(15);                           
}