3

I am having trouble with the GPIO pins on my two raspberry pis. I am working on a project that someone has worked on previously. The person set the same code for the button for both pis. Both pis utilize the code:

import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
GPIO.setup(21, GPIO.OUT, initial=1)
GPIO.setup(16, GPIO.IN, pull_up_down = GPIO.PUD_UP)
time.sleep(2)

while True:
    if(GPIO.input(16) == 0): print "Hello"
GPIO.cleanup()

When I run this code, the pi on top works; however, the pi on the bottom does not. Am i using the correct GPIO pins for the second? What should the GPIO pins on the bottom be?enter image description here

  • 2
    What is the desired behaviour of the bottom Pi ? When you push the button it connects GPIO 16 to ground - so 'GPIO.input(16) == 0' is true and it prints 'Hello' - but you don't have a button connected to the bottom Pi so 'GPIO.input(16) == 0' can never evaluate to true (so it never prints 'Hello') – KennetRunner Jul 21 '16 at 21:47
  • The person working on this project before said that the button could affect both pis. The purpose of the bottom pi is to take pictures as well, same exact thing the top pi is supposed to do. If you notice, the pins are connected between both pis. I started this project last week and am relatively new to rpis. The persom before me did all the work; however, now the button does not work for the bottom pi. The person says that the button had worked in the past for both pis. – Eugene Tian Jul 21 '16 at 22:25
2

You have the button connected to GPIO pin 16 and Ground on the top Pi, and both Pis have GPIO pin 21 connected together. The code you showed is setting GPIO pin 21 as an output (and to HIGH) and setting GPIO 16 to be pulled HIGH by default (on the top Pi). When the button is pressed GPIO pin 16 (on the top Pi) is connected to Ground so the statement:

GPIO.input(16) == 0

is true and 'Hello' is printed.

I'd guess the behaviour you want is for it to set GPIO pin 21 to LOW (instead of printing 'Hello'). That way you are changing the state of the GPIO pin 21 on the bottom Pi (and can have it react).

Change the:

print "Hello"

to:

GPIO.output(21, True)

on the top Pi.

Code for the bottom Pi would look something like this:

import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
GPIO.setup(21, GPIO.IN, pull_up_down = GPIO.PUD_UP)
time.sleep(2)

while True:
    if(GPIO.input(21) == 0): print "Hello"
GPIO.cleanup()
  • So the bottom Pi does not need the code: GPIO.setup(21, GPIO.OUT, initial=1)? – Eugene Tian Jul 22 '16 at 18:22
  • Can you explain how the top and bottom pi communicate with ur code above? I tried it yesterday and the button responded to both pis yay! But today I tried it again, im not sure if i changed something but it is not working again. – Eugene Tian Jul 22 '16 at 18:23
  • The GPIO pin 21 on the bottom Pi need to be set as an INPUT, because it is 'listening' for the top Pi to change from HIGH to LOW. When the top Pi does this it pulls the bottom Pis GPIO pin 21 to ground so GPIO.input(21) == 0 and the bottom Pi does it's stuff. Do NOT set the bottom Pi GPIO pin 21 as OUTPUT - you will have both GPIO pins driving one another in conflict... – KennetRunner Jul 22 '16 at 19:07
  • That looks correct. – KennetRunner Jul 22 '16 at 20:41
  • OK so both pis work. However, I want to loop the code. but only the top pi does that. I want to press the button and it displays 'hello' then when I press it again it says hello again on both pis. So far, I have to restart both pis for the button to display hello. Can someone help? – Eugene Tian Jul 22 '16 at 21:24
0

There should be no reason that the code should not work on the second pi other than system configuration errors. (permissions, wiring, etc.) If you are trying to use GPIO 21 as a one-way signal line, make sure that you are not setting both Pis to output, as that can cause unexpected results as well as possibly damaging the chips.
The GPIO output drivers are far more powerful than the input pullup and pulldown resistors in most devices, and the high drivers can usually exceed the power ratings of the chip's GPIO drivers if you short them to ground by using the output low driver instead of the input pulldown resistor.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.