How to fix led issue with servos and button

Hello, I need help with this LED issue I'm struggling with right now. my goal for this project is to get the LED light turn on by pressing the button with the servo motor moving to position and then press the button again to get the LED light turn off with the servo motor back in place. In this project, I have four servo motors with two push buttons that are functioning well. I only need two servos with a LED light to function with a button. When I connected the LED to the correct pin and write the code to upload to the Arduino Uno board, the LED is showing low dimming light which is not what I want; I want strong light up, and the servos are moving fine when I pressed the button. I don't know what is wrong with it. I looked at the code carefully and everything looks okay. I use a fully charged portable power bank as a power source. I might have missed something in the code. Please help me out. Thank you.

Here is the code I created:

#include <Servo.h>
#include <ezButton.h>


//constants won't change
const int BUTTON_PIN = 7;
const int BUTTON_PIN2 = 6;
const int SERVO_PIN = 9;
const int SERVO_PIN2 = 8;
const int SERVO_PIN3 = 5;
const int SERVO_PIN4 = 4;
const int LED_PIN    = 3;

int buttonState = 0; 
int ledState = LOW;


ezButton button1(7);
ezButton button2(6);
Servo servo;
Servo servo2;
Servo servo3;
Servo servo4;

 

// variables will change:
int angle1 = 0;
int angle2 = 0;
int angle3 = 0;
int angle4 = 0;

void setup() {
  Serial.begin(9600);
  button1.setDebounceTime(15);
  button2.setDebounceTime(15);
  servo.attach(SERVO_PIN);
  servo2.attach(SERVO_PIN2);
  servo3.attach(SERVO_PIN3);
  servo4.attach(SERVO_PIN4);


}


void loop() {
  button1.loop();
  button2.loop();

  buttonState = digitalRead(BUTTON_PIN);

  if(button1.isPressed() ) {
    Serial.println("The button1 is pressed");

    // toggle state of LED
    ledState =! ledState;

    // control LED arccoding to the toggleed sate
    digitalWrite(LED_PIN, ledState);

    // change angle of servo motor
    if(angle1 == 0 ) {
      angle1 = 90;
      angle2 = 0;
    }
    else {
      angle1 = 0;
      angle2 = 90;
    }

    if(angle2 ==0) {
      angle1 = 90;
      angle2 = 0;
    }
    else {
      angle1 = 0;
      angle2 = 90;
      
    }

    //control servo motor according to the angle
    servo.write(angle1);
    delay(15);
    servo4.write(angle2);
    delay(15);
    }


 if(button2.isPressed() ) {
    Serial.println("The button2 is pressed");

    // change angle of servo motor
    if(angle3 == 0 ) {
      angle3 = 90;
      angle4 = 0;
    }
    else {
      angle3 = 0;
      angle4 = 90;
    }

    if(angle4 ==0) {
      angle3 = 90;
      angle4 = 0;
    }
    else {
      angle3 = 0;
      angle4 = 90;

    }



    servo3.write(angle3);
    delay(15);
    servo2.write(angle4);
    delay(15);
    }
 
}

You might want to add this to setup( ) :

pinMode(LED_PIN, OUTPUT);

1 Like

@LarryD

I just added pinMode(LED_PIN, OUTPUT); under setup() and there is this strange reaction when I pressed the button. It shows the LED light flicking and the servos moving like jerking randomly when I pressed it. Is the power source the issue that generates too much power? I just added the resistor and it still shows the same result.

Show us a good schematic of your circuit.
Show us a good image of your ‘actual’ wiring.
Give links to components.


In the Arduino IDE, use Ctrl T or CMD T to format your code then copy the complete sketch.

Use the </> icon from the ‘reply menu’ to attach the copied sketch.


Servos need to be powered by an external 5-6v power supply, a common GND to the Arduino is needed.

1 Like

Yo @LarryD , it finally works! The LED lights up so bright and blinds my eyes for a moment. It was BEAUTIFUL! lol. The servos moving fine as expected when I pressed the button. What I just did was move the LED's GND from an external power supply to the Arduino GND. That LED's GND must be interfering with the servos through an external power supply that is causing the issue. Thank you so much for your help! I really appreciated it!!

Make sure your LED has a series current limiting resistor.

220Ω to 1k will work.

1 Like

@LarryD

Alright, I will add a resistor to it. Again, Thank you so much!

@tonywin101, your topic has been moved to a more suitable location on the forum. Installation and Troubleshooting is not for problems with your project :wink: See About the Installation & Troubleshooting category.

@sterretje thank you for let me know. Where is my topic moved to on the forum? I am still new with the whole forum here and trying to understand how they work.

When you open your topic, you will see it at the top.

image

1 Like