mkpappu
mkpappu

Reputation: 378

Arduino circuit turns on LED but not servo

I have a project where I have a button on a breadboard which activates a servo via a programmed Arduino. Currently, this is my code:

#include <Servo.h>

Servo sA;

int d=2;   // to store on or off value

void setup(){
  sA.attach(A0);
  pinMode(2,INPUT);
  pinMode(A0,OUTPUT);
  pinMode(13,OUTPUT);
  sA.write(90);
}

void loop(){
  d=digitalRead(2);
  if(d==0){
    sA.write(90);
    digitalWrite(13,HIGH);
  }elseThe
  sA.write(0);}
  digitalWrite(13,LOW);
} 

The LED is in here as a test of the button (which I had had issues with). When the button is pressed, the LED lights up as intended in the if statement. The servo (TowerPro MG995) is wired up directly to a VEX 7.2 volt battery for power, which I know works for power. However, the button is not activating the servo or registering a response at all. Is there an issue with the code? The servo's data cord is wired straight to pin A0 as in the code.

Upvotes: 0

Views: 79

Answers (2)

Dat Ha
Dat Ha

Reputation: 650

Electric problems

Just a reminder. your servo has a maximum voltage of 6.6V. A fully charged 7.2V VEX battery will be at 8.4V, because 7.2V is just the battery's average voltage (yep).


Code problems

  • No need to put pinMode(A0, OUTPUT);

Everything else seems OK.

Upvotes: 0

Fabio Henrique Fudoli
Fabio Henrique Fudoli

Reputation: 124

I rewrote your code, check your conections and try this code:

#include <Servo.h>

Servo sA;

int d=2;   // to store on or off value
void setup(){
  pinMode(d, INPUT);
  sA.attach(9); //pin 9
  //sA.write(90); //will move to 90degrees
}

void loop(){
  if(digitalRead(d)==HIGH){
  sA.write(90);
  digitalWrite(13,HIGH);
  }
  else{
  sA.write(0);
  digitalWrite(13,LOW);
  } 
}

Remember, the servo will move only if the button is pressed, if you release it will back.

Upvotes: 0

Related Questions