Control servo MG 996R via joystick

In today's blog post, we control a AZ-Delivery Servo MG996Rvia a Joystick module.


To do this, we need:

1x AZ-Delivery Servo MG996R

1x Joystick module

1x AZ-Delivery microcontroller, compatible with Arduino UNO R3

1x Set of 3 40 pcs. Jumper Wire m2m/f2m/f2f

The servo pulls up to 2.5A in operation - far too much for our microcontroller, compatible with Arduino Uno R3. Therefore, we need to supply it with an external power supply.


We build up the circuit below. It should be noted that we connect GND from the Arduino to the GND of the external power supply.

There are 3 cables on the servo. The darkest (in our case brown) is GND. Red is like almost always VCC (4.8 - 7.2 V current consumption: up to 2.5A), and orange is controlled by the microcontroller, compatible with Arduino Uno R3 and connected to pin 9.

The joystick has 5 connection pins which are connected to the microcontroller, compatible with Arduino Uno R3, as follows:



Microcontroller, compatible with Arduino Uno R3
















For programming we take the library with the simple name "Servo".

We also only use the X axis to rotate the servo in the desired direction.


#include <Servo.H>;

Servo servo1; Give the servo a name
#define joyX A0 Pin A0 - Joystick X
#define joyY A1 Pin A1 - Joystick Y
#define joyBTN D7  Pin D7 - Joystick Button

Int Item = 0, You = 0, xValue, yValue; Variables for position (pos), direction (dir), and the X and Y value of the joystick 

Void Setup() {   Serial.Begin(9600);   servo1.Attach (9); Control line from the servo is connected to pin 9

Void Loop() {   xValue = analogRead(joyX); Query the X position of the joystick   yValue = analogRead(joyY); Query the Y-position of the joystick   Output to Serial Monitor - only for troubleshooting   Serial.Print(Item);    Serial.Print("Atin");   Serial.Print(xValue);   Serial.Print("Atin");   If (xValue < 490){ If Joy after links <-      Item = --Item; pos gets energized   }       If (xValue > 500){ If Joy is right     Item = ++Item;  pos we increased   }   If (Item < 0){ So that pos does not become negative     Item = 0;   }   If (Item > 180){ So pos doesn't go over 180     Item = 180;   }   Delay(20);   servo1.Write(Item); Move servo to position "pos"

After uploading the code, we can now rotate the servo in the desired direction using the joystick.



We hope that our blog post today has inspired you and we look forward to your comments. Until the next post from AZ-Delivery, your expert in microelectronics!

For arduinoProjects for beginners

Leave a comment

All comments are moderated before being published

Recommended blog posts

  1. Install ESP32 now from the board manager
  2. Lüftersteuerung Raspberry Pi
  3. Arduino IDE - Programmieren für Einsteiger - Teil 1
  4. ESP32 - das Multitalent
  5. OTA - Over the Air - ESP programming via WLAN