Jon
Jon

Reputation: 113

RPi.GPIO pins - check state

I'm using the RPi.GPIO module in Python to turn on and off GPIO pins on the Raspberry Pi 3.

Is there a way to identify whether an output pin is ON or OFF and put that value into a variable that I can use to decide whether the pin is ON and needs to turn OFF, or is OFF and needs to turn ON.

Upvotes: 3

Views: 34746

Answers (1)

A Magoon
A Magoon

Reputation: 1210

from the docs

Doc Snippet

The code below should provide the functionality you are looking for.

import RPi.GPIO as GPIO

channel = 11
GPIO.setmode(GPIO.BCM)  
# Setup your channel
GPIO.setup(channel, GPIO.OUT)
GPIO.output(channel, GPIO.LOW)

# To test the value of a pin use the .input method
channel_is_on = GPIO.input(channel)  # Returns 0 if OFF or 1 if ON

if channel_is_on:
    # Do something here

Upvotes: 15

Related Questions