Controlling servo and LED lights

A little backround on my project. My hope is to control the position of a servo between 2 end points and also light an LED to indicate position. The hardware consists of a UNO R3, a PCA 9685 for the servos and TLC 5947 for the LEDs, a SPST switch. Power is by 9V power supply. 5V grounds are tied together in a block. There will be a minimum of 10 switches (2-11) and 10 servos.

The issue at this point is that the servo will go to one position fine but at the other it flutters.

I'm not sure if the problem lies in my code or my wiring but probably wiring. Any advice will be greatly appreciated!

Attached is the code and a wiring diagram.

#include <Wire.h>
#include <Adafruit_TLC5947.h>
#include <Adafruit_PWMServoDriver.h>
Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver();

#define NUM_TLC5947 1

#define data   A0
#define clock   A2
#define latch   A1
#define oe  -1  // set to -1 to not use the enable pin (its optional)

Adafruit_TLC5947 tlc = Adafruit_TLC5947(NUM_TLC5947, clock, data, latch);
#define Green0LED 0  //channel on TLC5947
#define Red1LED   1
#define Green2LED 2
#define Red3LED   3
#define Green4LED 4
#define Red5LED   5
#define Green6LED 6
#define Red7LED   7
#define Green8LED 8
#define Red9LED   9
#define Green10LED 10
#define Red11LED   11
#define Green12LED 12
#define Red13LED   13
#define Green14LED 14
#define Red15LED   15
#define Green16LED 16
#define Red17LED   17
#define Green18LED 18
#define Red19LED   19


int buttonPin0 = 2;   // Can add as many buttons as needed
int buttonState0 = 0;
int buttonPin1 = 3;
int buttonState1 = 0;
int buttonPin2 = 4;
int buttonState2 = 0;
int buttonPin3 = 5;
int buttonState3 = 0;
int buttonPin4 = 6;
int buttonState4 = 0;
int buttonPin5 = 7;
int buttonState5 = 0;
int buttonPin6 = 8;
int buttonState6 = 0;
int buttonPin7 = 9;
int buttonState7 = 0;
int buttonPin8 = 10;
int buttonState8 = 0;
int buttonPin9 = 11;
int buttonState9 = 0;





void setup() {
  Serial.begin(9600);
  Serial.println("pca9685_TurnoutFinal!");
  pwm.begin();
  pwm.setPWMFreq(60);
  tlc.begin();
  delay(30);

  pinMode(buttonPin0, INPUT_PULLUP);
  pinMode(buttonPin1, INPUT_PULLUP);
  pinMode(buttonPin2, INPUT_PULLUP);
  pinMode(buttonPin3, INPUT_PULLUP);
  pinMode(buttonPin4, INPUT_PULLUP);
  pinMode(buttonPin5, INPUT_PULLUP);
  pinMode(buttonPin6, INPUT_PULLUP);
  pinMode(buttonPin7, INPUT_PULLUP);
  pinMode(buttonPin8, INPUT_PULLUP);
  pinMode(buttonPin9, INPUT_PULLUP);

}

  void loop() {
  ///////////
  buttonState0 = digitalRead(buttonPin0);
  if (buttonState0 == HIGH) {               //swithc is on
    pwm.setPWM(0, 0, 370);                  //servo at channel 0 moves to position 370
    tlc.setPWM(Green0LED, 1000);  //LED at channel 0 turns on
    tlc.setPWM(Red1LED, 0);     //LED at channel 1 turns off
    tlc.write();
  }
  else  {                                    //switch is off
    pwm.setPWM(0, 0, 285);                   //servo at channel 0 moves to position 285
    tlc.setPWM(Green0LED, 0);      //LED at channel 0 turns off
    tlc.setPWM(Red1LED, 4000);   //LED at channel 1 turns on
    tlc.write();
  }

  buttonState1 = digitalRead(buttonPin1);
  if (buttonState1 == HIGH) {               //swithc is on
    pwm.setPWM(1, 0, 370);                  
    tlc.setPWM(Green2LED, 1000);  
    tlc.setPWM(Red3LED, 0);     
    tlc.write();
  }
  else  {                                    //switch is off
    pwm.setPWM(1, 0, 285);                   
    tlc.setPWM(Green2LED, 0);      
    tlc.setPWM(Red3LED, 4000);   
    tlc.write();
  }

  buttonState2 = digitalRead(buttonPin2);
  if (buttonState2 == HIGH) {               //swithc is on
    pwm.setPWM(2, 0, 370);                  
    tlc.setPWM(Green4LED, 1000);  
    tlc.setPWM(Red5LED, 0);     
    tlc.write();
  }
  else  {                                    //switch is off
    pwm.setPWM(2, 0, 285);                  
    tlc.setPWM(Green4LED, 0);      
    tlc.setPWM(Red5LED, 4000);   
    tlc.write();
  }

  buttonState3 = digitalRead(buttonPin3);
  if (buttonState3 == HIGH) {               //swithc is on
    pwm.setPWM(3, 0, 370);                  
    tlc.setPWM(Green6LED, 1000);  
    tlc.setPWM(Red7LED, 0);     
    tlc.write();
  }
  else  {                                    //switch is off
    pwm.setPWM(3, 0, 285);                   
    tlc.setPWM(Green6LED, 0);      
    tlc.setPWM(Red7LED, 4000);   
    tlc.write();
  }

  buttonState4 = digitalRead(buttonPin4);
  if (buttonState4 == HIGH) {               //swithc is on
    pwm.setPWM(4, 0, 370);                  //servo at channel 4 moves to position 370
    tlc.setPWM(Green8LED, 1000);  //LED at channel 8 turns on
    tlc.setPWM(Red9LED, 0);     //LED at channel 8 turns off
    tlc.write();
  }
  else  {                                    //switch is off
    pwm.setPWM(4, 0, 285);                   //servo at channel 4 moves to position 285
    tlc.setPWM(Green8LED, 0);      //LED at channel 8 turns off
    tlc.setPWM(Red9LED, 4000);   //LED at channel 9 turns on
    tlc.write();
  }

  buttonState5 = digitalRead(buttonPin5);
  if (buttonState5 == HIGH) {               //swithc is on
    pwm.setPWM(5, 0, 370);                  //servo at channel 5 moves to position 370
    tlc.setPWM(Green10LED, 1000);  //LED at channel 10 turns on
    tlc.setPWM(Red11LED, 0);     //LED at channel 11 turns off
    tlc.write();
  }
  else  {                                    //switch is off
    pwm.setPWM(5, 0, 285);                   //servo at channel 5 moves to position 285
    tlc.setPWM(Green10LED, 0);      //LED at channel 10 turns off
    tlc.setPWM(Red11LED, 4000);   //LED at channel 11 turns on
    tlc.write();
  }

  buttonState6 = digitalRead(buttonPin6);
  if (buttonState6 == HIGH) {               //swithc is on
    pwm.setPWM(6, 0, 370);                  //servo at channel 6 moves to position 370
    tlc.setPWM(Green12LED, 1000);  //LED at channel 12 turns on
    tlc.setPWM(Red13LED, 0);     //LED at channel 13 turns off
    tlc.write();
  }
  else  {                                    //switch is off
    pwm.setPWM(6, 0, 285);                   //servo at channel 6 moves to position 285
    tlc.setPWM(Green12LED, 0);      //LED at channel 12 turns off
    tlc.setPWM(Red13LED, 4000);   //LED at channel 13 turns on
    tlc.write();
  }

  buttonState7 = digitalRead(buttonPin7);
  if (buttonState7 == HIGH) {               //swithc is on
    pwm.setPWM(7, 0, 370);                  //servo at channel 7 moves to position 370
    tlc.setPWM(Green14LED, 1000);  //LED at channel 14turns on
    tlc.setPWM(Red15LED, 0);     //LED at channel 15 turns off
    tlc.write();
  }
  else  {                                    //switch is off
    pwm.setPWM(7, 0, 285);                   //servo at channel 7 moves to position 285
    tlc.setPWM(Green14LED, 0);      //LED at channel 14turns off
    tlc.setPWM(Red15LED, 4000);   //LED at channel 15 turns on
    tlc.write();
  }

  buttonState8 = digitalRead(buttonPin8);
  if (buttonState8 == HIGH) {               //swithc is on
    pwm.setPWM(8, 0, 370);                  //servo at channel 8 moves to position 370
    tlc.setPWM(Green16LED, 1000);  //LED at channel 16 turns on
    tlc.setPWM(Red17LED, 0);     //LED at channel 17 turns off
    tlc.write();
  }
  else  {                                    //switch is off
    pwm.setPWM(8, 0, 285);                   //servo at channel 8 moves to position 285
    tlc.setPWM(Green16LED, 0);      //LED at channel 16 turns off
    tlc.setPWM(Red17LED, 4000);   //LED at channel 17 turns on
    tlc.write();
  }

  buttonState9 = digitalRead(buttonPin9);
  if (buttonState9 == HIGH) {               //swithc is on
    pwm.setPWM(9, 0, 370);                  //servo at channel 9 moves to position 370
    tlc.setPWM(Green18LED, 1000);  //LED at channel 18 turns on
    tlc.setPWM(Red19LED, 0);     //LED at channel 19 turns off
    tlc.write();
  }
  else  {                                    //switch is off
    pwm.setPWM(9, 0, 285);                   //servo at channel 9 moves to position 285
    tlc.setPWM(Green18LED, 0);      //LED at channel 18 turns off
    tlc.setPWM(Red19LED, 4000);   //LED at channel 19 turns on
    tlc.write();
  }

  
  }

You don't seem to have any servo power to the PCA9685. If you're relying on the Arduino 5V pin it is a bad idea to power even one servo or motor from that. Trying to power a PCA9685 and 10 servos is definitely not good. With 9V on Vin the regulator/5V pin can safely supply about 150mA when even a single micro servo can need over 500mA, larger servos considerably more.

What servos are you using and which position (285 or 370) do they flutter at?

Steve

Hi,
Are you trying to get the servo to go from 0 to 180 degrees.
What position is 285?
If you are placing the servo at the end of its travel, that is 0 or 180, some servos, particularly cheap ones are not mechanically that accurate.
Try 5 to 175 degrees and see if the fault remains.

What servo are you using?
Can you post a link to specs/data?

How big is the 9V power supply?
How are you supplying the current for the servos?

Thanks.. Tom... :slight_smile:

Thanks everyone for the replies. You got me thinking, which is dangerous, but I got my endpoint values from a brand of servos. I made a change to the values and all seems good now.

Again, Thanks!