I have some experience with Arduino but I'm a complete newbie around RPi. I bought a RPi 3 for my thesis case study and recently I've been trying to get the GPIO to work. The output works fine, but I can't get the input to work. Anyway, my code goes like this:
import RPi.GPIO as GPIO prev_input = 0 GPIO.setmode(GPIO.BCM) GPIO.setwarnings(False) GPIO.setup(7, GPIO.OUT) GPIO.setup(3, GPIO.OUT) GPIO.setup(2, GPIO.IN) GPIO.output(3, GPIO.LOW) while True: GPIO.output(7, GPIO.HIGH) input = GPIO.input(2) if((not prev_input) and input): print('Button pressed\n') GPIO.output(3, GPIO.HIGH) else: print('Button released\n') GPIO.output(3, GPIO.LOW) prev_input = input time.sleep(0.05)
So, after many tries I even made some changes, to get the values of input and prev_input when the code is running... And no matter what, both of them are always 1 (except on the first run, on which input = 1 and prev_input = 0) and therefore, I get 'Button Released' every time.
The circuit is very simple. Two LEDs connected to the ground and the respective output on RPi (7 and 3) and a button connected between VCC (3.3V) and GPIO 2. I've tried ignoring the button and connecting the input directly to the ground or VCC, and still nothing changed. The LED connected to GPIO 7 works, as expected, and the other one does not.
There is one thing though, I made a mistake and I'm not sure if I may have damaged my board. I'm used to the GPIOs on Arduino, and so the first time I connected the button to the board I was using 5V VCC from the RPi itself. I read some notes on the web regarding the possibility of wasting your RPi by doing so. That GPIO (0) was not working, so when I found that the correct voltage was 3.3V I immediately changed the input to GPIO 2 and started getting results (incorrect ones, unfortunately).
Has anybody gone through a similar problem? Is it possible that I'm not getting it right because this mistake did ruin my RPi? Thanks in advance guys
As requested, I'm posting a photo of the circuit.
Ok guys, thanks to joan I've realized the incredibly stupid mistake I made. I confused the BCM and BOARD GPIO settings and named the input/outputs all wrong. Changing 7 to 4, 3 to 22 and 2 to 27, the circuit seems to be working almost fine (at least I know that the input is working, roughly).
But now, another issue: when the program runs, if I press the button, the LED lights up for just a moment and then remains off for more 20 turns or so, and then lights up again if the button is pressed. What should I do for a more precise control? AKA, for the LED to light up precisely when the button is pressed (as if I was using a sensor as an input, for instance)? Is there something wrong?
Ok, last one. I think I fixed the problem, I'm posting the results below in case anyone might have a similar problem on the future. I created an additional while loop for the 'button pressed' reading and also, replaced the button on my circuit for a slide switch. Those did the trick. Now, the LED is on and the message is 'Button pressed' while the switch is on. Thanks guys!
import time import RPi.GPIO as GPIO BUTTON=27 OUT1=22 OUT2=4 prev_input=0 GPIO.setmode(GPIO.BCM) GPIO.setwarnings(False) GPIO.setup(OUT1, GPIO.OUT) GPIO.setup(OUT2, GPIO.OUT) GPIO.setup(BUTTON, GPIO.IN) GPIO.output(OUT1, GPIO.LOW) GPIO.output(OUT2, GPIO.HIGH) while True: input = GPIO.input(BUTTON) if prev_input != input: if input: print('Button pressed\n') while input: GPIO.output(OUT1, GPIO.HIGH) input = GPIO.input(BUTTON) else: print('Button released\n') GPIO.output(OUT1, GPIO.LOW) prev_input = input time.sleep(0.05)