1

I have a simple code that read the input from pin 17 (BCM).

import RPi.GPIO as GPIO
from time import sleep

GPIO.setmode(GPIO.BCM)
GPIO.setup(17, GPIO.IN)

try:
  while True:
    print(GPIO.input(17))
    sleep(2)
except KeyboardInterrupt:
  GPIO.cleanup()

The case is when I don't enter a power, the value is 0 and when the power is entered, the value is 1.

But when I don't enter power at all, the value suddenly changes from 0 to 1, and 1 becomes 0 automatically repeated.

1

If the value floats between low and high that is because the GPIO is floating. Floating means the GPIO has not been set high (by connecting it to 3V3) or low (by connecting it to ground).

That is why a pull-up or pull-down is often connected to a GPIO - so that it is in a known fixed state when not overridden by a more powerful voltage.

E.g. see https://embeddedartistry.com/blog/2018/6/4/demystifying-microcontroller-gpio-settings

  • I don't know about this, I'm newbie for this thing. Thanks for your answer – willydwi77 Jul 20 at 14:37
  • if it worked the accept his answer – Allaw Hussein Aug 19 at 18:11

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.