athome42
Posts: 10
Joined: Tue Dec 25, 2012 7:02 pm
Location: Reading UK
Contact: Website

Controlling GPIO in using GPIO out

Sun Feb 10, 2013 2:17 pm

Hi,

OK very new at this but I'm failing to understand something very basic. I have made GPIO pin 22 as output and GPIO pin12 as input. Pin 22 is directly linked , hardwired, to pin 12 and with a voltmeter +ve connected via the link and connected 0V to ground.

The Python code I'm using is:-


#!/usr/bin/env python
import RPi.GPIO as GPIO
import time
#GPIO.cleanup()
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)
GPIO.setup(22, GPIO.OUT)
GPIO.setup(12, GPIO.IN)
input_value = GPIO.input(12)
a = 0
print ('hi'), a
while a < 5:

a = a + 1
input_value = 0
GPIO.output(22, True)
print ("Output high")
time.sleep(1)
if input_value != True:
print ("Input high")
time.sleep(2)
print ('hi'), a
GPIO.output(22, False)
print ("Output low")
if input_value == False:
print ("Input low")
time.sleep(2)

GPIO.cleanup()

and this is the output:-

>>>
hi 0
Output high
Input high
hi 1
Output low
Input low
Output high
Input high
hi 2
Output low
Input low
Output high
Input high
hi 3
Output low
Input low
Output high
Input high
hi 4
Output low
Input low
Output high
Input high
hi 5
Output low
Input low
>>>

Now what I don't understand is why if I use this:-

GPIO.output(22, True)
print ("Output high")
time.sleep(1)
if input_value == True:
print ("Input high")
the input is not reported as being high whereas using the same code but substituting True with False, it correctly reports output and input low.

If I change the above code the if input_value != True it reports correctly but the logic just doesn't stack up.

So what's the point I am missing here?
Al - http://athome42.com

User avatar
BirchJD
Posts: 159
Joined: Tue Nov 13, 2012 7:43 pm

Re: Controlling GPIO in using GPIO out

Sun Feb 10, 2013 8:15 pm

I haven't used Python so I can't really help with the programming.

However I would recommend using a 1K resistor if you are driving logic from a GPIO pin. The reason for this is that when I am experimenting I know I sometimes make mistakes. So to protect yourself from acidentally making both GPIO pins outputs at the same time with different logic levels, the resistor would provide a level of protection in some circumstances. For my projects I do this anyway as when the Raspberry Pi starts up I can not be sure what the state of the IO pins may be.
http://www.newsdownload.co.uk/

User avatar
bgreat
Posts: 235
Joined: Mon Jan 23, 2012 2:09 pm

Re: Controlling GPIO in using GPIO out

Sun Feb 10, 2013 8:50 pm

You need to read the input value before testing...

Code: Select all

GPIO.output(22, True)
print ("Output high")
time.sleep(1)
input_value = GPIO.input(12)
if input_value == True:
  print ("Input high")
else:
  print ("Input low")
The "=" does not assign a functionality to input_value, it executes the "GPIO.input(12)" function and assigns the return value to input_value at that point in time (code execution). If you want to read the current status of a pin, you must re-execute the "GPIO.input(12)" function call at the point in time (code execution) that you are interested in the value.

Also, please post your code within "Code" tags... with Python it is critical to preserve the indenting. Just select the block of code, then click on the "Code" button above the editor box.

Enjoy!
Bill

athome42
Posts: 10
Joined: Tue Dec 25, 2012 7:02 pm
Location: Reading UK
Contact: Website

Re: Controlling GPIO in using GPIO out

Tue Feb 12, 2013 11:24 am

Hi,

now I understand that I need to re-check the input pin.

Thanks,

Alan.
Al - http://athome42.com

Return to “Other projects”