Saturday, May 11, 2013

Arduino: Serial Communication Between Two Arduinos

I wanted to know more about inter-Arduino communication, so I did this project. I want to explore the different ways to communicate with and between Arduinos.

First up is serial communication. This is pretty simple. I expected there to be more hitches, but it turned out to be pretty straight forward. Well let's get to it.

First things first, HERE is the Arduino playground page for serial communication. You might want to read up on the different commands you have there.

Ok, first program. One Arduino transmits one integer every second counting from 1 to 10. The receiving Arduino then prints that number to the COM port where I can see it.  I only have one Arduino with a working USB to serial interface, so I used it as the Rx.

The code is simple enough. You can get mine HERE, but I would encourage anyone to try to write it yourself. It helps, and this is short enough. The only unexpected detail is the parseInt(). I first tried a simple read(). Well that doesn't work because that displays the ASCII character and not the integer value. I had forgotten that.

To wire it up, simply connect Tx (probably pin 1) of the Tx board to the Rx (probably pin 0) of the Rx board. Then connect a common ground between the two. Note that you may need to connect these pins after you load the program on the Arduino (since you are using that pin to communicate with the computer via the USB to serial chip). Leaving them connected caused an error for me. This might be avoided if you put a resistor in between the 2 boards, but I have not tried. By the way, if you're wondering where I got that super cool Tx Arduino, check out THIS post. It was really cheap fun!

Left: Rx                         Right: Tx
Well that was cool. Now I want to go the other way. The next program blinks the LED on the Rx board a specific number of times entered through the Serial Monitor connected to the Tx board.

The code again is not that difficult. HERE is mine. Again, use parseInt() and you're golden.

The wiring is equally simple. It's the same as you just did except in reverse the two boards. In fact, if you had two "real" Arduinos with an FTDI or similar, you wouldn't even need to do that. Just make sure you wire in the common ground as always.

Left: Tx                       Right: Rx

Well that's great and all, but I want to communicate both ways. Next program. This one takes an integer value from a user input through the Serial Monitor and then sends it to the Rx Arduino. The Rx then adds 5 to the integer an sends it back to the Tx. Note that the Tx and Rx labels are a bit arbitrary in this one. It's more of a master-slave relationship.


HERE is my code if you want it.


Wiring is similar to above. However, I used Serial1 on the Tx to communicate with the Rx board. This just kept the USB communication from interfering. This functionality is only available on the Arduino Mega as far as I know (besides 3rd party boards). See the Arduino Documentation linked at the beginning of this post for more details. It's also interesting to note that I noticed I didn't need to power the Rx board for it to work. It would draw power from the Tx pin. That was interesting. I powered it anyway.


Ok.  Last program. This one only uses one Arduino. It allows you to input a word , and it mirrors it back to you. Now this is not all that impressive in itself, but it is still very useful.

There are only a few major changes to the code we have been using. We need to make the variables we use char variables. Also we need to change the parseInt() to a plain read(). Another note, remember that serial is one bit at a time. So if you want to do a line return, you need to handle that yourself. I made it do one every time it saw a period. \n would be more traditional, but that was more work.

HERE is my program. It is pretty simple, but I am not a programmer. If your program is doing anything else you will probably want to use a char string. THIS looks like a good example of this.

Wiring nonexistent. Just connect your Arduino via USB.

That's all I have for now. Hope this was useful! If you want the full package of programs you can get them in zip HERE. Also, if you want to learn about serial communication with an ATtiny (a $2 microcontroller) check that out HERE. If you are looking for something else, try my communication label. There may be something in there that will interest you.

-Matthew