How to control servo motor using potentiometer and joystick module

how to control servo motor using potentiometer and joystick module

Hello, welcome back. In this tutorial, we will learn how to control a servo motor using a potentiometer and a Joystick module. We have presented in several previous articles how to control a servo motor in different ways. You can also study them if you want. Also, we can use this tutorial knowledge to build robotic arms, model aircraft and more. If you don’t know about the Joystick module, click on this link.

OK, let’s do this project step by step. The required components are given below.

Disclosure: These Amazon links are Affiliate links. As an Amazon Associate, I earn from qualifying purchases.

Step 1

Firstly, identify these components.

Step 2

Secondly, connect these components. To do this, use the circuit diagram below.

how to control servo motor using potentiometer and joystick module

Step 3

Thirdly, let’s creates the program for this project. It is as follows.

  • The complete program of this project – download
/*Servo motor control.
  created by the SriTu Hobby team.
  Read the code below and use it for any of your creation
*/

#include <Servo.h>
Servo servo;
void setup() {
  servo.attach(3);
  Serial.begin(9600);
}
void loop() {
  int x = analogRead(A0);
  int p = analogRead(A1);

  x = map(x, 0, 1024, 0, 180);
  p = map(p, 0, 1024, 0, 180);

  Serial.println(x);


  if (x == 86 && p > 0) {
    servo.write(p);
    Serial.println(p);
  } else if (p == 0 && x > 0 ) {
    servo.write(x);
    Serial.println(x);
  }
}

Code explanation

Firstly, the servo motor library is included.

#include <Servo.h>
Servo servo;

In the setup function,

void setup() {
  servo.attach(3);//Includes servo motor pin to the library 
  Serial.begin(9600);
}

In the loop function,

void loop() {
//Gets the joystick module X axis values
  int x = analogRead(A0);
//Gets the potentiometer values
  int p = analogRead(A1);

//Converts these values from 0 to 180
  x = map(x, 0, 1024, 0, 180);
  p = map(p, 0, 1024, 0, 180);

  Serial.println(x);

//These values are checked using the IF condition and writes on the servo
  if (x == 86 && p > 0) {
    servo.write(p);
    Serial.println(p);
  } else if (p == 0 && x > 0 ) {
    servo.write(x);
    Serial.println(x);
  }

Step 4

OK, now select board and port. After, upload this code to the Arduino board.

OK, enjoy this project. The full video guide is given below. So, we will meet in the next tutorial.

How to control servo motor using potentiometer and joystick module

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *