Arduino Potentiometer

Arduino Project – Potentiometer Servo Control & Memory

Arduino Potentiometer Servo Control & Memory

In this project, we will be exploring the control of a servo motor through the use of a potentiometer. Not only will we be able to control the motor, but we will also utilize a “memorization” routine which will record the movement of the servo and play it back in a loop. This application is extremely useful in robotics, automation and other industries where repetition and servo motion is crucial.

Required materials and hardware

Our project will require a few basic components to get it going. Note that this is a conceptual project and can be easily applied to more servos, controllers, etc. I’ve pre-selected the following items:

Optional Hardware:

Connecting your Arduino Potentiometer Servo Controller

Arduino PotentiometerThe connection made in this project are straight forward. I’m connecting the servo to pin 11 (orange wire), the push button to pin 12 and the potentiometer middle terminal to the A0 pin. Each item is connected to the power and ground rails accordingly.

Programming the servo controller and memory routines

CODE: FULL CODE ON GITHUB
The code may not appear very clear to a beginner and will be broken down for better explanation. Follow along!
#include
Servo test;
int AnalogOut = 0;
int NewAnalogOut = 0;
int pin_Button = 12;
int pin_Button_State = 0;
int pin_Button_State_Last = 0;
int storage[800];
int storage_loc = 0;
int recording = 0;

Several variables as well as a library are declared. The library in question is the Servo.h Arduino Servo Control library which simplifies servo control and allows the user to easily send the servo to a desired position. The variables are declared for a specific functionality of the program. AnalogOut and NewAnalogOut keeps track of the potentiometer readings. pin_ variables are used for the pin location and state of the button. The storage[800] array as well as the storage_loc are declared in order to store the position of the servo motor. Finally, recording will be used to count the number of button presses we are getting.


void setup() {
Serial.begin(9600);
test.attach(11);
pinMode(pin_Button, INPUT);
}
// testing
void loop() {
if (recording == 0) {
int sensorValue = analogRead(A0);
NewAnalogOut = map(sensorValue, 0, 1023, 0, 180);
if (abs(NewAnalogOut - AnalogOut) > 2) {
test.write(AnalogOut);
AnalogOut = NewAnalogOut;
}
}
delay(1);

In this section of the code, we start by declaring our setup function. A serial command is issued for debugging purposes followed by a servo initialization on pin 11 as well as an input declaration for the button. Inside of the main loop function, we are going through a loop which will send the servo to a position based on the potentiometer reading. I’ve added “debouncing” functionality on the microcontroller in order to reduce servo gitter.


if ( recording == 1) {
recording = 1;
if (storage_loc < 800) { storage[storage_loc] = NewAnalogOut; delay(100); Serial.println(storage[storage_loc]); storage_loc++; } } else if (recording > 1) {
while (1 == 1) {
storage_loc = 0;
if (storage_loc < 800 || storage[storage_loc] != 777) { test.write(storage[storage_loc]); delay(100); } } }

If our button has been pressed, we start the recording process. This will go on until we have stored 800 items in our array or the button has been pressed; in which case we store 777 to keep track of the end of our recording. If the button is pressed a third time, the arduino servo control program goes into an infinite while loop. This loop will keep playing back the storage array.


pin_Button_State = digitalRead(pin_Button);
if (pin_Button_State != pin_Button_State_Last) {
if (pin_Button_State == HIGH) {
recording++;
if (recording == 2) {
storage[storage_loc] = 777;
}
} else {
}
delay(50);
}
pin_Button_State_Last = pin_Button_State;
}

The final segment of the code is used to keep track of the button presses for the Servo Motor Controller. This is a standard reapplication of what is done on the Arduino website (ref: Arduino Button Code)

Conclusion

At the end of this project, you should be able to fully understand the Servo Motor Controller. The goal was to create a system which can "play back" the motion recorded through an arduino potentiometer in set intervals.

Make sure to update me with any projects you've built!

Thank you for reading & watching!
- EEEnthusiast

Comments 5

  1. servo control with memory :
    I am able to control the servo with potentiometer not 180 degrees though. When I press the pushbutton to start recording mode the setup does not respond any further. What could be the problem?

  2. Post
    Author

    Are you able to send any output signal to the command prompt? I would really need to look at the logs to figure out what is going on. Could you please provide the full code you’ve used in your application?

    Thanks,
    – EEEnthusiast

  3. I have used the final piece of code from your video link in github as well as the one on the video. Both did not work.

  4. Post
    Author

Leave a Reply

Your email address will not be published.

CommentLuv badge