Introduction

In this article, I am explaining about controlling the ServoMotor using Potentiometer in Arduino. In this article, I will control the Servo Motor in the adjustment of Potentiometer.
Parts of the List
  • Arduino Uno
  • Potentiometer
  • Servo Motor
  • Bread Board
  • Hook up wires
Connection
Servo Motor
  • Connect the Vcc of the Servo Motor to the 5v of the Arduino Uno.
  • Connect the gnd of the Bluetooth to the Gnd of the Arduino Uno.
  • Connect the Vin of the Bluetooth to the 09 of the Arduino Uno.
Potentiometer
  • Connect as per the preceding figure to the Arduino board and the LCD Display.
  • The Vin is connected to the LCD display in 03.
Programming
  1. #include <Servo.h>
  2. const int servo1 = 3; // first servo
  3. const int servo2 = 10; // second servo
  4. const int servo3 = 5; // third servo
  5. const int servo4 = 11; // fourth servo
  6. const int servo5 = 9; // fifth servo
  7. const int joyH = 2; // L/R Parallax Thumbstick
  8. const int joyV = 3; // U/D Parallax Thumbstick
  9. const int joyX = 4; // L/R Parallax Thumbstick
  10. const int joyP = 5; // U/D Parallax Thumbstick
  11. const int potpin = 0; // O/C potentiometer
  12. int servoVal; // variable to read the value from the analog pin
  13. Servo myservo1; // create servo object to control a servo
  14. Servo myservo2; // create servo object to control a servo
  15. Servo myservo3; // create servo object to control a servo
  16. Servo myservo4; // create servo object to control a servo
  17. Servo myservo5; // create servo object to control a servo
  18. void setup() {
  19. // Servo
  20. myservo1.attach(servo1); // attaches the servo
  21. myservo2.attach(servo2); // attaches the servo
  22. myservo3.attach(servo3); // attaches the servo
  23. myservo4.attach(servo4); // attaches the servo
  24. myservo5.attach(servo5); // attaches the servo
  25. // Inizialize Serial
  26. Serial.begin(9600);
  27. }
  28. void loop(){
  29. servoVal = analogRead(potpin);
  30. servoVal = map(servoVal, 0, 1023, 0, 179);
  31. myservo5.write(servoVal);
  32. delay(15);
  33. // Display Joystick values using the serial monitor
  34. outputJoystick();
  35. // Read the horizontal joystick value (value between 0 and 1023)
  36. servoVal = analogRead(joyH);
  37. servoVal = map(servoVal, 0, 1023, 0, 180); // scale it to use it with the servo (result between 0 and 180)
  38. myservo2.write(servoVal); // sets the servo position according to the scaled value
  39. // Read the horizontal joystick value (value between 0 and 1023)
  40. servoVal = analogRead(joyV);
  41. servoVal = map(servoVal, 0, 1023, 70, 180); // scale it to use it with the servo (result between 70 and 180)
  42. myservo1.write(servoVal); // sets the servo position according to the scaled value
  43. delay(15); // waits for the servo to get there
  44. // Read the horizontal joystick value (value between 0 and 1023)
  45. servoVal = analogRead(joyP);
  46. servoVal = map(servoVal, 0, 1023, 70, 180); // scale it to use it with the servo (result between 70 and 180)
  47. myservo4.write(servoVal); // sets the servo position according to the scaled value
  48. delay(15); // waits for the servo to get there
  49. // Read the horizontal joystick value (value between 0 and 1023)
  50. servoVal = analogRead(joyX);
  51. servoVal = map(servoVal, 0, 1023, 70, 180); // scale it to use it with the servo (result between 70 and 180)
  52. myservo3.write(servoVal); // sets the servo position according to the scaled value
  53. delay(15); // waits for the servo to get there
  54. }
  55. /**
  56. * Display joystick values
  57. */
  58. void outputJoystick(){
  59. Serial.print(analogRead(joyH));
  60. Serial.print ("---");
  61. Serial.print(analogRead(joyV));
  62. Serial.println ("----------------");
  63. Serial.print(analogRead(joyP));
  64. Serial.println ("----------------");
  65. Serial.print(analogRead(joyX));
  66. Serial.println ("----------------");
  67. }

Conclusion

In this article, we learned about controlling Servo Motor using Potentiometer. It is used to adjust and control the motor.