Combining Carbon Sensor & Datalogger Sketches


I’ve been trying to get my datalogger working with my carbon sensor. They each seem to be working individually (when I run each sketch separately). When I combine them I dont get any errors, but the sketch doesnt run past displaying “…card initialized.” in the serial monitor. I would appreciate any hints or guidance. Thank you.

//begin cozir setup
#include <SoftwareSerial.h>
SoftwareSerial mySerial(10, 9); // Tx, Rx pins on Ardunio
int co2 = 0;
double multiplier = 2*1000/1024;//1; 1 for 2% =20000 PPM, 10 for 20% = 200,000 PPM
uint8_t buffer[25];
uint8_t ind =0;
uint8_t index =0;
int fill_buffer(); // function prototypes here
int format_output();
//end cozir setup

//begin datalogger setup
#include <SPI.h>
#include <SD.h>
const int chipSelect = 8;
//end datalogger setup

void setup() {


while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only

Serial.print(“Initializing SD card…”);

// see if the card is present and can be initialized:
if (!SD.begin(chipSelect)) {
Serial.println(“Card failed, or not present”);
// don’t do anything more:
Serial.println(“card initialized.”);


void loop() {

//begin cozir void loop
//fill_buffer(); // function call that reacds CO2 sensor and fills buffer
//index = 0;
Serial.print(" Raw PPM “);
//index = 8; // In ASCII buffer, filtered value is offset from raw by 8 bytes
Serial.println(” Filtered PPM\n\n");
//end cozir void loop

//begin datalogger void loop
// make a string for assembling the data to log:
String dataString = “”;
for (int digitalPin = 9; digitalPin < 11; digitalPin++) {
int sensor = co2*multiplier;
dataString += String(sensor);
if (digitalPin < 10) {
dataString += “,”;
// open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.
File dataFile =“datalog.txt”, FILE_WRITE);
// if the file is available, write to it:
if (dataFile) {
// print to the serial port too:
// if the file isn’t open, pop up an error:
else {
Serial.println(“error opening datalog.txt”);
//end datalogger void loop


//begin cozir functions
int fill_buffer(void){
// Fill buffer with sensor ascii data
ind = 0;
while(buffer[ind-1] != 0x0A){ // Read sensor and fill buffer up to 0XA = CR
buffer[ind] =;
// buffer() now filled with sensor ascii data
// ind contains the number of characters loaded into buffer up to 0xA = CR
ind = ind -2; // decrement buffer to exactly match last numerical character
int format_output(void){ // read buffer, extract 6 ASCII chars, convert to PPM and print
co2 = buffer[15-index]-0x30;
co2 = co2+((buffer[14-index]-0x30)*10);
co2 +=(buffer[13-index]-0x30)*100;
co2 +=(buffer[12-index]-0x30)*1000;
co2 +=(buffer[11-index]-0x30)10000;
Serial.print("\n CO2 = ");
//end cozir functions

stay away from using pins 10,11 when using the SPI-bus.

Your code needs some more work..

stay away from using pins 10,11 when using the SPI-bus.

Your code needs some more work..

Thank you so much for replying. I changed TX from pin 10 to pin 6 and that did the trick! Have a wonderful day, kind sir.