Floating Pins, Pull-Up Resistors and Arduino

Welcome to today’s tutorial, where we delve into the essential topic of ‘floating pins’ in Arduino. A ‘floating pin’ is a frequent concern in electronics, impacting the reliability of your circuits.

This guide will provide a clear understanding of ‘floating pins’ and how implementing pull-up resistors can effectively address this issue, ensuring stable and precise Arduino projects.

Usually, one might think, if something’s floating, it’s a good thing. For instance, if a ship crashes, it floats in the ocean, and that’s better than sinking, right?

However, with electronics, that’s not the case. Consider a hairdryer in the tub; that doesn’t mix well. The bottom line: floating pins are detrimental.

This tutorial aims to explain why they are problematic and what precisely constitutes a floating pin.

Bathtub with a hair dryer representing how floating pins on Arduino is hazardous

Let’s float a pin!

Let’s establish a reference for this discussion. Sometimes, when working with Arduino, you may use digital inputs such as push buttons or sensors. These inputs typically have two states—on or off.

For example, a water detector indicates the presence or absence of water. You connect these sensors or buttons to a digital pin on the Arduino, and you use the digitalRead function to read the voltage from that pin and determine whether the pin is high or low.

Programming Electronics Academy members, use the coding challenges in the Bread and Butter: I/O section of the Arduino Course for Absolute Beginners to drive home these basic coding skills.

Not a member yet?  Sign up here.

Let’s use a simple push-button circuit to understand this floating pin concept better. Connect an Arduino board on a breadboard with a push button. One side of the button connects to ground, and the other side connects to digital pin 2.

Push button Arduino circuit

You monitor the voltage using the digitalRead function on digital pin 2. In this setup, when you press the button, digital pin 2 reads a low voltage (ground), and it returns a low value.

In the program, you use an if statement to check if digital pin 2 is low and then proceed with the desired action, such as turning on an LED.

However, the critical question is: what occurs at pin 2 when you’re not pressing the button? Let’s investigate this using the Arduino IDE.

Floating Pins – What the Serial Monitor shows

In the Arduino IDE, a simple sketch is created. First, pin 2 is designated as the input pin. Then, in the setup function, the mode of that pin is set as input, as discussed earlier, and serial communication is initiated. If some of this is unclear, you can refer to other resources for more details.

Now, let’s move on to the loop. First, a digitalRead is performed on that pin to determine its voltage. To do this, a variable named ‘sensorValue’ is declared and initialized to store the output of the digitalRead function, which will be either high (1) or low (0). Next, the serial print function is used to display this value on the serial monitor window.

//The input pin
int Pin_Input = 2;

void setup() {
  // put your setup code here, to run once:
  pinMode(Pin_Input, INPUT);

  Serial.begin(9600);

}  //close setup

void loop() {

  // read the voltage at the Input Pin
  int Sensor_Value = digitalRead(Pin_Input);

  //display the value at the sensor
  Serial.println(Sensor_Value);

}  // close loop

Now, observe the serial monitor; it displays ones and zeros in a seemingly random pattern. It appears as noise. Turning off auto-scroll allows for a closer examination. This demonstrates that pin 2 has no clear function when the button is not pressed; it’s in a floating state. Is this a problem? Does it matter? Yes, it does matter. Why? Let’s continue with the program and circuit setup to see how the LED performs.

Serial Monitor showing floating 1s and 0s from Arduino code

Floating Pins – What the Serial Monitor shows

To incorporate an if-else statement into the code, let’s begin with an explanation. The if-statement checks whether the sensor value equals “low.” If it does, instruct the Arduino to set digital pin 13 to “high.”

Most Arduino boards feature a built-in LED on pin 13. In this example, we use the Arduino UNO, which also has this LED. So, when the sensor value is low, the LED turns on.

This may seem counterintuitive at first, but let’s recall the circuit: when the button presses, pin 2 connects to ground, resulting in a voltage of 0 volts. In digital terms, 0 volts represents “low.”

Now, lets delve into the code. Assign the sensor value based on the output of digital read. When you press the button, digital read returns a value of 0, setting sensor value to 0.

Next, check if sensor value equals 0, indicating it’s low. If that’s the case, turn on the LED by setting digital pin 13 to “high.” If sensor value is anything else, turn it off.

So, the purpose becomes clear: pressing the button turns the light on, releasing it turns the light off. Before uploading the code, remember to set digital pin 13 as an output. Now, proceed with the upload.

//The input pin
int Pin_Input = 2;

void setup() {
  // put your setup code here, to run once:
  pinMode(Pin_Input, INPUT);

  Serial.begin(9600);

}  //close setup

void loop() {

  // read the voltage at the Input Pin
  int Sensor_Value = digitalRead(Pin_Input);

  //display the value at the sensor
  Serial.println(Sensor_Value);

  //if the button is pressed, turn the LED HIGH
  if (Sensor_Value == LOW) {

    digitalWrite(13, HIGH);


  } else {

    digitalWrite(13, LOW);

  }  //close if/else

}  // close loop

Programming Electronics Academy members, check out the Arduino Course for Absolute Beginners to practice using the Serial Library in your code.

Not a member yet?  Sign up here.

After uploading the code to the board, you may notice that the LED on pin 13 behaves strangely; it appears to pulsate or rapidly blink. This behavior is unexpected. Even when you don’t touch the button, the LED should not illuminate.

However, pressing the button stops the pulsation, making the LED brighter. Releasing the button causes it to start pulsating again. What’s causing this?

To investigate further, return to the serial monitor. Unfortunately, the serial monitor displays the same erratic behavior, showing a stream of ones and zeros.

The issue here lies with the “floating pin.” Regardless of pressing the button, noise interferes with the pin, causing an indeterminate value that incorrectly triggers button presses (resulting in a pulsating LED).

Eliminating floating pins with a Pull-up resistor

To resolve this issue, use a pull-up resistor. This component ties the floating pin to a known voltage, which in this case is 5V. Examine the revised breadboard layout, which includes an external pull-up resistor.

Arduino circuit showing pull up resistor, eliminating the floating pin

In addition to connecting the right side of the push button to pin 2, add a pull-up resistor connected to 5 volts. This resistor ensures that, when you’re not pressing the button, pin 2 reads a stable high signal, now connecting to 5V through the 10k resistor.

Returning to the Arduino IDE, you can see that the serial monitor consistently displays “1,” indicating that the pin is no longer floating. Pressing the button turns on the LED, and releasing it turns off the LED. This solution eliminates false button presses caused by a floating pin.

Programming Electronics Academy members, check the Basic Electronics course to gain an intuition about current and voltage in simple circuits.

Not a member yet?  Sign up here.

Let’s reiterate this main point: when you’re not pushing the button, the only closed circuit consists of the Uno’s 5V pin, the 10k resistor, and pin 2. This is why the serial monitor displays a ‘1.’

However, when you push the button, there are two potential closed circuits: the one mentioned earlier (with high resistance) and another one from ground, through the button, to pin 2 (with low resistance).

installing Arduino libraries

Installing Arduino Libraries | Beginners Guide

IoT sewage project

Pumping poo! An IoT sewage project

ESP32 webOTA updates

How to update ESP32 firmware using web OTA [Guide + Code]

error message Brackets Thumbnail V1

expected declaration before ‘}’ token [SOLVED]

Compilation SOLVED | 1

Compilation error: expected ‘;’ before [SOLVED]

Learn how to structure your code

14 Comments

  1. Lionel on May 29, 2015 at 1:55 pm

    Great video. This was one of the first problems i had with arduino and i couldn’t understand why my circuits weren’t working. It was very frustrating and i think most users will have come across it, but no-one tells you about it. You just get resistors placed mysteriously in the examples, and once you step out on your own you get nowhere. So good on you.

    Now i seem to remember i resolved by putting a resistor in the circuit in series with the button (in this example) which seemed to calm the noise. Could that be right?

    All the best,
    Lionel.

    • MICHAEL JAMES on June 6, 2015 at 9:58 pm

      Thanks Lionel! A resistor pulling the Pin to ground would have definitely done the job!

      • Joe Telford on August 14, 2020 at 4:57 pm

        In the example the switch is connected to ground. Are you saying we could replace that wire with a resistor? What value would you recommend?

        • Michael James on August 14, 2020 at 7:39 pm

          That’s correct – something around 10K is pretty common for the pullup/pulldown resistor value.

          • Jusitnas Petkauskas on September 17, 2021 at 10:50 pm

            Do you think 180 ohm would be enough?



          • Michael James on September 20, 2021 at 9:01 pm

            I don’t think it would be ideal – I think more like 3k-10K ohms for a pull up resistor would be good.



  2. James W Pyle III on March 24, 2020 at 12:16 pm

    That was very informative. I know I have seen a formula to use with LED’s for the resister value, but how do you know the right value of the resister to use for something like this?

    James

    • Joe Telford on August 14, 2020 at 4:47 pm

      Typically you just want to have the voltage at 5v with minimal current. a 10K resistor should work and only draw up to 0.5mA of current. If you need less current then try higher values like 22K . Ive had success with 33K resistor but unless power is a big issue I stick with 10K.

  3. Dr. Gary S. Bahret on March 27, 2020 at 8:12 pm

    Resistor looks like a 1k not 10k.

    • Clair Smyers on August 14, 2020 at 9:55 pm

      I sometimes have trouble with the red and orange on these new resistors. I’m used to the old carbon composition resistors but, this looks like brown, black, orange to me. that would be 10K. Sometimes I have to use a meter to make sure they are just that Gary. So I see what you mean.

  4. Sarat on May 1, 2020 at 2:16 am

    What happens if I connect it directly to 5v pin instead of through a resistor?

    • Joe Telford on August 14, 2020 at 4:51 pm

      Bang! not a good idea for 2 reasons:
      1. because when you put 5V direct to a pin it could compromise the Arduino as it can draw a lot of current into the pin.
      2. because when you press the button the 5v and the 0v will be connected to create a short circuit. This could damage the power supply and the arduino may no longer have enough power to operate.

  5. Peter on July 26, 2021 at 9:50 pm

    Hi,
    I tested your code on my Arduino Uno board with Arduino IDE 1.8.15 and I don’t get the floating state like your example. I got solid zeros.

    int pin_input = 2;

    void setup() {
    // put your setup code here, to run once:
    Serial.begin(9600);
    pinMode(pin_input, INPUT);
    }

    void loop() {
    // put your main code here, to run repeatedly:
    int sensor_value = digitalRead(pin_input);
    Serial.println(sensor_value);
    }

    • Steven J Greenfield AE7HD on November 13, 2023 at 11:30 am

      It depends on a lot of things. Humidity, board layout, leftover flux or schmutz on the board, etc. You could wipe your finger across the PCB and get a totally different result.

Leave a Comment