snielsen
Posts: 12
Joined: Fri Jan 12, 2018 1:06 pm

GPIOoutput from Rpi to another Rpi GPIOinput

Fri Jan 19, 2018 3:40 pm

Hello, Im trying to use this script to activate playback on a second Rpi. When button is pressed playback starts on the main Rpi, and the GPIO.output14 goes to a GPIO on the second Rpi. The problem is that the sound on the second Rpi starts without pressing button. I expected the GPIO 14 to be set at 0 untill button is pressed ... I havent included a resistor in the curcuit yet (Iget the in my mailbox soon, that might be the problem?)

I am used to analogue tape recorders and relays, python language is like Chinese to me and Im setting up an solo exhibition with this program in a sound installation soon ... :? :? :?

Code: Select all

import RPi.GPIO as GPIO
GPIO.setwarnings(False)
from time import sleep     
GPIO.setmode(GPIO.BCM)           
GPIO.setup(14, GPIO.OUT)
GPIO.output(14, 0)

import simpleaudio as sa
import time

from gpiozero import Button
button = Button(17)

button.when_pressed = GPIO.output(14, 1)
button.when_released = GPIO.output(14, 0)
  
while True:            
    if button.is_pressed:
        wave_obj = sa.WaveObject.from_wave_file('samples/test3.wav')
        play_obj = wave_obj.play()
        play_obj.wait_done()


    else:  
        GPIO.output(14, 0)         
        time.sleep (0.1)

pcmanbob
Posts: 4338
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: GPIOoutput from Rpi to another Rpi GPIOinput

Fri Jan 19, 2018 4:31 pm

When trying to solve problems like this its good to have a visual indication of what's happening,

so try putting an LED connected in series with a 470 ohm resistor between gpio 14 and ground, then you can see if the output is actually doing what you expect.

and if you are going to use gpio 14 you need to disable the serial interface that normally uses that pin or just use a different gpio.
Remember we want information.......................no information no help
The use of crystal balls & mind reading is not supported

User avatar
Burngate
Posts: 5436
Joined: Thu Sep 29, 2011 4:34 pm
Location: Berkshire UK
Contact: Website

Re: GPIOoutput from Rpi to another Rpi GPIOinput

Fri Jan 19, 2018 6:04 pm

... and the GPIO.output14 goes to a GPIO on the second Rpi
Since you're used to analogue tape recorders, you will of course have connected the grounds of the Pis together ...

boyoh
Posts: 1167
Joined: Fri Nov 23, 2012 3:30 pm
Location: Selby. North Yorkshire .UK

Re: GPIOoutput from Rpi to another Rpi GPIOinput

Fri Jan 19, 2018 11:16 pm

Sugest switching the O/P between I/P of the Pi, you use a Opto Isolator
No need to common the grounds, This will give electrical and noise isolation.
BoyOh ( Selby, North Yorkshire.UK)
Some Times Right Some Times Wrong

User avatar
Paeryn
Posts: 2142
Joined: Wed Nov 23, 2011 1:10 am
Location: Sheffield, England

Re: GPIOoutput from Rpi to another Rpi GPIOinput

Fri Jan 19, 2018 11:17 pm

snielsen wrote:
Fri Jan 19, 2018 3:40 pm

Code: Select all

button.when_pressed = GPIO.output(14, 1)
button.when_released = GPIO.output(14, 0)
I don't think these two lines do what you think they do. I assume you are expecting pin 14 to go high when the input button is pressed and go low when it is released but that isn't what you have put.

button.when_pressed and button.when_released are supposed to be assigned the function to call in those instances but you have instead assigned them the return value of the GPIO.output() calls at the moment you made the assignments.

GPIO.output() returns None and setting either button.when_pressed or button.when_released to None means don't do anything when that happens. So basically you turned pin 14 high then turned it low when trying to set it up and ended up asking gpiozero to do nothing in response to those events.
She who travels light — forgot something.

snielsen
Posts: 12
Joined: Fri Jan 12, 2018 1:06 pm

Re: GPIOoutput from Rpi to another Rpi GPIOinput

Sat Jan 20, 2018 8:58 pm

Thanks a lot guys!
by boyoh » Sat Jan 20, 2018 12:16 am

Sugest switching the O/P between I/P of the Pi, you use a Opto Isolator
No need to common the grounds, This will give electrical and noise isolation.
Yes, it sounds resonable to consider this solution if it eliminates noise etc... if I understand it correctly you mean using the same GPIO as in and out on the main Rpi??

Anyhow I found a 340 ohm resistor and tried this code with a led connected to GPIO 2 as output. It seemes to work, and next step is to connect the second Rpi (though I get the correct resistors delivered monday, it might be safer to wait, or is 340 ohm OK?)

Code: Select all

import RPi.GPIO as GPIO
GPIO.setwarnings(False)
from time import sleep     
GPIO.setmode(GPIO.BCM)           
GPIO.setup(2, GPIO.OUT)
GPIO.output(2, 0)

import simpleaudio as sa
import time

from gpiozero import Button
button = Button(17)

 
while True:            
    if button.is_pressed:
        GPIO.output(2, 1)
        time.sleep (0.1)
        GPIO.output(2, 0)
        wave_obj = sa.WaveObject.from_wave_file('samples/test3.wav')
        play_obj = wave_obj.play()
        play_obj.wait_done()


    else:  
        time.sleep (0.1)

snielsen
Posts: 12
Joined: Fri Jan 12, 2018 1:06 pm

Re: GPIOoutput from Rpi to another Rpi GPIOinput

Sat Jan 20, 2018 9:22 pm

The LED gives a short flash(0.1), and yes, it seems to work. But I did not have patience to wait for other resistors, so I connected the second Rpi where the wav file starts playing just by pressing F5 on keyboard, without Buttonpress on main Rpi ... strange to me :|

boyoh
Posts: 1167
Joined: Fri Nov 23, 2012 3:30 pm
Location: Selby. North Yorkshire .UK

Re: GPIOoutput from Rpi to another Rpi GPIOinput

Sun Jan 21, 2018 12:00 pm

snielsen wrote:
Sat Jan 20, 2018 8:58 pm
Thanks a lot guys!
by boyoh » Sat Jan 20, 2018 12:16 am

Sugest switching the O/P between I/P of the Pi, you use a Opto Isolator
No need to common the grounds, This will give electrical and noise isolation.
Yes, it sounds resonable to consider this solution if it eliminates noise etc... if I understand it correctly you mean using the same GPIO as in and out on the main Rpi??

Anyhow I found a 340 ohm resistor and tried this code with a led connected to GPIO 2 as output. It seemes to work, and next step is to connect the second Rpi (though I get the correct resistors delivered monday, it might be safer to wait, or is 340 ohm OK?)

Code: Select all

import RPi.GPIO as GPIO
GPIO.setwarnings(False)
from time import sleep     
GPIO.setmode(GPIO.BCM)           
GPIO.setup(2, GPIO.OUT)
GPIO.output(2, 0)

import simpleaudio as sa
import time

from gpiozero import Button
button = Button(17)

 
while True:            
    if button.is_pressed:
        GPIO.output(2, 1)
        time.sleep (0.1)
        GPIO.output(2, 0)
        wave_obj = sa.WaveObject.from_wave_file('samples/test3.wav')
        play_obj = wave_obj.play()
        play_obj.wait_done()


    else:  
        time.sleep (0.1)
(-------------------------------------( Pi/1---------------)- (-----(Pi/2)------)(--(Pi/1---)--------------------------(Pi/2)-------------------------------)
GPIO o/p-------( R )----------( a opto k )----0v)- ( 3.3v rail-------( c opto e )----------( 1k )-----------( gpio I/P )--------( 10k )---0v-

Same circuit but in reverse to communicate from Pi 2 to Pi 1

calculate R to give 5ma for op to Transmitter

Circuit shown will give a Active 1 (high ) I/P

Can modifier to give a Active 0 (low ) I/P
BoyOh ( Selby, North Yorkshire.UK)
Some Times Right Some Times Wrong

User avatar
davidcoton
Posts: 3160
Joined: Mon Sep 01, 2014 2:37 pm
Location: Cambridge, UK

Re: GPIOoutput from Rpi to another Rpi GPIOinput

Sun Jan 21, 2018 4:53 pm

snielsen wrote:
Sat Jan 20, 2018 8:58 pm
Yes, it sounds resonable to consider this solution if it eliminates noise etc... if I understand it correctly you mean using the same GPIO as in and out on the main Rpi??
For single way communication, you can use any GPIO at either end, as long as it isn't doing anything else (and some GPIOs have things assigned by default that you would have to turn off).
I would choose different pins, so that if I ever did need two-way communication, I would have to make fewer code changes (both would transmit on , say, GPIO 2 (pin 3) and both would receive on GPIO 3 (pin 5).
As long as I don't need I2C at the same time.
"Thanks for saving my life." See https://www.raspberrypi.org/forums/viewtopic.php?p=1327656#p1327656
“Raspberry Pi is a trademark of the Raspberry Pi Foundation”

snielsen
Posts: 12
Joined: Fri Jan 12, 2018 1:06 pm

Re: GPIOoutput from Rpi to another Rpi GPIOinput

Sun Jan 21, 2018 6:40 pm

I still dont understand why the script seems to work well when testing only with LED, and do not work when I connect it to gpio 17 on the second rpi ( playback starts imedeately when jumpers are connected, before pressing input button on main rpi).

pcmanbob
Posts: 4338
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: GPIOoutput from Rpi to another Rpi GPIOinput

Mon Jan 22, 2018 11:54 am

Then you must have some thing wrong with either your programming or the connection.

with pi's wired like this

Image

the pi with the switch running this code

Code: Select all

import RPi.GPIO as GPIO
import time


GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(24, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(17, GPIO.OUT)
GPIO.output(17, GPIO.LOW)

while True:
    
    if GPIO.input(24) == 0:
        GPIO.output(17, GPIO.LOW)
        print "stop"
    else:
        GPIO.output(17, GPIO.HIGH)
        print "send"
time.sleep (0.5)
and the pi with the LED running this code

Code: Select all

import RPi.GPIO as GPIO
import time


GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(17, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(18, GPIO.OUT)
GPIO.output(18, GPIO.LOW)

while True:
    
    while GPIO.input(17) == 0:
        print GPIO.input(17)
        time.sleep(0.1)
    
    print GPIO.input(17)
    GPIO.output(18, GPIO.HIGH)
    time.sleep(1)
    GPIO.output(18, GPIO.LOW)
    time.sleep(1)
    

The LED flashes at one second intervals while ever the button is pressed .
Remember we want information.......................no information no help
The use of crystal balls & mind reading is not supported

snielsen
Posts: 12
Joined: Fri Jan 12, 2018 1:06 pm

Re: GPIOoutput from Rpi to another Rpi GPIOinput

Wed Jan 24, 2018 12:15 pm

Thanks a lot, everyone!

My problem is solved. I have inverted the 0 and 1 in GPIO.output(2, x) at line 6, 16 and 18 in the script running on Rpi nr 1.

Code: Select all

import RPi.GPIO as GPIO
GPIO.setwarnings(False)
from time import sleep     
GPIO.setmode(GPIO.BCM)           
GPIO.setup(2, GPIO.OUT)
GPIO.output(2, 1)

import simpleaudio as sa
import time

from gpiozero import Button
button = Button(17)

 
while True:            
    if button.is_pressed:
        GPIO.output(2, 0)
        time.sleep (0.1)
        GPIO.output(2, 1)
        wave_obj = sa.WaveObject.from_wave_file('samples/test3.wav')
        play_obj = wave_obj.play()
        play_obj.wait_done()


    else:  
        time.sleep (0.1)
Attachments
gpio01 (1).jpg
gpio01 (1).jpg (88.9 KiB) Viewed 408 times

Return to “Beginners”

Who is online

Users browsing this forum: Baidu [Spider], dshaw619, Google [Bot] and 54 guests