0

I am using two pins of GPIO as input and I want to check whether both are high or low at a time so I want to use a logical AND operation to check the status of both the pins, but I coudnt get that right as I have wrote few lines as below.

import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BOARD)
GPIO.setup(11,GPIO.OUT)
GPIO.setup(12,GPIO.OUT)

GPIO.output(11,1)
GPIO.output(12,1)
time.sleep(1)
GPIO.output(11,0)
GPIO.output(12,0)
while True:
  if(GPIO.output(11) && GPIO.output(12) == 1)
    print"both the leds are ON"
  else:
    print"both the LED's are OFF"

But this is'nt working. So please help me. How can I get this logical operations done? Error I am facing is 'syntax error'.

2

I have taken some liberties with your code. In particular I changed from BOARD mode to BCM mode as it was easier for me to test. I tested by changing the GPIO levels outside the script.

Notes

  1. && is logical and in C etc. Python uses and.
  2. There is no point in changing the LED level twice before the while loop. The while loop will not see those transitions.
  3. You can read the level of a GPIO set as an output.
  4. I added a time.sleep(0.1) in the while loop so other programs get a chance to run.

.

#!/usr/bin/env python

import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BCM)
GPIO.setup(11,GPIO.OUT)
GPIO.setup(12,GPIO.OUT)

GPIO.output(11,1)
GPIO.output(12,0)

while True:
  if GPIO.input(11) == 1 and GPIO.input(12) == 1:
    print("both the leds are ON")
  elif GPIO.input(11) == 0 and GPIO.input(12) == 0:
    print("both the LED's are OFF")
  else:
    print("one LED is ON, one is OFF")

  time.sleep(0.1)
  • thanks @joan it cleared my doubt,i have actually used 'and' and tried but yours worked well – Sharath Vedala Sep 9 '15 at 9:48
  • Hi, i am using pyfirmata to connect to arduino 2560/arduino mega,I need all 53 I/o of arduino for blinkng Led but i can only use upto 13 pins of it.what should i do to access all the I/o pins with my raspberry pi.I have used 'board.digital[14].write(1)'.it is showing error:' index out of range' .Please help me solving this or atleast suggest me how can i use all the pins or the maximum pins.thanks – Sharath Vedala Sep 9 '15 at 9:52
  • @SharathVedala I have no knowledge of firmata or pyfirmata. You probably ought to open a new question and give the relevant details. – joan Sep 9 '15 at 10:10
  • but today i cannot ask a new question.Its ok – Sharath Vedala Sep 9 '15 at 10:29
  • @SharathVedala Someone may answer if they read your comment but a new question (tomorrow?) will be seen by more people. – joan Sep 9 '15 at 11:03
0

I think you want to read an input ? Syntax error was due to because you forgot to put : after your if statement.

Here, you can use this,

import RPi.GPIO as GPIO 
import time

GPIO.setmode(GPIO.BOARD) 
GPIO.setup(11,GPIO.IN) 
GPIO.setup(12,GPIO.IN)

while True: 
    if(GPIO.input(11) == 1 and GPIO.input(12) == 1):
        print"both the leds are ON" 
    else: 
        print"both the LED's are OFF"

For more info: refer to this documentation.

Hope it helps.

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.