FR_Aramis
Posts: 2
Joined: Sat May 24, 2014 2:00 am

RaspberryPi - Arduino serial connection

Sat May 24, 2014 2:28 am

Hello,

I have some problems connecting my Raspberry Pi with my Arduino, and making them exchanging messages through a serial connection.

Context:

With four other people, we are building a robot to take part in a French competition. It has several actuators (such as servomotors or pumps) and sensors (sonars, bumpers) that we use to complete the different tasks offered by the competition rules. The robot has to be entirely autonomous and able to complete a 90s match.

We are using both cards:
- The Arduino controls all the different actuators and receives the sensors information
- The Raspberry Pi runs the program that dictates the actions the robot will perform during the match

We need to be able to receive informations from the sensors, as well as to be able to send orders to the different actuators which are controlled by the Arduino. To do so, the Arduino and the Raspberry Pi have to exchange messages which contain, on one side, the information, and on the other side, the orders.

Our problem:

We created a serial connection between the Arduino and the Raspberry Pi by a USB wire.

The Raspberry Pi program opens the serial connection (baud rate 9600), and then both cards start exchanging messages. Messages consist in series of 20 unsigned chars. Both cards have "emission" and "reception" methods. While the "reception" method is called every 300 miliseconds, the "emission" method is called every 1500 miliseconds. In order to avoid overflow, we also have a method which "flushes" the connection if more than 60 characters are available on the serial.

1) Arduino -> Pi works perfectly.
2) Pi -> Arduino does not work very well...

We want to make sure the connection can work continuously for 90 seconds at least, but it appears it cannot maintain itself stable for that long. It sometimes work - our test program asks the Arduino to move up and down a servomotor evey X miliseconds, which it sometimes does, but not always. Sometimes, the Arduino crashes and reboots. We thought it could be because the connection was lost and recreated (which causes Arduinos to reboot), or maybe because of an overflow. It does not seem to be a software issue, so I am asking if some of you tried to connect a Raspberry Pi and an Arduino via a serial USB connection and had similar issues?

- I read the "Pi -> Arduino" problem could come from a power difference. The Pi should be provided with 3 V while the Arduino received 5 V.
- Which libraries should be used to create and use the serial connection? We are using C++, and <wiringSerial> on the Pi, and the default serial library on the Arduino.
- Does the Raspberry Pi support 115200 baud rate serial connection?

Thank you for any information you could give me, any help would be greatly appreciated.

User avatar
AndyD
Posts: 2331
Joined: Sat Jan 21, 2012 8:13 am
Location: Melbourne, Australia
Contact: Website

Re: RaspberryPi - Arduino serial connection

Sat May 24, 2014 9:42 am

FR_Aramis wrote:We created a serial connection between the Arduino and the Raspberry Pi by a USB wire.

- I read the "Pi -> Arduino" problem could come from a power difference. The Pi should be provided with 3 V while the Arduino received 5 V.
If you are using a USB serial connection from your Arduino to your Raspberry Pi (i.e. connected to the Raspberry Pi via USB rather than GPIO), then you are using 5V. The 3.3V issue is when you use the serial connection of the GPIO connectors. Is that what you mean by We created a serial connection between the Arduino and the Raspberry Pi by a USB wire.
FR_Aramis wrote: - Which libraries should be used to create and use the serial connection? We are using C++, and <wiringSerial> on the Pi, and the default serial library on the Arduino.
WiringSerial should be fine. It appears to be a very thin wrapper around the standard tc*/ioctl/... function calls that work with any serial connection in Linux.
FR_Aramis wrote: - Does the Raspberry Pi support 115200 baud rate serial connection?
Yes, many people have successfully used 115200 baud rate communicating between a Raspberry Pi and an Arduino.

I have had success writing command to an Arduino at 115200 baud from the Raspberry Pi. However, at a reasonable speed. I was driving an Arduino LCD Shield using custom characters with commands from the Raspberry Pi. I was getting acceptable frame rates for my investigation.

I am sorry that I can't suggest a solution to your specific problem. I would try setting the connection at 115200 baud rather than 9600.

User avatar
pluggy
Posts: 3635
Joined: Thu May 31, 2012 3:52 pm
Location: Barnoldswick, Lancashire,UK
Contact: Website

Re: RaspberryPi - Arduino serial connection

Sat May 24, 2014 1:40 pm

Have you 'doctored' the OS to disable the serial port being used by the console ?

http://www.hobbytronics.co.uk/raspberry-pi-serial-port

You can ignore the bit about minicom, you don't need it. (you'll arouse Joe_Schmoe who has a real downer on it ;) ).

I have a Pi and Arduino happily conversing by the GPIO serial, It actually a lot more stable in my experience than using the Pi's USB. You can connect the Pi TX directly to the Arduino RX, but you need to 'adjust' the voltage for the Arduino TX to Pi RX, I use a voltage divider consisting of a 10k and 18k resistor but there are other simple ways of doing it.

I just use STTY to set the serial at the Pi end and use BASH scripting with echo and cat/tail. I use 115200 baud rate.
I use single character communication from the Pi to the Arduino, it avoids issues with the iffy RX on the Arduino, it gets confused if there is too much going on whilst it is receiving.

Take a look at what it does : http://www.pluggy.me.uk/homeserver
Don't judge Linux by the Pi.......
I must not tread on too many sacred cows......

FR_Aramis
Posts: 2
Joined: Sat May 24, 2014 2:00 am

Re: RaspberryPi - Arduino serial connection

Sat May 24, 2014 8:50 pm

Have you 'doctored' the OS to disable the serial port being used by the console ?
Yes. We hoped it would solve our problems but apparently they did not come from there.
I have a Pi and Arduino happily conversing by the GPIO serial, It actually a lot more stable in my experience than using the Pi's USB. You can connect the Pi TX directly to the Arduino RX, but you need to 'adjust' the voltage for the Arduino TX to Pi RX, I use a voltage divider consisting of a 10k and 18k resistor but there are other simple ways of doing it.
We are working on it. Hope it works better...

We made a few progresses. It would seem that we were exchanging messages "too fast" for either card. We started to get good results with a 1500 miliseconds emission period (both cards) and a 60 miliseconds reception period (both card), with a 9600 baud rate. We tried to lower these periods, but even with increasing the baud rate to 115200, we did not succeed in lowering them very much (the best we achieved was a 300 miliseconds emission period on the Arduino and 900 miliseconds emission period on the Pi... which is barely enough for our project).

We are also trying to figure out whether the length of the message could be the main issue: our messages are 20 char long from the Arduino and 17 char long from the Pi.

abroun
Posts: 31
Joined: Sat Jun 29, 2013 10:13 am
Contact: Website

Re: RaspberryPi - Arduino serial connection

Sun May 25, 2014 1:25 pm

Hi there,

I've used the Pi with an Arduino for building a number of robots and have had no problem getting communication to work reliably at 57600bps over USB. 115200bps should also work but I haven't tried it much.

I tend to use Python with PySerial on the Pi side, but wiringSerial should work just well. Not sure how useful Python code would be to you but I can provide a link if required.

Which Arduino board are you using? Uno? Also, do you have any test code you can post showing your problem? Perhaps we can offer some pointers.

Regards

Alan

Return to “Troubleshooting”