element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet & Tria Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • About Us
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      • Japan
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Vietnam
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Arduino
  • Products
  • More
Arduino
Arduino Forum ~ ANSWERED ~ If this is a repeat, my apologies - 4 servos all turning at once with one potentiometer...This is NOT the way the project is to work.  Please help
  • Blog
  • Forum
  • Documents
  • Quiz
  • Events
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Arduino to participate - click to join for free!
Actions
  • Share
  • More
  • Cancel
Forum Thread Details
  • State Verified Answer
  • Replies 67 replies
  • Answers 3 answers
  • Subscribers 406 subscribers
  • Views 4321 views
  • Users 0 members are here
  • answered
Related

~ ANSWERED ~ If this is a repeat, my apologies - 4 servos all turning at once with one potentiometer...This is NOT the way the project is to work.  Please help

Former Member
Former Member over 12 years ago

Hello all,

I am working on a project with 4 servos, and 4 potentiometers.

I will include my coding momentarily.  Here is my dilemma.

As the title states: "4 servos all turning at once with one potentiometer...This is NOT the way the project is to work."

The project is to turn each servo unit with its own potentiometer (dial style).  I have compiled my coding and it all comes out ok, I then upload it with no issue.

I then attempt to turn the pot and all 4 turn, and if I may add, with quite a bit of jitterness.

Can you please take a moment, look at the code and advise where you think the problem is.

This code was the KNOB example from the Arduino UNO example library.  I removed the name of the person as I have continued to add to the program.

You will see the things I have added as I have notated them.

 

Thanks in advance,

Anna

 

Now the code:

 

#include <Servo.h>

Servo myservo0;  // create servo object to control a servo
Servo myservo1;  // i added
Servo myservo2;  // i added
Servo myservo3;  // i added

 

int potpin0 = 0;  // analog pin used to connect the potentiometer (i added the zero to the potpin before the equal sign)
int val0;    // variable to read the value from the analog pin

int potpin1 = 1;  // i added
int val1;         // i added

 

int potpin2 = 2;  // i added
int val2;         // i added 

 

int potpin3 = 3;  //  i added
int val3;         //  i added


void setup()
{
  myservo0.attach(3);  // attaches the servo on pin 3 to the servo object --- I changed it to 3 from previously 9 - Anna
  myservo1.attach(5);  // i added
  myservo2.attach(6);  // i added
  myservo3.attach(9);  // i added
}

void loop()
{
  val0 = analogRead(potpin0);            // reads the value of the potentiometer (value between 0 and 1023)
  val0 = map(val0, 0, 1023, 0, 179);     // scale it to use it with the servo (value between 0 and 180)
  myservo0.write(val0);                  // sets the servo position according to the scaled value
  delay(15);                             // waits for the servo to get there

 

val1 = analogRead(potpin1);            // i added
  val1 = map(val1, 0, 1023, 0, 179);     // i added
  myservo1.write(val1);                  // i added 
  delay(15);

 

  val2 = analogRead(potpin2);            // i added
  val2 = map(val2, 0, 1023, 0, 179);     // i added
  myservo2.write(val2);                  // i added
  delay(15);

 

  val3 = analogRead(potpin3);            // i added
  val3 = map(val3, 0, 1023, 0, 179);     // i added
  myservo3.write(val3);                  // i added
  delay(15);
}

 

Message was edited by: Annasta Brandon My project question was answered, now on to part 2... http://www.element14.com/community/thread/24030

  • Sign in to reply
  • Cancel

Top Replies

  • mcb1
    mcb1 over 12 years ago in reply to billabott +2
    I had a look at this discussion last night, but unfortunately our IT department does some weird java filtering, so I can't reply. Thanks Bill for the invite. The suggestions made so far look correct, but…
  • terrydark
    terrydark over 12 years ago in reply to mcb1 +2
    Hi Just to confirm Mark's statements... "The Arduino reference has mixed messages regarding the name to use for the Analogue inputs. A0, 0 all seem to work BUT it has to know its doing an analogue reading…
  • Former Member
    Former Member over 12 years ago +1
    Annasta, Did you intend for server0 and potpin3 to both use pin 3?
Parents
  • billabott
    0 billabott over 12 years ago

    @Anna

     

    Q:  What are the resistance values of your 4 potentiometers?  100 ohms, 1K ohms, 10K ohms, 100K ohms, 1M ohms, 10M ohms?

     

    v = i * r

    i = 5V / 100 = 50 mA per pot.

     

    And what is the total capacity for the USB port driven supply on the UNO?  I think 200 mA.

     

    The PC/laptop is capable of a maximum current drain of 100mA-500mA(max) and it is overcurrent protected.  Your mileage may vary.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • mcb1
    0 mcb1 over 12 years ago in reply to billabott

    I had a look at this discussion last night, but unfortunately our IT department does some weird java filtering, so I can't reply.

     

    Thanks Bill for the invite.

    The suggestions made so far look correct, but I think the issue is in the analogue reading, not the proximity of the output wiring.

     

    There is one thing that needs to be done.

    The 5v supply unless its a large analogue model with knobs and meters, will be suffering from a lack of decent capacitors.

     

    Please try to add one or two 1000uF or larger (multiples also work) across the servo 5v supply close to the actual servo.

    Motors draw 3-4 times their running current when starting, and hence your little servos that are 100mA suddenly all turn into 3-400mA devices.

    Coupled with the fact they are all incorrectly driving at the same time, and they all beat each other to death.

     

     

    The Arduino reference has mixed messages regarding the name to use for the Analogue inputs.

    A0, 0 all seem to work BUT it has to know its doing an analogue reading.

     

    It does clearly say to connect the other pins.(as Nico pointed out)

    If the analog input pin is not connected to anything, the value returned by analogRead() will fluctuate based on a number of factors (e.g. the values of the other analog inputs, how close your hand is to the board, etc.).

    Delay

    The examples all use a delay(15) to allow the servo to actually get there, before trying to change where it is.

    This may be enough but will depend on the loading and performance of the servo.

     

    Annasta

    I suggest trying your single pot on potpin0 (A0) but with the others (potpin1,2,3) shorted to ground (or use a 1k to 10k). This ensures their value is known to be zero.

    I also recommend sending the value to terminal so you can see what is happening.

    I've also added a delay to slow it all down. You need something to allow the servo to get there.

     

     

    hence

    #include <Servo.h>

    Servo myservo0;  // create servo object to control a servo
    Servo myservo1;  // i added
    Servo myservo2;  // i added
    Servo myservo3;  // i added

     

    int potpin0 = 0;     // analog pin used to connect the potentiometer (i added the zero to the potpin before the equal sign)
    int val0;               // variable to read the value from the analog pin

    int potpin1 = 1;    // i added
    int val1;               // i added

    int potpin2 = 2;    // i added
    int val2;               // i added 

    int potpin3 = 5;    // i added
    int val3;               // i added


    void setup()
    {

     

    Serial.begin(57600);

     

      myservo0.attach(5);       // attaches the servo on pin 5 which conicides with A0 object potpin 0
      myservo1.attach(7);       //  i added pin  7 which conicides with A1 to the servo object potpin 1
      myservo2.attach(8);       //  i added pin  8 which conicides with A2 to the servo object potpin 2
      myservo3.attach(12);     //  i added pin 12 which conicides with A5 to the servo object potpin 3
    }

    void loop()
    {


      val0 = analogRead(potpin0);            // reads the value of the potentiometer (value between 0 and 1023)
      val0 = map(val0, 0, 1023, 0, 179);     // scale it to use it with the servo (value between 0 and 180)
      myservo0.write(val0);                  // sets the servo position according to the scaled value


      Serial.println(val0, DEC);


      val1 = analogRead(potpin1);            // i added
      val1 = map(val1, 0, 1023, 0, 179);     // i added
      myservo1.write(val1);                  // i added 

     

    Serial.println(val1, DEC);

     

      val2 = analogRead(potpin2);            // i added
      val2 = map(val2, 0, 1023, 0, 179);     // i added
      myservo2.write(val2);                  // i added

     

      Serial.println(val2, DEC);

     

      val3 = analogRead(potpin3);            // i added
      val3 = map(val3, 0, 1023, 0, 179);     // i added
      myservo3.write(val3);                  // i added

     

    Serial.println(val3, DEC);


    delay(300);          //add the delay back in and slow the whole thing down.
    }

     

     

    I also recommend slowing down the loop, as the analogRead only takes 0.001 seconds.

     

    See how this goes, and then we can look at some other diagnostic tricks like forcing the servo to a known value.

     

    Mark

    • Cancel
    • Vote Up +2 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • terrydark
    0 terrydark over 12 years ago in reply to mcb1

    Hi

     

    Just to confirm Mark's statements...

     

    "The Arduino reference has mixed messages regarding the name to use for the Analogue inputs.

    A0, 0 all seem to work BUT it has to know its doing an analogue reading.

     

    It does clearly say to connect the other pins.(as Nico pointed out)"

     

    To add to answer on analog pin naming. I was curious about this as I knew the A0, A1 ... etc were constants but I had never checked what value they represented.

     

    Well ...A0 is a constant equating to 14, A1 = 15, A2 = 16 ... and so on but in the analogRead function, A0 and 0 are treated as A0, A1 and 1 as 1, etc

     

    And on A/D pins

    -----------------------

    Unconnected analog inputs tend to track the value on the connected input(s).  This I proved by experimentation by connecting a pot on A0 and leaving the rest unconnected.  The results were:

     

    A0 = 495

    A1 = 486

    A2 = 476

    A3 = 465

     

    ... and as I varied the pot and A0 followed steadily, A1,2,3 all followed along, albeit, shifting somewhat randomly around the A0 value.

     

    This will explain the servos being tied together

     

    Hope this helps

     

    Terry

    • Cancel
    • Vote Up +2 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
Reply
  • terrydark
    0 terrydark over 12 years ago in reply to mcb1

    Hi

     

    Just to confirm Mark's statements...

     

    "The Arduino reference has mixed messages regarding the name to use for the Analogue inputs.

    A0, 0 all seem to work BUT it has to know its doing an analogue reading.

     

    It does clearly say to connect the other pins.(as Nico pointed out)"

     

    To add to answer on analog pin naming. I was curious about this as I knew the A0, A1 ... etc were constants but I had never checked what value they represented.

     

    Well ...A0 is a constant equating to 14, A1 = 15, A2 = 16 ... and so on but in the analogRead function, A0 and 0 are treated as A0, A1 and 1 as 1, etc

     

    And on A/D pins

    -----------------------

    Unconnected analog inputs tend to track the value on the connected input(s).  This I proved by experimentation by connecting a pot on A0 and leaving the rest unconnected.  The results were:

     

    A0 = 495

    A1 = 486

    A2 = 476

    A3 = 465

     

    ... and as I varied the pot and A0 followed steadily, A1,2,3 all followed along, albeit, shifting somewhat randomly around the A0 value.

     

    This will explain the servos being tied together

     

    Hope this helps

     

    Terry

    • Cancel
    • Vote Up +2 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
Children
No Data
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube