Interfacing multiple servo with arduino probmlem

Thread Starter

layana

Joined Oct 16, 2020
9
Hi,

I want to interface multiple servo sg90 with arduino nano,
I tried the following code


Code:
#include <Servo.h>

Servo myservof1; Servo myservof2;
Servo myservof3;  Servo myservof4;  Servo myservof5;

int potpin = A0; 
int val;   

void setup() {
myservof1.attach(9);  myservof2.attach(3);
myservof3.attach(5);
myservof4.attach(6);
myservof5.attach(10);
}

void loop() {
  val = analogRead(potpin);            // reads the value of the potentiometer (value between 0 and 1023)
  val = map(val, 0, 1023, 0, 180);     // scale it for use with the servo (value between 0 and 180)
  myservof1.write(val);
myservof2.write(val);
myservof3.write(val);
myservof4.write(val);
myservof5.write(val);
  delay(15);                          
}
I tried the two following circuits

fgd.jpg

But no motor worked,but when i tried the example code for one servo motor it worked.


Could anyone help me
 

Phil-S

Joined Dec 4, 2015
238
Plenty of examples.
Google "Arduino control of multiple servos"
Have looked at the Arduino site?
Also try and avoid delay() and use millis() instead.
You don't want to introduce pauses in running the program.
 

Thread Starter

layana

Joined Oct 16, 2020
9
I tried alot of suggestions but the problem still exists.

Finally I replaced the nano with arduino uno,the motor worked

Thank you all
 

Reloadron

Joined Jan 15, 2015
7,517
Here is a code sample similar to what you want.

Servo Analog to Position:
#include <Servo.h>

Servo myservo1; // list the servos 5 total servos
Servo myservo2;
Servo myservo3;
Servo myservo4;
Servo myservo5;

int potpin=A0; // choose analog input

void setup()
{
  myservo1.attach(3); // servo control pins 3, 5, 6, 9, and 10
  myservo2.attach(5);
  myservo3.attach(6);
  myservo4.attach(9);
  myservo5.attach(10);

  myservo1.write(90); // set all servos to mid-point
  myservo2.write(90);
  myservo3.write(90);
  myservo4.write(90);
  myservo5.write(90);
delay(100);

}

void loop() {
  int val = analogRead(0);
  val = map(val, 0, 1023, 0, 180);
  myservo1.write(val); // write to rach servo between 0 and 180
  myservo2.write(val);
  myservo3.write(val);
  myservo4.write(val);
  myservo5.write(val);

delay(15); // delay 15 msec and loop again

  }
Using the library Servo.h can be a problem. The above code does work but... Here is a portion of Servo.h

Servo.h partial code description:
The methods are:
    Servo - Class for manipulating servo motors connected to Arduino pins.
    attach(pin )  - Attaches a servo motor to an I/O pin.
    attach(pin, min, max  ) - Attaches to a pin setting min and max values in microseconds
    default min is 544, max is 2400

    write()     - Sets the servo angle in degrees.  (invalid angle that is valid as pulse in microseconds is treated as microseconds)
    writeMicroseconds() - Sets the servo pulse width in microseconds
    read()      - Gets the last written servo pulse width as an angle between 0 and 180.
    readMicroseconds()   - Gets the last written servo pulse width in microseconds. (was read_us() in first release)
    attached()  - Returns true if there is a servo attached.
    detach()    - Stops an attached servos from pulsing its I/O pin.

   
    #define MIN_PULSE_WIDTH       544     // the shortest pulse sent to a servo 
    #define MAX_PULSE_WIDTH      2400     // the longest pulse sent to a servo
    #define DEFAULT_PULSE_WIDTH  1500     // default pulse width when servo is attached
    #define REFRESH_INTERVAL    20000     // minimum time to refresh servos in microseconds
As to a typical servo and the position signal. Servos need a refresh rate of about 50 Hz or 20 mSec. Frequency/period are specific to controlling a specific servo. A typical servo motor expects to be updated every 20 ms with a pulse between 1 ms and 2 ms, or in other words, between a 5 and 10% duty cycle on a 50 Hz waveform. With a 1.5 ms pulse, the servo motor will be at the natural 90 degree position. With a 1 ms pulse, the servo will be at the 0 degree position, and with a 2 ms pulse, the servo will be at 180 degrees.

Looking at the above Servo.h code sample:
default min is 544, max is 2400
the 544 and 2400 are expressed in uSec so .544 mSec to 2.4 mSec. Using the above code sample for a basic servo we get a 20 mSec pulse (50 Hertz) but the pulse width will be 544 uSec to 2400 uSec. So when we use a write between 0 and 180 we are not getting the usual pulse width between 1.0 and 2.0 mSec but a pulse width between 0.544 mSec and 2.4 mSec.

Just something to keep in mind.

Anyway the code sample I show above should work. I viewed it on a scope and counter. With my 10K pot set full CCW I get a pulse of 0.544 mSec and at full CW I get a 2.4 mSec pulse as expected. I also used an Arduino Uno but it should work on a Nano. Not sure why it would not.

Ron
 

denis73

Joined Jan 17, 2017
1
Hi,

I want to interface multiple servo sg90 with arduino nano,
I tried the following code


Code:
#include <Servo.h>

Servo myservof1; Servo myservof2;
Servo myservof3;  Servo myservof4;  Servo myservof5;

int potpin = A0;
int val;  

void setup() {
myservof1.attach(9);  myservof2.attach(3);
myservof3.attach(5);
myservof4.attach(6);
myservof5.attach(10);
}

void loop() {
  val = analogRead(potpin);            // reads the value of the potentiometer (value between 0 and 1023)
  val = map(val, 0, 1023, 0, 180);     // scale it for use with the servo (value between 0 and 180)
  myservof1.write(val);
myservof2.write(val);
myservof3.write(val);
myservof4.write(val);
myservof5.write(val);
  delay(15);                         
}
I tried the two following circuits

View attachment 262449

But no motor worked,but when i tried the example code for one servo motor it worked.


Could anyone help me
Attach the Servo variable to a pin. Note that in Arduino 0016 and earlier, the Servo library supports only servos on only two pins: 9 and 10.
 

Phil-S

Joined Dec 4, 2015
238
Changing from a Nano to a Uno shouldn't make a scrap of difference.
Maybe something got a bit warm?
If you are using clones, there can be problems with Nanos and bootloading.
There is also the possibility that the smaller chip size of the 328 in the Nano is more likely to fail under fault conditions.
 
Top