Program problem

Hello, I recently tried to run the program below but no servo motor reacts. However, the LEDs light up so the circuit works and by testing them on another arduino board with a simple code, the motors work. I need your help because I don't know much about programming. Thanks in advance (PS: I use an arduino nano board and an adafruit PCA9685)

#include <Wire.h>
#include <Adafruit_PWMServoDriver.h>

Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver();

// Adjust for global change in small sg90 servo movement //
#define SERVOMIN 150
#define SERVOMAX 450
/////////////////////////////////////////////////////
#define SERVO_FREQ 50

uint8_t servonum = 0;
int buttonPin = 2;
int ledPin = 6;
int buttonState = 0;
int globalPos = 1;
unsigned long previousMillis = 0;
unsigned long interval = 5000;
int a = 60;

int animDelay = 1;
int mainServoL = 0;
int mainServoR = 1;
int servoBrowCenter = 4;

// Adjust below numbers for individual servos
int mainServoClosed = 750;
int mainServoOpen = 1950;
int servoBrowCenterOpen = 40;
int servoBrowCenterClosed = 120;
int servoCheeksOpen = 20;
int servoCheeksClosed = 90;
int servoNoseSideOpen = 10;
int servoNoseSideClosed = 86;
int servoBrowSideOpen = 30;
int servoBrowSideClosed = 90;
int servoNoseCenterOpen = 1;
int servoNoseCenterClosed = 110;


void setup() {
  Serial.begin(9600);
  Serial.println("Boot Up");
  pwm.begin();
  pwm.setOscillatorFrequency(27000000);
  pwm.setPWMFreq(SERVO_FREQ);
  pinMode(buttonPin, INPUT_PULLUP);
  delay(50);
  pwm.sleep();
}

int getAngleToPulse(int angle) {
  return map(angle, 0, 180, SERVOMIN, SERVOMAX);
}

void loop() {

  buttonState = digitalRead(buttonPin);
  if (buttonState == 0) {
    Serial.println("Wake up");
    pwm.wakeup();

    if (globalPos > 0) {
      Serial.println("Opening");
      // CHEEKS
      for (uint16_t pulselen = servoCheeksClosed; pulselen >= servoCheeksOpen; pulselen--) {
        pwm.setPWM(9, 0, getAngleToPulse(servoCheeksClosed + servoCheeksOpen - pulselen));
        pwm.setPWM(8, 0, getAngleToPulse(pulselen));
      }
      delay(animDelay);
      Serial.println("1. Cheeks Open");
      // NOSE Side
      for (uint16_t pulselen = servoNoseSideClosed; pulselen >= servoNoseSideOpen; pulselen--) {
        pwm.setPWM(6, 0, getAngleToPulse(servoNoseSideClosed + servoNoseSideOpen - pulselen));
        pwm.setPWM(7, 0, getAngleToPulse(pulselen));
      }
      delay(animDelay);
      Serial.println("2. Nose Side Open");
      //BROW Center
      for (uint16_t pulselen = servoBrowCenterClosed; pulselen >= servoBrowCenterOpen; pulselen--) {
        pwm.setPWM(servoBrowCenter, 0, getAngleToPulse(pulselen));
      }
      delay(animDelay);
      Serial.println("3. Brow Center Open");
      // BROW Side
      for (uint16_t pulselen = servoBrowSideOpen; pulselen <= servoBrowSideClosed; pulselen++) {
        pwm.setPWM(2, 0, getAngleToPulse(pulselen));
        pwm.setPWM(3, 0, getAngleToPulse(servoBrowSideOpen + servoBrowSideClosed - pulselen));
      }
      delay(animDelay);
      Serial.println("4. Brow Side Open");
      // NOSE Center
      for (uint16_t pulselen = servoNoseCenterClosed; pulselen >= servoNoseCenterOpen; pulselen--) {
        pwm.setPWM(5, 0, getAngleToPulse(pulselen));
      }
      delay(animDelay);
      Serial.println("5. Nose Center Open");
      // EYES
      analogWrite(ledPin, 0); 
      // MAIN SERVOS
      for (uint16_t microsec = mainServoClosed; microsec < mainServoOpen; microsec +=5) {
        pwm.writeMicroseconds(mainServoL, microsec);
        pwm.writeMicroseconds(mainServoR, (mainServoOpen+mainServoClosed-microsec));
      }
      globalPos = 0;
      delay(animDelay);
      Serial.println("6. Main Open");
    } else {
      Serial.println("Closing");
      //MAIN SERVOS
      for (uint16_t microsec = mainServoOpen; microsec > mainServoClosed; microsec-=5) {
        pwm.writeMicroseconds(mainServoL, microsec);
        pwm.writeMicroseconds(mainServoR, (mainServoOpen+mainServoClosed-microsec));
      }
      delay(animDelay);
      //NOSE Center
      for (uint16_t pulselen = servoNoseCenterOpen; pulselen <= servoNoseCenterClosed; pulselen++) {
        pwm.setPWM(5, 0, getAngleToPulse(pulselen));
      }
      
      delay(animDelay);
      
      // BROW Side
      for (uint16_t pulselen = servoBrowSideClosed; pulselen >= servoBrowSideOpen; pulselen--) {
        pwm.setPWM(2, 0, getAngleToPulse(pulselen));
        pwm.setPWM(3, 0, getAngleToPulse(servoBrowSideOpen + servoBrowSideClosed - pulselen));
      }
      delay(animDelay);
      // BROW Center
      for (uint16_t pulselen = servoBrowCenterOpen; pulselen <= servoBrowCenterClosed; pulselen++) {
        pwm.setPWM(servoBrowCenter, 0, getAngleToPulse(pulselen));
      }
      delay(animDelay);
      // NOSE Side
      for (uint16_t pulselen = servoNoseSideOpen; pulselen <= servoNoseSideClosed; pulselen++) {
        pwm.setPWM(6, 0, getAngleToPulse(servoNoseSideClosed + servoNoseSideOpen - pulselen));
        pwm.setPWM(7, 0, getAngleToPulse(pulselen));
      }
      delay(animDelay);
      //CHEEKS
      for (uint16_t pulselen = servoCheeksOpen; pulselen <= servoCheeksClosed; pulselen++) {
        pwm.setPWM(9, 0, getAngleToPulse(servoCheeksClosed + servoCheeksOpen - pulselen));
        pwm.setPWM(8, 0, getAngleToPulse(pulselen));
      }
      delay(animDelay);
      //EYES
      analogWrite(ledPin, 255);
      globalPos = 1;
      delay(100);
      Serial.println("Sleep");
      pwm.sleep();
    }
    delay(500);
  }
  delay(10);
}

Step 1 - connect only 1 servo, and see if it functions. I strongly suspect your 10 servos have overloaded the power supply.

why are you writing in English in the French speaking category ?

I moved your topic to an appropriate forum category.

In the future, please take some time to pick the forum category that best suits the subject of your topic. There is an "About the _____ category" topic at the top of each category that explains its purpose.

This is an important part of responsible forum usage, as explained in the "How to get the best out of this forum" guide. The guide contains a lot of other useful information. Please read it.

Thanks in advance for your cooperation.

Looks like you are supplying 5V from a USB socket to the Vin pin. You need at least 6.5~7V for Vin. With 5V, you should power the Nano though the 5V pin.

You will damage the Nano unless you add current limiting resistors for the two LEDs.

1 Like

I’ve tried a single one and it works for a second and then stopped working.For now it is sometimes making small noises but not moving.

So i should power both of the boards with different power supplies ?

One supply should be enough, provided it has sufficient capacity and quality so that if all motors begin to move at the same time, it is still able to maintain a steady 5V to power the Arduino.

If you can't guarantee that, two supplies would be safer.

The power supply to the servo shield needs to be grounded to the rest of the system (nano and other power supply).

Hi for updates, the motors are now working but some of them seem to dont turn enough. I bought a pack of multiple steppers from AliExpress, could it be related to that?

This is good.

Which motors do not?

What is "enough" and what is not enough?

Draw a wiring diagram showing all connections.

Hi, I am following the diagram I posted in my first message.
For the main purpose, these motors are used to make a mark5 iron man helmet with each piece separating for the others when opening the helmet. When I say « not enough rotation » it’s because some of them almost don’t rotate so the pieces are not moving enough. I’ll send a video during the day so you can better understand.

Surely, adding steppers to a servo circuit will be a problem.

If you think I'm nitpicking, remember, you're on a technical forum, where technical details are of paramount importance. Proofreading what you post before you post it in order to catch misleading errors and provide clear communication is very, very important.

If you simply misspoke, and they are really 3-pin SG90 servos, then have you done a one-at-a-time test, to verify that all the servos move through the range you expect? Dime store(Amazon) servos aren't always worth the time, or the dime.

I have never found purchasing items to cause problems in projects that don't use them. :wink:

If you meant should you suspect things purchased at ridiculously low prices, from anywhere, of being junk, then yes.

If you meant to ask will switching to steppers help solve you problem, then no. Servos are appropriate muscle for this as I understand what you are attempting.

a7

You are right about the vocabulary, I’ m sorry for that. I only use servos and I tested them individually with an elegoo uno board and they worked but i never try with the actual program.

Your drawing does not show motors.

How do you "know" the motors are not working?

Make a drawing. Find an example for the device you want to use. Describe your observations. Describe your expectations.

Sorry they are servos not motors and they appear on the diagram. They are working for now not rotating all the way. Maybe the 3d printed parts could block the servos rotation. I’ll put a picture of the project soon

What is not all the way? 179 degrees? 135 degrees?

Have you tried adjusting SERVOMIN and SERVOMAX?

Is the library you are using the right library?

I haven’t try to change the program for now because my usb cable is broken so i cannot compile the program to the card but when i’ll buy a new one, i’ll try to change the angle

I am using the program of someone else and all the libraries seems to be correct

This is most important, and should be corrected before proceeding: Your code has "magic numbers" which is using a pin number rather than defining a descriptive variable with a pin number. Because of your "magic number" scheme, your code is using one pin for more than one device. These are probably referencing mux channels, not Arduino pins. You should make meaningful variable names (int leftCheek = 8, rightCheek = 9; and others)

Some cables are data, some are power-only. I like labeling cables (red = power only).

[edit]
Try this Adafruit example for eight servos on channels 0 through 7...

#include <Wire.h>
#include <Adafruit_PWMServoDriver.h>

Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver();
#define SERVOMIN  150
#define SERVOMAX  600
#define USMIN  600
#define USMAX  2400
#define SERVO_FREQ 50

uint8_t servonum = 0;

void setup() {
  Serial.begin(9600);
  Serial.println("8 channel Servo test!");

  pwm.begin();
  pwm.setOscillatorFrequency(27000000);
  pwm.setPWMFreq(SERVO_FREQ);

  delay(10);
}

void setServoPulse(uint8_t n, double pulse) {
  double pulselength;

  pulselength = 1000000;
  pulselength /= SERVO_FREQ;
  Serial.print(pulselength);
  Serial.println(" us per period");
  pulselength /= 4096;
  Serial.print(pulselength); Serial.println(" us per bit");
  pulse *= 1000000;
  pulse /= pulselength;
  Serial.println(pulse);
  pwm.setPWM(n, 0, pulse);
}

void loop() {
  Serial.println(servonum);
  for (uint16_t pulselen = SERVOMIN; pulselen < SERVOMAX; pulselen++) {
    pwm.setPWM(servonum, 0, pulselen);
  }

  delay(500);
  for (uint16_t pulselen = SERVOMAX; pulselen > SERVOMIN; pulselen--) {
    pwm.setPWM(servonum, 0, pulselen);
  }

  delay(500);
  for (uint16_t microsec = USMIN; microsec < USMAX; microsec++) {
    pwm.writeMicroseconds(servonum, microsec);
  }

  delay(500);
  for (uint16_t microsec = USMAX; microsec > USMIN; microsec--) {
    pwm.writeMicroseconds(servonum, microsec);
  }

  delay(500);
  servonum++;
  if (servonum > 7) servonum = 0;
}