1

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()
  • 1
    Add a 10Kohm resistor between each GPIO pin you're using and a GND pin. – Dougie Feb 1 at 18:23
  • Connecting 5V to a GPIO will destroy the Pi – Milliways Feb 1 at 22:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.