{"id":6595,"date":"2021-04-19T18:06:08","date_gmt":"2021-04-19T17:06:08","guid":{"rendered":"https:\/\/blog.fh-kaernten.at\/ingmarsretro\/2021\/04\/19\/co2-measurement-with-scd30-arduino-and-matlab\/"},"modified":"2021-04-19T18:06:08","modified_gmt":"2021-04-19T17:06:08","slug":"co2-measurement-with-scd30-arduino-and-matlab","status":"publish","type":"post","link":"https:\/\/blog.fh-kaernten.at\/ingmarsretro\/2021\/04\/19\/co2-measurement-with-scd30-arduino-and-matlab\/","title":{"rendered":"CO2 measurement with SCD30, Arduino and Matlab"},"content":{"rendered":"<div class=\"pvc_clear\"><\/div>\n<p id=\"pvc_stats_6595\" class=\"pvc_stats all  \" data-element-id=\"6595\" style=\"\"><i class=\"pvc-stats-icon medium\" aria-hidden=\"true\"><svg aria-hidden=\"true\" focusable=\"false\" data-prefix=\"far\" data-icon=\"chart-bar\" role=\"img\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" viewBox=\"0 0 512 512\" class=\"svg-inline--fa fa-chart-bar fa-w-16 fa-2x\"><path fill=\"currentColor\" d=\"M396.8 352h22.4c6.4 0 12.8-6.4 12.8-12.8V108.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v230.4c0 6.4 6.4 12.8 12.8 12.8zm-192 0h22.4c6.4 0 12.8-6.4 12.8-12.8V140.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v198.4c0 6.4 6.4 12.8 12.8 12.8zm96 0h22.4c6.4 0 12.8-6.4 12.8-12.8V204.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v134.4c0 6.4 6.4 12.8 12.8 12.8zM496 400H48V80c0-8.84-7.16-16-16-16H16C7.16 64 0 71.16 0 80v336c0 17.67 14.33 32 32 32h464c8.84 0 16-7.16 16-16v-16c0-8.84-7.16-16-16-16zm-387.2-48h22.4c6.4 0 12.8-6.4 12.8-12.8v-70.4c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v70.4c0 6.4 6.4 12.8 12.8 12.8z\" class=\"\"><\/path><\/svg><\/i> <img loading=\"lazy\" decoding=\"async\" width=\"16\" height=\"16\" alt=\"Loading\" src=\"https:\/\/blog.fh-kaernten.at\/ingmarsretro\/wp-content\/plugins\/page-views-count\/ajax-loader-2x.gif\" border=0 \/><\/p>\n<div class=\"pvc_clear\"><\/div>\n<p style=\"text-align: justify\"><a href=\"http:\/\/blog.fh-kaernten.at\/ingmarsretro\/files\/2021\/04\/IMG_20210412_112508-scaled.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"size-medium wp-image-5753 alignleft\" src=\"http:\/\/blog.fh-kaernten.at\/ingmarsretro\/files\/2021\/04\/IMG_20210412_112508-300x225.jpg\" alt=\"\" width=\"300\" height=\"225\" srcset=\"https:\/\/blog.fh-kaernten.at\/ingmarsretro\/files\/2021\/04\/IMG_20210412_112508-300x225.jpg 300w, https:\/\/blog.fh-kaernten.at\/ingmarsretro\/files\/2021\/04\/IMG_20210412_112508-1024x768.jpg 1024w, https:\/\/blog.fh-kaernten.at\/ingmarsretro\/files\/2021\/04\/IMG_20210412_112508-768x576.jpg 768w, https:\/\/blog.fh-kaernten.at\/ingmarsretro\/files\/2021\/04\/IMG_20210412_112508-1536x1152.jpg 1536w, https:\/\/blog.fh-kaernten.at\/ingmarsretro\/files\/2021\/04\/IMG_20210412_112508-2048x1536.jpg 2048w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a>This project &#8211; actually a mini project &#8211; might also be interesting for one or the other. It is the now well-known and frequently used carbon dioxide sensor SCD30 (CO2 sensor) from the manufacturer Sensirion. There are a number of projects that can be found on the internet. As part of a quick test setup, I tried to read out the data from the sensor using an Arduino Uno board in order to then display it in a plot using the Matlab software. The data transfer takes place via the serial interface or via the serial protocol of the USB-UART.<\/p>\n<p style=\"text-align: justify\">To connect the SCD30 to the Arduino, you need the power supply and the I\u00b2C data bus &#8211; so in total just four wires. This means that the minimum configuration is fulfilled and the data can be read out.<\/p>\n<p style=\"text-align: justify\"><a href=\"http:\/\/blog.fh-kaernten.at\/ingmarsretro\/files\/2021\/04\/IMG_20210412_112252-scaled.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"size-medium wp-image-5757 alignleft\" src=\"http:\/\/blog.fh-kaernten.at\/ingmarsretro\/files\/2021\/04\/IMG_20210412_112252-300x225.jpg\" alt=\"\" width=\"300\" height=\"225\" srcset=\"https:\/\/blog.fh-kaernten.at\/ingmarsretro\/files\/2021\/04\/IMG_20210412_112252-300x225.jpg 300w, https:\/\/blog.fh-kaernten.at\/ingmarsretro\/files\/2021\/04\/IMG_20210412_112252-1024x768.jpg 1024w, https:\/\/blog.fh-kaernten.at\/ingmarsretro\/files\/2021\/04\/IMG_20210412_112252-768x576.jpg 768w, https:\/\/blog.fh-kaernten.at\/ingmarsretro\/files\/2021\/04\/IMG_20210412_112252-1536x1152.jpg 1536w, https:\/\/blog.fh-kaernten.at\/ingmarsretro\/files\/2021\/04\/IMG_20210412_112252-2048x1536.jpg 2048w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a>The sensor itself works on the principle of NDIR technology. (NDIR = non-dispersive-infrared). That means the sensor is a small spectrometer. The medium to be examined is fed into a sample chamber. The sample chamber is illuminated by an infrared source and the IR light shines through the medium and a very narrow-band wavelength filter and then hits the IR detector. The wavelength of the filter is designed in such a way that precisely those wavelengths are let through that are absorbed by the molecules of the medium (gas). Depending on the number of molecules or the density of the gas, fewer light beams are recognized by the detector. A second measuring chamber, which is filled with a reference gas, serves as a reference. A controller on the sensor evaluates this information and forwards it in the form of ppm via the I\u00b2C (or switchable MOD-Bus) interface. There is also a temperature and humidity sensor on the board, the data of which can also be read out via the bus. The preset I\u00b2C address of the SCD30 is 0x61. The exact information on the data protocol can be found in the documentation from Sensirion.<a href=\"http:\/\/blog.fh-kaernten.at\/ingmarsretro\/files\/2021\/04\/anschluss-rotated.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"size-medium wp-image-6548 alignleft\" src=\"http:\/\/blog.fh-kaernten.at\/ingmarsretro\/files\/2021\/04\/anschluss-282x300.jpg\" alt=\"\" width=\"282\" height=\"300\" srcset=\"https:\/\/blog.fh-kaernten.at\/ingmarsretro\/files\/2021\/04\/anschluss-282x300.jpg 282w, https:\/\/blog.fh-kaernten.at\/ingmarsretro\/files\/2021\/04\/anschluss-rotated.jpg 382w\" sizes=\"auto, (max-width: 282px) 100vw, 282px\" \/><\/a><\/p>\n<p style=\"text-align: justify\">Ideally, as almost always, there is already a ready-made library suitable for students for the various microcontrollers. So you don&#8217;t have to worry anymore and can read out the data from the connected sensor directly. The example programs can be found under the examples of the libraries.<\/p>\n<div id=\"attachment_6547\" style=\"width: 829px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/blog.fh-kaernten.at\/ingmarsretro\/files\/2021\/04\/specs.jpg\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-6547\" class=\"wp-image-6547 size-full\" src=\"http:\/\/blog.fh-kaernten.at\/ingmarsretro\/files\/2021\/04\/specs.jpg\" alt=\"\" width=\"819\" height=\"410\" srcset=\"https:\/\/blog.fh-kaernten.at\/ingmarsretro\/files\/2021\/04\/specs.jpg 819w, https:\/\/blog.fh-kaernten.at\/ingmarsretro\/files\/2021\/04\/specs-300x150.jpg 300w, https:\/\/blog.fh-kaernten.at\/ingmarsretro\/files\/2021\/04\/specs-768x384.jpg 768w\" sizes=\"auto, (max-width: 819px) 100vw, 819px\" \/><\/a><p id=\"caption-attachment-6547\" class=\"wp-caption-text\">electrical specifications<\/p><\/div>\n<p style=\"text-align: justify\">The Arduino with 3.3V or 5V can be used for the supply voltage of the sensor. However, caution is advised when using the I\u00b2C bus: Here the input high level is set at 1.75-3.0V and the output high level with a maximum of 2.4V. But on an Arduino the levels are 5V !! So a level shifter has to be built in here &#8211; or at least, suitable resistors for a quick test.<\/p>\n<p style=\"text-align: justify\">The program code listed here essentially comes from the example of the library by Nathan Seidle from SparkFun Electronics:<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #f8f8f8;overflow: auto;width: auto;border: solid gray;border-width: .1em .1em .1em .8em;padding: .2em .6em\">\n<table>\n<tbody>\n<tr>\n<td>\n<pre style=\"margin: 0;line-height: 125%\"> 1\r\n 2\r\n 3\r\n 4\r\n 5\r\n 6\r\n 7\r\n 8\r\n 9\r\n10\r\n11\r\n12\r\n13\r\n14\r\n15\r\n16\r\n17\r\n18\r\n19\r\n20\r\n21\r\n22\r\n23\r\n24\r\n25\r\n26\r\n27\r\n28\r\n29\r\n30\r\n31\r\n32\r\n33\r\n34\r\n35\r\n36\r\n37\r\n38\r\n39\r\n40<\/pre>\n<\/td>\n<td>\n<pre style=\"margin: 0;line-height: 125%\"><span style=\"color: #408080;font-style: italic\">\/*<\/span>\r\n<span style=\"color: #408080;font-style: italic\">By: Nathan Seidle SparkFun Electronics  <\/span>\r\n<span style=\"color: #408080;font-style: italic\">Library: http:\/\/librarymanager\/All#SparkFun_SCD30  <\/span>\r\n<span style=\"color: #408080;font-style: italic\">*\/<\/span>\r\n\r\n<span style=\"color: #bc7a00\">#include &lt;Wire.h&gt;<\/span>\r\n<span style=\"color: #bc7a00\">#include \"SparkFun_SCD30_Arduino_Library.h\" <\/span>\r\nSCD30 airSensor;\r\n\r\n<span style=\"color: #b00040\">void<\/span> <span style=\"color: #0000ff\">setup<\/span>()  \r\n{\r\n  Wire.begin();\r\n  Serial.begin(<span style=\"color: #666666\">9600<\/span>);\r\n  <span style=\"color: #408080;font-style: italic\">\/\/Serial.println(\"SCD30 Example\");<\/span>\r\n  airSensor.begin(); <span style=\"color: #408080;font-style: italic\">\/\/This will cause readings to occur every two seconds<\/span>\r\n}\r\n\r\n<span style=\"color: #b00040\">void<\/span> <span style=\"color: #0000ff\">loop<\/span>()  \r\n{\r\n  <span style=\"color: #008000;font-weight: bold\">if<\/span> (airSensor.dataAvailable())\r\n  {\r\n   <span style=\"color: #408080;font-style: italic\">\/\/ Serial.print(\"co2(ppm):\");<\/span>\r\n   \r\n    Serial.print(airSensor.getCO2());\r\n\r\n    <span style=\"color: #408080;font-style: italic\">\/\/Serial.print(\" temp(C):\");<\/span>\r\n    Serial.print(<span style=\"color: #ba2121\">\",\"<\/span>);\r\n    Serial.print(airSensor.getTemperature(), <span style=\"color: #666666\">1<\/span>);\r\n\r\n   <span style=\"color: #408080;font-style: italic\">\/\/ Serial.print(\" humidity(%):\");<\/span>\r\n    Serial.print(<span style=\"color: #ba2121\">\",\"<\/span>);\r\n    Serial.print(airSensor.getHumidity(), <span style=\"color: #666666\">1<\/span>);\r\n\r\n    Serial.println();\r\n  }\r\n  <span style=\"color: #008000;font-weight: bold\">else<\/span>\r\n    <span style=\"color: #408080;font-style: italic\">\/\/Serial.println(\"No data\");<\/span>\r\n\r\n  delay(<span style=\"color: #666666\">500<\/span>);\r\n}\r\n<\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">With these lines of code in the Arduino Uno and the correct wiring (SDA -&gt; to Arduino A4 and SCL -&gt; to Arduino A5 via a suitable level converter) you can continue with Matlab. The Arduino should now output the following lines in a serial terminal: (example)<\/p>\n<p>473,28.5,12.9<br \/>\n473,28.5,13.0<br \/>\n470,28.5,13.1<br \/>\n469,28.5,12.9<br \/>\n466,28.5,12.9<br \/>\n465,28.5,12.7<br \/>\n465,28.5,12.5<br \/>\n463,28.6,12.6<br \/>\n461,28.6,12.5<br \/>\n463,28.5,12.4 &#8230; und so weiter<\/p>\n<p style=\"text-align: justify\">This now has to be read into Matlab and recorded over a definable period and at the same time displayed in a plot. The matlab script here makes it possible &#8230; (pn if someone needs it)<\/p>\n<p style=\"text-align: justify\"><a href=\"http:\/\/blog.fh-kaernten.at\/ingmarsretro\/files\/2021\/04\/matlabscreen.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-6554 size-full\" src=\"http:\/\/blog.fh-kaernten.at\/ingmarsretro\/files\/2021\/04\/matlabscreen.jpg\" alt=\"\" width=\"862\" height=\"601\" srcset=\"https:\/\/blog.fh-kaernten.at\/ingmarsretro\/files\/2021\/04\/matlabscreen.jpg 862w, https:\/\/blog.fh-kaernten.at\/ingmarsretro\/files\/2021\/04\/matlabscreen-300x209.jpg 300w, https:\/\/blog.fh-kaernten.at\/ingmarsretro\/files\/2021\/04\/matlabscreen-768x535.jpg 768w\" sizes=\"auto, (max-width: 862px) 100vw, 862px\" \/><\/a>The result is a plot that shows the course of CO2 in the room (in this case on my office desk).<\/p>\n<p><a href=\"http:\/\/blog.fh-kaernten.at\/ingmarsretro\/files\/2021\/04\/co2verlauf.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-5755 size-large\" src=\"http:\/\/blog.fh-kaernten.at\/ingmarsretro\/files\/2021\/04\/co2verlauf-1024x514.jpg\" alt=\"\" width=\"474\" height=\"238\" srcset=\"https:\/\/blog.fh-kaernten.at\/ingmarsretro\/files\/2021\/04\/co2verlauf-1024x514.jpg 1024w, https:\/\/blog.fh-kaernten.at\/ingmarsretro\/files\/2021\/04\/co2verlauf-300x150.jpg 300w, https:\/\/blog.fh-kaernten.at\/ingmarsretro\/files\/2021\/04\/co2verlauf-768x385.jpg 768w, https:\/\/blog.fh-kaernten.at\/ingmarsretro\/files\/2021\/04\/co2verlauf-1536x770.jpg 1536w, https:\/\/blog.fh-kaernten.at\/ingmarsretro\/files\/2021\/04\/co2verlauf.jpg 1920w\" sizes=\"auto, (max-width: 474px) 100vw, 474px\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"pvc_clear\"><\/div>\n<p id=\"pvc_stats_6595\" class=\"pvc_stats all  \" data-element-id=\"6595\" style=\"\"><i class=\"pvc-stats-icon medium\" aria-hidden=\"true\"><svg aria-hidden=\"true\" focusable=\"false\" data-prefix=\"far\" data-icon=\"chart-bar\" role=\"img\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" viewBox=\"0 0 512 512\" class=\"svg-inline--fa fa-chart-bar fa-w-16 fa-2x\"><path fill=\"currentColor\" d=\"M396.8 352h22.4c6.4 0 12.8-6.4 12.8-12.8V108.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v230.4c0 6.4 6.4 12.8 12.8 12.8zm-192 0h22.4c6.4 0 12.8-6.4 12.8-12.8V140.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v198.4c0 6.4 6.4 12.8 12.8 12.8zm96 0h22.4c6.4 0 12.8-6.4 12.8-12.8V204.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v134.4c0 6.4 6.4 12.8 12.8 12.8zM496 400H48V80c0-8.84-7.16-16-16-16H16C7.16 64 0 71.16 0 80v336c0 17.67 14.33 32 32 32h464c8.84 0 16-7.16 16-16v-16c0-8.84-7.16-16-16-16zm-387.2-48h22.4c6.4 0 12.8-6.4 12.8-12.8v-70.4c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v70.4c0 6.4 6.4 12.8 12.8 12.8z\" class=\"\"><\/path><\/svg><\/i> <img loading=\"lazy\" decoding=\"async\" width=\"16\" height=\"16\" alt=\"Loading\" src=\"https:\/\/blog.fh-kaernten.at\/ingmarsretro\/wp-content\/plugins\/page-views-count\/ajax-loader-2x.gif\" border=0 \/><\/p>\n<div class=\"pvc_clear\"><\/div>\n<p>This project &#8211; actually a mini project &#8211; might also be interesting for one or the other. It is the now well-known and frequently used carbon dioxide sensor SCD30 (CO2 sensor) from the manufacturer Sensirion. There are a number of projects that can be found on the internet. As part of a quick test setup,&hellip; <br \/> <a class=\"read-more\" href=\"https:\/\/blog.fh-kaernten.at\/ingmarsretro\/2021\/04\/19\/co2-measurement-with-scd30-arduino-and-matlab\/\">Read more<\/a><\/p>\n","protected":false},"author":86,"featured_media":5754,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"ngg_post_thumbnail":0,"footnotes":""},"categories":[1592],"tags":[2037,2031,2036,2034,1920,1839,2032,2035,2033],"class_list":["post-6595","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-arduino-en","tag-arduino-and-carbondioxide","tag-arduino-matlab-en","tag-co2-sensor-en","tag-co2-with-matab","tag-ic-en","tag-rs232-en","tag-scd30-en","tag-sensirion-en","tag-sensor-en"],"a3_pvc":{"activated":true,"total_views":993,"today_views":1},"_links":{"self":[{"href":"https:\/\/blog.fh-kaernten.at\/ingmarsretro\/wp-json\/wp\/v2\/posts\/6595","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.fh-kaernten.at\/ingmarsretro\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.fh-kaernten.at\/ingmarsretro\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.fh-kaernten.at\/ingmarsretro\/wp-json\/wp\/v2\/users\/86"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.fh-kaernten.at\/ingmarsretro\/wp-json\/wp\/v2\/comments?post=6595"}],"version-history":[{"count":0,"href":"https:\/\/blog.fh-kaernten.at\/ingmarsretro\/wp-json\/wp\/v2\/posts\/6595\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.fh-kaernten.at\/ingmarsretro\/wp-json\/wp\/v2\/media\/5754"}],"wp:attachment":[{"href":"https:\/\/blog.fh-kaernten.at\/ingmarsretro\/wp-json\/wp\/v2\/media?parent=6595"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.fh-kaernten.at\/ingmarsretro\/wp-json\/wp\/v2\/categories?post=6595"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.fh-kaernten.at\/ingmarsretro\/wp-json\/wp\/v2\/tags?post=6595"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}