Arduino Forum

Using Arduino => Sensors => Topic started by: DirtGambit on Feb 11, 2012, 07:40 pm

Title: Temp, Humidity CO2 sensor and library.
Post by: DirtGambit on Feb 11, 2012, 07:40 pm
Hi, I thought I'd make a post about a sensor I recently purchased and its compatibility with arduino.
I was on the lookout for a sensor that would do temperature, humidity and co2. As I was looking for an all in one solution I settled on the cozir range of sensors http://www.cozir.com. After looking at the datasheet I thought it would interface easily with the arduino. I purchased mine directly from Gas Sensing Solutions. It works on the Arduino 3.3v, GND, and 2 digital pins for software serial TX,RX it took about 2 minutes to attach it and get it working.

(http://img684.imageshack.us/img684/9193/photo1ipd.jpg)

(http://img710.imageshack.us/img710/2111/57082139.jpg)

As soon as I hooked it up I started the SoftwareSerial example and after a minor modification the sensor was outputting the environment variables in streaming mode, this is the default mode, in this mode it sends out a stream of data at about two per second, as I was wanting to get the data at a time that suits my sketch I put the sensor in polling mode, this allowed me more control over what was happening, once set in polling mode it stays on this mode until you change it which was handy as I could set the sensor up in a separate sketch then I don't have to worry about including extra code in my project sketch.

I spoke with Rob Tillaart on the forum about it and he suggested a library be made, I had already hacked up my own library but it was a first version to get all the functionality of the sensor like changing digital filter and calibration into my project, Rob provided a blueprint library to start off with plus code which we worked on and tested for the last few weeks.

I have attached the library as a zip file in this post, It is 1.0 compatible. Thanks go out to Rob.

A few things to note, the sensor takes about 2 seconds to warm up, before that expect -100 for temp, putting a 2-3 second delay in setup stops this happening. Streaming mode hasn't been implemented in this version of the library, you can easily choose streaming mode from within the library but if you do you'll have to parse the data yourself, this will be getting worked on over the next few weeks. The digital filtered c02 value by default is set to 32, I found my graphs were moving quite a bit and after testing I have settled on 128, it gives a nice smooth output. Calibration with fresh air, nitrogen and known concentration are also implemented in this version of the lib and when you first power up the sensor you will have to calibrate it.

(http://img209.imageshack.us/img209/7493/templog.png)
(http://img12.imageshack.us/img12/6989/rhlog.png)
(http://img337.imageshack.us/img337/1564/co2log.png)

Please read the datasheet and software user guide before using this library.

Datasheet : http://www.co2meters.com/Documentation/Datasheets/COZIR-Data-Sheet-RevC.pdf
Software User Guide : http://www.co2meters.com/Documentation/Manuals/COZIR-Software-User-Guide-AL12-RevA.pdf

Here is a basic sketch to get it all outputting on the serial.

(http://img12.imageshack.us/img12/9832/serialout.png)

Code: [Select]
#include <SoftwareSerial.h>
#include "cozir.h"

SoftwareSerial nss(3,2);
COZIR czr(nss);

void setup()
{
 Serial.begin(9600);
 delay(3000);
 //czr.SetOperatingMode(CZR_POLLING);
 //czr.SetOperatingMode(CZR_STREAMING);
}

void loop()
{
 float t = czr.Celsius();
 float f = czr.Fahrenheit();
 float h = czr.Humidity();
 int c = czr.CO2();
 int digi = czr.GetDigiFilter();
 
 Serial.print("Celcius : ");Serial.println(t);
 Serial.print("Fahrenheit : ");Serial.println(f);
 Serial.print("Humidity : ");Serial.println(h);
 Serial.print("CO2 : ");Serial.println(c);
 Serial.print("Digital Filter : ");Serial.println(digi);    
}


Enjoy!!!!
Title: Re: Temp, Humidity CO2 sensor and library.
Post by: robtillaart on Feb 11, 2012, 08:20 pm

Some small remarks about the cozir library.

1) There are a few functions we commented out as they can "screw-up" the behavior of the device. We advice users to read the datasheet (twice) before uncommenting and using these functions. Mind you the Cozir-sensor is relative expensive (CO2 sensors all are) and you don't want to spoil it..

2) We implemented POLLING as primary acquisition strategy as this fits the single threading behavior of the Arduino best. We have some ideas to get the STREAMING mode working in a nice way but that will take a few days.

3) There is one function to get the value of the optional lightsensor, which is not clear with respect to units.

4) There are still some minor todo's in the code, that might be solved in a future version.

Comments and remarks are as allways welcome!


Title: Re: Temp, Humidity CO2 sensor and library.
Post by: Spandy19 on Feb 16, 2012, 09:15 pm
hey DirtGambit

the way you have displayed the results from your sensor is exactly what I am wanting to do.
I am using SHT15 sensor(temp,humidity). Can you please post the code for graphing and display section. Robtillaart told me it is C# code and I need to download the compiler..I am a newbie at this stuff..Please help me out. I want to graph the history ; time on the x-axis and temp, humidity on to the y-axis and display on a web server. At the moment my web server displays current temp,humidity. I like your format of display.soo please post the code for me...

Thankz heaps :)
Title: Re: Temp, Humidity CO2 sensor and library.
Post by: DirtGambit on Feb 18, 2012, 12:25 pm
Hi Spandy, The images are screen grab/crop of a much larger application I developed to graph environmental variables, The library I use is zedgraph, they do an asp .net version for graphing on the web but I dont have any experience in using that, going by the tutorial it seems syntactically similar to what I have used developing this in c#. Mostly its just configuring how your graphs look, also for the timestamp I store this in unixtime then convert when Im filling up the arrays of data, sorry I cant be of much help, I havent developed anything for the web that uses graphing.

check out http://www.codeproject.com/Articles/5431/A-flexible-charting-library-for-NET (http://www.codeproject.com/Articles/5431/A-flexible-charting-library-for-NET)

the examples dont look that great but zedgraph is so configurable it doesnt take long to change the look of your graphs.

Title: Re: Temp, Humidity CO2 sensor and library.
Post by: Spandy19 on Feb 19, 2012, 08:38 pm
Thank you..I will try it :)
Title: Re: Temp, Humidity CO2 sensor and library.
Post by: gaines on Mar 26, 2012, 11:52 pm
I was interested in this sensor so I contacted GSS which relayed my request to their North American partner, AirTest Technologies. I was very impressed with the quick response time and they promptly provided me with information on their products.

For my purposes, the Corzir-A (Ambient) with a range of 0-2000 ppm is perfect. It also has optional temperature and humidity sensors. The part number for that combination is COZIR-A-2000-X-H and I'm told it's $183.

How does that compare to other CO2 sensors? It's definitely more than I was hoping to spend, but at the same time it may be worth the price if it simplifies my project.
Title: Re: Temp, Humidity CO2 sensor and library.
Post by: DirtGambit on Mar 27, 2012, 09:53 am
Hi Gaines, I havent tested the cozir against any other Co2 sensor but it seems accurate enough, I usually calibrate via fresh air and that gives me a ball park figure for what Im using the sensor for, it certainly responds very well if the house has been empty all day and someone comes in it will pick up that very quickly. $183 is not a bad price, and its all in one, so no need for many libs, wires etc.

The only drawback from an arduino point of view is the sensor communicates through serial which makes the process a bit more difficult getting the data you want, it would have been nice to have a digital version, 3 pins and just asks the sensor for the values but this limits its application I suppose because GSS also supply a usb cable that fits directly to the sensor and can run via their software without the need for any dev board.

I do think its something GSS should consider, getting rid of the serial and going digital might lower the cost. Getting rid of streaming mode is another thing Id like to see, its a pain in the @$$. :)

Regards
Title: Re: Temp, Humidity CO2 sensor and library.
Post by: robtillaart on Mar 27, 2012, 06:54 pm
Hi DirtGambit,

Can you determine the presence of someone with the sensor?  detecting an intruder? how fast?
Would be a nice new application !
Title: Re: Temp, Humidity CO2 sensor and library.
Post by: DirtGambit on Mar 27, 2012, 07:43 pm
Oh yeah for sure as long as it wasnt really time critical, it stays between 390 - 420ppm (+-10ppm) all day, within 5 minutes of someone coming into the livingroom it will quite happily go up to 550ppm especially if you go near the sensor, I had some friends round one night, 4 people in a smallish living room, I was shocked to see 1500ppm on the graph.

A few years ago there was a study into co2 concentration in old schools, the average co2 during school time was > 2000ppm in some of them, in this case peaks were found at 3500ppm (I read a study once that had peaks of 4500ppm), a few schools had really good results but they were still > 1200ppm and had very good ventilation.

DirtGambit
Title: Re: Temp, Humidity CO2 sensor and library.
Post by: robtillaart on Mar 27, 2012, 10:01 pm
5 minutes seems rather long, but in those 5 minutes it goes from ~400-550 so it rises ~30ppm/minute. That means that one could detect it within 60 seconds that the CO2 level has rissen, and if well positioned maybe within 30 seconds. WHat is the minimum time you can detect in your graphs? If it is within 1 minute it would be great (OK depends on size of the room etc.

Do you see the same change in temperature - gues not- and humidity - could be? Humans exhale water and CO2 (they are reverse trees :) so those two should change significantly from some average value.

Ideas pop in my mind, like counting people by means of their CO2 output. Fun!
Title: Re: Temp, Humidity CO2 sensor and library.
Post by: threebadwheels on Apr 01, 2012, 01:11 pm
At the top of this post there is a second photo.  What is it?  An enclosure for the sensor and antenna?

Also, Cozir mentions the sensor has a light sensor?  Is it an LDR?  Does anyone know?

Thanks,

Thomas
Title: Re: Temp, Humidity CO2 sensor and library.
Post by: robtillaart on Apr 01, 2012, 01:42 pm
Quote
Does anyone know?

You could contact the manufacturer ...  IIRC from the datasheet not all models support this (not sure)
Title: Re: Temp, Humidity CO2 sensor and library.
Post by: DirtGambit on Apr 02, 2012, 10:40 am
I asked for ldr functionality in my sensor and was told it wasn't being supported anymore, the datasheets haven't been updated to reflect this. The ldr was an optional extra, so at order time you are just told the ldr isn't available. I liked the idea of an ldr, but i think these sensors are being targeted to the commercial sector (handheld devices) where an ldr might not be as useful as opposed to the arduino/electronics community where having one sensor do many things is desirable depending on what you are making. ldrs are 10 a penny so just hook one up along side the cozir, thats what i did.

The enclosure is from proto-pic.co.uk, it was lying around so i put it together and the antenna is connected to a bluesmurf module, it has a decent range with the antenna.

Title: Re: Temp, Humidity CO2 sensor and library.
Post by: robtillaart on Apr 02, 2012, 05:39 pm

Does the cozir sensor still has a connector for the LDR?
Title: Re: Temp, Humidity CO2 sensor and library.
Post by: threebadwheels on Apr 03, 2012, 03:41 am
This is disappointing.  I thought I had found a sensor that would do so much. I am wondering if there are any with all four sensors as Cozir has stated in the documentation.

I would like to create a sensor module that will sense C02, temperature, humidity, light and motion and connect them all to an xbee network to stream on the Internet. 

Is PIR a good sensor for motion?  What is a good precision LDR? 

I plan to place these modules on each floor of my home to detect variations in temperature and humidity, alert on C02 and motion, and adjust an LED to shed light in the room as it is needed.

Thanks,

Thomas
Title: Re: Temp, Humidity CO2 sensor and library.
Post by: DirtGambit on Apr 03, 2012, 10:22 am
I dont know rob, I havent opened the sensor to find out, also i dont know if they have altered the firmware of sensor to stop outputting null values if the sensor has no ldr fitted, the sensors output is always rh,Temp,unfiltered co2, filtered co2.

badwheels thats quite a demand from any all in one sensor, the cozir does most of what you need, if you were to have all those separate you would have quite a breadboard and be in wire/lib hell, your project sounds cool though good luck.

Title: Re: Temp, Humidity CO2 sensor and library.
Post by: serdargoksal on Jan 10, 2013, 10:46 am
Hi all,
Does anyone have an idea where to get COZIR.h library ?
Title: Re: Temp, Humidity CO2 sensor and library.
Post by: robtillaart on Jan 10, 2013, 05:33 pm
yes.
the lib is attached on the first post (it includes the .h file)
Title: Re: Temp, Humidity CO2 sensor and library.
Post by: robtillaart on Jan 10, 2013, 05:55 pm
latest version I have is 1.03 - see attachment - but it is a long time since I edited in it. there may be some work in progress on some points in it.
Note that the calibration functions must be used careful and do not use them if you don't understand their impact.

#all disclaimers apply ;)

If you have comments remarks let us know.
Title: Re: Temp, Humidity CO2 sensor and library.
Post by: plantgrower on Jan 17, 2013, 10:34 am
the attachment returns Error 503 Service Unavailable

Do you have code that works on an arm nxp lpc1769 ?

My CO2 readings seem high what's the best way to calibrate this device. I only have outside air to calibrate against.

many thanks
Title: Re: Temp, Humidity CO2 sensor and library.
Post by: DirtGambit on Jan 17, 2013, 11:04 am
Just calibrate via fresh air which gives a value of about 380, maybe your in an area of high co2, I have a main road next to my house with a lot of traffic and average values were 420-450, are your values wildly out of range?
Title: Re: Temp, Humidity CO2 sensor and library.
Post by: nschmal1 on Jan 25, 2013, 06:39 pm
What program did you use to display the graphs of the temperature, humidity, co2?
Title: Re: Temp, Humidity CO2 sensor and library.
Post by: foxlox on Mar 14, 2013, 01:21 pm
hi!

I'm using a COZIR™ Wide Range Sensor, http://www.gassensing.co.uk/wp-content/uploads/2013/02/COZIR_-WideRange_-Datasheet_GSS.pdf, and I tried to use the cozir.h library as mentioned in the first post, but without success. Must I observe some particular attention? I'm tryng it with only usb cable attached to my Arduino UNO, perhaps I need an external 12V input for Arduino? I need only 3.3v to power the sensor.

I receive nothing from sensor, all zeroed values...

I tried also the sample from Cozir for this sensor to read byte-to-byte from serial but I receive only 255 and never 0x0A for end transmission.

please help me thx.

   [fortunato]
Title: Re: Temp, Humidity CO2 sensor and library.
Post by: robtillaart on Mar 14, 2013, 02:57 pm
One often occuring problem with rs322 devices is that rx and tx need to be reversed. Did you try that?

Furthermore the sensor high bit is only 3.3Volt. The UNO needs at least 4.5 volt to recognize it as a HIGH signal. Check this with a voltmeter.
You might need a 3.3<->5 voltage converter for this.

Furthermore you could check if the analog out gives a (changing) voltage. Can you check?
Title: Re: Temp, Humidity CO2 sensor and library.
Post by: foxlox on Mar 16, 2013, 06:04 pm
Hi robtillaart

which pin I could check for analog voltage?

I have an external max3232 interface, which pins I must connect? I found that:

sensor tx: t1n
sensor rx: r1out
arduino tx: r1n
arduino rx: t1out

it's correct? I receive nothing :-(
Title: Re: Temp, Humidity CO2 sensor and library.
Post by: robtillaart on Mar 17, 2013, 11:50 am
Quote
which pin I could check for analog voltage?

according to - http://www.gassensing.co.uk/wp-content/uploads/2013/02/COZIR_-WideRange_-Datasheet_GSS.pdf -

pin 9 is analogue

Quote
I receive nothing :-(

A voltmeter should be able to detect a signal from the TX pin of the sensor.

this is the way to connect serial devices.
sensor TX --> Arduino RX
senosr RX <-- Arduino TX

(disclaimer, I don't have this sensor myself)
Title: Re: Temp, Humidity CO2 sensor and library.
Post by: foxlox on Mar 19, 2013, 06:48 am
Hi robtillaart,

I connected the sensor as you hint, with 5v instead of 3.3v and it works correctely.

thx!!!
Title: Re: Temp, Humidity CO2 sensor and library.
Post by: robtillaart on Mar 19, 2013, 07:40 am
Good to hear that!

If you have remarks wrt the lib please post it in this thread
that might be things missing, buggy, etc ;)
Title: Re: Temp, Humidity CO2 sensor and library.
Post by: jjaroska on Mar 25, 2013, 07:16 pm
Does anybody have a picture of the inside of the enclosure or DirtGambit, would it be too much trouble to see the inside of the box?  Also, does anybody have a link for that enclosure?

Thanks
Title: Re: Temp, Humidity CO2 sensor and library.
Post by: robtillaart on Mar 25, 2013, 07:27 pm
You can send him a PM with your request...
Title: Re: Temp, Humidity CO2 sensor and library.
Post by: lachaud on May 24, 2013, 03:41 pm
Hi DirtGambit ,
I have some difficulties to work with Cozir unit, to change mode and try in Mode2.
I try to send this lines : mySerial.print("K 2\r\n");
and then verify cozir received the message, expecting Response: "K #\r\n" like explain in " cozir software user's guide"
to test the response, I proceed like following:

//essai pour verifie rchangement de mode
while(buffer[ind-1] != 0x0A)
{
if(mySerial.available())
{
buffer[ind] = mySerial.read();
ind++;
}

for(int i=0; i < ind+1; i++)
{
Serial.write(buffer);//test
Serial.print("\t");
}
but the response is
? ? ?
So, should it be possible to have your library to understand how it works?
Best regards
Title: Re: Temp, Humidity CO2 sensor and library.
Post by: CAJP on Jul 19, 2013, 04:39 pm
Hi DirtGambit,

I have copied your library and your code, and my Arduino uno is happy with the code. I have attached the sensor to my arduino how you did. But when i look at the readings, they are just repeated every 0.5 Seconds and the co2, humidity and temperature reading first 3 digits are always the same.

Can you please suggest what i could be doing wrong

thank you very much!!
Title: Re: Temp, Humidity CO2 sensor and library.
Post by: robtillaart on Aug 31, 2013, 09:36 pm
a link from the manufacturer - schematic + simple code
see - http://www.co2meters.com/Documentation/AppNotes/AN128-%20Cozir_Arduino.pdf -
Title: Re: Temp, Humidity CO2 sensor and library.
Post by: Toledor on Jan 26, 2014, 04:41 pm
I don't get it to work, I hope someone can point out what's going wrong.

The sample sketch provided by Cozir (http://www.co2meters.com/Documentation/AppNotes/AN128-%20Cozir_Arduino.pdf), as posted earlier here, gives me weird values:
Code: [Select]
Co2 = ovf ppm
Co2 = ovf ppm
Co2 = ovf ppm
Co2 = ovf ppm
Co2 = -1152925184.00 ppm
Co2 = -1152925184.00 ppm
Co2 = ovf ppm
Co2 = ovf ppm
Co2 = ovf ppm
Co2 = ovf ppm
Co2 = ovf ppm
Co2 = ovf ppm
Co2 = ovf ppm
Co2 = ovf ppm
Co2 = ovf ppm
Co2 = ovf ppm
Co2 = -4208868864.00 ppm
Co2 = -4208868864.00 ppm
Co2 = ovf ppm


At first I didn't even get these values, but in the code Rx and Tx seem to be exchanged. Just switch the wires if you get no results at all.

I also tried the sketch in the first post of this topic the library 1.03 by Rob Tillaart & Michael Hawthorne, nothing happens. When I use the same sketch with the old library that's attached with the first post, I get this on my monitor:
Code: [Select]

Celcius : 0.00
Fahrenheit : 32.00
Humidity : 0.00
CO2 : 0
Digital Filter : 0


Sometimes after a reset, the values are a bit different, I think due to calibration or lack thereof:
Code: [Select]
Celcius : 0.20
Fahrenheit : 32.36
Humidity : 0.20
CO2 : 2
Digital Filter : 2


I uncommented the line below, because I read the sketch is supposed to work with polling and the default mode is streaming. The line implicates it's used to enable polling:
Code: [Select]
czr.SetOperatingMode(CZR_POLLING);

However, with the other line activated, to set streaming, I get similar readings:
Code: [Select]
Celcius : 0.10
Fahrenheit : 32.18
Humidity : 0.10
CO2 : 1
Digital Filter : 1


Title: Re: Temp, Humidity CO2 sensor and library.
Post by: robtillaart on Jan 26, 2014, 04:43 pm
As I still have no such sensor (my wishlist is too long)  I cannot replicate your problem, sorry
Title: Re: Temp, Humidity CO2 sensor and library.
Post by: blitzscholar on Apr 18, 2014, 05:45 am
Hey there!  I need assistance on my project using the COZIR sensor.

The first thing I did was hook up the sensor to my arduino and tested the sample code found on the COZIR website but I am getting the same results as Toledor. I would like the sensor to output co2, temp, and humidity readings.

Question for Dirt Gambit and Jon:

How do I use your C library in my arduino sketch? Is there a certain directory I need to save the library in? When I try to run the sketch that you had posted at first, I get several errors:

test1.ino:2:20: error: cozir.h: No such file or directory
test1:5: error: 'COZIR' does not name a type
test1.ino: In function 'void setup()':
test1:7: error: redefinition of 'void setup()'
Sen_design:32: error: 'void setup()' previously defined here
test1.ino: In function 'void loop()':
test1:15: error: redefinition of 'void loop()'
Sen_design:42: error: 'void loop()' previously defined here
test1:17: error: 'czr' was not declared in this scope

Help would be so much appreciated  :)
Title: Re: Temp, Humidity CO2 sensor and library.
Post by: robtillaart on Apr 18, 2014, 07:21 pm
about installation of libraries - http://arduino.cc/en/Guide/Libraries -

should be enough to get things compiling.
Title: Re: Temp, Humidity CO2 sensor and library.
Post by: blitzscholar on Apr 19, 2014, 04:33 am
Thanks for the quick response Rob!
I got the code to work in conjunction with the library however I seem to have the same issue that Toledor is having. My readings are :

Celcius : 0.20
Fahrenheit : 32.36
Humidity : 0.20
CO2 : 2
Digital Filter : 2

There is no change to these results, any idea what the cause might be?

Also the polling mode doesn't seem to be working properly . I un-commented the line which allowed the CZR_POLLING mode, however the code still spits out readings infinitely. 
Title: Re: Temp, Humidity CO2 sensor and library.
Post by: blitzscholar on Apr 19, 2014, 04:52 am
Please disregard my last post I just needed to switch my Rx and Tx  to pins 3 and 2 on my arduino board to match with the code  :smiley-roll-sweat:

However I would still like  to operate in polling mode. I am getting a constant flush of data from the sensor even though I am attempting to operate in polling mode. Here is my sketch:

Code: [Select]
#include <SoftwareSerial.h>
#include "cozir.h"

SoftwareSerial nss(3,2);
COZIR czr(nss);

void setup()
{
  Serial.begin(9600);
  delay(3000);
  czr.SetOperatingMode(CZR_POLLING);
  //czr.SetOperatingMode(CZR_STREAMING);
}

void loop()
{
  float t = czr.Celsius();
  float f = czr.Fahrenheit();
  float h = czr.Humidity();
  int c = czr.CO2();
  int digi = czr.GetDigiFilter();
 
  Serial.print("Celcius : ");Serial.println(t);
  Serial.print("Fahrenheit : ");Serial.println(f);
  Serial.print("Humidity : ");Serial.println(h);
  Serial.print("CO2 : ");Serial.println(c);
  Serial.print("Digital Filter : ");Serial.println(digi);   
}


Notice I un-commented the line where CZR_POLLING is. I may be going about the whole process wrong though...
Any ideas?
Title: Re: Temp, Humidity CO2 sensor and library.
Post by: robtillaart on Apr 19, 2014, 12:19 pm
apparently your code always return 2. (Fahrenheit is a 0.2Celsius  converted)

I peeked into the library and saw that there are open ends e.g. in the request function (which is at the core)

I have attached a datasheet with another Arduino program, you might give that a try.
Title: Re: Temp, Humidity CO2 sensor and library.
Post by: ftmilton on Jun 02, 2014, 04:19 am
I'm hoping somebody is still looking at this. I want to modify this library to work with hardware serial but there are some things I don't understand. Specifically, the constructor looks very strange.
Title: Re: Temp, Humidity CO2 sensor and library.
Post by: robtillaart on Jun 02, 2014, 07:20 pm
Can you explain what is strange in your opinion?
Title: Re: Temp, Humidity CO2 sensor and library.
Post by: DirtGambit on Jul 05, 2014, 11:48 pm
Hello and sorry I havent replied to any posts, i havent touched an arduino for a long time, so tonight i took my COZIR sensor and arduino and had a play around, i indeed got some of the problems that many of the users have been having, i was able to reproduce the errors and quickly found out that the sensor need calibrated, i will include the library to this post that i used tonight and also put some functions in and comment them out so in Setup() you can do calibration or change digital filter.

Here is the code i used for my main sketch

Code: [Select]

#include <SoftwareSerial.h>
#include "cozir.h"

SoftwareSerial nss(2,3);
COZIR czr(nss);

void setup()
{
 Serial.begin(9600);
 delay(5000);
 //czr.SetOperatingMode(CZR_POLLING);
 //czr.SetOperatingMode(CZR_STREAMING);
 //czr.CalibrateFreshAir();
// czr.SetDigiFilter(64);
}

void loop()
{
 delay(4000);
 float t = czr.Celsius();
 float f = czr.Fahrenheit();
 float h = czr.Humidity();
 int c = czr.CO2();
 int digi = czr.GetDigiFilter();

 Serial.print("Celcius : ");Serial.println(t);
 Serial.print("Fahrenheit : ");Serial.println(f);
 Serial.print("Humidity : ");Serial.println(h);
 Serial.print("CO2 : ");Serial.println(c);
 Serial.print("Digital Filter : ");Serial.println(digi);
 Serial.println("");
}


Some of the problems people have been having is due to not calibrating the sensor at first use

Here is a step by step
uncomment czr.SetOperatingMode(CZR_POLLING);
Upload Sketch
comment czr.SetOperatingMode(CZR_POLLING);
and uncomment czr.CalibrateFreshAir();
Upload Sketch
comment czr.CalibrateFreshAir();
Upload sketch
and if you want to change digital filter do the same, as the settings are kept on the sensor you never need to do these again unless you want to. I have found if i uncomment them all the sensor plays up and gives bad values until i comment out and upload the sketch again, i dont know why this is, it would have been such a better sensor if it were polling by default.

I have attached the lib i used tonight to this post

Regards
DirtGambit





Title: Re: Temp, Humidity CO2 sensor and library.
Post by: 34R7 on Jul 07, 2014, 07:13 pm
Here's what I have:

Arduino Uno R2 + Cozir Ambient + DirtGambit's work: running
Arduino Uno R3 + Cozir Ambient + DirtGambit's work: running
Arduino Mega 2560 R3 + Cozir Ambient + DirtGambit's work: fail

I'm guessing I need a logic inverter, to get a 5V signal on the Mega, since all else failed.

O, and thanks DirtGambit, you made the Cozir sensor a lot easier to use!

Blessings!
Title: Re: Temp, Humidity CO2 sensor and library.
Post by: DirtGambit on Jul 07, 2014, 08:06 pm
Thanks 34R7 but its Rob Tillaart you need to thank too.

Im glad your having success with the sensor, I tried using the sketch that GSS provided and I couldnt get it to work at all, the library still needs work and Im sure very soon it will happen.

Good luck with your projects.
Title: Re: Temp, Humidity CO2 sensor and library.
Post by: 34R7 on Jul 07, 2014, 09:21 pm
Dankuwel, Rob Tillaart!

I just ordered a logic converter, I'll let you know what's the outcome!

Cheers!
Title: Re: Temp, Humidity CO2 sensor and library.
Post by: vespapierre on Jul 07, 2014, 11:03 pm
Hi,
I have tried DirtGambit's library, tried all the possibilities ( 5v, pins, and so on ) what can be found here, but the same problem:
Code: [Select]
Celcius : 0.00
Fahrenheit : 32.00
Humidity : 0.00
CO2 : 0
Digital Filter : 0

I am trying this with Mega board...

Maybe there is a problem with the sofwareserial...? When using sim900 shield, there was a hwserial lib instead of the swserial...
Do you have any idea?
It would be great to make it work on mega...
Title: Re: Temp, Humidity CO2 sensor and library.
Post by: vespapierre on Jul 07, 2014, 11:29 pm
Dear All,

I have found the solution to use this sensor with mega 2560!
As Softwareserial notes:
"Note:
Not all pins on the Mega and Mega 2560 support change interrupts,
so only the following can be used for RX:
10, 11, 12, 13, 50, 51, 52, 53, 62, 63, 64, 65, 66, 67, 68, 69"

So let's connect the RX pin of the sensor to mega pin 51, and TX pin of the sensor to mega pin 50 and use DirtGambit's sketch with modification:

Code: [Select]
SoftwareSerial nss(50,51); // TX and RX pins

And smile when you realise this is working fine after calibration!
Title: Re: Temp, Humidity CO2 sensor and library.
Post by: 34R7 on Jul 08, 2014, 01:59 pm

Thanks, vespapierre! Works like a charm now!
Title: Re: Temp, Humidity CO2 sensor and library.
Post by: vespapierre on Jul 08, 2014, 03:26 pm
Happy to hear that!!! Great!
Note: if you are using MEGA 2560 board, and you are using ICSP pins ( for example wiznet shield ), than do not use 50 - 51 pins, because ICSP using in this case 50-51-52 pins. So in case try using other pins from the availables( for example 11-12).
Usually pin10 is wiznet, pin4 is sd, pin13 is onboard led on mega...
Anyway I do not know what and where is 62, 63, 64, 65, 66, 67, 68, 69  ...  :~
Title: Re: Temp, Humidity CO2 sensor and library.
Post by: 34R7 on Jul 08, 2014, 04:53 pm

Anyway I do not know what and where is 62, 63, 64, 65, 66, 67, 68, 69  ...  :~

Pin 54 is A0, so pin 62 is A8, 63 is A9, 64 is A10...  I just tried out A8-A9, and it works perfectly!
Hope thIS gives you some more options.
Title: Re: Temp, Humidity CO2 sensor and library.
Post by: vespapierre on Jul 09, 2014, 08:03 am
aaah thanks! I am testing on pins 11-12. works also perfect :)
Title: Re: Temp, Humidity CO2 sensor and library.
Post by: tartak00 on Jul 28, 2014, 12:34 am
Hello All. New to this forum and arduino so im at a loss for what im doing wrong.

I've used the new code/re calibration last listed to yield positive CO2 readings, yet my humidity and temperature readings are all 0. Is this an error within the sensor or my doing?
Title: Re: Temp, Humidity CO2 sensor and library.
Post by: hector915 on Sep 01, 2014, 03:31 pm
Hi everybody , Recently I've got the cozir wide range

(http://cdn.shopify.com/s/files/1/0019/5952/products/COZIR-Wide-Range-5-100_-CO2-Sensor_large.jpg?v=1371852847)

I just read the datasheet and the process  to do the calibration part is the same I've used the library provided by
DirtGambit and robtillaart  ,my doubt is about this process we need to calibrate the sensor each time I turn on the arduino device or I do need to recalibrate the sensor ?

Thanks for your support.

Hector
Title: Re: Temp, Humidity CO2 sensor and library.
Post by: roder on Oct 21, 2014, 04:06 am
I've used the library provided by
DirtGambit and robtillaart 
Where are these libraries available?  They do not seem to be attached to this thread. :(
Title: Re: Temp, Humidity CO2 sensor and library.
Post by: ajaved200 on Oct 21, 2014, 01:48 pm
Hi, I have the same issue as roder have.. I can not find the library cozir.h attached with the post
Title: Re: Temp, Humidity CO2 sensor and library.
Post by: ajaved200 on Oct 21, 2014, 03:24 pm
yes.
the lib is attached on the first post (it includes the .h file)
Dear robtillaart,
I can not find the cozir.h file attached with the first post. Could you please guide me how can I download this file.

Regards,
Abbas
Title: Re: Temp, Humidity CO2 sensor and library.
Post by: robtillaart on Oct 21, 2014, 04:06 pm
I have to search for the latest version I have, 
"I'll be back"
Title: Re: Temp, Humidity CO2 sensor and library.
Post by: robtillaart on Oct 21, 2014, 04:23 pm
Attached my latest cozir 0.1.03 lib version. There is a sketch included which compiles.
Disclaimer: I still do not have such sensor so I cannot test it in real life ...

@DirtGambit
Is it OK to include this library in my Github repo?
Title: Re: Temp, Humidity CO2 sensor and library.
Post by: ajaved200 on Oct 21, 2014, 05:03 pm
Thanks a lot Rob for the file.
I tried to run it but on monitor nothing is displaying. I am using 1.0.5 version. May be its compatibility issue ?
Title: Re: Temp, Humidity CO2 sensor and library.
Post by: ajaved200 on Oct 21, 2014, 05:29 pm
Dear Rob Tillart,
I tried to run this code http://www.co2meters.com/Documentation/AppNotes/AN128-%20Cozir_Arduino.pdf -

I tried to send the command " z\r\n" to get the co2 values but it didn't work for me so i removed the while condition and it started displaying co2 values

//while(buffer[ind-1] != 0x0A)
 {
 if(mySerial.available())
 {
 buffer[ind] = mySerial.read();
 ind++;
 }
 //}


Now when I included the cozir.h file and tried to run this code (posted by @DirtGambit) nothing is displaying on serial monitor. I tried this arduino 1.0.3 as well.

#include <SoftwareSerial.h>
#include "cozir.h"

SoftwareSerial nss(3,2);
COZIR czr(nss);

void setup()
{
 Serial.begin(9600);
 delay(3000);
 //czr.SetOperatingMode(CZR_POLLING);
 //czr.SetOperatingMode(CZR_STREAMING);
}

void loop()
{
 float t = czr.Celsius();
 float f = czr.Fahrenheit();
 float h = czr.Humidity();
 int c = czr.CO2();
 int digi = czr.GetDigiFilter();
 Serial.print("Celcius : ");Serial.println(t);
 Serial.print("Fahrenheit : ");Serial.println(f);
 Serial.print("Humidity : ");Serial.println(h);
 Serial.print("CO2 : ");Serial.println(c);
 Serial.print("Digital Filter : ");Serial.println(digi);   
}
Title: Re: Temp, Humidity CO2 sensor and library.
Post by: robtillaart on Oct 21, 2014, 06:16 pm
I can't say as I do not have a sensor to verify problems, sorry :(
(reaction on post - 2 :)
Title: Re: Temp, Humidity CO2 sensor and library.
Post by: robtillaart on Oct 21, 2014, 06:28 pm
Try this echo script
Code: [Select]

#include <SoftwareSerial.h>

SoftwareSerial nss(3,2);

void setup()
{
  Serial.begin(9600);
  nss.begin(9600);
  delay(3000);
}

void loop()
{
  if (nss.available())
  {
    Serial.write(nss.read());
  }
}


does it show anything?
Title: Re: Temp, Humidity CO2 sensor and library.
Post by: ajaved200 on Oct 21, 2014, 06:39 pm
lemme try i ll let u know in a while
Title: Re: Temp, Humidity CO2 sensor and library.
Post by: ajaved200 on Oct 21, 2014, 06:43 pm
yes it displayed this


ÿÿ

I have connected sensor RX -> with Arduino pin#3
and sensor TX _> with pin #2
Title: Re: Temp, Humidity CO2 sensor and library.
Post by: ajaved200 on Oct 21, 2014, 07:36 pm
I added Serial.println("test");

it displayed this text on monitor...
strangely I have to re insert the wire connecting the sensor's RX with microcontroller pin#3 in order to display the text on the serial monitor.
Title: Re: Temp, Humidity CO2 sensor and library.
Post by: robtillaart on Oct 21, 2014, 07:50 pm
yes it displayed this


ÿÿ

I have connected sensor RX -> with Arduino pin#3
and sensor TX _> with pin #2
that means you have made a hard wired connection between the hardware Serial and the software serial. 
The if statement in the code should do the copying but only if there is data available on the SW serial.
the yy indicates there is nothing to read()  (In fact it is ASCII char 255 or -1)
Title: Re: Temp, Humidity CO2 sensor and library.
Post by: robtillaart on Oct 21, 2014, 07:52 pm
Code: [Select]
#include <SoftwareSerial.h>

SoftwareSerial nss(3,2);

void setup()
{
  Serial.begin(9600);
  nss.begin(9600);
  delay(3000);
  nss.println("z")
}

void loop()
{
  if (nss.available() > 0)
  {
    Serial.write(nss.read());
  }
}


slightly different, added a "z" to tickle the sensor, and an explicit >0 test 
can you try?
Title: Re: Temp, Humidity CO2 sensor and library.
Post by: ajaved200 on Oct 21, 2014, 08:53 pm

I tried this as well.. still showing "ÿ"


Code: [Select]
#include <SoftwareSerial.h>

SoftwareSerial nss(3,2);

void setup()
{
  Serial.begin(9600);
  nss.begin(9600);
  delay(3000);
  nss.println("z")
}

void loop()
{
  if (nss.available() > 0)
  {
    Serial.write(nss.read());
  }
}


slightly different, added a "z" to tickle the sensor, and an explicit >0 test
can you try?
Title: Re: Temp, Humidity CO2 sensor and library.
Post by: robtillaart on Oct 21, 2014, 09:28 pm
Which version of the IDE are you using?
Which board? UNO/MEGA?
The COZIR is connected to pin 2 and 3 I assumed....Right?
Title: Re: Temp, Humidity CO2 sensor and library.
Post by: ajaved200 on Oct 21, 2014, 10:07 pm
I am using audrino UNO and yess cozir is connected with pin 2 and pin 3
Title: Re: Temp, Humidity CO2 sensor and library.
Post by: ajaved200 on Oct 21, 2014, 10:08 pm
i tried with audrino v 1.0.5 and v 1.0.3 as well
Title: Re: Temp, Humidity CO2 sensor and library.
Post by: robtillaart on Oct 21, 2014, 10:14 pm
I'm out of options now, as said before I do not have such sensor to replicate your project, sorry
Title: Re: Temp, Humidity CO2 sensor and library.
Post by: ajaved200 on Oct 21, 2014, 10:25 pm
I can understand. Thanks a lot for your help.
Title: Re: Temp, Humidity CO2 sensor and library.
Post by: roder on Nov 02, 2014, 02:20 am
I posted the Cozir library to github for convenience (and a project that I was working on).

https://github.com/roder/cozir
Title: Re: Temp, Humidity CO2 sensor and library.
Post by: roder on Nov 06, 2014, 07:07 am
For anyone is using a Pinoccio (https://pinocc.io), which uses the ATmega2560 chip:

I used `SCK` as pin `11` and `MISO` as pin `12`

`SoftwareSerial nss(11,12);`

https://github.com/Pinoccio/core-pinoccio/blob/master/avr/variants/pinoccio/pins_arduino.h#L48-L49
Title: Re: Temp, Humidity CO2 sensor and library.
Post by: nitrojet on Nov 09, 2014, 07:55 pm
Interestingly as soon as I take a reading in my loop:

lcd.setCursor(0,0);lcd.print("CO2 : "); lcd.print(czr.CO2());

My ads1115 running on the i2c bus stops taking readings!

If I rem this line out, it works fine??

The CO2 reading works fine anyway.
Title: Re: Temp, Humidity CO2 sensor and library.
Post by: robtillaart on Nov 09, 2014, 08:23 pm
Quote
nterestingly as soon as I take a reading in my loop:
lcd.setCursor(0,0);lcd.print("CO2 : "); lcd.print(czr.CO2());
My ads1115 running on the i2c bus stops taking readings!
If I rem this line out, it works fine??
The CO2 reading works fine anyway.
The I2C communication might be corrupted by the LCD communication. How is the LCD connected?

do you have pull ups on the I2C lines?
Title: Re: Temp, Humidity CO2 sensor and library.
Post by: nitrojet on Nov 09, 2014, 08:28 pm
Wow, you are a great help to the community!

Yes I am using an i2c LCD but like I say, it works fine if I remark out any lines that read the cozir, but as soon as I take a reading from the cozir, the communication with the adc on the i2c bus stops!

My sketch takes a reading from the adc first, this works and is displayed on the lcd (and the serial monitor), then a CO2 which works fine, on the next loop (as a CO2 reading has now been taken) the adc (ads1115) returns nothing!

Very strange.
Title: Re: Temp, Humidity CO2 sensor and library.
Post by: nitrojet on Nov 09, 2014, 09:52 pm
I thought the pullup resistors are internal to the atmega, I am using an Uno.

Should I disable them? I think they are software turn offable.
Title: Re: Temp, Humidity CO2 sensor and library.
Post by: robtillaart on Nov 09, 2014, 10:27 pm
I am no EE but IIRC the internal pullups are not the right size.
the longer the wires the lower the pullups should be.
Title: Re: Temp, Humidity CO2 sensor and library.
Post by: nitrojet on Nov 10, 2014, 01:02 am
Interestingly, if I pull the LCD out completely, no change (as I half expected)

If I pull the CO2 out of pins 2 & 3, the ADS1115 works fine (as I also half expected)

If I try the softwareserial on pins 10&11, I accidentally plugged in back to front, the ADC worked fine, as soon as I turned the CO2 round the right way, the ADC stopped reading.

Must be a conflict in the code?

I will try the alternative sample code maybe?
Title: Re: Temp, Humidity CO2 sensor and library.
Post by: robtillaart on Nov 10, 2014, 08:58 pm
Can it be that all the devices together draw to much current, thereby corrupting signals.??
Title: Re: Temp, Humidity CO2 sensor and library.
Post by: nitrojet on Nov 10, 2014, 10:28 pm
No as the lcd is 40ma and when I unplug that, it still plays up yet I can even run a TIP120 or other devices. The USB is 500ma and so I am not sure.

I appreciate your help.

I am a bit stuck.
Title: Re: Temp, Humidity CO2 sensor and library.
Post by: robtillaart on Nov 11, 2014, 08:41 pm
Arduino can only deliver 5-10mA per pin - check the specification, - http://arduino.cc/en/Main/arduinoBoardUno -


or try it with a separate power supply for the peripherals...(GND's connected)
Title: Re: Temp, Humidity CO2 sensor and library.
Post by: nitrojet on Nov 11, 2014, 10:01 pm
Well the CO2 and the ADS1115 connected and just using the serial monitor as you suggested but still not. The LCD draws 40ma but straight from the 5v rail, not from one of the PWM or Analog outs, even still, I get the first result in from the ADS1115 and then the result from the cozir and then once it tries to get another result from the ADS1115, it returns nothing. Very weird
Title: Re: Temp, Humidity CO2 sensor and library.
Post by: Dew321 on Nov 12, 2014, 03:46 am
Hi Everyone,

     I recently purchased a COZIR wide range 65% sensor, and I hooked it up to Arduino using the code and library provided by DirtGambit. However, for the sensor when it reaches around 65000 ppm it will undergo a wrap-around, where it will start back at 0 and then continue going up from there. Does anyone know if this is caused by something in the code that DirtGambit provided, or if it is something to do with the sensor itself (since it is 65%, so it should go to 650,000 ppm according to the CO2meter website, not 65000).

      Thank you
     
Title: Re: Temp, Humidity CO2 sensor and library.
Post by: robtillaart on Nov 13, 2014, 07:50 pm
Can be something in the code, I'll need to check.

an unsigned int wraps around at 65355->0

which version of the library are you using?

can you post your sketch and point out where it wraps?

Title: Re: Temp, Humidity CO2 sensor and library.
Post by: robtillaart on Nov 13, 2014, 07:55 pm
The interface of the 01.03 version has a

   uint16_t CO2();

that means it can only return values between 0..65535
Title: Re: Temp, Humidity CO2 sensor and library.
Post by: Dew321 on Nov 13, 2014, 10:46 pm
Hi robtillaart,

    Thank you for your reply! The sketch I am using is the exact same as the one that DirtGambit provided, although I changed int c = czr.CO2();  into a double so that the range would be wider, as with just an int it faltered at around 13000 ppm. Also, I am using the library from github that was provided by Roder on this thread.
    Hmm...I'll take a look at the uin16_tCO2(). Is there a way to make it go up to around 650,000 ppm, which is the max for this particular sensor?
Title: Re: Temp, Humidity CO2 sensor and library.
Post by: robtillaart on Nov 14, 2014, 07:22 pm
Hi robtillaart,
...
    Hmm...I'll take a look at the uin16_tCO2(). Is there a way to make it go up to around 650,000 ppm, which is the max for this particular sensor?
definitely there is, however I (still) do not have such sensor so I can try to provide a patch that you should test. (did work earlier in this thread ;)
Title: Re: Temp, Humidity CO2 sensor and library.
Post by: robtillaart on Nov 14, 2014, 07:32 pm
in the main sketch change this
Code: [Select]
void loop()
{
  Serial.println("Loop");
  float t = czr.Celsius();
  float f = czr.Fahrenheit();
  float h = czr.Humidity();
  uint32_t c = czr.CO2();
 
  Serial.print("Celcius = ");Serial.println(t);
  Serial.print("Fahrenheit = ");Serial.println(f);
  Serial.print("Humidity = ");Serial.println(h);
  Serial.print("CO2 = ");Serial.println(c);
 
  delay(3000);
}



in cozir.h change this
Code: [Select]
uint32_t CO2();


in cozir.cpp change this
Code: [Select]
uint32_t COZIR::CO2()
{
  return Request("Z");
}

and

Code: [Select]

uint32_t COZIR::Request(const char* s)
{
  Command(s);
  // empty buffer
  buffer[0] = '\0';
  // read answer; there may be a 100ms delay!
  // TODO: PROPER TIMEOUT CODE.
  delay(200);
  int idx = 0;
  while(CZR_Serial.available())
  {
    buffer[idx++] = CZR_Serial.read();
  }
  buffer[idx] = '\0';

  uint32_t rv = 0;
  switch(buffer[0])
  {
    case 'T' :
rv = atoi(&buffer[5]);
            if (buffer[4] == 1) rv += 1000;
// negative values are mapped above 1000..1250 => capture this in Celsius()
break;
default :
rv = atol(&buffer[2]);
break;
  }
  return rv;
}


and change the version number to 0.1.04 if it works  :)


Title: Re: Temp, Humidity CO2 sensor and library.
Post by: Dew321 on Nov 15, 2014, 09:43 pm
Hi robtillart,


      Thank you for your advice! The wrap-around is fixed now for the sensor. Also, would you know how exactly the CalibrateFreshAir() function works within this library? Does it calibrate by setting the measured air of the environment to 450 ppm and then basing measurements relative to that, or does it do it some other way?
       I am also looking to try to incorporate a small heating element into the arduino, perhaps through the use of nichrome wire. Would you know any possible sources for code that is able to regulate the temperature of the wire, or any references in general?

       Thanks again for all your help :)
Title: Re: Temp, Humidity CO2 sensor and library.
Post by: robtillaart on Nov 16, 2014, 02:19 pm
Hi robtillart,

      Thank you for your advice! The wrap-around is fixed now for the sensor.
good to hear!,
I'll make a 0.1.04 version of the library today.


Quote
Also, would you know how exactly the CalibrateFreshAir() function works within this library? Does it calibrate by setting the measured air of the environment to 450 ppm and then basing measurements relative to that, or does it do it some other way?
As said earlier in the thread, I do not have such sensor, so nada hands on experience.


Quote
I am also looking to try to incorporate a small heating element into the arduino, perhaps through the use of nichrome wire. Would you know any possible sources for code that is able to regulate the temperature of the wire, or any references in general?
What is the temperature range you want to control?
How accurate / precise should it be?

You might need a PID controller.


Title: Re: Temp, Humidity CO2 sensor and library.
Post by: robtillaart on Nov 16, 2014, 04:26 pm
Added the cozir lib to github so it is tracked also outside this thread.
URL: - https://github.com/RobTillaart/Arduino/tree/master/libraries -

current version is 0.1.04,
+ added larger range for CO2()
Title: Re: Temp, Humidity CO2 sensor and library.
Post by: ukcircus on Dec 06, 2014, 11:33 pm
Hi Rob,

Thanks for your work on this library, its helped my project along really nicely. I just installed the 1.04 version and decided to test it with your sketch1.ino file with an UNO (Arduino 1.06). It is now throwing up some errors at me:

/Users/Ukcircus/Documents/Arduino/libraries/cozir/cozir.cpp:232: error: prototype for 'uint32_t COZIR::Request(const char*)' does not match any in class 'COZIR'
/Users/Ukcircus/Documents/Arduino/libraries/cozir/Cozir.h:100: error: candidate is: uint16_t COZIR::Request(const char*)

Version 1.0 of the library still works fine. Unfortunately I don't have enough experience with libraries to really troubleshoot this myself. Any advice?

Thanks
Title: Re: Temp, Humidity CO2 sensor and library.
Post by: robtillaart on Dec 07, 2014, 11:24 am
My mistake

you need to     

uint16_t Request(const char* );

into

uint32_t Request(const char* );


in the .h file

I will update github asap
Title: Re: Temp, Humidity CO2 sensor and library.
Post by: robtillaart on Dec 07, 2014, 11:42 am
updated .h in https://github.com/RobTillaart/Arduino/tree/master/libraries/Cozir
Title: Re: Temp, Humidity CO2 sensor and library.
Post by: ukcircus on Dec 15, 2014, 10:02 am
Awesome thanks!
Title: Re: Temp, Humidity CO2 sensor and library.
Post by: robtillaart on Dec 15, 2014, 08:57 pm
You're welcome, if there are more questions let me know.
Title: Re: Temp, Humidity CO2 sensor and library.
Post by: Dew321 on Dec 19, 2014, 07:50 am
Hi RobTillart,

     Do you know of any good PID controllers for regulating the temperature of a heating element and of how to tune the variables? We are currently using the one by brett beauregard (http://playground.arduino.cc/Code/PIDLibrary) but we are having trouble with it, especially with the tuning variables.

       Thanks
Title: Re: Temp, Humidity CO2 sensor and library.
Post by: robtillaart on Dec 19, 2014, 10:50 am
Hi Dew321,

these pages about PID tuning might be helpful - http://www.pcbheaven.com/wikipages/PID_Theory/?p=1 -
Title: Re: Temp, Humidity CO2 sensor and library.
Post by: Dew321 on Dec 19, 2014, 09:14 pm
Thank you Rob!

    I'm currently using the arduino to heat up a nichrome wire by attaching alligator clips from the arduino wire to the nichrome wire, and then using PWM (through the analogwrite function) to regulate the temperature. However, when I set to analogwrite function to around 80 or more, the serial port will suddenly disconnect. I have verified this, as it works when the alligator clips connecting the nichrome wire to the arduino wire are not attached to it but will disconnect right when the clip meets the wire.
        Would you happen to know why this occurs and how to prevent it? It seems to happen only when I am using the co2 sensor and the heating element on the arduino board at the same time (i put the code regulating both in the same sketch, and it works fine at lower values in the analogwrite)

     
Title: Re: Temp, Humidity CO2 sensor and library.
Post by: CrossRoads on Dec 19, 2014, 09:33 pm
Gonna smoke your output pin. They are only good for 40mA absolute max. With low analogwrite levels there may be some recovery time between narrow  high pulses - but as they get wider you are really overstressing things, if you haven't damaged the pin already.
The USB port is only good for 500mA, that is likely part of the issue as well.
Title: Re: Temp, Humidity CO2 sensor and library.
Post by: Dew321 on Dec 20, 2014, 07:36 am
Hi CrossRoads,

     Thank you for your help! Sorry for the cross posts; won't do those again. As for the problem, I fixed it by simply  connecting a 4 battery pack (4 AA batteries--6 v), which got rid of it. 
Title: Re: Temp, Humidity CO2 sensor and library.
Post by: Dew321 on Dec 25, 2014, 01:39 am
Also, has anyone experienced changes in CO2 readings when the CO2 sensor is exposed to higher temperatures, like 40 deg C? I'm not sure how high a temperature would start altering the values somehow
Title: Re: Temp, Humidity CO2 sensor and library.
Post by: robtillaart on Dec 25, 2014, 11:40 am
@Dew321
Have you checked the datasheet for info?
if so what does it say?
Title: Re: Temp, Humidity CO2 sensor and library.
Post by: Dew321 on Dec 26, 2014, 09:36 am
@robtillaart. Yep, I have, and that's what the trouble is. It should be that the co2 sensor handles up to around 50 deg C fine, but when I am using it near a heat source that's not even close to that range it begins to act strangely. For instance, the Co2 levels will suddenly drop to an abnormally low level, and it'll stay that way for a bit. Not sure if it's a problem with the sensor, or if the heat is affecting the environment around it
Title: Re: Temp, Humidity CO2 sensor and library.
Post by: Dew321 on Dec 29, 2014, 07:49 am
Actually, I found this article (http://www.mdpi.com/1424-8220/13/6/7079) that describes how temperature and pressure will affect the reading of the sensor by giving an altered co2 levels (as would temp/press affects the number of co2 molecules read by the sensor, but not the actual concentration). Should be useful to anyone else who might be wondering why their sensor is giving different readings.
       However, would anyone have any idea for possible arduino code that could correct for this difference? And also my sensor is shooting up in co2 value much more than what is given in the article.
       
Title: Re: Temp, Humidity CO2 sensor and library.
Post by: robtillaart on Dec 29, 2014, 11:30 am
They are talking about a different sensor , possibly a different technique if I read correctly (just scanned it quickly). That means deviations are also different.

The only solution I can imagine is to define a lookup table that gives a correction factor for a certain temperature.

then you would get a formula something like

// e.g. temp from 40..200degrees in steps of 4 degrees
float correctionFactor[40]; 

index = constrain (readTemperature() - 40)/4, 0, 40);

CO2 = readCO2() * correctionFactor[ index ];


Title: Re: Temp, Humidity CO2 sensor and library.
Post by: nitrojet on Jan 13, 2015, 12:08 am
Rob, I notice your library is hard coded (would need to edit the cozir.h or cozir.cpp file) to use the software serial?

Is this the case and what would need to be changed to use hardware serial on say a Leonardo board?

Many thanks
Title: Re: Temp, Humidity CO2 sensor and library.
Post by: robtillaart on Jan 13, 2015, 07:19 pm
you should be able to do something like this
Code: [Select]

#include <cozir.h>

#if defined(ARDUINO) && ARDUINO >= 100
  #include <SoftwareSerial.h>
  SoftwareSerial nss(3,2);
#else
  #include <NewSoftSerial.h>
  NewSoftSerial nss(3,2);
#endif


COZIR czr(nss);

COZIR czr2(Serial);



disclaimer: I do not have such sensor, only helped build the lib
Title: Re: Temp, Humidity CO2 sensor and library.
Post by: nitrojet on Jan 15, 2015, 01:28 am
Thank you for your quick response but I get the following

Arduino: 1.5.8 (Windows 7), Board: "Arduino Mega or Mega 2560, ATmega2560 (Mega 2560)"

sketch1.ino:13:18: error: no matching function for call to 'COZIR::COZIR(HardwareSerial&)'
sketch1.ino:13:18: note: candidates are:
In file included from sketch1.ino:1:0:
C:\Users\Acer\Documents\Arduino\libraries\cozir/cozir.h:58:2: note: COZIR::COZIR(SoftwareSerial&)
  COZIR(SoftwareSerial&);// : CZR_Serial(nss)
  ^
C:\Users\Acer\Documents\Arduino\libraries\cozir/cozir.h:58:2: note:   no known conversion for argument 1 from 'HardwareSerial' to 'SoftwareSerial&'
C:\Users\Acer\Documents\Arduino\libraries\cozir/cozir.h:55:7: note: COZIR::COZIR(const COZIR&)
 class COZIR
       ^
C:\Users\Acer\Documents\Arduino\libraries\cozir/cozir.h:55:7: note:   no known conversion for argument 1 from 'HardwareSerial' to 'const COZIR&'
Error compiling.
Title: Re: Temp, Humidity CO2 sensor and library.
Post by: robtillaart on Jan 15, 2015, 06:48 pm
dived into the library code, indeed hardware Serial is not supported.
Sorry

update: I might be able to patch the lib coming weekend to support HWSerial
Title: Re: Temp, Humidity CO2 sensor and library.
Post by: robtillaart on Jan 17, 2015, 01:34 pm
Made a 0.1.06BETA version of the Cozir library: see attachment

+ added support for HardwareSerial
+ removed pre 1.0 Arduino support ==> removed support for NewSOftSerial
+ added examples for HW Serial

as I do not have a sensor I cannot test:
- compiles for UNO, MEGA  (AVR only)

@nitrojet
Can you please give it a try, and report if it works / other remarks.
Title: Re: Temp, Humidity CO2 sensor and library.
Post by: nitrojet on Jan 21, 2015, 12:56 am
Sorry, don't seem to be able to get it to work on a Mega2560, no code appears in the serial window, I assume that just calling the nss or sws instance causes the program to halt as it doesn't even display the

 Serial.print("Cozir SoftwareSerial: ");
 
and when I delete all cozir related, I am able to print to the serial window. Is it to do with level shifting required?
Title: Re: Temp, Humidity CO2 sensor and library.
Post by: nitrojet on Jan 22, 2015, 11:12 pm
Just decided to run it all at 5v which is ok according to the datasheet and still no luck either software or hardware mode on the Arduino Mega2560

Very strange
Title: Re: Temp, Humidity CO2 sensor and library.
Post by: robtillaart on Jan 23, 2015, 07:16 pm
Can you post the exact code you use?
And you are running it on a MEGA?
Title: Re: Temp, Humidity CO2 sensor and library.
Post by: nitrojet on Jan 23, 2015, 10:46 pm
Here is the software example

Code: [Select]
//
//    FILE: CozirDemoSoftwareSerial.ino
//  AUTHOR: Rob Tillaart
// VERSION: 0.1.00
// PURPOSE: demo of Cozir lib
//    DATE: 2015-jan-17
//     URL: http://forum.arduino.cc/index.php?topic=91467.0
//
// Released to the public domain
//

#include <cozir.h>

#include <SoftwareSerial.h>
//SoftwareSerial sws(3, 2);
SoftwareSerial sws(50,51); // TX and RX pins

COZIR czr(sws);

void setup()
{
  Serial.begin(9600);
  Serial.print("Cozir SoftwareSerial: ");
  Serial.println(COZIR_LIB_VERSION);
  Serial.println();

  delay(1000);
}

void loop()
{
  float t = czr.Celsius();
  float f = czr.Fahrenheit();
  float h = czr.Humidity();
  uint32_t c = czr.CO2();

  Serial.print("Celcius =\t");    Serial.println(t);
  Serial.print("Fahrenheit =\t"); Serial.println(f);
  Serial.print("Humidity =\t");   Serial.println(h);
  Serial.print("CO2 =\t");        Serial.println(c);
  Serial.println();

  delay(3000);
}





and here is the hardware example


Code: [Select]
//
//    FILE: CozirDemoHardwareSerial.ino
//  AUTHOR: Rob Tillaart
// VERSION: 0.1.00
// PURPOSE: demo of Cozir lib (>= 0.1.06)
//    DATE: 2015-jan-17
//     URL: http://forum.arduino.cc/index.php?topic=91467.0
//
// Released to the public domain
//

#include <cozir.h>
// <SoftwareSerial.h> is needed as the cozir lib uses it.
// even when only using HWserial
#include <SoftwareSerial.h>

// HARDWARE INSTANCE for MEGA
COZIR czr(Serial1);

void setup()
{
  Serial.begin(9600);
  Serial.print("Cozir HardwareSerial: ");
  Serial.println(COZIR_LIB_VERSION);
  Serial.println();

  delay(1000);
}

void loop()
{
  float t = czr.Celsius();
  float f = czr.Fahrenheit();
  float h = czr.Humidity();
  uint32_t c = czr.CO2();

  Serial.print("Celcius =\t");    Serial.println(t);
  Serial.print("Fahrenheit =\t"); Serial.println(f);
  Serial.print("Humidity =\t");   Serial.println(h);
  Serial.print("CO2 =\t");        Serial.println(c);
  Serial.println();

  delay(3000);
}


I am using a Arduino Mega 2560
Title: Re: Temp, Humidity CO2 sensor and library.
Post by: robtillaart on Jan 24, 2015, 11:55 am
could you please do the following -

Hardware Serial example comment the whole content of loop();

in the library comment the content of the constructor so it doesn't do (almost) anything.

Code: [Select]

COZIR::COZIR(HardwareSerial& hws) : CZR_Serial(hws)
{
    # hws.begin(9600);
    # init();
}


this way only part of the constructor is called and we can see where it blocks.
Title: Re: Temp, Humidity CO2 sensor and library.
Post by: nitrojet on Jan 24, 2015, 09:06 pm
OK, I now get

Cozir HardwareSerial: 0.1.06

in the serial monitor.

Title: Re: Temp, Humidity CO2 sensor and library.
Post by: robtillaart on Jan 25, 2015, 10:24 am
now lets add one line to the constructor - setting the baudrate

Code: [Select]

COZIR::COZIR(HardwareSerial& hws) : CZR_Serial(hws)
{
    hws.begin(9600);
    # init();
}


please check the output.
Title: Re: Temp, Humidity CO2 sensor and library.
Post by: nitrojet on Jan 25, 2015, 10:50 am
I get

C:\Users\Acer\Documents\Arduino\libraries\cozir\cozir.cpp:32:7: error: invalid preprocessing directive #init
     # init();
       ^
Error compiling.

but if I do this

Code: [Select]
COZIR::COZIR(HardwareSerial& hws) : CZR_Serial(hws)
{
    hws.begin(9600);
//    # init();
}


I get this

Cozir HardwareSerial: 0.1.06

Title: Re: Temp, Humidity CO2 sensor and library.
Post by: robtillaart on Jan 25, 2015, 11:00 am
Sorry # is python comment my mistake

nest step is to add the init step
Code: [Select]

COZIR::COZIR(HardwareSerial& hws) : CZR_Serial(hws)
{
    hws.begin(9600);
    init();
}
Title: Re: Temp, Humidity CO2 sensor and library.
Post by: robtillaart on Jan 25, 2015, 11:02 am
might be time to write a Cozir simulator ... (should not be too hard)
Title: Re: Temp, Humidity CO2 sensor and library.
Post by: nitrojet on Jan 25, 2015, 11:51 am
Ok, I have done that, but surely we are back to how it was and I now have nothing in the serial monitor.

I really appreciate the time you are spending on this.
Title: Re: Temp, Humidity CO2 sensor and library.
Post by: robtillaart on Jan 25, 2015, 12:46 pm
please make the following changes to the library

Code: [Select]

void COZIR::init()
{
    SetOperatingMode(CZR_POLLING);
    // delay for initialization
    delay(1200);
}

void COZIR::SetOperatingMode(uint8_t mode)
{
    sprintf(buffer, "K %u", mode);

    Serial.println(buffer);
    Serial.flush();

    Command(buffer);
}

void COZIR::Command(const char* s)
{
    // TODO
    // CZR_Serial.println(s);

    Serial.println("1");
    Serial.flush();

    CZR_Serial.print(s);

    Serial.println("2");
    Serial.flush();

    CZR_Serial.print("\r\n");

    Serial.println("3");
    Serial.flush();
}


setting the operating mode does send a string "k 2\r\n" to the device. With the above changes we should see where it blocks. My assumption is it will print the "1" and then stops.
 
Title: Re: Temp, Humidity CO2 sensor and library.
Post by: nitrojet on Jan 25, 2015, 01:21 pm
Interestingly enough, I modified the library as you said and it returns

bäÿ

in the serial monitor!
Title: Re: Temp, Humidity CO2 sensor and library.
Post by: robtillaart on Jan 25, 2015, 01:23 pm
OK, clearly the initialization fails ... time to think ...
Title: Re: Temp, Humidity CO2 sensor and library.
Post by: nitrojet on Jan 30, 2015, 12:24 am
Is there a way that I can just use serial.read to just check the cozir is plugged in correctly?


Regards
Title: Re: Temp, Humidity CO2 sensor and library.
Post by: nitrojet on Feb 01, 2015, 01:40 pm
How about I test this on an Uno first to find where the problem is, however I would need to output the data as an LED light up or lcd screen as obviously the uno only has one hardware serial
Title: Re: Temp, Humidity CO2 sensor and library.
Post by: Toledor on Feb 13, 2015, 02:40 pm
Some of the problems people have been having is due to not calibrating the sensor at first use

Here is a step by step
uncomment czr.SetOperatingMode(CZR_POLLING);
Upload Sketch
comment czr.SetOperatingMode(CZR_POLLING);
and uncomment czr.CalibrateFreshAir();
Upload Sketch
comment czr.CalibrateFreshAir();
Upload sketch
Thanks so much DirtGambit and Rob, thanks to this, my sensor is finally functional!

There is one small issue though: after I upload DirtGambit's sketch or switch off/on the Uno, I have to disconnect the wire in pin 3, that is connected to the sensor's Rx. Does anyone understand why? Nevermind, there is just a delay of nearly 13 seconds before the first values are shown and I was impatient.
Title: Re: Temp, Humidity CO2 sensor and library.
Post by: kslstn on Feb 15, 2015, 06:11 pm
Hi all, I put some sketches for the CoZIR sensor on GitHub (https://github.com/kslstn/CozirTools). Most notably a logger for measurement. Because I ran in quite a few problems before I got my sensor working, I took DirtGambit's code and split it up into different tools with comments about how I got things to work. Another reason to split it up into separate files is that this way you can just upload, say the Calibrate sketch to calibrate the sensor and then upload another sketch again to do something else with the sensor. That way you don't have to comment and uncomment lines in one file all the time.

I hope the sketches are useful to other novice users like myself!

My next tool will be getting measurements to show on an LCD panel, after that I want to make a program that logs measurements to an SD card.
Title: Re: Temp, Humidity CO2 sensor and library.
Post by: Dew123 on Feb 19, 2015, 08:02 pm
Hi everyone,

     A few posts back I think I posted about the distortions in the readings from the CO2 sensor when it is placed in an environment with a hotter temperature. We talked to the CO2meter company, and we found that to fix the discrepancy we needed to keep a continuous flow of air across the sensor with a mini vacuum pump, since increasing the temp/pressure will alter the turbulence of the air flow (also, if you put the sensor into a sealed chamber and heat it, the co2 levels will increase because of the pressure increase, making the CO2 molecules move around more quickly and registered more frequently by the NDIR sensor)
      If you use a small vacuum pump (like one from Parker, 3.3V and around $30) it should eliminate any factors caused by pressure and temperature. Just some advice.

   
      Also, would anyone happen to know how to create a running slope without keeping the array/buffer from maxing out? (kinda like a running average, except you measure the slope continuously as you get more data points). Would a circular buffer work for this kind of thing, so that it will only take the average slope between like 5 points at a time?

      Thanks for your hellp
Title: Re: Temp, Humidity CO2 sensor and library.
Post by: Dew321 on Feb 22, 2015, 05:57 am
Hi Everyone,

    Oops, it seems that I used another account when posting the previous post. However, would anyone know how to calculate the rate of change of generated values using arduino (for example, calculating the rate of change of a CO2 increase or decrease)? Would it be possible to use an array without it maxing out in values over time (like it would clear itself periodically?)

     Thanks
Title: Re: Temp, Humidity CO2 sensor and library.
Post by: Simas32 on Feb 23, 2015, 12:58 am
Hey everyone.  First time poster here. 

Currently running the Github library and sample sketch posted by DirtGambit (thanks much).  The COZIR is functioning when I run a sketch shell on my Mega 2560 and hook up the COZIR TX and RX to the hardware serial on the Mega.  The humidity, temp, and CO2 readings all display on the output.  However, when I run the sample sketch, the output is as follows:

Fahrenheit : 32.00
Humidity : 0.00
CO2 : 0
Digital Filter : 0

When I adjust the Digital Filter value, all values change directly with the digital filter value.  I'm stumped.  Please help.
Title: Re: Temp, Humidity CO2 sensor and library.
Post by: kslstn on Feb 26, 2015, 09:43 am
@Dew321, I'm not entirely sure if I understand entirely what you want, but I created a sketch for logging to SD card where from 15 measurements only the highest and lowest are saved and an average is calculated for every added measurement, until the measurements for that array are saved. If I'm correct, you want to see the velocity at which change happens, so you only have to divide by time after that. The snippet:

Code: [Select]
  float currentCO2 = (float)czr.CO2();
  if ((currentCO2 < CO2[0]) || (pollsInLog == 1)){// save CO2 if it's the lowest value in this interval.
    CO2[0] = currentCO2;
  }
  CO2[1] = (CO2[1] * (pollsInLog - 1)/pollsInLog) + (currentCO2 / pollsInLog);// calculate and save average
  if (currentCO2 > CO2[2]){// save CO2 if it's the highest value in this interval.
    CO2[2] = currentCO2;
  }


The complete sketch is over here on GitHub (https://github.com/kslstn/CozirTools/blob/master/LogToSD/LogToSD.ino).


If others are looking for inspiration for logging measurements to an SD card: check it out :-)
Title: Re: Temp, Humidity CO2 sensor and library.
Post by: kslstn on Feb 26, 2015, 09:46 am
@Simas, I had that issue too. After setting the sensor to poll mode and starting the calibration manually, everything worked fine with Rob's library.

You can find the sketches to do that here on the forum and in my repo (https://github.com/kslstn/CozirTools) (the SetToPolling and Calibrate sketches).

Edit: Also make sure the sensor is connected to pin 2 and 3. If, after trying the above, you still get the 0 readings, try switching the wires, there was some confusing about Tx and Rx. And give it a bit of time. After it's connected to power, the sensor needs about 5 seconds before it measures correctly.
Title: Re: Temp, Humidity CO2 sensor and library.
Post by: nitrojet on Mar 03, 2015, 12:59 pm
Simas, how did you get it working with the Mega 2560?

I don't seem to be able to get too far with hardware serial, considering writing from scratch but if you have been able to, please let me know.

Regards
Title: Re: Temp, Humidity CO2 sensor and library.
Post by: robtillaart on Mar 04, 2015, 10:01 pm
@nitrojet

took a while before I had some time to dive into it. Find attached a version of the library with a slightly modified interface that works for both Software and HardwareSerial; The examples compile on a MEGA

Please give it a try

Title: Re: Temp, Humidity CO2 sensor and library.
Post by: kslstn on Mar 08, 2015, 09:01 pm
Because I wanted to get rid of the delay() in the library (my project has fading LEDs), I created a rather different version (https://github.com/kslstn/Kozir). It's a bit harder to use and doesn't have all the settings and calibration functions. In return it's a bit lighter and as mentioned, doesn't slow your loop down.

Instead of a single call, two functions have to be called from within the loop. The first send the request to the sensor, the second is repeated until the sensor returns data 200 ms later. There's an example sketch (https://github.com/kslstn/Kozir/blob/master/example/simpleExample/simpleExample.ino) that demonstrates it. Maybe it's of use to anyone!
Title: Re: Temp, Humidity CO2 sensor and library.
Post by: jmnijsse on Apr 08, 2015, 01:39 pm
Hello,

I have a Cozir ambient sensor +RH/T.
Until last week it was oke.
And now, without any changes (as far as i know) it stops Serial.printing the values.
It stops whit any serial.printing, such as a testing text Serial.println("setup");

I used this code:
Code: [Select]

#include <SoftwareSerial.h>
#include "cozir.h"

SoftwareSerial nss(3,2);
COZIR czr(nss);

void setup()
{
  Serial.begin(9600);
  delay(3000);
  czr.SetOperatingMode(CZR_POLLING);
  //czr.SetOperatingMode(CZR_STREAMING);
  //czr.CalibrateFreshAir();
  //czr.SetDigiFilter(64);
}

void loop()
{
  Serial.println("loop");
  float t = czr.Celsius();
  float f = czr.Fahrenheit();
  float h = czr.Humidity();
  int c = czr.CO2();
  int digi = czr.GetDigiFilter();
 
  Serial.print("Celcius : ");Serial.println(t);
  Serial.print("Fahrenheit : ");Serial.println(f);
  Serial.print("Humidity : ");Serial.println(h);
  Serial.print("CO2 : ");Serial.println(c);
  Serial.print("Digital Filter : ");Serial.println(digi);   
}


And when I, at DirtGambit's earlier post, uncomment the 'czr.SetOperatingMode(CZR_POLLING);' line, Arduino comes with an error:

F:\Software\USB-stick\arduino-1.0.5-r2\libraries\cozir103/cozir.h: In function 'void setup()':
F:\Software\USB-stick\arduino-1.0.5-r2\libraries\cozir103/cozir.h:97: error: 'void COZIR::SetOperatingMode(uint8_t)' is private
Cozir_CO2:12: error: within this context

Calibrate and filter options are allowed. Only at Polling and Streaming uncommenting he gives an error.

What happened?
What means 'is private' in this case?

Thanks,

Johan

Title: Re: Temp, Humidity CO2 sensor and library.
Post by: robtillaart on Apr 08, 2015, 07:51 pm
Apparently the 1.0.3 and up version declares    

Code: [Select]
void SetOperatingMode(uint8_t mode);

as private.

That means that a user (you) of the class are not allowed to use that function, only public functions are callable as user of the class.

The example code seems to be copied from the 1.0.2 version and is not compatible with 1.0.3 and up.


disclaimer: I do not have a Cozir sensor to verify

Title: Re: Temp, Humidity CO2 sensor and library.
Post by: jmnijsse on Apr 09, 2015, 08:59 am
Ah, thats the problem.
I had play with different libraries. And with a clean up stored only the newest. Combined with an older sketch.
Thanks, it works again...

J.
Title: Re: Temp, Humidity CO2 sensor and library.
Post by: jmnijsse on Apr 09, 2015, 03:21 pm
Another question (perhaps it belongs to another subforum...).

I used v-usb to put the values in Excel. https://www.hackster.io/rayburne/arduino-to-excel-using-v-usb (https://www.hackster.io/rayburne/arduino-to-excel-using-v-usb)
For many sensors it works fine.

BUt with the Cozir, it fills 20 rows, or 50 and it stops.

The cozir is a low-power sensor, so it couldn't make a voltage-ripple in the v-usb protocol?
Maybe the SoftwareSerial library uses extra power? I don't think so, but you never know...

This is the total code:
Code: [Select]
/*
This sketch is used for measuring CO2 with a COZIR Ambient sensor.
 With the V-USB protocol the values are dropped in Excel or any spreadsheet whatsoever
 */

#include "UsbKeyboard.h"
//#include <avr/pgmspace.h>
#include <SoftwareSerial.h>
#include "cozir.h"

// #define BUTTON_PIN 10
#define WIP 1
int sch = 3;

int sensorValue = 0;        // value read from the pot
int outputValue = 0;        // value output to the PWM (analog out)
int delaytime = 400;
int delaytimes = (delaytime/1000);

unsigned long time;
long oldtime;
long newtime;

String sOut;


SoftwareSerial nss(6,13);
COZIR czr(nss);

int freeRam () {
  extern int __heap_start, *__brkval;
  int v;
  return (int) &v - (__brkval == 0 ? (int) &__heap_start : (int) __brkval);
}

void delayMs(unsigned int ms) {    // Safe delay helper function
  for (int i = 0; i < ms; i++) {
    delayMicroseconds(1000);
  }
}


void setup() {
  //setup the COZIR sensor

  Serial.begin(9600);
  delay(3000);
  //czr.SetOperatingMode(CZR_POLLING);
  //czr.SetOperatingMode(CZR_STREAMING);
  //czr.CalibrateFreshAir();
  //czr.SetDigiFilter(64);


  //setup the V-usb protocol
  // Disable timer0 since it can mess with the USB timing. Note that
  // this means some functions such as delay() will no longer work.
  TIMSK0&=!(1<<TOIE0);
  // Clear interrupts while performing time-critical operations
  cli();
  // Force re-enumeration so the host will detect us
  usbDeviceDisconnect();
  delayMs(250);
  usbDeviceConnect();

  // Set interrupts again
  sei();
  pinMode(11, INPUT);
  pinMode(sch, INPUT);

  for (int i = 0; i < 250; i++){            // 10 second delay in lieu of using Button press on pin 10
    digitalWrite(13, !digitalRead(13));
    // while (digitalRead(BUTTON_PIN) != 0)
    UsbKeyboard.update();
    delayMs(20);

  }
  //setup the headers in the first row of Excel
  set_header();

}

void loop() {

  //initialize the state of the button which gives an enter after filling first row  
  int sch_state = digitalRead(sch);

  //potmeter to set the frequency of sampling
  int fr_pot = analogRead(A2);
  long frequency = map(fr_pot, 0, 1023, 500, 10000);

  //get the CO2-value
  int c = czr.CO2();

  //set the timestamp
  newtime = oldtime + frequency;
  oldtime = newtime;


  // Put it all in al string
  UsbKeyboard.update();
  sOut = "";
  sOut = (String(newtime));
  KeyStrokeAlpNum(sOut);
  UsbKeyboard.sendKeyStroke(43);
  sOut = (String(c));
  KeyStrokeAlpNum(sOut);
  UsbKeyboard.sendKeyStroke(0x50);

  //looks for buttonstate: HIGH makes a table, LOW overwrite the measurement
  if (sch_state == HIGH){
    for (int x = 0; x < 1; x++){
      UsbKeyboard.sendKeyStroke(KEY_ENTER);
    }
  }

  //delay between samples
  delayMs(frequency);  
}

void set_header(){

  String first_sensor;
  String second_sensor;
  String delaytijd;

  delaytijd = "TIJD MILLIS";
  first_sensor = "SENSOR 1";            // Using capitals is neccessary
  second_sensor = "SENSOR 2";
  KeyStrokeAlpNum(delaytijd);
  UsbKeyboard.sendKeyStroke(43);  
  KeyStrokeAlpNum(first_sensor);
  UsbKeyboard.sendKeyStroke(43);    
  KeyStrokeAlpNum(second_sensor);
  UsbKeyboard.sendKeyStroke(KEY_ENTER);
  UsbKeyboard.sendKeyStroke(0x50);
  UsbKeyboard.sendKeyStroke(0x50);
}


void KeyStrokeAlpNum(String Sc){
  int sPoint = Sc.length();          // Pointer to end-of-String (inc. null)
  for (int x = 0; x < sPoint; x++){
    int y = x + 1;
    if (Sc.substring(x, y) == "H"){  // substring single character: y = x+1
      UsbKeyboard.sendKeyStroke(KEY_H, MOD_SHIFT_LEFT);
    }
    else if (Sc.substring(x, y) == "L"){
      UsbKeyboard.sendKeyStroke(KEY_L, MOD_SHIFT_LEFT);
    }
    else if (Sc.substring(x, y) == " "){
      UsbKeyboard.sendKeyStroke(KEY_SPACE);
    }
    else if (Sc.substring(x, y) == "1"){
      UsbKeyboard.sendKeyStroke(KEY_1);
    }
    else if (Sc.substring(x, y) == "2"){
      UsbKeyboard.sendKeyStroke(KEY_2);
    }
    else if (Sc.substring(x, y) == "3"){
      UsbKeyboard.sendKeyStroke(KEY_3);
    }
    else if (Sc.substring(x, y) == "4"){
      UsbKeyboard.sendKeyStroke(KEY_4);
    }
    else if (Sc.substring(x, y) == "5"){
      UsbKeyboard.sendKeyStroke(KEY_5);
    }
    else if (Sc.substring(x, y) == "6"){
      UsbKeyboard.sendKeyStroke(KEY_6);
    }
    else if (Sc.substring(x, y) == "7"){
      UsbKeyboard.sendKeyStroke(KEY_7);
    }
    else if (Sc.substring(x, y) == "8"){
      UsbKeyboard.sendKeyStroke(KEY_8);
    }
    else if (Sc.substring(x, y) == "9"){
      UsbKeyboard.sendKeyStroke(KEY_9);
    }
    else if (Sc.substring(x, y) == "0"){
      UsbKeyboard.sendKeyStroke(KEY_0);
    }
    else if (Sc.substring(x, y) == "."){
      UsbKeyboard.sendKeyStroke(55);
    }
    else if (Sc.substring(x, y) == "-"){
      UsbKeyboard.sendKeyStroke(45);
    }
    else if (Sc.substring(x, y) == "A"){
      UsbKeyboard.sendKeyStroke(KEY_A, MOD_SHIFT_LEFT);
    }
    else if (Sc.substring(x, y) == "B"){
      UsbKeyboard.sendKeyStroke(KEY_B, MOD_SHIFT_LEFT);
    }
    else if (Sc.substring(x, y) == "C"){
      UsbKeyboard.sendKeyStroke(KEY_C, MOD_SHIFT_LEFT);
    }
    else if (Sc.substring(x, y) == "D"){
      UsbKeyboard.sendKeyStroke(KEY_D, MOD_SHIFT_LEFT);
    }
    else if (Sc.substring(x, y) == "E"){
      UsbKeyboard.sendKeyStroke(KEY_E, MOD_SHIFT_LEFT);
    }
    else if (Sc.substring(x, y) == "F"){
      UsbKeyboard.sendKeyStroke(KEY_F, MOD_SHIFT_LEFT);
    }
    else if (Sc.substring(x, y) == "G"){
      UsbKeyboard.sendKeyStroke(KEY_G, MOD_SHIFT_LEFT);
    }
    else if (Sc.substring(x, y) == "I"){
      UsbKeyboard.sendKeyStroke(KEY_I, MOD_SHIFT_LEFT);
    }
    else if (Sc.substring(x, y) == "J"){
      UsbKeyboard.sendKeyStroke(KEY_J, MOD_SHIFT_LEFT);
    }
    else if (Sc.substring(x, y) == "K"){
      UsbKeyboard.sendKeyStroke(KEY_K, MOD_SHIFT_LEFT);
    }
    else if (Sc.substring(x, y) == "M"){
      UsbKeyboard.sendKeyStroke(KEY_M, MOD_SHIFT_LEFT);
    }
    else if (Sc.substring(x, y) == "N"){
      UsbKeyboard.sendKeyStroke(KEY_N, MOD_SHIFT_LEFT);
    }
    else if (Sc.substring(x, y) == "O"){
      UsbKeyboard.sendKeyStroke(KEY_O, MOD_SHIFT_LEFT);
    }
    else if (Sc.substring(x, y) == "P"){
      UsbKeyboard.sendKeyStroke(KEY_P, MOD_SHIFT_LEFT);
    }
    else if (Sc.substring(x, y) == "Q"){
      UsbKeyboard.sendKeyStroke(KEY_Q, MOD_SHIFT_LEFT);
    }
    else if (Sc.substring(x, y) == "R"){
      UsbKeyboard.sendKeyStroke(KEY_R, MOD_SHIFT_LEFT);
    }
    else if (Sc.substring(x, y) == "S"){
      UsbKeyboard.sendKeyStroke(KEY_S, MOD_SHIFT_LEFT);
    }
    else if (Sc.substring(x, y) == "T"){
      UsbKeyboard.sendKeyStroke(KEY_T, MOD_SHIFT_LEFT);
    }
    else if (Sc.substring(x, y) == "U"){
      UsbKeyboard.sendKeyStroke(KEY_U, MOD_SHIFT_LEFT);
    }
    else if (Sc.substring(x, y) == "V"){
      UsbKeyboard.sendKeyStroke(KEY_V, MOD_SHIFT_LEFT);
    }
    else if (Sc.substring(x, y) == "W"){
      UsbKeyboard.sendKeyStroke(KEY_W, MOD_SHIFT_LEFT);
    }
    else if (Sc.substring(x, y) == "X"){
      UsbKeyboard.sendKeyStroke(KEY_X, MOD_SHIFT_LEFT);
    }
    else if (Sc.substring(x, y) == "Y"){
      UsbKeyboard.sendKeyStroke(KEY_Y, MOD_SHIFT_LEFT);
    }
    else if (Sc.substring(x, y) == "Z"){
      UsbKeyboard.sendKeyStroke(KEY_Z, MOD_SHIFT_LEFT);
    }

  }
  // Return
}

/*  Additional translations
 
 45 -
 46 =
 47 [
 48 ]
 49 \
 50 \
 51 ;
 52 '
 53 `
 54 ,
 55 .
 56 /
 
 84 /
 85 *
 86 -
 87 +
 */

Title: Re: Temp, Humidity CO2 sensor and library.
Post by: kslstn on Apr 12, 2015, 09:31 pm
jmnijsse, if the sensor's signal output voltage would be too low, you wouldn't get any values from it. Moreover, it's powered by the Arduino, so don't worry about that.

Your issue has nothing to do with the sensor. I've had a issue with similar symptoms though. In my case, it happens only when a sketch is big and after multiple logs. Therefore, I'm pretty sure it's a memory issue. As I don't have all your libraries installed, I can't verify the size of the compiled program, so it may be something else.
Title: Re: Temp, Humidity CO2 sensor and library.
Post by: jmnijsse on Apr 13, 2015, 08:44 am
Ok, thanks.
The size is 12.366 bites (of 32.256)

I'll looking further in this way...


J.
Title: Re: Temp, Humidity CO2 sensor and library.
Post by: robtillaart on Apr 13, 2015, 06:34 pm
Your application uses strings quite intensively and as the String class uses dynamic memory it gets possibly (big chance) fragmented. You could do a test the amount of freeRAM with this function

Code: [Select]

int freeRam ()
{
    extern int __heap_start, *__brkval;
    int v;
    return (int) &v - (__brkval == 0 ? (int) &__heap_start : (int) __brkval);
};

Calling at a fixed location in loop() should always give the same result. If it changes you have a memory leak.
Title: Re: Temp, Humidity CO2 sensor and library.
Post by: toledofjose on Jun 22, 2015, 10:37 am
Hey everyone.  First time poster here. 

Currently running the Github library and sample sketch posted by DirtGambit (thanks much).  The COZIR is functioning when I run a sketch shell on my Mega 2560 and hook up the COZIR TX and RX to the hardware serial on the Mega.  The humidity, temp, and CO2 readings all display on the output.  However, when I run the sample sketch, the output is as follows:

Fahrenheit : 32.00
Humidity : 0.00
CO2 : 0
Digital Filter : 0

When I adjust the Digital Filter value, all values change directly with the digital filter value.  I'm stumped.  Please help.
@Simas, I had that issue too. After setting the sensor to poll mode and starting the calibration manually, everything worked fine with Rob's library.

You can find the sketches to do that here on the forum and in my repo (https://github.com/kslstn/CozirTools) (the SetToPolling and Calibrate sketches).

Edit: Also make sure the sensor is connected to pin 2 and 3. If, after trying the above, you still get the 0 readings, try switching the wires, there was some confusing about Tx and Rx. And give it a bit of time. After it's connected to power, the sensor needs about 5 seconds before it measures correctly.
i have the same problem as simas but i cant fixed it with the solution provided by kslstn .
After calibrate manually ,set the polling mode and digitalfilter
the values in my console are the same as simas.

By now , i have no idea how to fix that problem . I read the user´s guide , the datasheet and tried few codes  included the manufacturer´s .
Title: Re: Temp, Humidity CO2 sensor and library.
Post by: kalllelll on Aug 03, 2015, 05:47 pm
Hi,

I've got the Cozir Wide Range Co2 Sensor and as I hope an easy-to-solve problem :-) It's about the delay(2000) in the "loop" . Without this everything works quite well and I see z ##### z ##### in my seriell monitor every second a new line with new values. With the delay(2000) (2000ms just for testing purpose) my seriell monitor delays the appearance of every singe sign. Is there an easy way to fix this or do I have to get it to work with the cozir.h library? At the end I've to log the Co2 values of an experiment for 7 days and store it on a SD Card (which already works). But one value every 5 or 10 minutes is more than enough.

Code: [Select]
#include <SoftwareSerial.h>

SoftwareSerial nss(11,10);

void setup()
{
  Serial.begin(9600);
  nss.begin(9600);
  delay(3000);
}

void getStuff()
{
  if (nss.available())
  {
    Serial.write(nss.read());
  }
}
void loop()
{
  getStuff();
  delay(2000);
}



I would appreciate your help

Title: Re: Temp, Humidity CO2 sensor and library.
Post by: robtillaart on Aug 03, 2015, 10:23 pm
try this code, it is delay-free and prints to serial only one in ten, and to SD every meansurement

Code: (dummy) [Select]

#include <SoftwareSerial.h>

SoftwareSerial nss(11,10);

char buffer[32];
int idx = 0;
int count = 0;

void setup()
{
    Serial.begin(9600);
    nss.begin(9600);

    delay(3000);
}

void loop()
{
    if (nss.available())
    {
        char c = nss.read();
        buffer[idx] = c
        idx++;
        buffer[idx] = 0; // keep the buffer \0 terminated
        if (c == '\n')
        {
            count++;
            write_to_SD_(buffer);
            if (count == 10)
            {
                 Serial.println(buffer);
                 count = 0;
            }
            idx = 0;
        }
    }
   
   
}
Title: Re: Temp, Humidity CO2 sensor and library.
Post by: kalllelll on Aug 06, 2015, 05:08 pm
Thanks very much robtillaart, I appreciate your help very much! It works very well. For unknown reasons I had to change serial.print into serial.write at the end
Title: Re: Temp, Humidity CO2 sensor and library.
Post by: oxiii on Aug 30, 2015, 04:35 pm
To everyone still struggling with the readings, here's my piece of advice:

Of course this is under your responsibility. I hereby dismiss any responsibility for any direct or indirect damages.

I've been stuck for some time trying to figure out why neither the analog nor the serial connection seemed to work over my Mega clone. I read through the datasheet and manual and followed to the detail to no avail. I was using the 3v3 power pin to power up this Cozir-WR sensor. I then tried stepping up the sensor serial output through a transistor to 5v in case the Arduino couldn't tell the highs and nothing. In point of fact I was advised to try the 5v but I was reluctant as I wouldn't want to break this rather expensive part. Big thanks for your continued support, BTW (I don't know your username here, sorry). I eventually found this chart: http://www.co2meters.com/Documentation/Datasheets/Sensor-Comparison-Chart.pdf and decided to give the 5v power source a go. The analog pin was working beautifully. I then tried the serial but it wasn't. I thought I'd try switching the wires and that did it! I then tried the 3v3 source again but it stopped working.

So, try the 5v source and try switching the Rx and Tx pin wires, because either the sensor's or some other documentation are wrong. After all, isn't a Tx supposed to go into an Rx?

Good luck!
Title: Re: Temp, Humidity CO2 sensor and library.
Post by: robtillaart on Aug 30, 2015, 07:56 pm
> So, try the 5v source and try switching the Rx and Tx pin wires, because either the sensor's or some other documentation are wrong. After all, isn't a Tx supposed to go into an Rx?

Serial is full of obscurities. IIRC it depends if the device is a serial DTE or DCE. The TX of a DTE is connected to the TX of a DCE, so no crossing of  Tx and Rx. if you connect 2 DTE's or 2 DCE 's the you need to cross Tx and Rx (null modem connection).
[is quite some time ago]

DTE stands for Data Terminal Equipment, and. DCE stands for Data Communications Equipment.
Title: Re: Temp, Humidity CO2 sensor and library.
Post by: Zoey_P on Oct 02, 2015, 05:19 am
Hello,I use your library and code to run my sensor,but I get nothing.It seems that the sensor doesn't work but connection is right..Well,I just start to learn arduino...I don't know which step is wrong,can you help me?Thank you !!!
Title: Re: Temp, Humidity CO2 sensor and library.
Post by: robtillaart on Oct 02, 2015, 11:57 am
Please post your code used, COZIR library version number and a picture/drawing of your schematics.
Which board are you using?
Which version of the IDE?

Please note I do not have such sensor although I was involved in creating the library.
Title: Re: Temp, Humidity CO2 sensor and library.
Post by: yogeshsingh on Oct 24, 2015, 12:12 pm
i am using cozir ambient form past 3 month .
It was working as a charm in streaming mode.

Now,
I am using sensor in polling mode and after getting the data
i put it in power save mode that is K 0\r\n
(say after 5 min getting the value of co2)

Most of the time it give co2 value as 0(zero) or anything greater than 5001.
as i am using ambient which is not suppose to value greater greater than 5001.
Even if it wok for some cycle it get stuck after 10 to 12 cycle and to get value value i have to reset
the controller.

Is there any solution to this or somebody else also face this problem.
Title: Re: Temp, Humidity CO2 sensor and library.
Post by: Erik455 on Jan 07, 2016, 10:01 pm
I am getting these errors:
cozir_scetch.cpp.o: In function `loop':
C:\Users\Erik\Documents\Arduino/cozir_scetch.ino:18: undefined reference to `COZIR::Celsius()'
C:\Users\Erik\Documents\Arduino/cozir_scetch.ino:19: undefined reference to `COZIR::Fahrenheit()'
C:\Users\Erik\Documents\Arduino/cozir_scetch.ino:20: undefined reference to `COZIR::Humidity()'
C:\Users\Erik\Documents\Arduino/cozir_scetch.ino:21: undefined reference to `COZIR::CO2()'
cozir_scetch.cpp.o: In function `__static_initialization_and_destruction_0':
C:\Users\Erik\Documents\Arduino/cozir_scetch.ino:6: undefined reference to `COZIR::COZIR(SoftwareSerial&)'
collect2.exe: error: ld returned 1 exit status
Title: Re: Temp, Humidity CO2 sensor and library.
Post by: robtillaart on Jan 08, 2016, 04:05 pm
Welcome Erik,

you need to tell more, and at least post the code (in code tags == </> button).
Also tell how & where you did install the library
Title: Re: Temp, Humidity CO2 sensor and library.
Post by: Erik455 on Jan 08, 2016, 11:20 pm
Thanks Rob! I used the files on https://github.com/RobTillaart/Arduino/tree/master/libraries/Cozir.

But now I use the original version (0.1.01) of cozir.h and everything works.
Title: Re: Temp, Humidity CO2 sensor and library.
Post by: robtillaart on Jan 08, 2016, 11:33 pm
Didn't the last version work?

(I don't have the sensor, mainly helped to stabilize and optimize the lib)
Title: Re: Temp, Humidity CO2 sensor and library.
Post by: cope on Feb 08, 2016, 11:13 am
I have a Cozir ambient sensor and the CO2, temperature and relative humidity output is working fine thanks to all the input I got from this thread. The problem is that the CO2 values are too high so I want to 'calibrate' the sensor in fresh air. Now I have a problem to implement this in a sketch. I see the CalibrateFreshAir() function in the cpp file but I have no idea how to use this (no cpp knowledge). Is there someone that could explain to me how to go on?
Thanks in advance,
Cor
Title: Re: Temp, Humidity CO2 sensor and library.
Post by: cope on Feb 08, 2016, 03:37 pm
I would like to share another observation. I am running the sketch I use for measuring CO2, temperature and relative humidity with the old 1.01 library. So I thought to upgrade to 6.01beta.
It turns out that the SetOperatingMode function does not work as it should. I only can get things going again by making SetOperatingMode public, comment out SetOperatingMode in the Cozir.cpp and place the call again in the sketch.
Does anybody know if am I doing something wrong here, and what?
--Cor--
Title: Re: Temp, Humidity CO2 sensor and library.
Post by: 34R7 on Feb 20, 2016, 11:22 pm
@ cope :

get this in your setup
Code: [Select]
// czr.SetOperatingMode(CZR_POLLING);
 //czr.SetOperatingMode(CZR_STREAMING);
// czr.CalibrateFreshAir();


1. uncomment the operating mode you want -> upload
2. comment the operating mode again and uncomment CalibrateFreshAir() ->upload
3. comment CalibrateFreshAir() ->upload
 and it should work
Title: Re: Temp, Humidity CO2 sensor and library.
Post by: cope on Feb 25, 2016, 05:29 pm
Thanks a lot for your support. It turned out that I had to give the czr.CalibrateFreshAir() command twice. It did not always work with one command.
But it works now, thanks again.
-- Cor --
Title: Re: Temp, Humidity CO2 sensor and library.
Post by: arduale on Mar 02, 2016, 09:21 pm
Hallo everybody,
I have a cozir-wx 60 sensor and it works fine in streaming mode. I need to operate in polling, so i will try this library. I Hope it will help.
Thank for your great support!
Title: Re: Temp, Humidity CO2 sensor and library.
Post by: arduale on Mar 03, 2016, 11:39 am
hi!
it seams that this library does not work with my sensor, which is a cozir wx 60, slightly different from cozir ambient. here is the datasheet of my sensor: cozir wx 60 (http://www.gassensing.co.uk/wp-content/uploads/2015/09/COZIR_-WideRange_-Datasheet_GSS.pdf)
the pin scheme seems the same as ambient sensor.
i have connected the same way described in this thread: pin2-tx, pin3-rx.
if i use the sketch provided by co2meter, it works fine. but it is in streaming mode, and i need to use it in polling mode, in order to read co2 values each 30 sec.
i tryied cozir.h library version 1.0, and 1.0 and 1.03, but the only thing i was able to get is:

Celcius : 0.00
Fahrenheit : 32.00
Humidity : 0.00
CO2 : 0
Digital Filter : 0

i did not find the link at 1.05 version, which should be stable, if you can tell me where to download this, i could try.

just to give you some informations, if i run the sketch of dirtgambit:

Code: [Select]

#include <SoftwareSerial.h>
#include "cozir.h"

SoftwareSerial nss(2,3);
COZIR czr(nss);

void setup()
{
 Serial.begin(9600);
 delay(5000);
 //czr.SetOperatingMode(CZR_POLLING);
 //czr.SetOperatingMode(CZR_STREAMING);
 //czr.CalibrateFreshAir();
// czr.SetDigiFilter(64);
}

void loop()
{
 delay(4000);
 float t = czr.Celsius();
 float f = czr.Fahrenheit();
 float h = czr.Humidity();
 int c = czr.CO2();
 int digi = czr.GetDigiFilter();

 Serial.print("Celcius : ");Serial.println(t);
 Serial.print("Fahrenheit : ");Serial.println(f);
 Serial.print("Humidity : ");Serial.println(h);
 Serial.print("CO2 : ");Serial.println(c);
 Serial.print("Digital Filter : ");Serial.println(digi);
 Serial.println("");
}


and something happen, because after this sketch, the sensor probably is set in polling mode, because my sketch, which works in streamning mode, does not work anymore. i must send the command to put the sensor in streaming mode, to make it work again whit my sketch.

if anyone could help me, it would really be appreciated, since i need this sensor working in polling mode!

thanks,
bye!

EDIT: i use arduino uno and ide 1.6.7
Title: Re: Temp, Humidity CO2 sensor and library.
Post by: tkvib on Jun 13, 2016, 09:52 pm
Hi,

i want to use the gsm shield2 to send via sms if the co2 value is out of range.
I use a cozir co2 sensor.

Separately all is fine, but I got problems using both libraries together in one sketch.

I'm not so skilled, but i guess it's a problem with the using of the SoftwareSerial.

So i tried to use the Hardwareserial sketch created by robtillaart. But i get weird values, also without any sensor connected to my mega.

Now I use again the libraries from roder. The cozir probe is working fine. But the same problems recure if i put the gsm library into the sketch.

Anybody knows this problem?

Thanks tkvib
Title: Re: Temp, Humidity CO2 sensor and library.
Post by: Belal1987 on Nov 08, 2016, 10:28 pm
Hi all,
Is there a way to calibrate the COZIR CO2 sensor using a reference value? I think the fresh air calibration assumes a 450ppm concentration. is there a way to pass a specific number?

Thanks,
B.
Title: Re: Temp, Humidity CO2 sensor and library.
Post by: V-king on Mar 24, 2017, 10:44 pm
HiBela
For calibrating Cozir and MinIr sensors, I would recommend using a terminal program (e.g. Hyperterminal or CO2meter.com's GasLab software, which will recognize many sensors bought through CO2meter.com !). This way, you can connect your sensor through a UART to USB-interface (such as your FTDI arduino-mini programmer or even a chinese CP1202). But instead of you having to write the instructions, you simply enter the basic communication codes as stated in GSS' datasheets for your sensor (send line ends with line feeds !).
This will keep your arduino code limited to the minimum required for use.
Since revision AL19 (2013), the communication protocol for Cozir, SprintIR, Misir and MinIR sensors is the same for the three types of sensors. To calibrate the lower span point, you can either choose to use a known gas concentration or to use a fresh air calibration.
The problem is that, due to global warming, fresh air tends to contain more and more CO2 every year, and that actually, on the northern hemisphere, fresh air contains over 400 ppm, depending on the area. In 2016, the CO2-concentration in the southern hemisphere has also exceeded 400 ppm.
But lets's assume that fresh air contains 400 ppm, then you can simply send the command G\r\n to your sensor, which sets the zero point assuming the sensor is in 400 ppm.
If you want it more precise and you know the real value of fresh air, e.g. you want to set the lower span point of, let's say, 450 ppm, then you have to send the command F 410 450\r\n where the first value is your actual reading and the second one the known gas concentration in ppm.
Title: Re: Temp, Humidity CO2 sensor and library.
Post by: atsaz on Aug 03, 2017, 05:00 pm
Hi,

I have just got my Cozir wide range 5-100% co2 sensor and i have uploaded the code on this forum for it to start co2 measurements. Could some one please help me understand a couple of things. Thanks alot.


1) i believe there are multiple models of cozir for example 20%, 65% , 100% range etc .. How does this library handle these models. Because for example if i am using 100% range co2 sensor than do i have to multiply my output with 100 ? if i am using 20% range sensor than do i have to multiply co2 measurement with 10 ? .. So on .. where and how does this library takes this in to account ?
Right now i have followed the instructions in this forum of commenting and uncommenting

Code: [Select]
//czr.SetOperatingMode(CZR_POLLING);
//czr.SetOperatingMode(CZR_STREAMING);

//czr.CalibrateFreshAir();


to calibrate the sensor for first use. And after that i get the following measurement from my sensor on the serial monitor:


Celcius: 29.20
Farrenheit : 84.56
Co2: 1
Digital Filter: 16

Celcius: 29.20
Farrenheit : 84.56
Co2: 0
Digital Filter: 16

Celcius: 29.20
Farrenheit : 84.56
Co2: 0
Digital Filter: 16


NOTE: CO2 values.

When i blow on it the values go up to 20.

My question: Shouldn't these values be in the range of something like 400-600 ppm . (My environment is normal office with window open for fresh air with one person in the room)...



Thank you people for the love forum.
Title: Re: Temp, Humidity CO2 sensor and library.
Post by: timothypaul26 on Aug 21, 2017, 03:59 pm
I have been using the cozir sensor for a few years now, and your code/libraries have been great. I would like to add it to a TFT touch screen and for the life of me cannot find a way to print the readings to the screen. What is the easiest way to go about tackling such a task? Seems like reading the Serial.print since we are already establishing that would be the easiest, but does that still work when you're not connected to a computer? I am not asking for someone to write the code, because I want to learn how to do this myself, but to point me where I can learn the easiest and most efficient way to perform the task.
Title: Re: Temp, Humidity CO2 sensor and library.
Post by: robtillaart on Aug 21, 2017, 08:40 pm
@timothypaul26

First learn to work with the TFT of your choice without any sensor or so, just have simple messages like "Hello" and move from there to e.g. the time in millis() since start. What you need to learn is to erase part of the screen before writing a new value to it.
Then connect the Cozir and capture the data from it and send it to the TFT, updating in a similar way.

Title: Re: Temp, Humidity CO2 sensor and library.
Post by: timothypaul26 on Aug 21, 2017, 09:09 pm
thanks Rob, I have my tft working, and using a 1307 rtc and I can send all of that data. Maybe I do need to work more, but I cannot figure out what in the cozir.h can be sent. I'm assuming that I need to write varriables and such? Is there an example that you could reccomend? For practice?
Title: Re: Temp, Humidity CO2 sensor and library.
Post by: robtillaart on Aug 22, 2017, 04:37 pm
(I do not have a COZIR sensor but helped writing the library for it)

Besides the CO2 level you could print the other values the sensor can provide (lux and temp). Besides the actual level you could print the average upto that moment or so. Or peak info of the last 7 days?

Or did I misinterpret your question?
Title: Re: Temp, Humidity CO2 sensor and library.
Post by: timothypaul26 on Aug 22, 2017, 06:31 pm
I think that my sensor only has temp, humidity and co2. Is it possible to read the pins for those or do I have to read off of the serial, or is this an IIC setup? I'm not sure where to look into how to create the varriables and statements to get the information from the sensor to print to the TFT.
Title: Re: Temp, Humidity CO2 sensor and library.
Post by: timothypaul26 on Aug 22, 2017, 06:43 pm
I guess I should know that it's serial only as it states that in the data sheet... And that is where my trouble is, i'm not sure how to use the serial communication. I'll continue reading and see if that helps me get a bit closer to my goal.
Title: Re: Temp, Humidity CO2 sensor and library.
Post by: robtillaart on Aug 22, 2017, 11:32 pm
The code in post #166 above might help you to get all info out of the sensor...
Title: Re: Temp, Humidity CO2 sensor and library.
Post by: timothypaul26 on Aug 23, 2017, 01:14 pm
I had that code from the example. Every time I would try to myGLCD.print(c(), CENTER, 0); I would get an error. After reading through the UTFT manual a few times I realized that it told you how to print a float... myGLCD.printNumF(c(), CENTER, 0); did the trick. Once I got that figured out things went well from there. I was having a funny issue where my rtc and my co2/temp/humidity would only update for 5 seconds and freeze. Only thing I could think of is that there was some sort of serial conflict? I changed the rtc speed to 4k, and it seems to have fixed the issue, but I'm not sure that it's the best fix, might be more of a band-aid? Time will tell I guess. I really do appreciate your patients with me and the work that you and DirtGambit have done on this library, also thanks to Henning for his work. My hat is off to all of you.
Title: Re: Temp, Humidity CO2 sensor and library.
Post by: Sklindt on Nov 27, 2017, 01:40 pm
Hello everyone, i have read the entire thread now, can i still can't get my COZIR av-2000 sensor to work.

First off i have the sensor without temperature and humidity so only the CO2 sensor.

have today 27/11-2017 downloaded the newest versions of the cozir.h
i am trying to get this to work on an ATMega2560 and using the following code


#include <SoftwareSerial.h>
#include "cozir.h"

SoftwareSerial nss(11,12);
COZIR czr(nss);

void setup()
{
 Serial.begin(9600);
 delay(5000);
 czr.SetOperatingMode(CZR_POLLING);
 //czr.SetOperatingMode(CZR_STREAMING);
 //czr.CalibrateFreshAir();
// czr.SetDigiFilter(64);
}

void loop()
{
 delay(1000);
 float t = czr.Celsius();
 float f = czr.Fahrenheit();
 float h = czr.Humidity();
 int c = czr.CO2();
 int digi = czr.GetDigiFilter();

 Serial.print("Celcius : ");Serial.println(t);
 Serial.print("Fahrenheit : ");Serial.println(f);
 Serial.print("Humidity : ");Serial.println(h);
 Serial.print("CO2 : ");Serial.println(c);
 Serial.print("Digital Filter : ");Serial.println(digi);
 Serial.println("");
}


and i am getting the following output:

Celcius : 0.20
Fahrenheit : 32.36
CO2 : 2
Digital Filter : 2

Hope somone can help

Ps nothing happens when i blow on it..
Title: Re: Temp, Humidity CO2 sensor and library.
Post by: KawasakiZx10r on Aug 07, 2018, 11:27 pm
I just bought one of these COZIR co2 sensors the "COZIR-AH-5000" and after trying the library they offer i could not get it to read the data, However i was able to use the post he provides in this thread to calibrate it, by uncommenting and commenting out the given lines in his example.

But again not reading the data, the library example was just printing 0.00 or nothing at all.

So i looked through the library and wrote a shorter but just as accurate code that you can just paste into your C code, you can also use any serial method, as the library only allowed software serial.

:)  :)  :)  :)

Here is my code to read the data when set to Streaming mode "Default mode"

Code: [Select]
char buffer[32];
char buff[6];
byte bufferCount = 0;
float celsius;
float fahrenheit;
float humidity;
int co2;
int digiRead;


void setup()
{
  Serial1.begin(9600);
  Serial.begin(115200);
  Serial.println(F("Starting system."));
}

void loop()
{
  getSerialData();
}

place this code in a new tab to keep your project organized.
Code: [Select]
void getSerialData() {
  char input = Serial1.read();
  if (input > 0 && input != 32 && bufferCount < 32) {  // 0 is null, 32 is a space
    buffer[bufferCount] = input;
    // Serial.print(input);  // expected format: H00588T01263Z00456z00456
    bufferCount++;
  }
  else if (input == '\n') { // input == 10, but '\n' is easier to read.
    bufferCount = 0;
    formatData();
  }
}

void formatData() {
  //Serial.print("\nbuffer: "); Serial.println(buffer);
  // ============================ Humidity ============================
  sortBuffer(1);
  humidity = atof(buff);
  humidity = humidity * 0.1;
  clearBuff();
  // ============================ Temperature =========================
  sortBuffer(7);
  celsius = atof(buff);
  if (celsius < 1000)
  {
    celsius = 0.1 * celsius;
  }
  else
  {
    celsius = 0.1 * (celsius - 1000);
  }
  fahrenheit = celsius * 1.8 + 32;
  clearBuff();
  // ============================ Digital Read ========================
  sortBuffer(13);
  digiRead = atoi(buff);
  clearBuff();
  // ============================ Raw Co2 =============================
  sortBuffer(19);
  co2 = atoi(buff);
  clearBuff();
  // =========================== Clear the main buffer ===============
  for (byte n = 0; n < 32; n++) {
    buffer[n] = 0;
  }
  // ============================ Print Data =========================
  Serial.print(F("\nHumidity: ")); Serial.println(humidity);
  Serial.print(F("Celsius: ")); Serial.println(celsius);
  Serial.print(F("Fahrenheit: ")); Serial.println(fahrenheit);
  Serial.print(F("Digital Co2 ppm: ")); Serial.println(digiRead);
  Serial.print(F("Raw Co2 pmm: ")); Serial.println(co2);
}

void sortBuffer(byte start) {
  for (byte i = start; i < start + 5; i++) {
    buff[i - start] = buffer[i];
  }
}

void clearBuff() {
  for (byte n = 0; n < 6; n++) {
    buff[n] = 0;
  }
}



Output:

Humidity: 46.80
Celsius: 25.90
Fahrenheit: 78.62
Digital Co2 ppm: 459
Raw Co2 pmm: 529

And here is my code for Polling mode, COZIR set to mode 2 Polling !
Which i recommend using, it more efficient and quicker than Streaming mode and compared
to the library recommended in this thread, my code is far quicker and uses less resources, being
22 Bytes in total, and only two 50ms delays compared to the 250 ms delay that said library sends after each request being 4 in total.
Code: [Select]
// https://www.airtest.com/support/datasheet/COZIRSerialInterface.pdf

//    Command             Description               Example         Notes

//    G\r\n               fresh air calibration     G\r\n           Assumes 450ppm in fresh air !
//    F ##### #####\r\n   exact calibration         F 410 400\r\n   Note the space after F and the first value
//    K #\r\n             set mode                  K 1\r\n         Mode 0 = Command Mode,  Mode 1 = Streaming Mode,  Mode 2 = Polling Mode. --- Note the space after K
//    Z\r\n               get Co2                   Z\r\n
//    T\r\n               get temperature           T\r\n
//    H\r\n               get humidity              H\r\n

char buffer[8];
byte bufferCount = 0;
byte receiveCount = 0;
float celsius;
float humidity;
int digitalCo2;
// timer vars
unsigned long currentMillis;
unsigned long oneSecond_PreviousMillis;
const long oneSecondInterval = 1000;


void setup() {
  Serial1.begin(9600); // here i am using an Arduino Mega2560 on Serial 1, if your using a Uno you can use software serial instead.
  // Or just use the Main Serial below which i am using for data output, but if you do just use the main serial alone be care what you print when debugging else you may
  // Send a cmd to the COZIR and calibrate it in the wrong situation i.e gas mode or whatever so please be careful.
  Serial.begin(115200);
  Serial.println(F("Starting system."));
}

void loop() {
  currentMillis = millis();
  if (currentMillis - oneSecond_PreviousMillis >= oneSecondInterval) { // one second timer
    cozSerialData();
    oneSecond_PreviousMillis = currentMillis;
  }
}

void cozSerialData(void) {
  Serial1.println(F("Z")); // request co2
  delay(50);
  Serial1.println(F("T")); // request celsius
  delay(50);
  Serial1.println(F("H")); // request R/H
  bool foundValues = false;
  while (!foundValues) {
    char input = Serial1.read();
    if (input > 0) {
      if (input != '\n' && input != 32 && bufferCount < 8) { // get response from COZIR
        buffer[bufferCount] = input;
        bufferCount++;
      }
      else if (input == '\n') { // sort Serial data once we have received the \n
        bufferCount = 0;
        //Serial.println(buffer);
        for (byte i = 0; i < 7; i++) { // shuffle array back one position to get rid of the capital letter
          buffer[i] = buffer[i + 1];
        }
        receiveCount++;
        if (receiveCount == 1) {
          digitalCo2 = atoi(buffer);
          Serial.print(F("\nDigital Co2 ppm: ")); Serial.println(digitalCo2);
        }
        else if (receiveCount == 2) {
          celsius = atof(buffer);
          if (celsius < 1000) {
            celsius = 0.1 * celsius;
          }
          else {
            celsius = 0.1 * (celsius - 1000);
          }
          Serial.print(F("Celsius: ")); Serial.println(celsius);
        }
        else if (receiveCount == 3) {
          humidity = atof(buffer);
          humidity = humidity * 0.1;
          Serial.print(F("Humidity: ")); Serial.println(humidity);
          receiveCount = 0;
          foundValues = true;
        }
        for (byte i = 0; i < 8; i++) { // clear the entire buffer array
          buffer[i] = 0;
        }
      }
    }
  }
}


Output:

Digital Co2 ppm: 518
Celsius: 25.10
Humidity: 51.70

Title: Re: Temp, Humidity CO2 sensor and library.
Post by: morr8362 on Jan 17, 2019, 04:14 am
This library is great! I am looking through the data sheet and the library and I see there are options to show Light and/or LED intensity.   Which is called using "O". How can I get that to print to serial?



second question for KawasakiZx10r. I downloaded your .ino file and it doesn't print any data just the commands for H Z and T. Is there more I need to do to make use of your script?

Thanks for all the hard work you have put into this.
Title: Re: Temp, Humidity CO2 sensor and library.
Post by: KawasakiZx10r on Jan 21, 2019, 02:54 pm
Depends which codes are you running if you are running the second one, you must make sure that you have sent the command to set the CO2 sensor for polling mode.

In case you are unsure how to change the mode you must just send a capital k using serial print line, just send it the setup, mode 1 is streaming and mode 2 is polling.
Title: Re: Temp, Humidity CO2 sensor and library.
Post by: morr8362 on Jan 21, 2019, 07:40 pm
I have been operating in polling mode. Would Serial.Println ("K 2"); need to be re declared during the setup?

 I have been using the script by Dirtgambit I have gone back to see if Humidity and Temp read but now only the CO2 seems to be reading.
Title: Re: Temp, Humidity CO2 sensor and library.
Post by: nagol10h on Jan 23, 2019, 06:17 pm
Hi everyone,

Is anyone having problems using this cozir(versions 1 to 1.03 tested so far) library with the arduino create web editor?

I have been able to use my CO2 sensor using a sketch made by Jason Berger(does not use the cozir lib)

http://forum.arduino.cc/index.php?action=dlattach;topic=91467.0;attach=78152

code below
( I placed the sensor inside a 5% co2 incubator and changed the multiplier until it showed the correct ppm(50,000) for 5% CO2) (final value set to 70)

Code: [Select]

/*
Cozir Sample code
 Written by: Jason Berger ( for Co2meter.com)


// From PDF guide http://forum.arduino.cc/index.php?action=dlattach;topic=91467.0;attach=78152


 This sketch connects will connect to a Cozir gss or SprintIR sensor
 and report readings back to the host computer over usb. The value is
 stored in a global variable 'co2' and can be used for any number of applications.

 pin connections:

 Arduino________Cozir Sensor
 GND ------------------ 1 (gnd)
 3.3v------------------- 3 (Vcc)
 10 -------------------- 5 (Rx)
 11 -------------------- 7 (Tx)
 */
#include <SoftwareSerial.h>
SoftwareSerial mySerial(10, 11); // RX, TX
String val= ""; //holds the string of the value
double co2 =0; // holds the actual value
double multiplier = 70; //each range of sensor has a different value. ````
 // up to 2% =1
 // up to 65% = 10
 //up to 100% = 100;
uint8_t buffer[25];
uint8_t ind =0;
void setup()
{
 Serial.begin(9600); //Start Serial connection with host
 Serial.println("Co2Meter.com Cozir Sample");


 mySerial.begin(9600); //Start Serial connection with Sensor

}
void loop()
{
 //Cozir sensors ship from the factory in streaming mode
 //So we read incoming bytes into a buffer until we get '0x0A' which is the ASCII value for new-line
 while(buffer[ind-1] != 0x0A)
 {
 if(mySerial.available())
 {
 buffer[ind] = mySerial.read();
 ind++;
 }
 }

 report(); //Once we get the '0x0A' we will report what is in the buffer
}
void report()
{
 //Cycle through the buffer and send out each byte including the final linefeed

 /*
 each packet in the stream looks like "Z 00400 z 00360"
 'Z' lets us know its a co2 reading. the first number is the filtered value
 and the number after the 'z' is the raw value.
 We are really only interested in the filtered value

 */

 for(int i=0; i < ind+1; i++)
 {
 if(buffer[i] == 'z') //once we hit the 'z' we can stop
 break;

 if((buffer[i] != 0x5A)&&(buffer[i] != 0x20)) //ignore 'Z' and white space
 {
 val += buffer[i]-48; //because we break at 'z' the only bytes getting added are the numbers
 // we subtract 48 to get to the actual numerical value
 // example the character '9' has an ASCII value of 57. [57-48=9]


 }
 }

 co2 = (multiplier * val.toInt()); //now we multiply the value by a factor specific ot the sensor. see the cozir software guide

 Serial.print( "Co2 = ");
 Serial.print(co2);
 Serial.println(" ppm");
 ind=0; //Reset the buffer index to overwrite the previous packet
 val=""; //Reset the value string
}



I have read this entire thread and there is a lot of great info but I have not seen any talk of using the arduino create web editor with the cozir library.

Does the cozir library work on the arduino create web editor by automatically pulling the #included libraries from github? This is my understanding from reading about the way the web editor uses libraries.

I got the library and example sketch from

https://github.com/RobTillaart/Arduino/tree/master/libraries/Cozir

Using just the example sketch provided below without adding any libraries to the custom section of the web editor...

Code: [Select]


#include <cozir.h>

#if defined(ARDUINO) && ARDUINO >= 100
  #include <SoftwareSerial.h>
  SoftwareSerial nss(3,2);
#else
  #include <NewSoftSerial.h>
  NewSoftSerial nss(3,2);
#endif


COZIR czr(nss);

void setup()
{
  Serial.begin(9600);
  Serial.println("Setup");
  delay(1000);
}

void loop()
{
  Serial.println("Loop");
  float t = czr.Celsius();
  float f = czr.Fahrenheit();
  float h = czr.Humidity();
  uint16_t c = czr.CO2();
 
  Serial.print("Celcius = ");
  Serial.println(t);
  Serial.print("Fahrenheit = ");
  Serial.println(f);
  Serial.print("Humidity = ");
  Serial.println(h);
  Serial.print("CO2 = ");
  Serial.println(c);
 
  delay(3000);
}




 I get the error:
-------------------------------------
 Detecting libraries used...

/tmp/217961788/Cozir_test/Cozir_test.ino:5:19: fatal error: cozir.h: No such file or directory

compilation terminated.

exit status 1
-------------------------

If I uploaded the Cozir library (I tried versions cozir 1.0 to cozir 0.1.03 so far)  to the custom libraries section of the arduino create web editor and then try to compile the same code as above I get a slightly different error

------------------
Multiple libraries were found for "SoftwareSerial.h"

Used: /home/ubuntu/opt/cores/arduino/avr/libraries/SoftwareSerial

Not used: /home/ubuntu/opt/libraries/latest/printoo_library_1_0_2

Not used: /home/ubuntu/opt/libraries/latest/dabble_1_0_0

Using library cozir in folder: /tmp/930274234/custom/cozir (legacy)

Using library SoftwareSerial at version 1.0 in folder: /home/ubuntu/opt/cores/arduino/avr/libraries/SoftwareSerial

/tmp/930274234/custom/cozir/cozir.cpp:14:19: fatal error: Cozir.h: No such file or directory

#include "Cozir.h"

^

compilation terminated.

exit status 1

-----------------------------------------

In this scenario It seems to be recognizing one library(cozir.h) and then not being able to find the one with a Capital C (Cozir.h or maybe cozir.cpp?)

So it seems that uploading the cozir library to the custom section of the web editor does SOMETHING but still does not get it to compile completely. I have not been able to get past this problem and be able to use the functionality of this great library

I hope that it is something simple I am missing as a beginner
 
I am not sure where to go from here and any advice would be much appreciated!

Cozir libraries used to upload to the custom section of arduino create web editor are attached
Title: Re: Temp, Humidity CO2 sensor and library.
Post by: morr8362 on Jan 29, 2019, 11:44 pm


I'm also noob but I've gotten a lot better thanks to the help I've received through this forum.

A few things I would try:

1. Remove the files that contain the "SoftwareSerial" library from your libraries folder and see if that helps.

Quote
Used: /home/ubuntu/opt/cores/arduino/avr/libraries/SoftwareSerial

Not used: /home/ubuntu/opt/libraries/latest/printoo_library_1_0_2

Not used: /home/ubuntu/opt/libraries/latest/dabble_1_0_0
2. find a PC and Download the desktop IDE, I feel like I've read a lot of posts where people are having difficulties with the web version. 

3. If it's looking for Cozir.h and you have cozir.h it won't find the header file.


Hope this helps.
Title: Re: Temp, Humidity CO2 sensor and library.
Post by: AmphenolSensors on Feb 08, 2019, 02:05 pm
Suugest you just try an Amphenol Sensors T6713, half the cost, 5V, I2C, just as accurate, and can change address if you need.

Code is:
#define ADDR_6713  0x15 // default I2C slave address

byte data[6];
int CO2ppmValue;

void GetCO2PPM()
{
  // start I2C
  Wire.beginTransmission(ADDR_6713);
  Wire.write(0x04);
  Wire.write(0x13);
  Wire.write(0x8B);
  Wire.write(0x00);
  Wire.write(0x01);

  // end transmission
  Wire.endTransmission();

  // read report of current gas measurement in ppm after delay!
  delay(5);

  Wire.requestFrom(ADDR_6713, 4);    // request 4 bytes from slave device
  data[0] = Wire.read();
  data[1] = Wire.read();
  data[2] = Wire.read();
  data[3] = Wire.read();

  CO2ppmValue = ((data[2] & 0x3F ) << 8) | data[3];
}

Other code examples available at: https://github.com/AmphenolAdvancedSensors/Telaire