GPIO input toggle value

General project help for Adafruit customers

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
ccesario
 
Posts: 8
Joined: Sat Dec 13, 2014 7:21 pm

GPIO input toggle value

Post by ccesario »

Hello all,

Does somebody could explain why the GPIO INPUT toggle values ? HIGH/LOW.

I don't have nothing connected on GPIO PIN 17 and see the code result.

Code: Select all

$ python readGpio.py

Port 17 is 0/LOW/False - LED OFF
Port 17 is 0/LOW/False - LED OFF
Port 17 is 0/LOW/False - LED OFF
Port 17 is 0/LOW/False - LED OFF
Port 17 is 0/LOW/False - LED OFF
Port 17 is 0/LOW/False - LED OFF
Port 17 is 1/HIGH/True - LED ON
Port 17 is 1/HIGH/True - LED ON
Port 17 is 1/HIGH/True - LED ON
Port 17 is 1/HIGH/True - LED ON
Port 17 is 1/HIGH/True - LED ON
Port 17 is 1/HIGH/True - LED ON
Port 17 is 1/HIGH/True - LED ON
Port 17 is 1/HIGH/True - LED ON
Port 17 is 1/HIGH/True - LED ON
Port 17 is 1/HIGH/True - LED ON
Port 17 is 0/LOW/False - LED OFF
Port 17 is 0/LOW/False - LED OFF
Port 17 is 0/LOW/False - LED OFF
Port 17 is 0/LOW/False - LED OFF
Port 17 is 0/LOW/False - LED OFF
Port 17 is 0/LOW/False - LED OFF
Port 17 is 0/LOW/False - LED OFF
Port 17 is 0/LOW/False - LED OFF
Port 17 is 0/LOW/False - LED OFF
Port 17 is 0/LOW/False - LED OFF
Port 17 is 0/LOW/False - LED OFF
Port 17 is 0/LOW/False - LED OFF
Port 17 is 0/LOW/False - LED OFF
Port 17 is 0/LOW/False - LED OFF
Port 17 is 0/LOW/False - LED OFF
Port 17 is 0/LOW/False - LED OFF
Port 17 is 0/LOW/False - LED OFF
Port 17 is 0/LOW/False - LED OFF
Port 17 is 0/LOW/False - LED OFF
Port 17 is 0/LOW/False - LED OFF
Port 17 is 1/HIGH/True - LED ON
Port 17 is 1/HIGH/True - LED ON
Port 17 is 1/HIGH/True - LED ON
Port 17 is 1/HIGH/True - LED ON
Port 17 is 1/HIGH/True - LED ON
Port 17 is 1/HIGH/True - LED ON
Port 17 is 1/HIGH/True - LED ON
Port 17 is 1/HIGH/True - LED ON
Port 17 is 1/HIGH/True - LED ON
Port 17 is 1/HIGH/True - LED ON
Port 17 is 1/HIGH/True - LED ON
Port 17 is 1/HIGH/True - LED ON
Port 17 is 1/HIGH/True - LED ON
Port 17 is 1/HIGH/True - LED ON
Port 17 is 1/HIGH/True - LED ON
Port 17 is 1/HIGH/True - LED ON
Port 17 is 1/HIGH/True - LED ON
Port 17 is 1/HIGH/True - LED ON
Port 17 is 1/HIGH/True - LED ON
Port 17 is 1/HIGH/True - LED ON
Port 17 is 1/HIGH/True - LED ON
Port 17 is 1/HIGH/True - LED ON
Port 17 is 1/HIGH/True - LED ON
Port 17 is 1/HIGH/True - LED ON
Port 17 is 1/HIGH/True - LED ON
Port 17 is 1/HIGH/True - LED ON
Port 17 is 1/HIGH/True - LED ON
Port 17 is 1/HIGH/True - LED ON
Port 17 is 0/LOW/False - LED OFF
Port 17 is 0/LOW/False - LED OFF
Port 17 is 0/LOW/False - LED OFF
Port 17 is 0/LOW/False - LED OFF
Port 17 is 0/LOW/False - LED OFF
Port 17 is 0/LOW/False - LED OFF
Port 17 is 0/LOW/False - LED OFF
Port 17 is 0/LOW/False - LED OFF
Port 17 is 0/LOW/False - LED OFF
Port 17 is 0/LOW/False - LED OFF
Port 17 is 0/LOW/False - LED OFF
Port 17 is 0/LOW/False - LED OFF
Port 17 is 0/LOW/False - LED OFF
Port 17 is 0/LOW/False - LED OFF
Port 17 is 0/LOW/False - LED OFF
Port 17 is 0/LOW/False - LED OFF
Port 17 is 0/LOW/False - LED OFF
Port 17 is 0/LOW/False - LED OFF
Port 17 is 0/LOW/False - LED OFF
Port 17 is 0/LOW/False - LED OFF
Port 17 is 0/LOW/False - LED OFF
Port 17 is 0/LOW/False - LED OFF
Port 17 is 0/LOW/False - LED OFF
Port 17 is 0/LOW/False - LED OFF
Port 17 is 0/LOW/False - LED OFF
Port 17 is 0/LOW/False - LED OFF
Port 17 is 1/HIGH/True - LED ON
Port 17 is 1/HIGH/True - LED ON
Port 17 is 1/HIGH/True - LED ON
Port 17 is 1/HIGH/True - LED ON
Port 17 is 1/HIGH/True - LED ON
Port 17 is 1/HIGH/True - LED ON
Port 17 is 1/HIGH/True - LED ON
Port 17 is 1/HIGH/True - LED ON
Port 17 is 1/HIGH/True - LED ON
Port 17 is 1/HIGH/True - LED ON
Port 17 is 1/HIGH/True - LED ON
Port 17 is 1/HIGH/True - LED ON
Port 17 is 1/HIGH/True - LED ON
Port 17 is 1/HIGH/True - LED ON
Port 17 is 1/HIGH/True - LED ON
Port 17 is 1/HIGH/True - LED ON
This is the simple code

readGpio.py

Code: Select all

import RPi.GPIO as GPIO
from time import sleep
GPIO.setmode(GPIO.BCM)
GPIO.setup(17, GPIO.IN)

try:
    while True:
        if GPIO.input(17):
            print "Port 17 is 1/HIGH/True - LED ON"
        else:
            print "Port 17 is 0/LOW/False - LED OFF"
        sleep(0.1)
finally:
    GPIO.cleanup()
Does somebody have idea about this ?

Thank you

Carlos

User avatar
zener
 
Posts: 4567
Joined: Sat Feb 21, 2009 2:38 am

Re: GPIO input toggle value

Post by zener »

If nothing is connected then the input could be "floating". Connect it to high or low and see what it does.

User avatar
ccesario
 
Posts: 8
Joined: Sat Dec 13, 2014 7:21 pm

Re: GPIO input toggle value

Post by ccesario »

Hello,

As I said, I have nothing connected in this PIN, I have only a WIRE JUMPER plugged into this PIN and the other point this WIRE JUMPER does not connected.

This is the problem.

Thanks again.

Carlos
Attachments
GPIO.png
GPIO.png (179.78 KiB) Viewed 394 times

User avatar
adafruit_support_mike
 
Posts: 67446
Joined: Thu Feb 11, 2010 2:51 pm

Re: GPIO input toggle value

Post by adafruit_support_mike »

That would do it..

Any long piece of wire is an antenna, and we live in a world saturated with electromagnetic noise.

The wire is probably picking up signals from the AC power running through your walls, but if you have flourescent lights within about 10', those will add significant noise too.

User avatar
ccesario
 
Posts: 8
Joined: Sat Dec 13, 2014 7:21 pm

Re: GPIO input toggle value

Post by ccesario »

Hello,
My Raspberry PI is near to my Wireless Router, can be this the problem.

Is there anything to prevent this ? Besides put each one so far :)

Thanks a lot

Carlos

User avatar
adafruit_support_bill
 
Posts: 88092
Joined: Sat Feb 07, 2009 10:11 am

Re: GPIO input toggle value

Post by adafruit_support_bill »

Is there anything to prevent this ?
Yes. If you want it to read LOW, connect it to ground.
If you want it to read HIGH, connect it to 3.3v.

If you leave it unconnected it will continue to float and the value will change randomly.

User avatar
ccesario
 
Posts: 8
Joined: Sat Dec 13, 2014 7:21 pm

Re: GPIO input toggle value

Post by ccesario »

Hi, I connected it (GPIO17) in my optcoupler test circuit (attached in this post) to identify it when the LEd is ON or OFF, but the GPIO continue "toggling" values.


There is something wrong ?!

Thanks again

Carlos
Attachments
OptocouplerRPI.jpg
OptocouplerRPI.jpg (859.16 KiB) Viewed 364 times

User avatar
adafruit_support_bill
 
Posts: 88092
Joined: Sat Feb 07, 2009 10:11 am

Re: GPIO input toggle value

Post by adafruit_support_bill »

Your opto-isolator is isolating the Pi from the output side of the circuit, so the GPIO pin is still 'floating'.
In order to measure a signal, you need a common ground reference. There is no ground connection between your Raspberry Pi and the output side of your circuit.

User avatar
ccesario
 
Posts: 8
Joined: Sat Dec 13, 2014 7:21 pm

Re: GPIO input toggle value

Post by ccesario »

Oh, I'ĺl change it and test again.

Thank you by explanation, I'll post the results there.

Carlos

User avatar
ccesario
 
Posts: 8
Joined: Sat Dec 13, 2014 7:21 pm

Re: GPIO input toggle value

Post by ccesario »

Hello,

Now it is attached the correct circuit.
It is working as expected!

A bit detail is that code is working with inverse logic. When led is ON, it is identified as LOW and when led is OFF it is identified as HIGH.

If someone had an explantion for this ... I appreciate it!

Thank you

Carlos
Attachments
optocoupler.png
optocoupler.png (494.42 KiB) Viewed 304 times

User avatar
adafruit_support_bill
 
Posts: 88092
Joined: Sat Feb 07, 2009 10:11 am

Re: GPIO input toggle value

Post by adafruit_support_bill »

Your optio-isolator is switching the "low-side" of the circuit. When the opto-isolator output is off, the resistor pulls the GPIO input HIGH and the LED is OFF. When the optio-isolator turns on, it connects the GPIO pin and the cathode of the LED to GND and the LED turns ON.

User avatar
ccesario
 
Posts: 8
Joined: Sat Dec 13, 2014 7:21 pm

Re: GPIO input toggle value

Post by ccesario »

Well, so this is OK. This is a expected behaivor.. right?

Or this circuit can be changed to HIGH to be HIGH and LOW to be LOW ?

Thanks a lot by help and explanations.

Carlos

User avatar
adafruit_support_bill
 
Posts: 88092
Joined: Sat Feb 07, 2009 10:11 am

Re: GPIO input toggle value

Post by adafruit_support_bill »

That is expected behavior for this circuit.

Locked
Please be positive and constructive with your questions and comments.

Return to “General Project help”