Code: Select all
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
# Variable for the BCM GPIO pin number
READ_1 = 2
LED_1 = 16
# Setup the GPIO pins for output
GPIO.setup(READ_1, GPIO.IN)
GPIO.setup(LED_1, GPIO.OUT)
# Loop to blink our LEDs
try:
while True:
print("On 1")
GPIO.output(LED_1, GPIO.HIGH)
time.sleep(1)
IN1 = GPIO.input(READ_1)
print("On 1 reads : ",IN1)
GPIO.output(LED_1, GPIO.LOW)
time.sleep(1)
IN1 = GPIO.input(READ_1)
print("Off 1 reads : ",IN1)
except KeyboardInterrupt: # Ctrl-C to stop
GPIO.cleanup(LED_1)
pass
The LED outputs work fine, but the button value has had issue.
The First gpiozero button issue with error "Failed to add edge detection"
is fixed thanks to : bcroston commented on Nov 3, 2018
Now the code is the issue.
I do not want to wait, just read the current state, so I think the correct usage is:
button.value
Using the button as a switch from GND to pin2 works.
Using the button as a LED output pin sense stay FALSE with the output turning on an LED through a resistor?
I tried pull_up=True/False with no difference?
Code: Select all
from gpiozero import LED, Button
from time import sleep
# Variable for the BCM GPIO pin number
button = Button(2)
LED_1 = LED(16)
# Loop to blink our LEDs
try:
while True:
print("On 1")
LED_1.on()
sleep(1)
read1 = button.value
print("On 1 read ",read1)
print("Off 1")
LED_1.off()
sleep(1)
read1 = button.value
print("Off 1 read ",read1)
except KeyboardInterrupt: # Ctrl-C to stop
LED_1.off()
print("Clean up done")
pass