We attached a wire to pin 40 and another one to 3V3 supply. Also attached an LED+resistor between 5 volt and pin number 38. When the wires do not touch each other, the LED constantly emits and all the values will be 1 on the python interpreter. But when they touch each other, there will be random 0s and 1s, and LED keeps being on and off randomly. I have deleted the "sleep" lines to see what happens, and I saw fast oscillating of 0s and 1s, but no LED light (I think the LED is not fast enough to catch the voltage changes). I wonder if it is a hardware problem, or a code problem?
Here is the code:
import RPi.GPIO as GPIO from time import sleep while True: GPIO.setmode(GPIO.BOARD) GPIO.setup(40,GPIO.IN) GPIO.setup(38,GPIO.OUT) try: if GPIO.input(40): GPIO.output(38,GPIO.HIGH) print(1) sleep(1) GPIO.cleanup() else: GPIO.output(38,GPIO.LOW) print(0) sleep(1) GPIO.cleanup() except KeyboardInterrupt: GPIO.cleanup()