14

I've been playing a lot with the Raspberry Pi lately. But I have a problem.

When I set up an input pin to check whether electricity is sent to it or not, it gives me rather unstable answers.

A few observations I've made to clarify my problem:

  1. When I connect the circuit, and electricity flows to the input pin, the value it gives me is consistently TRUE or 1.

  2. When I break the circuit by removing a LED that is a part of it (when the input value should be 0) it gives me random values, shifting between 0 and 1.

  3. When I remove all the cables from my Pi it gives me the consistent value of 0

  4. If i have all the GPIO cables removed it gives me the value of 0, but if I simply touch the input pin with a piece of electricity leading metal, (leading to nothing at the other end) it starts with the random values again. It does not give me random values unless I have something connected to the output pin as well. It does not have to lead anywhere, it just has to touch the output pin. And some other piece of metal has to touch the input pin. No connection at all between them.

I wonder why it starts giving me random values as the Input GPIO pin touches any metal.

And even more intresting, why does it give me consistant 1s or TRUEs when I actually send electricity to it?

3
  • Edit to statement number 4: It does not give me random values unless i have something connected to the outputpin as well. It does not have to lead anywhere, it just has to touch the output pin. And some other piece of metal has to touch the input pin. No connection at all between them
    – Gustaf
    Commented Mar 23, 2014 at 15:24
  • Welcome to Raspberry Pi Stack Exchange! You do know you can edit your question to fix change that info instead of commenting it? Also, pop into the chat from time to time. We always love new company and it'll help show that this site is useful, growing, and has an involved user base. Thanks! Commented Mar 23, 2014 at 15:45
  • 1
    This is because the pins are floating. More info at electronics.stackexchange.com/a/35705
    – Gerben
    Commented Mar 23, 2014 at 17:40

1 Answer 1

18

The problem

When I connect the circuit, and electricity flows to the input pin, the value it gives me is consistently TRUE or 1.

True, but what happens when ground is connected to the GPIO input pin? The value it would give would always be false.

  • When power is connected to the input pin: The input pin detects a 1 (true).
  • When ground is connected to the input pin: The input pin detects a 0 (false).
  • When nothing is connected to the input pin: The input pin gets confused.

When nothing is connected the pin is in a "floating" state, and the output is not predictable. it is in an undefined state, it's neither 0 nor 1, and the detected value can change strangely, hence your random results. In electronics this is known as the third state.

Some more detail from a Wikipedia article:

In digital circuits, a high impedance (also known as hi-Z, tri-stated, or floating) output is not being driven to any defined logic level by the output circuit. The signal is neither driven to a logical high nor low level; this third condition leads to the description "tri-stated". Such a signal can be seen as an open circuit (or "floating" wire).

The general solution

The solution is a pull-down resistor. Think of this as an extra component which does nothing when the line is getting a 0 or 1. But when the line is idle, it suddenly kicks into action and gives a 0.

The Raspberry Pi solution

Luckily for you, the Rpi has a built in pull-down, you need to activate it programmatically. Here is a python script for that:

import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setup(23, GPIO.IN, pull_up_down = GPIO.PUD_DOWN)
//Now your input is stable.

Side note

Don't connect the power directly to the pin as it would be dangerous! Make sure there is enough resistance in the circuit. Also, never connect the 5V power directly to the GPIO as it only accepts 3.3V.

2
  • Do you know the equivalent C# code when running on Windows 10 IoT core platform? Commented Mar 11, 2017 at 13:01
  • This answer is golden! Thx
    – domih
    Commented Jun 17, 2017 at 12:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.