Arduino Micro giving false and arbitrary high and low values while looping

Hey Guys.

Tringing to get two gpio inputs read using a Arduino Micro.

However when running this little debugging sketch below, my output on the serial command for for the pins is arbitrarily switching even nothing is connected to the board. Also both pins outputs are in synch with each other.

while reading the documentation it said

“If the pin isn’t connected to anything, digitalRead() can return either HIGH or LOW (and this can change randomly).”

If that is the case how are we able to connect digitla pins as input gpio and wait for high and low triggers from outside sensors

This is the outputs i am getting on the serial monitor…

00
00
00
00
01
11
11
11
11
11
11
00
00

Really stumped…

const int ONN = 4;    
const int OFF = 5;       

void setup()                       
{
  Serial.begin(9600);            
  pinMode(ONN, INPUT);
  pinMode(OFF, INPUT);

}

void loop()                        
{

  delay(250);                    
  int Digi4 = digitalRead(ONN);
  int Digi5 = digitalRead(OFF);

  Serial.print(Digi4);
  Serial.println(Digi5);
}

Really stumped...

Google "floating pin" and quit being stumped.

great. now i understand digital pins are senstive to interfence.

so i added this code to my initial set up.

void setup()                       
{
  Serial.begin(9600);            
  pinMode(ONN, INPUT);
  digitalWrite(ONN, LOW); 
  pinMode(OFF, INPUT);
  digitalWrite(OFF, LOW); 

}

but the outputs are still starting as 11 11 11 11

is it becuase i am using digital should i be using analog. My attached sensor is giving GPIO outputs, which are digital no????

so i added this code to my initial set up.

You turned the internal pullup resistors off. Why?

My attached sensor is giving GPIO outputs, which are digital no?

Typically, yes. But, I'm almost certain that you didn't open a Mouser catalog and order a "sensor". I'm almost positive that you ordered a very specific kind of sensor. So, quit playing games and tell us what kind of sensor it is and how it is actually wired.

With the pull ups off the pins are still floating. Change those to HIGH or use INPUT_PULLUP in your pinMode calls and the pins will read reliably HIGH until your switch or whatever makes and connects them to ground. A pressed button will read LOW and an unpressed button will read HIGH.

Hi,

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Tom.... :)

hi guys sorry no games.

it was not a sensor just a bjt transistor switch circuit designed to act like a polarity detector, giving two GPIO outputs.

Hi,

hi guys sorry no games.

it was not a sensor just a bjt transistor switch circuit designed to act like a polarity detector, giving two GPIO outputs.

We don't play games, have you solved your problem?

If not can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

So we can give you more accurate answers/suggestions.

Tom..... :)