0

I was trying to see whether my board operates as expected with random readings on floating pins. Oddly it does not. The following code produces all HIGH readings:

#define PIN_MISO 12

void setup() {
  pinMode(PIN_MISO, INPUT);
  Serial.begin(9600);
}

void loop() {
  uint16_t data = 0;
  Serial.print("MISO digitalRead: ");
  data = digitalRead(PIN_MISO);
  Serial.println(data, BIN);
  delay(300);
}

MISO digitalRead: 1
MISO digitalRead: 1
MISO digitalRead: 1
MISO digitalRead: 1
MISO digitalRead: 1
MISO digitalRead: 1
MISO digitalRead: 1
MISO digitalRead: 1
MISO digitalRead: 1
MISO digitalRead: 1
MISO digitalRead: 1
MISO digitalRead: 1
MISO digitalRead: 1
MISO digitalRead: 1
MISO digitalRead: 1
MISO digitalRead: 1
MISO digitalRead: 1
MISO digitalRead: 1
MISO digitalRead: 1
MISO digitalRead: 1
MISO digitalRead: 1
MISO digitalRead: 1
MISO digitalRead: 1
MISO digitalRead: 1
MISO digitalRead: 1
MISO digitalRead: 1
MISO digitalRead: 1
MISO digitalRead: 1
MISO digitalRead: 1
MISO digitalRead: 1

I tried grounding pin 12 and after a few seconds the readings all came back LOW as expected. However leaving it floating produces all HIGH instead of random readings.

I tried the same program for pins 3 and then A3 with more weird output: all LOW.

What am I missing here?

3
  • 1
    If it's floating, then anything can influence the reading. Humidity, temperature, nearby pins/traces, a human body nearby (try placing your finger near the pin), manufacturing differences, nearby radio sources (WiFi, BT). But in the end of the day, it doesn't matter, as you should rely on a certain value from floating pins. Just add a pull-up or pull-down resistor. – Gerben Jan 2 '17 at 16:03
  • You are missing longer wires, touch or something noisy nearby (like DC motor) – KIIV Jan 2 '17 at 16:03
  • Why you expect to behave randomly every read? According to Arduino docs, you should be using the [random][1] function to get random numbers, of course this is a pseudo random generator which you have to [randomSeed][2], for this you could use an analogRead to a floating input. [1]: arduino.cc/en/Reference/Random [2]: arduino.cc/en/Reference/RandomSeed – GLM Jan 2 '17 at 16:18
1

Floating means it would generate any value. This could be a result of innumerable factors and it is HIGH in this case! you cannot expect anything other than a HIGH or LOW.

Think of it this way- It's fluctuating in a range of about 900-1000 but the range is still very close to 1023 hence it results in a HIGH.

Some other time it may be near the lower values then it would show LOW.

1
  • While an intermediate level is possible, it may be more probable that the actual voltage is a definite legal "high" but that there is nothing holding it there with any authority - only the vaguest of leakage current, easily overcome by any other driver or source of charge. – Chris Stratton Mar 4 '17 at 16:47
-1

Change:

pinMode(PIN_MISO, INPUT);

to

pinMode(PIN_MISO, INPUT_PULLUP);

This will enable the internal 20k pull-up and you should then always read HIGH is the pin is floating.

Using INPUT does not enable the internal pull-up (Ref: https://www.arduino.cc/en/Reference/pinMode)

2
  • 3
    This does not answer the original question! – jfpoilpret Jan 2 '17 at 20:15
  • Actually, this answer did work for me, the implication being, "you're doing something that's undefined, so there's no point in speculating on what you might read; instead do something like this if you're going to be reading floating pins." – cjs May 3 '17 at 5:26

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.