Go Down

Topic: Interfacing CO2 sensor with Android phone (Read 2002 times) previous topic - next topic


Jul 12, 2011, 10:02 pm Last Edit: Jul 13, 2011, 02:27 am by erwinreu Reason: 1
Hello, I am working on interfacing a CO2 sensor with an Android phone via Arduino. I'm using this toolkit called Amarino


So far I have the bluetooth module communicating with the arduino over serial and the phone can successfully connect to the bluetooth module. Now the CO2 sensor I plan on connecting to the arduino via serial as well, so after some reading I decided I needed to use a software serial implementation as my arduino the Duemilanove only has one serial.

Now communication with the co2 sensor involves a Master/Slave situation. I must send the CO2 sensor 8 hexadecimal bytes and then read seven hex bytes of which the 4th and fifth concatenated is the co2 concentration.

I have some code, but as this is my first Arduino and I just learned about serial communication I am unsure if I am going in the write direction code wise.

Code: [Select]
 Sends sensor data to Android

#include <MeetAndroid.h>

//Software Serial Start
// include the SoftwareSerial library so you can use its functions:
#include <SoftwareSerial.h>

#define rxPin 2
#define txPin 3
// set up a new serial port
SoftwareSerial mySerial =  SoftwareSerial(rxPin, txPin);
byte pinState = 0;
byte inArray[8];
byte outArray[7];
//Software Serial End

MeetAndroid meetAndroid;

void setup()  
 //Software Serial Start
   // define pin modes for tx, rx, led pins:
 pinMode(rxPin, INPUT);
 pinMode(txPin, OUTPUT);
 // set the data rate for the SoftwareSerial port
 //Software Serial End
// Bluetooth Module Baud rate
 //Software Serial Start

void loop()
 meetAndroid.receive(); // you need to keep this in your loop() to receive events
 // print out the character:
 for (int i = 0; i<8; i+=1){
   mySerial.print(inArray[i], HEX);
   // listen for new serial coming in:
 for (int i=0; i<7; i++){
   outArray[i] = mySerial.read();
 /* To be implemented: obtaining the CO2 readings.
  concatenate outArray[4] and outArray[5] and convert to
 String ppm = "CO2 in ppm";
 //send result to Android
 // add a little delay otherwise the phone is pretty busy

I am quite new at this; any help is welcome and appreciated.


What CO2 sensor are you using?

It sounds similar to one that I was working with.


Jul 13, 2011, 02:12 am Last Edit: Jul 13, 2011, 02:28 am by erwinreu Reason: 1
I'm using this sensor.


I guess what I'm asking is: Is my communication over the Software Serial being done correctly?


You should use NewSoftSerial iso SoftSerial as it is far more stable.
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)


I haven't used NewSoftSerial yet. But I don't think you'll have much trouble.


Thanks for the advice; I'll look into it.

Andrew Robinson


This is Andrew Robinson with CO2 Meter. I saw your post this morning.

I would recommend using I2C communication personally, it's a lot simpler and you'll be able to use the hardware I2C engine most likely.

Check out the simple Arduino application note, it's for the older platforms, but it should be directly relevant:

Do let me know if you have any further questions! We love to help out people using our product in interesting ways.
Andrew Robinson

Go Up

Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

via Egeo 16
Torino, 10131