Sunday, January 15, 2012

Amarino Software Serial Libary

I bought a really, really inexpensive Bluetooth module on eBay a while back to play around with on my Arduino ... and I've been playing around with Amarino (learn more about that on the Amarino website) which allows Bluetooth communication between an Android phone or tablet and an Arduino ... this is again related to my reading the Arduino + Android Projects for the Evil Genius book (by Simon Monk).

Unfortunately, I've tried and tried to get the two to communicate, with no luck.  I figured out how to change baud rates on the Bluetooth module using the Arduino Serial Monitor, so I knew I could send and receive fine between the Bluetooth module and the Arduino ... I could connect from my Android tablet to the Bluetooth module, so I knew Bluetooth was working (or, at least, connecting), but I could never get the Serial commuincation to work afterwards ... well, I could send to the Android, but I couldn't receive from the Android.   And I knew the Android was sending because if I held down the reset button on the Arduino (so the sketch that opened the Serial port wasn't running) I would get the data from the Android in the Arduino Serial Monitor (or garbage if the baud rate was wrong) ... so, I was definitely receiving!   But opening the serial port in the Sketch (with the Serial.begin() call) seemed to be causing me grief ... and the MeetAndroid library required that I did it ... so ... Catch 22 (if I called the Serial.begin() I couldn't receive data, but if I didn't I could, but I had to 'cause Amarino understandably requires it to be open) .

Anyhow ... then I found that if I wrote a little Arduino sketch that opened both the normal Serial Port and a Software Serial port, and moving the wires that ran between the TX/RX on the Arduino and Bluetooth to the pins specified in the Software Serial initialization that I could get the data coming from the Android no problem. 

So, I decided to play around and change the Amarino MeetAndroid library so that it used a Software Serial port rather than a Hardware Serial port ... make some minor changes to a couple Amarino sample sketches and voila ... it worked!

I have added a link where you can download a SoftwareSerial library-based version of the Amarino MeetAndroid library and I've also included the modified sample sketches.
So, for example ... for the Amarino Test application:

The first few lines look like this (past the comments):

 #include <MeetAndroid.h>  
 MeetAndroid meetAndroid;  

The SoftwareSerial version now looks like this:

 #include <SoftwareSerial.h> // absolutely required  
 #include <MeetAndroidSS.h>  
 MeetAndroidSS meetAndroid(9600, 1, 0);  // (baudrate, RXpin, TXpin)  

In addition, you need to remove the Serial.begin() call from the setup() function (the Software Serial port is intialized in the MeetAndroidSS class initialization and NOT with the Serial.begin() call ... in-fact if you leave in the Serial.begin() ... it probably won't work ... especially if you keep the RX and TX pin numbers at 1 and 0 respectively.

Oh ... and I did add a meetAndroid.send() call in the Test sketch testEvent() function so that I could see an 'Arduino Says' message in the Amarino Monitor.

I have changed the class / directory name so you can have both the SoftwareSerial-based and HardwareSerial-based libraries installed at the same time.

And remember ... the Baud Rate on your Bluetooth module has to still be the same as the Baud Rate specified in the class initialization call.  I've found the highest I can use without any problems is 57600 ... I'd recommend starting low at 9600 baud and working you way up to 115200 baud to see if/when it stops working.

Also, remember, if you are using a Mega or Mega 2560, there are limitations on which pins can be successfully used with the SoftwareSerial library ... 1 and 0 aren't included in those pins ... I personally just change the 1 and 0 to 51 and 50 (and connect wires appropriately).... that way all I have to do is remember that I need to add a 5 before both the RX and TX pin numbers if I'm using my Mega 2560 rather than my Uno (note: 11 and 10 are also available similarly as usable with the SoftwareSerial library for the Mega and Mega 2560, but the MultiColorLampTutorial sample uses both of these pins for LED output, so be careful) ... here's an example using 51 and 50:

 MeetAndroidSS meetAndroid(9600, 51, 50); // (baudrate, RXpin, TXpin)  

The MeetAndroidSS Library and all modified Amarino examples can be found here.

If you are working with the samples from the Arduino + Android Projects for the Evil Genius book (by Simon Monk).

The changes are as follows for the ch01_droid_droid sketch:

1.  Add an #include <SoftwareSerial.h> as the first line of the sketch.
2.  Change the #include <MeetAndroid.h> to #include <MeetAndroidSS.h>
3.  Change MeetAndroid phone; to MeetAndroidSS phone(baudRate, 1, 0);
     or whatever pins you use
4.  Delete the Serial.begin(baudRate); line in the setup() function.
5.  And don't forget that you may need to change the baudRate value to something other than 9600.

Full credit goes to the original authors.


1/16/2012 (or really late on 1/15)
  • Converted remainder of samples provided in the HardwareSerial version.  
  • Added getSerial() function allowing access to Software Serial port.  See SensorEvents sample for an example of its use.
  • Added reminder regarding Mega and Mega 2560 compatibility issues with the SoftwareSerial library in the post above.
  • Added the changes necessary for the Evil Genius book sample in the post above.
  • Moved Libraries and Samples to GitHub.


  1. Very cool! I was looking to move my serial port to a different pin to be able to use the Serial Monitor at the same time, this should help. THX!

  2. Thank you for removing a nasty road block. Please keep up the good work!

  3. Thank, this help me. Joss!!

  4. Thanks Jeff, this is what I'm looking for.. :)