0

As I am new to the Raspberry Pi my question may look simple to people My code is :

import RPi.GPIO as GPIO

GPIO.setmode(GPIO.BOARD)

GPIO.setup(7, GPIO.OUT)

print (GPIO.input(7))

    while(True): 

           if(GPIO.input(7)==GPIO.HIGH): 

                      print("abc")

When I called first PRINT before while loop it says 0 as pin 7 is LOW by default but in WHILE loop it keeps on printing abc. Can someone please explain this behaviour?

  • 1
    Seems to be expected behaviour, but as the program actually does nothing this is meaningless – Milliways Aug 5 at 10:58
  • Am just checking pin status, want to use this concept in my project but this floating behaviour is what i am not able to get, is this expected behaviour????? – Lalit Verma Aug 5 at 11:01
  • Without any details of what is connected YES. – Milliways Aug 5 at 11:03
  • Should i explain or show pic if you can help me out with this? – Lalit Verma Aug 5 at 11:06
  • 1
    The pin state can change due to external factors if it's connected to something. It can "float" to random values if it's not connected. It's not clear what you expect here or why you think that the value of on input pin would keep the same value under these circumstances. – Brick Aug 5 at 13:16
0

The most likely explanation is that the GPIO is floating, i.e. randomly changing between low and high. This will be the case until you explicitly set a voltage at the GPIO.

Perhaps print the current level in the loop.

  • Thankyou for answer but when i tried the same with GPIO 40 (Board) it is giving me normal behaviour unlike pin 7 – Lalit Verma Aug 5 at 10:31
  • Perhaps that GPIO has been set to a voltage. – joan Aug 5 at 10:33
  • I am using only GND and Pin , No pin has set to any voltage pin. In breadboard using pin 40 as output and GND for complting circuit. – Lalit Verma Aug 5 at 10:34
  • 1
    Some GPIO default LOW, some HIGH, some have no defaults. You need to add a clear photo to your question and print the GPIO level in that loop. It is pointless speculating without the facts only you can supply. – joan Aug 5 at 10:37
  • Thankyou Joan your answer on this raspberrypi.org/forums/viewtopic.php?t=62191 helped me instead of ua answer on stackoverflow – Lalit Verma Aug 5 at 14:52

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.