1

Im practicing with an arduino UNO board (I'm a begginer) and I'm trying to turn some LED's on depending on which analog pin is connected to 5V. Here's my code:

int statusLed = 11;
int out1 = 2;
int out2 = 3;
int out3 = 4;
int out4 = 5;
int in1 = A0;
int in2 = A1;
int in3 = A2;
int in4 = A3;
int statusLedBrightness = 3;
int incBrillo = 3;
int fadeDelay = 5;

void blinkFade(int pin){
  analogWrite(pin, statusLedBrightness);
  statusLedBrightness = statusLedBrightness + incBrillo;
  if (statusLedBrightness <= 0 || statusLedBrightness >= 255){
    incBrillo = -incBrillo;   
  } 
  delay(fadeDelay);
  }

void wait(){  
  digitalWrite(out1, LOW);
  digitalWrite(out2, LOW);
  digitalWrite(out3, LOW);
  digitalWrite(out4, LOW);
  blinkFade(statusLed);
}



void setup() {
  // put your setup code here, to run once:
  pinMode(out1,OUTPUT);
  pinMode(out2,OUTPUT);
  pinMode(out3,OUTPUT);
  pinMode(out4,OUTPUT);
  pinMode(in1, INPUT);
  pinMode(in2, INPUT);
  pinMode(in3, INPUT);
  pinMode(in4, INPUT);
  Serial.begin(115200);
}

void loop() {
  // put your main code here, to run repeatedly:
  if (analogRead(in1)>=500){
    Serial.println("1: ");
    Serial.println(analogRead(in1)); //Print "1" and the read of pin A0
    turnOff();
    digitalWrite(out1,HIGH);
  }
  else if (analogRead(in2)>=500){
    Serial.println("2: ");
    Serial.println(analogRead(in2));//Print "2" and the read of pin A1
    turnOff();
    digitalWrite(out2,HIGH);
  }
  else if (analogRead(in3)>=500){
    Serial.println("3: ");
    Serial.println(analogRead(in3));//Print "3" and the read of pin A2
    turnOff();
    digitalWrite(out3,HIGH);
  }
  else if (analogRead(in4)>=500){
    Serial.println("4: ");
    Serial.println(analogRead(in4));//Print "4" and the read of pin A3
    turnOff();
    digitalWrite(out4,HIGH);
  }
  else{
    Serial.println("Stand By");
    wait();
  }

}

The problem Im having is when I connect a wire from 5V to any analog pin (A0 to A3) it always runs this segment of code:

Serial.println("1: ");
Serial.println(analogRead(in1));
turnOff();
digitalWrite(out1,HIGH);

By my understanding it's detecting as if it always was A0 the one that is connected to 5V but i dont understand why is this happening or how to solve it.

This is a screen capture of serial monitor when 5V is connected to pin A2. It should print "3:" and the reading [] 1

1

You're not showing your schematic but I'm sure that you are simply leaving the Ax pins OPEN when you are not connecting them to your +V line. In that case they are "floating" and as such they can float HIGH or LOW or just about anywhere. What's happening is that when you hook up +V to any of them, that has the side-effect of pulling at least A0 HIGH along with it since you have allowed it to float.

Two things to do here:

  1. Don't allow lines to float and expect deterministic behavior. Tie them to +V or to GROUND when you are not using them.
  2. Pull the Ax lines to +V or GND via a resistor like 10K Ohms. This will prevent them from floating and will give you predictable results.
1
  • that was it, adding resistors to pull Ax lines to GND did it. Thanks! – Lucas Fernandez Dec 2 '20 at 19:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.