psicor
Posts: 6
Joined: Mon Feb 03, 2014 3:00 pm

Connect two Pi's via GPIO ?

Sun Oct 09, 2016 10:17 pm

I would appreciate some advice on connecting two Pi's (call them "sender" and "receiver") together using only GPIO pins. The sender is a Pi 2 and the receiver is a Pi zero, both are powered independently via 5v step down buck regulators from a single 12v supply.

What I want to do is send a signal from the sender to the receiver, get the receiver to do something interesting and then get the receiver to send an acknowledgement or negative acknowledgment back to the sender. I have done this already in software by writing a simple sockets app, but this (obviously) depends on a network connection - which could prove unreliable. I would prefer to eliminate the network and use a more reliable bit of wire between the two Pis.

The first question I have relates to electrical connections (I'm OK with software but electronics is a bit of a mystery) - is it sufficient to simply link the two Pi's GPIO pins with a bit of wire, or do I need to limit current with a resistor. Also, do I need to link the ground lines of the two voltage regulators together (they may be producing slightly different voltages).

Second question - this setup has to survive a boot of either or both Pi's. Given that the GPIO pins are variously up/down or input/output during the boot, I'm thinking that signalling by setting a single pin high may not be a reliable approach - do I need to implement some form of handshake protocol between the Pi's. I'm thinking that maybe I could create a chain of pulses of defined duration to indicate a signal and decode this at the receiver.

I'm sure that someone must have done this before, but I can't find any references to it. Any advice would be much appreciated.

ElEscalador
Posts: 671
Joined: Tue Dec 15, 2015 4:55 pm
Location: Detroit, MI USA
Contact: Website

Re: Connect two Pi's via GPIO ?

Sun Oct 09, 2016 10:25 pm

You can do this with the two designated uart pins (the standard hardware serial bus)- no resistor needed. Just those two wire plus tie the Pi's grounds together. make sure you enable uart in the config file. (It might say enable serial in the GUI raspi configuration app)
My Autonomous Robot Project and a few of my other projects below.

https://lloydbrombach.wordpress.com/

psicor
Posts: 6
Joined: Mon Feb 03, 2014 3:00 pm

Re: Connect two Pi's via GPIO ?

Sun Oct 09, 2016 11:12 pm

Thanks - good call - I didn't think of using the UART.

Are there any recommended high level Python libraries for UART control ? I can modify my Python sockets app to redirect to UART and retain all the existing handshaking/setup logic - that will save a lot of time.

User avatar
joan
Posts: 14093
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Connect two Pi's via GPIO ?

Mon Oct 10, 2016 8:34 am

The standard Python serial library is packaged as python-serial which you will probably need to install.

sudo apt-get install python-serial python3-serial

stevend
Posts: 210
Joined: Fri Oct 11, 2013 12:28 pm

Re: Connect two Pi's via GPIO ?

Mon Oct 10, 2016 12:46 pm

Bear in mind that if directly connecting GPIO pins (which includes unbuffered UART pins), you should keep the distance down to avoid noise pickup - no hard and fast rules, but I'd suggest a few inches at most.
Regardless of whether you use parallel or serial, you can buffer the signals using various types of driver and receiver to extend the length (RS-422 differential drivers are good for >1km at 9600 baud, IIRC)
If hard wiring is acceptable, why not use all Pis with a network port, and stick with a network connection? You can probably use a crossover cable. It'll be much faster than most of the alternatives, can be extended over a reasonable distance, and most of the tricky software work has been done. Even with a basic serial link you'll have to write software to manage error detection and recovery, which can get complicated.

psicor
Posts: 6
Joined: Mon Feb 03, 2014 3:00 pm

Re: Connect two Pi's via GPIO ?

Mon Oct 10, 2016 1:13 pm

Thanks to everyone for the very good advice. I'll probably try each option and pick whatever seems most reliable.

I can limit distance between the Pi's to about 20cm and I can live with 9600 baud - the messages being passed are small and infrequent. I've already written code to deal with message segmentation, sequencing and recovery for the sockets interface, so hopefully I can simply re-use that with UART.

As an aside - I've just realised that if I remove network access I'll need to install a realtime clock on one of the Pi's to enable recovery (from a time-stamped log file) following power down or failure. It's amazing how such simple projects can get so complicated so quickly !

User avatar
RaTTuS
Posts: 10381
Joined: Tue Nov 29, 2011 11:12 am
Location: North West UK

Re: Connect two Pi's via GPIO ?

Mon Oct 10, 2016 1:22 pm

this should work for RPi0's
viewtopic.php?f=44&t=51633&start=25#p828064

approx 1Mb/s
How To ask Questions :- http://www.catb.org/esr/faqs/smart-questions.html
WARNING - some parts of this post may be erroneous YMMV

1QC43qbL5FySu2Pi51vGqKqxy3UiJgukSX
Covfefe

richrarobi
Posts: 271
Joined: Sun Feb 08, 2015 1:13 pm

Re: Connect two Pi's via GPIO ?

Tue Oct 25, 2016 7:12 pm

What is it about networking you think is unreliable?
Seems like a shame not to use the best components that are designed for the job?

stderr
Posts: 2178
Joined: Sat Dec 01, 2012 11:29 pm

Re: Connect two Pi's via GPIO ?

Tue Oct 25, 2016 11:59 pm

richrarobi wrote:What is it about networking you think is unreliable?
The router after it overheats because the manufacturer thinks that it is more important to make wifi routers look like spaceships or many legged crabs than it is to make sure they are within their thermal budget.
Seems like a shame not to use the best components that are designed for the job?
Being forced into a star topology because you are using twisted pair ethernet might not be the best solution for the job. What if there isn't a switch close to the pies and your scheme would mean either adding one or running another ethernet cable?


Return to “Interfacing (DSI, CSI, I2C, etc.)”