Senseair Arduino

  • Upload
    kgoulas

  • View
    232

  • Download
    0

Embed Size (px)

Citation preview

  • 8/13/2019 Senseair Arduino

    1/5

    CO2Meter.comIndoor Air Quality Products

    Revision: 1.1

    LastUpdated: 1/3/2010

    Author: AndrewRobinson

    Application Note: Interfacing with Arduino over I2CThe Arduino makes an ideal platform for prototyping and data collection with the K series of CO2 sensors.

    Electrical Connections

    Interfacing with the sensor electrically is easy. Although the sensor runs at 3.3V it is tolerant of 5V logic levels. Adirect electrical connection is possible using the Arduinos hardware I2C pins as follows:

    Arduino analog input 5 - I2C SCL

    Arduino analog input 4 - I2C SDA

    Both the Arduino and CO2 sensor have built-in pull-up resistors.

    The sensor will be wired using the I2C terminal in the following drawing:

  • 8/13/2019 Senseair Arduino

    2/5

  • 8/13/2019 Senseair Arduino

    3/5

    CO2Meter.comIndoor Air Quality Products

    Revision: 1.1

    LastUpdated: 1/3/2010

    Author: AndrewRobinson

    AppendixA:SampleCode/ / CO2 Meter K- ser i es Exampl e I nt er f ace

    / / by Andrew Robi nson, CO2 Met er

    / / Tal ks vi a I 2C t o K30/ K22/ K33/ Logger sensors and di spl ays CO2 val ues/ / 12/ 31/ 09

    #i ncl ude

    / / We wi l l be usi ng t he I 2C hardware i nt er f ace on t he Ar dui no i n/ / combi nat i on wi t h t he bui l t - i n Wi r e l i br ar y t o i nt er f ace.

    / / Ar dui no anal og i nput 5 - I 2C SCL/ / Ar dui no anal og i nput 4 - I 2C SDA

    / *I n t hi s exampl e we wi l l do a basi c r ead of t he CO2 val ue and checksum ver i f i cat i on. For more advanced appl i cat i ons pl ease see t he I 2C Comm gui de.

    */

    int co2Addr = 0x68; / / Thi s i s t he def aul t addr ess of t he CO2 sensor , 7bi t s shi f t ed l ef t .

    void setup( ) {Ser i al . begi n( 9600) ; Wi r e. begi n ( ) ; pi nMode( 13, OUTPUT) ; / / We wi l l use t hi s pi n as a r ead- i ndi cat or Ser i al . pr i nt l n( "What a wonder f ul day, t o read at mospher i c CO2 concent r at i ons! " ) ;

    }

    / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / Funct i on : i nt r eadCO2( ) / / Ret ur ns : CO2 Val ue upon success, 0 upon checksum f ai l ur e/ / Assumes : - Wi r e l i br ar y has been i mport ed successf ul l y./ / - LED i s connect ed t o I O pi n 13/ / - CO2 sensor addr ess i s def i ned i n co2_addr / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / int r eadCO2( ) {int co2_val ue = 0; / / We wi l l st or e t he CO2 val ue i nsi de t hi s var i abl e.

    di gi t al Wri t e( 13, HI GH) ; / / On most Ar dui no pl at f or ms t hi s pi n i s used as an i ndi cat or l i ght .

    / / / / / / / / / / / / / / / / / / / / / / / / / / / * Begi n Wr i t e Sequence */ / / / / / / / / / / / / / / / / / / / / / / / / / /

    Wi r e. begi nTransmi ssi on( co2Addr ) ; Wi r e. send( 0x22) ; Wi r e. send( 0x00) ; Wi r e. send( 0x08) ; Wi r e. send( 0x2A) ;

  • 8/13/2019 Senseair Arduino

    4/5

    CO2Meter.comIndoor Air Quality Products

    Revision: 1.1

    LastUpdated: 1/3/2010

    Author: AndrewRobinson

    Wi r e. endTransmi ssi on( ) ;

    / / / / / / / / / / / / / / / / / / / / / / / / / / * End Wr i t e Sequence. */ / / / / / / / / / / / / / / / / / / / / / / / / /

    / *We wai t 10ms f or t he sensor t o process our command. The sensor s' s pr i mar y dut i es ar e t o accurat el ymeasur e CO2 val ues. Wai t i ng 10ms wi l l ensur e thedat a i s pr oper l y wr i t t en t o RAM

    */

    del ay( 10) ;

    / / / / / / / / / / / / / / / / / / / / / / / / / / * Begi n Read Sequence */ / / / / / / / / / / / / / / / / / / / / / / / / /

    / *Si nce we request ed 2 byt es f r om t he sensor we must r ead i n 4 byt es. Thi s i ncl udes t he payl oad, checksum, and command st at us byt e.

    */

    Wi r e. r equest From( co2Addr , 4) ;

    byt e i = 0; byt e buf f er [ 4] = {0, 0, 0, 0};

    / *

    Wi r e. avai l abl e( ) i s not nessessar y. I mpl ement at i on i s obscur e but we l eavei t i n her e f or por t abi l i t y and t o f ut ur e pr oof our code

    */ while( Wi r e. avai l abl e( ) ) {

    buf f er [ i ] = Wi r e. r ecei ve( ) ; i ++;

    }

    / / / / / / / / / / / / / / / / / / / / / / / / * End Read Sequence */ / / / / / / / / / / / / / / / / / / / / / / /

    / *Usi ng some bi t wi se mani pul at i on we wi l l shi f t our buf f er i nt o an i nt eger f or gener al consumpt i on

    */

    co2_val ue = 0; co2_val ue | = buf f er [ 1] & 0xFF; co2_val ue = co2_val ue

  • 8/13/2019 Senseair Arduino

    5/5

    CO2Meter.comIndoor Air Quality Products

    Revision: 1.1

    LastUpdated: 1/3/2010

    Author: AndrewRobinson

    if( sum == buf f er [ 3] ) {

    / / Success! di gi t al Wri t e( 13, LOW) ; return co2_val ue;

    }else{

    / / Fai l ure! / *

    Checksum f ai l ur e can be due to a number of f act ors, f uzzy el ect r ons, sensor busy, et c.

    */

    di gi t al Wri t e( 13, LOW) ; return 0;

    }}

    void l oop( ) {

    int co2Val ue = r eadCO2( ) ; if( co2Val ue > 0) {

    Ser i al . pr i nt ( "CO2 Val ue: " ) ; Ser i al . pr i nt l n( co2Val ue) ;

    }else{

    Ser i al . pr i nt l n( "Checksum f ai l ed / Communi cat i on f ai l ur e" ) ; }

    del ay( 2000) ; }

    Note:Checksumfailuresdohappenperiodically.Thesecanbepartiallyavoidedbyincreasingthedelaytime

    betweenwritingtherequestpacketandpollingforaresponse.Howevertheycannotbeeliminated.The

    sensorsprimaryfunctionsarerelatedtoaccuratelymeasuringCO2concentrationsandasasideeffect

    communicationisoftendelayedascrucial,timesensitiveoperationsaretakenplace. Planyourcode

    accordingly.Amoreadvancedapplicationwouldinvolveproperretrylogic.