Arduino Project – Potentiometer Servo Control & Memory

///Arduino Project – Potentiometer Servo Control & Memory

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

By | 2016-09-23T22:44:13+00:00 January 22nd, 2016|Arduino, Tutorials|14 Comments

About the Author:

Welcome to EEEnthusiast.com My name is Vlad, I'm an Electrical Engineer and I love to build and teach. I graduated in 2013 and have tried my best to share my electrical knowledge and experience with viewers and readers from all over the world. You can find me on my personal site or LinkedIn!

14 Comments

  1. rangaswamy August 21, 2016 at 2:12 am

    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. vishwajeet khade October 8, 2016 at 7:48 am

    same problem i face plz give any solution on it

  3. EEEnthusiast October 9, 2016 at 1:52 pm

    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

  4. rangaswamy October 9, 2016 at 5:13 pm

    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.

  5. EEEnthusiast October 10, 2016 at 11:09 am

    Hi, what was the error or outcome of you running the code?

  6. John October 21, 2016 at 12:21 pm

    Hello EEENTHUSIAST,

    I have an issue when pressing the button to record the servo position. Please let me know how I can fix the problem, if possible. Please take a look at the code

    #include

    Servo Servo1;

    int AnalogOut = 0; // Read value from pot
    int NewAnalogOut = 0; // Read value from pot
    int pin_Button = 12; // button pin
    int pin_Button_State = 0 ;// can be boolean value
    int pin_Button_State_Last = 0;

    // have 800 int stored in it for the purpse of storing value at each time for the position of the servo
    int storage[700];
    int storage_loc = 0; // Storage location that use as the pointer for the array
    int recording = 0;

    void setup() {
    Serial.begin(9600);
    Servo1.attach(11);
    pinMode(pin_Button, INPUT);

    }
    void loop() {

    pin_Button_State = digitalRead(pin_Button);

    Serial.println(pin_Button_State);
    // Check for the state of the button
    if (pin_Button_State != pin_Button_State_Last) {

    if (pin_Button_State == LOW) {
    recording++;
    if (recording == 2) {

    // set 666 at the current storage location
    // for the purpose of seal the sweep of the servo (endpoit)
    // can be anything else as long as it is an int
    storage[storage_loc] = 666;
    }
    }
    else {
    }
    delay(50);
    }
    // keep track of the state of the button so no switch can occur
    pin_Button_State_Last = pin_Button_State;

    // no button press state
    if (recording == 0) {
    int sensorValue = analogRead(A0);
    NewAnalogOut = map(sensorValue, 0, 1023, 0, 180);

    // buffer zone to prevent jumping
    if (abs(NewAnalogOut – AnalogOut) > 2) {
    Servo1.write(AnalogOut);
    AnalogOut = NewAnalogOut;
    Serial.println(AnalogOut);
    }
    }
    delay(1);

    // button is pressed, recording begin
    if (recording == 1) {
    //recording = 1;
    if (storage_loc 1) {

    // infinite loop
    while (true) {
    storage_loc = 0;
    if (storage_loc < 1000 || storage[storage_loc] != 666) {
    Servo1.write(storage[storage_loc]);
    delay(100);
    }
    }
    }
    }

  7. Trobiase December 3, 2016 at 6:15 am

    I could control the servo normally but , when pressing the button to record, the servo doesn’t work, I tried to print out the potetiometer data from A0 to the serial monitor during the process only to find that the values remain zero when recording. So , maybe because nothing is stored , the servo does not move during the loop stage

  8. Heally January 8, 2017 at 7:41 am

    Hi…
    i want to use the method to control up to 6 servos …could you help to provide me the schematic and the code in full
    tq

  9. chris January 12, 2017 at 11:48 pm

    It works at first when moving the potentiometer but then I am having a problem when I switch the 12 to high to start memory, by pushing the tact switch, the tx light comes on and nothing happens until I hit reset.

  10. Saifullah Memon March 19, 2017 at 10:17 am

    I can control my servo through potentiometer but when i press the push button ,i can’t teach the servo..
    Please guide me..

  11. Tisyam March 20, 2017 at 5:31 pm

    i used final piece of code. But, when i verify show this error.
    “Sketch uses 3826 bytes (11%) of program storage space. Maximum is 32256 bytes.
    Global variables use 1843 bytes (89%) of dynamic memory, leaving 205 bytes for local variables. Maximum is 2048 bytes.
    Low memory available, stability problems may occur.”

    And code can’t upload to arduino.

  12. Tisyam March 20, 2017 at 5:34 pm

    i used the final piece of code. But, when i verify show error.
    “Sketch uses 3826 bytes (11%) of program storage space. Maximum is 32256 bytes.
    Global variables use 1843 bytes (89%) of dynamic memory, leaving 205 bytes for local variables. Maximum is 2048 bytes.
    Low memory available, stability problems may occur.”

    And code can’t upload to arduino

  13. Rahul March 23, 2017 at 1:34 am

    Can i get the code for 4 Servo motors attached.

  14. Rahul March 23, 2017 at 4:07 am

    Same problem.
    After pressing the button, motor does not respond anything.
    Please help me out with the Correct Circuit Diagram and code.

Leave A Comment

CommentLuv badge

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Show Buttons
Hide Buttons