dasdino
Posts: 9
Joined: Wed Apr 08, 2015 7:50 am

Best way to connect the PI + Arduino

Wed Apr 08, 2015 7:57 am

Hello Folks

I been reading about this on the web like at:

http://blog.retep.org/2014/02/15/connec ... using-i2c/

As I found there are 3 ways to connect the PI + Arduino:

1, USB
2, I2C
3, SPI

What I need is not the fastest but the most ROBOUST which remains stable over the long term. I don't want to squash through thousands of bits every time on the wire between the 2 device. All I need is to send some commands to the ardu (like turn relay X on) and get some read backs (like input from infrared remote).


For what it matters the USB is definitely not that. I have a working home control installation where I control my Arduino from an x86 linux server with a C control program (which should be as fast as it gets), still I ran into a lot of issues over time that the usb connection just stucks in and I either have to reboot the arduino or kill this C control program and restart it.

The other thing is that the Arduino need a capacitor to be connected between 2 legs, otherwise it will reset every time you try to open the port. So for example if I decide to close this C program on the linux host which closes the /dev/ttyACM0 port that would make the Arduino reset and that's not what I want since it has some automation functions on it's own (which it must be able to do even without the computer connected to it (for example turn on lights by remote button presses). So the computer control in my case is just another layer of control. The downside of adding this CAP is it will make the reprogramming impossible, so I had to add it with a switch, what I switch off every time I want to reprogram the Ardu.


So what I planning to do is to replace this installation with an RPI (1/2) + Arduino board where I can run the control program directly from the Pi, the question is just how stable will this be on the long term.

The useful comments I found on that site regarding this was:

"I’ve been struggling to get reliable communication between my raspberry pi and an arduino micro. It works 99% of the time and fails 1% of the time."

"Sorry for the late reply. As you point out This is a known issue with the current I2C kernel drivers and clock stretching.
What I’ve been doing to get around this problem is I manually add delays to my code – one after sending the command and before reading the response and another after the response."


Thanks

User avatar
DougieLawson
Posts: 36844
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: Best way to connect the PI + Arduino

Wed Apr 08, 2015 9:02 am

There's a fourth way using the RPi's UART on pins 8&10 (GPIO14/15) with a level shifter.

There's no one method that's any better than another. With a UNO (for example) USB is simplest since the hardware is already there and all you need is a USB-A to USB-B cable.
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

User avatar
B.Goode
Posts: 9038
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: Best way to connect the PI + Arduino

Wed Apr 08, 2015 9:54 am

Perhaps it would be helpful to look at real-life examples of how other people have gone about it:

http://www.raspberrypi.org/introducing-gertduino/

http://skpang.co.uk/catalog/piduino-kit-p-1337.html

http://rasp.io/duino/

mung
Posts: 506
Joined: Fri Nov 18, 2011 10:49 am

Re: Best way to connect the PI + Arduino

Wed Apr 08, 2015 8:50 pm

All ways are probably equally good for simple comms, but there are pros and cons if there are other factors to consider.
SPI fastest
rs232 is ubiquitous
i2c allows multiple devices to share the bus.
usb really means rs232 over usb but avoids need for voltage level conversion.

I would probably use i2c as its the way I normally do it.

To simplify things get an arduino that runs on 3.3volt then you don't need level conversion.
Also try and use shielded cables.

There is plenty of easy to undestand code available in the arduino IDE and rpi forums, make your choice and start experimenting.

User avatar
panik
Posts: 369
Joined: Fri Sep 23, 2011 12:29 pm
Location: Netherlands

Re: Best way to connect the PI + Arduino

Thu Apr 09, 2015 12:51 pm

dasdino wrote:So what I planning to do is to replace this installation with an RPI (1/2) + Arduino board where I can run the control program directly from the Pi, the question is just how stable will this be on the long term.
The AVRPi boards I sell make it easy to have USART, SPI and I2C connections by placing jumpers. The AVRPi-32U4 also adds the possibility of sending raw USB HID messages back and forth (or to other USB hosts). But since I've been developing heavily on them, they've only been running for 'days' on end (not 'weeks', let alone 'months').

Because of the error rate on the USART at high baud rates on the Arduino (up to -3.5% or even 8%) there will eventually be some glitches using the USART pins 14/15 on the Pi. These can be avoided if you run the AVR on 1.8432, 3.6864, 7.3728, 11.0592, 14.7456 or 18.4320 MHz. I'm not sure why the USART connection over USB CDC on your Arduino gets stuck. Maybe something else is going on, like counters that are overflowing?

There's the clock stretching issue on the I2C as you mention, which may or may not be avoided with the delay.

SPI may be your best option. Since the Gertboard, Gertduino, RasPiO Duino and AVRPi board all use at least some of the SPI pins to program the chip, you only need to add a slave select connection. On the AVRPi boards that's just another jumper.

Also, on the AVRPi-32U4, the SPI pins are separate from the 'regular' Arduino pins, so all of the Arduino pins are free to use for your relays and sensors.

http://www.onandoffables.com
Microcontroller addon boards and software for Raspberry Pi A+/B+/Pi2:
- ARMinARM: ARM Cortex-M3 (STM32)
- AVRPi: ATmega32U4 & ATmega328 ("Arduino")
http://www.onandoffables.com

dasdino
Posts: 9
Joined: Wed Apr 08, 2015 7:50 am

Re: Best way to connect the PI + Arduino

Thu Apr 09, 2015 2:26 pm

Hello

Thanks for the responses. I agree with the last poster, replacing my current UNO with this AVRPI will be the best option.

@"To simplify things get an arduino that runs on 3.3volt then you don't need level conversion.
Also try and use shielded cables."

Well I still have 2 questions regarding this avrpi:

1, "Voltage 3.3 Volt (5V tolerant)" What does this actually means? My current ardu uses relay cards, HF shift registers, etc. I designed this system for 5V. I need a drop in replacement here, I do not want to redesign it. Is this means that I can still feed the AVRPI with 5V without problems?

2, USB resetting. Will the same cap be necessary in order *not to reset* the arduino, every time I open a data connection to it if I use AVRPI?

Thank you!

User avatar
panik
Posts: 369
Joined: Fri Sep 23, 2011 12:29 pm
Location: Netherlands

Re: Best way to connect the PI + Arduino

Thu Apr 09, 2015 2:56 pm

dasdino wrote:1, "Voltage 3.3 Volt (5V tolerant)" What does this actually means? My current ardu uses relay cards, HF shift registers, etc. I designed this system for 5V. I need a drop in replacement here, I do not want to redesign it. Is this means that I can still feed the AVRPI with 5V without problems?
The AVRPi boards run on 3.3V. A 3.3V AVR pin as an input will take 5V without damage, as long as it's not also connected to an RPi pin (the RPi pin will be damaged). Depending on the peripherals, 3.3V may (or may not) be high enough to register as 'high' on a 5V peripheral input pin.

It's possible to run the AVRPi-328 board standalone on 5V, but that kinda defeats the purpose (especially for your use case). Also, if you designed the system around the Arduino shield pinout it's not a physical drop in replacement without at least some soldering.
dasdino wrote:2, USB resetting. Will the same cap be necessary in order *not to reset* the arduino, every time I open a data connection to it if I use AVRPI?
The AVRPi will only reset when the reset pin is pulled low. In any other scenario the AVR keeps running, no matter what data connection you open. In fact, I'm not sure what cap you mean exactly and what it's supposed to fix. There are some workarounds regarding reset on a regular (USB) Arduino, but the AVRPi is more straight forward and doesn't need any patches, fixes or hacks.
Microcontroller addon boards and software for Raspberry Pi A+/B+/Pi2:
- ARMinARM: ARM Cortex-M3 (STM32)
- AVRPi: ATmega32U4 & ATmega328 ("Arduino")
http://www.onandoffables.com

dasdino
Posts: 9
Joined: Wed Apr 08, 2015 7:50 am

Re: Best way to connect the PI + Arduino

Fri Apr 10, 2015 7:42 am

Hello

I talking about this reset:

http://arduino.stackexchange.com/questi ... the-sketch

"To avoid this behavior, a 10μF capacitor between Reset and Ground is enough, but you can also cut the RESET-EN trace to prevent the auto reset permanently."

Although if you do prevent it to reset then you will have problem when you upload a new firmware into it or at least you will always get the error from the IDE.
I don't know if this is necessary on higher boards than the UNO.

User avatar
panik
Posts: 369
Joined: Fri Sep 23, 2011 12:29 pm
Location: Netherlands

Re: Best way to connect the PI + Arduino

Fri Apr 10, 2015 11:28 am

Ah, I see. Thanks for clarification.

This is not going to be an issue with boards that use the GPIO pins to upload firmware. You can open and close connections to /dev/ttyAMA0 without the board resetting. The same with the extra virtual serial (over USB) /dev/ttyACM0 that the AVRPi-32U4 offers. Using the GPIOs to program the chip avoids the need for a bootloader too, by the way.

This was mentioned about the terminal emulator settings:
Setting the flow control to none will allow you to connect/disconnect from the serial without resetting your board.
Does that work for you?
Microcontroller addon boards and software for Raspberry Pi A+/B+/Pi2:
- ARMinARM: ARM Cortex-M3 (STM32)
- AVRPi: ATmega32U4 & ATmega328 ("Arduino")
http://www.onandoffables.com

Return to “Advanced users”