Floating Inputs

Here's what our alligator clip setup looks like when nothing is connected to the BLUE clip.

You might think that the voltage would be 0V and the digital pin would read LOW. After all, it's not attached to anything. It's attached to nothing. And 0 is like nothing. However, voltages don't exist at a point - they exist between two points. And in the case of the floating input, one of those points is not fixed to any known value. Therefore, the voltage is also unknown.

The end result is that the actual value is unpredictable, which isn't very useful. You can try using the sketch from the previous section and watch for the value to change. However, the following sketch will monitor the pin constantly (very fast) and detect when it changes.

Download: file
///////////////////////////////////////////////////////////////////////////////
// Circuit Playground Digital In - Floating Input
//
// Author: Carter Nelson
// MIT License (https://opensource.org/licenses/MIT)

#include <Adafruit_CircuitPlayground.h>

int initialValue;

///////////////////////////////////////////////////////////////////////////////
void setup() {
  Serial.begin(9600);
  while (!Serial) ;
  
  CircuitPlayground.begin();
  
  pinMode(3, INPUT);

  initialValue = digitalRead(3);

  Serial.print("Initial value = "); Serial.println(initialValue);
  Serial.println("Waiting for that to change...");
}

///////////////////////////////////////////////////////////////////////////////
void loop() {
  int reading = digitalRead(3);
  
  if (reading != initialValue) {
    Serial.print("It changed! value = "); Serial.println(reading);
    while (true) ; // sit here forever
  }
}

With this code loaded and running on the Circuit Playground, open the Serial Monitor.

Tools -> Serial Monitor

Now pick up the BLUE alligator clip and try touching it with your finger. You may need to try touching it several times, but hopefully at some point it will see a change and report it as shown below. Note that your initial value might be 1 - it totally depends on what ever randomness is happening in your test environment.

The fix for this is pretty straight forward. You just set things up so that the digital input, the BLUE alligator clip, is always attached to a known value, in this case either the RED (3.3V) alligator clip or the BLACK (0V) alligator clip. This is called "pulling" the input, as you are forcing it (pulling it) to a specific value. When the input is pulled to 3.3V it is called "pulling up". When the input is pulled to 0V it is called "pulling down". You can go either way.

But wait! Remember we can't touch the RED and BLACK wires directly to each other. So if one of those is already attached to the BLUE clip, how do we ever touch the other one? The answer is to use a resistor - and guess what it's called? Well, a pull up resistor or a pull down resistor, as the case may be.

Let's play with both.

This guide was first published on Feb 26, 2017. It was last updated on Feb 26, 2017. This page (Floating Inputs) was last updated on Jul 17, 2019.