materials · web viewk-30 co2 breadboard data logger materials arduino pro mini 328 3.3v/8mhz k-30...

13
K-30 CO2 Breadboard Data Logger Materials Arduino Pro Mini 328 3.3V/8MHz K-30 10,000ppm CO2 sensor microSD Transflash breakout SanDisk microSD memory card 4GB DC Barrel jack adapter – breadboard compatible Wall Adapter power supply – 5V DC 2A FTDI Basic Breakout 3.3V Breadboard Headers and wires as necessary

Upload: duongdang

Post on 18-Apr-2018

217 views

Category:

Documents


4 download

TRANSCRIPT

Page 1: Materials · Web viewK-30 CO2 Breadboard Data Logger Materials Arduino Pro Mini 328 3.3V/8MHz K-30 10,000ppm CO2 sensor microSD Transflash breakout SanDisk microSD memory card 4GB

K-30 CO2 Breadboard Data LoggerMaterialsArduino Pro Mini 328 3.3V/8MHz

K-30 10,000ppm CO2 sensor

microSD Transflash breakout

SanDisk microSD memory card 4GB

DC Barrel jack adapter – breadboard compatible

Wall Adapter power supply – 5V DC 2A

FTDI Basic Breakout 3.3V

Breadboard

Headers and wires as necessary

Circuit

Page 2: Materials · Web viewK-30 CO2 Breadboard Data Logger Materials Arduino Pro Mini 328 3.3V/8MHz K-30 10,000ppm CO2 sensor microSD Transflash breakout SanDisk microSD memory card 4GB

Instructions1. Solder the header pins:

Start with the Arduino Pro-mini. The side pins we want facing down (away from the numbered side) and the top and middle pins facing up (opposite of previous, see pictures for details).

It is recommended to use a third arm and a fine-tip iron to accomplish all header soldering. Be careful not to heat and potentially damage components on the boards or accidently bridge any connections.

Additional header pins need to be added to the SD-breakout and the K-30 CO2 sensor. Note that the CD pin on the SD breakout is not used, however it is a good idea to attach a pin anyways. There are only 4 pins required to read the CO2 sensor out of dozens and so soldering only the 4 needed is recommended on the K-30.

Page 3: Materials · Web viewK-30 CO2 Breadboard Data Logger Materials Arduino Pro Mini 328 3.3V/8MHz K-30 10,000ppm CO2 sensor microSD Transflash breakout SanDisk microSD memory card 4GB

2. Build the circuit.

Starting with a breadboard, place the microcontroller, SD breakout, sensor, and barrel jack spread out so none of the pins will overlap, see picture for example. On the barrel jack there is a third grounding leg which can be cut off to better fit onto the breadboard. The positive and negative terminals of the barrel jack are also labeled in the picture below.

Connect the components according to the schematic on the first page. Use jumper cables or wires as available. Pin connections can be double checked by reviewing the sample Arduino code which lists the pin connections required at the beginning of the code. In the end you should have something looking like below:

Page 4: Materials · Web viewK-30 CO2 Breadboard Data Logger Materials Arduino Pro Mini 328 3.3V/8MHz K-30 10,000ppm CO2 sensor microSD Transflash breakout SanDisk microSD memory card 4GB

In reality it will probably look like this:

3. Connecting the Arduino

Install the Arduino IDE which can be found at [https://www.arduino.cc/en/Main/Software]. Consult the readme or the Arduino website for issues installing the software.

Attach the FTDI breakout to the supplied micro-USB cable and then attach to the top header pins of the Arduino. To ensure it is attached properly there is BLK and GRN printed on the breakout and microcontroller which should be matched up on the same sides with each other.

When this is completed attach the USB to a computer with the Arduino IDE installed.

Page 5: Materials · Web viewK-30 CO2 Breadboard Data Logger Materials Arduino Pro Mini 328 3.3V/8MHz K-30 10,000ppm CO2 sensor microSD Transflash breakout SanDisk microSD memory card 4GB

4. Uploading the code

Open the Arduino software. Initially check to ensure the libraries Wire.h and SD.h are installed. This can be accomplished by navigating to:

Sketch -> Include Library -> Manage Libraries…

Search for “wire” and “sd” and make sure the appropriate libraries have an “INSTALLED” message next to them. The SD library may be a few from the top when searched for. If either library is not installed, simply click on it and click the “Install” button on the right.

When both libraries are confirmed as installed navigate to:

Tools -> Board: “…”-> Arduino Pro or Pro Mini

When Pro or Pro Mini is selected navigate a few options down in the Tools menu to:

Tools -> Port: “…” -> COM#

If you are properly connected to the microcontroller and the correct board is selected there should be one port option available.

When the port is selected copy the code provided in the appendix into the text area of the Arduino IDE. Make sure all previous text (including the initial “void setup()” text or anything else initially existing) is deleted fully before copying the new text in.

When the new code is in place click the checkmark in the IDE to Verify the code. This will check to make sure the libraries are installed and see if there are any errors in the code.

When finished compiling, upload the code to the microcontroller by clicking the arrow next to the verify button. A series of rapidly blinking lights should be seen on the FTD breakout. When complete the code is uploaded and the FTD breakout can be unplugged from the Arduino.

The data logger is now ready to be used! Plug in the 5V supply and the SD card into the card-slot and readings will begin to be taken. Functionality of the K-30 can be confirmed by a blinking LED within the sensor housing.

The SD card will record readings every two seconds in a .csv file according the provided code. Consult the code notations for questions concerning how to change the output of the sensor readings or any of the other workings of the data logger.

For questions or concerns contact [someone who knows what they’re doing].

Page 6: Materials · Web viewK-30 CO2 Breadboard Data Logger Materials Arduino Pro Mini 328 3.3V/8MHz K-30 10,000ppm CO2 sensor microSD Transflash breakout SanDisk microSD memory card 4GB

Arduino Code// Sample code for K-30 CO2 sensor datalogger to SD breakout

// Talks via I2C to K30/K33 Sensors and displays CO2 values

#include <Wire.h> #include <SD.h>

// We will be using the I2C hardware interface on the Arduino in // combination with the built-in Wire library to interface. // Arduino analog input 5 - I2C SCL // Arduino analog input 4 - I2C SDA /* * * SD card attached to SPI bus as follows (for Redboard): ** MOSI - pin 11 (DI) ** MISO - pin 12 (DO) ** CLK - pin 13 (SCK) ** CS - pin 10*/

// This is the time resolution of sensor readings in ms.int readingTime = 2000;

/* In this example we will do a basic read of the CO2 value and checksum verification. For more advanced applications see the I2C Comm guide. */ int co2Addr = 0x68; // This is the default address of the CO2 sensor, 7bits shifted left.

// Chip Select pin is tied to pin 8 on the SparkFun SD Card Shieldconst int chipSelect = 8;

void setup() { // Open serial communications and wait for port to open: Serial.begin(9600); //initialize wire Wire.begin (); Serial.println("Application Note AN-102: Interface Arduino to K-30");

Serial.print("Initializing SD card..."); // make sure that the default chip select pin is set to // output, even if you don't use it: pinMode(chipSelect, OUTPUT); pinMode(10,OUTPUT); // 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: return; }

Page 7: Materials · Web viewK-30 CO2 Breadboard Data Logger Materials Arduino Pro Mini 328 3.3V/8MHz K-30 10,000ppm CO2 sensor microSD Transflash breakout SanDisk microSD memory card 4GB

Serial.println("card initialized."); Serial.println(); //make the headers Serial.println("Time(h:m:s),CO2 reading"); File dataFile = SD.open("co2reads.csv", FILE_WRITE); dataFile.print("Time(h:m:s),CO2 reading"); dataFile.println(); //create a new row to read data more clearly dataFile.close(); //close file}

/////////////////////////////////////////////////////////////////// // Function : int readCO2() // Returns : CO2 Value upon success, 0 upon checksum failure // Assumes : - Wire library has been imported successfully. // - LED is connected to IO pin 13 // - CO2 sensor address is defined in co2_addr /////////////////////////////////////////////////////////////////// int readCO2() { int co2_value = 0; // Store the CO2 value inside this variable.

// On most Arduino platforms this pin is used as an indicator light. ////////////////////////// /* Begin Write Sequence */ ////////////////////////// Wire.beginTransmission(co2Addr);

Wire.write(0x22);

Wire.write(0x00);

Wire.write(0x08);

Wire.write(0x2A);

Wire.endTransmission(); ///////////////////////// /* End Write Sequence. */ /////////////////////////

/* Wait 10ms for the sensor to process our command. The sensors's primary duties are to accurately measure CO2 values. Waiting 10ms ensures the data is properly written to RAM */ delay(10); ///////////////////////// /* Begin Read Sequence */ ///////////////////////// /*

Page 8: Materials · Web viewK-30 CO2 Breadboard Data Logger Materials Arduino Pro Mini 328 3.3V/8MHz K-30 10,000ppm CO2 sensor microSD Transflash breakout SanDisk microSD memory card 4GB

Since we requested 2 bytes from the sensor we must read in 4 bytes. This includes the payload, checksum, and command status byte. */ Wire.requestFrom(co2Addr, 4); byte i = 0; byte buffer[4] = {0, 0, 0, 0}; /* Wire.available() is not necessary. Implementation is obscure but we leave it in here for portability and to future proof our code */ while (Wire.available()) { buffer[i] = Wire.read(); i++; } /////////////////////// /* End Read Sequence */ /////////////////////// /* Using some bitwise manipulation we will shift our buffer into an integer for general consumption */ co2_value = 0; co2_value |= buffer[1] & 0xFF; co2_value = co2_value << 8; co2_value |= buffer[2] & 0xFF; byte sum = 0; //Checksum Byte sum = buffer[0] + buffer[1] + buffer[2]; //Byte addition utilizes overflow if (sum == buffer[3]) {

// Success! return co2_value; } else { /* Checksum failure can be due to a number of factors, fuzzy electrons, sensor busy, etc. */ return 0; } } void loop() { // make a string for assembling the data to log: String 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. // this opens the file and appends to the end of file // if the file does not exist, this will create a new file. // use .txt or .csv for either text or Excel files, make sure it matches in setup!

Page 9: Materials · Web viewK-30 CO2 Breadboard Data Logger Materials Arduino Pro Mini 328 3.3V/8MHz K-30 10,000ppm CO2 sensor microSD Transflash breakout SanDisk microSD memory card 4GB

// the entire filename including extension must be in quotes File dataFile = SD.open("co2reads.csv", FILE_WRITE); int co2Value = readCO2(); if (co2Value > 0) { if (dataFile) { //math to get an easibly read time (millis() must be long) unsigned long msTime = millis(); int hTime = msTime/3600000; int mTime = msTime/60000-hTime*60; int sTime = msTime/1000-mTime*60; //write to uSD card dataFile.print(hTime); dataFile.print(":"); dataFile.print(mTime); dataFile.print(":"); dataFile.print(sTime); dataFile.print(", "); dataFile.print(co2Value); dataFile.println(); //create a new row to read data more clearly dataFile.close(); //close file //output also on Serial monitor for debugging Serial.print(hTime); Serial.print(":"); Serial.print(mTime); Serial.print(":"); Serial.print(sTime); Serial.print(","); Serial.println(co2Value); Serial.println(); //make it easy on the eyes } // if the file isn't open, pop up an error: else{ Serial.print("error opening the file"); } } else { Serial.println("Checksum failed / Communication failure"); } delay(readingTime); } /* * Code used from: * SD_Datalogger.ino <https://github.com/sparkfun/microSD_Shield> * CO2 Meter K-series Example Interface <co2meter.com> */