If your circuit is behaving weirdly, switching on when you touch a wire or move your hand over the circuit you almost certainly have a floating input. You can solve this problem with a pull-up resistor. Many AVRs have built-in pull-up resistors that you can turn on using code.

In Sketch:

pinMode(2, INPUT_PULLUP);

In BascomAVR:

Config Portb.1 = Input Set Portb.1

There’s a great article at SparkFun about Pull-up Resistors.

    • MapleEngineerOPM
      link
      fedilink
      English
      arrow-up
      2
      ·
      edit-2
      1 year ago

      Sure…I’m going to give it my best shot.

      Computers and MCUs are digital. They think in 0s and 1s. A 0 is normally ground and a 1 is normally the supply voltage. Most of the circuits that I work on are 5V (in reality around 5V but let’s say 5V for arguments sake.) So ground (0V) is 0 and supply voltage 5V is 1. The problem is that the world isn’t digital and electricity isn’t digital. There are an infinite number of other voltages between GND (0V) and 5V. You can have 1.3V or 2.8V or 4.26534V, for example. So…we have some questions to answer. As we leave 0V and head to 5V at what point does the MCU start to believe that the pin is high instead of low? It depends on the MCU and the supply voltage. At some point as the voltage rises the MCU is going to say, “Ok…that’s a high instead of a low.” Another question is, what value does the pin of an MCU have if it isn’t connected to either GND or 5V? There is likely some voltage as a result of the circuitry inside the MCU. Is that above or below the point where the MCU thinks the pin is high?

      So…a floating input is a pin that’s designated as an input but that isn’t connected to anything. The MCU might interpret that voltage as just below the threshold or just above the threshold to decide that the input is high. If you have a wire connected to that pin it might have an induced voltage as a result of the voltage in other wires in the circuit. If you touch that wire or you wave your hand over the circuit or even touch another nearby wire it can change the voltage on that pin so that the MCU interprets it as a high and your circuit does something you’re not expecting it to do.

      That’s a floating input. It isn’t tied high or low, you don’t know what value it actually is, and the MCU might interpret it as either a 0 or a 1 and it might change without you intending for it to.

      So…you always want your logic so that a button being pressed pulls the MCU pin to ground. That’s rule number 1. That means that you always want the MCU to interpret the pin as being high until you press the button. In order to do that you need a pull-up resistor which pulls the pin to 5V weakly. That’s rule number 2. You can use a 10K resistor or a 4.7K resistor to do that. 10K is normally fine. That pulls the pin to 5V so that the MCU interprets is as high when the button isn’t pressed but pressing the button pulls the pin strongly to ground and the MCU knows that you’ve pressed the button.

      I hope that helps and that it was ELI5 enough.