{"id":69,"date":"2019-12-03T17:34:26","date_gmt":"2019-12-03T17:34:26","guid":{"rendered":"https:\/\/www.galagaking.org\/?p=69"},"modified":"2020-01-12T22:25:36","modified_gmt":"2020-01-12T21:25:36","slug":"esphome-co2-sensor","status":"publish","type":"post","link":"https:\/\/www.galagaking.org\/nl\/2019\/12\/03\/esphome-co2-sensor\/","title":{"rendered":"ESPHome \u2013 CO2 sensor"},"content":{"rendered":"<p>Following the ESPHome <a href=\"https:\/\/www.galagaking.org\/nl\/2019\/11\/05\/esphome-workshop\/\">introduction<\/a>, we use the Winsen CO2 sensor to measure indoor CO2 level. For reference look at the <a href=\"https:\/\/www.winsen-sensor.com\/d\/files\/MH-Z19B.pdf\">datasheet<\/a>. <\/p>\n\n\n\n<figure class=\"wp-block-image is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.winsen-sensor.com\/d\/propic\/MH-Z19B.jpg\" alt=\"\" width=\"323\" height=\"323\"\/><\/figure>\n\n\n\n<p>The sensor outputs the measured CO2 level in a PWM signal and a UART (serial) stream. We will use the UART interface in our example.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Connection<\/h2>\n\n\n\n<p>We need four connections: power (5V &#8211; 150mA, GND, Rx and Tx). If you use the sensor with the JST6 cable you can solder plugs on these wires, or use Dupont crimp connectors. In the case you solder them you can connect Red \/ Black (Power) to one 2 pin female header, and Blue\/Green (Rx\/Tx) to another 2 pin header as shown.<\/p>\n\n\n\n<p><img loading=\"lazy\" decoding=\"async\" width=\"150\" height=\"267\" class=\"wp-image-77\" style=\"width: 150px;\" src=\"http:\/\/www.galagaking.org\/wp-content\/uploads\/2019\/12\/20191202_224126-scaled.jpg\" alt=\"\" srcset=\"https:\/\/www.galagaking.org\/wp-content\/uploads\/2019\/12\/20191202_224126-scaled.jpg 1440w, https:\/\/www.galagaking.org\/wp-content\/uploads\/2019\/12\/20191202_224126-169x300.jpg 169w, https:\/\/www.galagaking.org\/wp-content\/uploads\/2019\/12\/20191202_224126-576x1024.jpg 576w, https:\/\/www.galagaking.org\/wp-content\/uploads\/2019\/12\/20191202_224126-768x1365.jpg 768w, https:\/\/www.galagaking.org\/wp-content\/uploads\/2019\/12\/20191202_224126-864x1536.jpg 864w, https:\/\/www.galagaking.org\/wp-content\/uploads\/2019\/12\/20191202_224126-1152x2048.jpg 1152w\" sizes=\"auto, (max-width: 150px) 100vw, 150px\" \/><img loading=\"lazy\" decoding=\"async\" width=\"150\" height=\"267\" class=\"wp-image-79\" style=\"width: 150px;\" src=\"http:\/\/www.galagaking.org\/wp-content\/uploads\/2019\/12\/20191202_224609-scaled.jpg\" alt=\"\" srcset=\"https:\/\/www.galagaking.org\/wp-content\/uploads\/2019\/12\/20191202_224609-scaled.jpg 1440w, https:\/\/www.galagaking.org\/wp-content\/uploads\/2019\/12\/20191202_224609-169x300.jpg 169w, https:\/\/www.galagaking.org\/wp-content\/uploads\/2019\/12\/20191202_224609-576x1024.jpg 576w, https:\/\/www.galagaking.org\/wp-content\/uploads\/2019\/12\/20191202_224609-768x1365.jpg 768w, https:\/\/www.galagaking.org\/wp-content\/uploads\/2019\/12\/20191202_224609-864x1536.jpg 864w, https:\/\/www.galagaking.org\/wp-content\/uploads\/2019\/12\/20191202_224609-1152x2048.jpg 1152w\" sizes=\"auto, (max-width: 150px) 100vw, 150px\" \/><\/p>\n\n\n\n<p>Both on Wemos D1 and NodeMCU v2 the 5V power line (or Vin) is besides Ground (or &#8216;G&#8217;). Connect the power connector according to the colours (5V is Red, Ground is Black).<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"680\" height=\"517\" src=\"http:\/\/www.galagaking.org\/wp-content\/uploads\/2019\/12\/2019-12-01_23-17-11.png\" alt=\"\" class=\"wp-image-83\" srcset=\"https:\/\/www.galagaking.org\/wp-content\/uploads\/2019\/12\/2019-12-01_23-17-11.png 680w, https:\/\/www.galagaking.org\/wp-content\/uploads\/2019\/12\/2019-12-01_23-17-11-300x228.png 300w\" sizes=\"auto, (max-width: 680px) 100vw, 680px\" \/><\/figure>\n\n\n\n<p>The Rx \/ Tx lines are connected to D1 (GPIO5), Green, Tx from module and D2 (GPIO4), Blue, Rx to module.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">YAML file<\/h2>\n\n\n\n<p>We will use ESPHome for the software. If you are not familair with ESPHome, have a look at our <a href=\"https:\/\/www.galagaking.org\/nl\/2019\/11\/05\/esphome-workshop\/\">previous workshop<\/a>. Now we can add the sensor and the UART to the YAML file. First we will activate the uart. Feel free to use other pins if you use another ESP module.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">#D1:&nbsp;Green&nbsp;Wire&nbsp;\/&nbsp;mhz19\n#D2:&nbsp;Blue&nbsp;Wire&nbsp;\/&nbsp;mhz19\nuart:\n&nbsp;&nbsp;rx_pin:&nbsp;D1\n&nbsp;&nbsp;tx_pin:&nbsp;D2\n&nbsp;&nbsp;baud_rate:&nbsp;9600<\/pre>\n\n\n\n<p>We will add the MHZ sensor just before the BME280 sensor. There can only be one &#8216;sensor&#8217; section! The sensor has also a temperature sensor, we will not use it here. More details on the <a href=\"https:\/\/esphome.io\/components\/sensor\/mhz19.html\">ESPHome site<\/a>.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sensor:\n&nbsp;&nbsp;-&nbsp;platform:&nbsp;mhz19\n&nbsp;&nbsp;&nbsp;&nbsp;co2:\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;name:&nbsp;\"MHZ19&nbsp;CO2&nbsp;Value\"\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;id:&nbsp;co2value\n&nbsp;&nbsp;&nbsp;&nbsp;temperature:\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;name:&nbsp;\"MH-Z19&nbsp;Temperature\"\n &nbsp;&nbsp; automatic_baseline_calibration:&nbsp;false&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;update_interval:&nbsp;60s <\/pre>\n\n\n\n<p>If we upload our code, the ESP will initialize the sensor and read the values. They are also available at the webinterface and will be published on the MQTT chanel if you activated this.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Add a display<\/h2>\n\n\n\n<p>To show the values on a small I2C display, we first have to connect the display to the I2C bus. That is a challenge, because the BME280 sensor also shares this bus. On a breadboard it is easy to connect both the Display and the BME280, if you want to solder you have to create a &#8216;splitter&#8217;. This can be done by soldering, or with crimp connectors.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"225\" height=\"225\" src=\"http:\/\/www.galagaking.org\/wp-content\/uploads\/2019\/12\/image.jpeg\" alt=\"Afbeeldingsresultaat voor ssd1306\" class=\"wp-image-84\" srcset=\"https:\/\/www.galagaking.org\/wp-content\/uploads\/2019\/12\/image.jpeg 225w, https:\/\/www.galagaking.org\/wp-content\/uploads\/2019\/12\/image-150x150.jpeg 150w, https:\/\/www.galagaking.org\/wp-content\/uploads\/2019\/12\/image-100x100.jpeg 100w\" sizes=\"auto, (max-width: 225px) 100vw, 225px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-gallery columns-3 is-cropped wp-block-gallery-1 is-layout-flex wp-block-gallery-is-layout-flex\"><ul class=\"blocks-gallery-grid\"><li class=\"blocks-gallery-item\"><figure><img loading=\"lazy\" decoding=\"async\" width=\"576\" height=\"1024\" src=\"http:\/\/www.galagaking.org\/wp-content\/uploads\/2019\/12\/20191202_221754-576x1024.jpg\" alt=\"\" data-id=\"73\" data-full-url=\"http:\/\/www.galagaking.org\/wp-content\/uploads\/2019\/12\/20191202_221754-scaled.jpg\" data-link=\"https:\/\/www.galagaking.org\/?attachment_id=73\" class=\"wp-image-73\" srcset=\"https:\/\/www.galagaking.org\/wp-content\/uploads\/2019\/12\/20191202_221754-576x1024.jpg 576w, https:\/\/www.galagaking.org\/wp-content\/uploads\/2019\/12\/20191202_221754-169x300.jpg 169w, https:\/\/www.galagaking.org\/wp-content\/uploads\/2019\/12\/20191202_221754-768x1365.jpg 768w, https:\/\/www.galagaking.org\/wp-content\/uploads\/2019\/12\/20191202_221754-864x1536.jpg 864w, https:\/\/www.galagaking.org\/wp-content\/uploads\/2019\/12\/20191202_221754-1152x2048.jpg 1152w, https:\/\/www.galagaking.org\/wp-content\/uploads\/2019\/12\/20191202_221754-scaled.jpg 1440w\" sizes=\"auto, (max-width: 576px) 100vw, 576px\" \/><\/figure><\/li><li class=\"blocks-gallery-item\"><figure><img loading=\"lazy\" decoding=\"async\" width=\"576\" height=\"1024\" src=\"http:\/\/www.galagaking.org\/wp-content\/uploads\/2019\/12\/20191202_222158-576x1024.jpg\" alt=\"\" data-id=\"75\" data-full-url=\"http:\/\/www.galagaking.org\/wp-content\/uploads\/2019\/12\/20191202_222158-scaled.jpg\" data-link=\"https:\/\/www.galagaking.org\/?attachment_id=75\" class=\"wp-image-75\" srcset=\"https:\/\/www.galagaking.org\/wp-content\/uploads\/2019\/12\/20191202_222158-576x1024.jpg 576w, https:\/\/www.galagaking.org\/wp-content\/uploads\/2019\/12\/20191202_222158-169x300.jpg 169w, https:\/\/www.galagaking.org\/wp-content\/uploads\/2019\/12\/20191202_222158-768x1365.jpg 768w, https:\/\/www.galagaking.org\/wp-content\/uploads\/2019\/12\/20191202_222158-864x1536.jpg 864w, https:\/\/www.galagaking.org\/wp-content\/uploads\/2019\/12\/20191202_222158-1152x2048.jpg 1152w, https:\/\/www.galagaking.org\/wp-content\/uploads\/2019\/12\/20191202_222158-scaled.jpg 1440w\" sizes=\"auto, (max-width: 576px) 100vw, 576px\" \/><\/figure><\/li><li class=\"blocks-gallery-item\"><figure><img loading=\"lazy\" decoding=\"async\" width=\"576\" height=\"1024\" src=\"http:\/\/www.galagaking.org\/wp-content\/uploads\/2019\/12\/20191202_222705-576x1024.jpg\" alt=\"\" data-id=\"76\" data-full-url=\"http:\/\/www.galagaking.org\/wp-content\/uploads\/2019\/12\/20191202_222705-scaled.jpg\" data-link=\"https:\/\/www.galagaking.org\/?attachment_id=76\" class=\"wp-image-76\" srcset=\"https:\/\/www.galagaking.org\/wp-content\/uploads\/2019\/12\/20191202_222705-576x1024.jpg 576w, https:\/\/www.galagaking.org\/wp-content\/uploads\/2019\/12\/20191202_222705-169x300.jpg 169w, https:\/\/www.galagaking.org\/wp-content\/uploads\/2019\/12\/20191202_222705-768x1365.jpg 768w, https:\/\/www.galagaking.org\/wp-content\/uploads\/2019\/12\/20191202_222705-864x1536.jpg 864w, https:\/\/www.galagaking.org\/wp-content\/uploads\/2019\/12\/20191202_222705-1152x2048.jpg 1152w, https:\/\/www.galagaking.org\/wp-content\/uploads\/2019\/12\/20191202_222705-scaled.jpg 1440w\" sizes=\"auto, (max-width: 576px) 100vw, 576px\" \/><\/figure><\/li><\/ul><\/figure>\n\n\n\n<p>GND should be connected to G or Ground, VCC to the 3V power line (some displays will accept 5v as well). SCL to D6 (GPIO12) and SDA to D7 (GPIO13). I2C was already installed in our previous YAML experiment. We will need a font, which can be downloaded <a href=\"https:\/\/github.com\/khanhtq\/bebas-neue-fontface\/blob\/master\/fonts\/BebasNeue-Regular.ttf\">here<\/a>. You are free to download other fonts and experiment with a nice layout.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">i2c:\n&nbsp;&nbsp;sda:&nbsp;D7\n&nbsp;&nbsp;scl:&nbsp;D6\n&nbsp;&nbsp;scan:&nbsp;True\n font:\n&nbsp;&nbsp;-&nbsp;file:&nbsp;\"BebasNeue-Regular.ttf\"\n&nbsp;&nbsp;&nbsp;&nbsp;id:&nbsp;slk\n&nbsp;&nbsp;&nbsp;&nbsp;size:&nbsp;20\n&nbsp;&nbsp;-&nbsp;file:&nbsp;\"BebasNeue-Regular.ttf\"\n&nbsp;&nbsp;&nbsp;&nbsp;id:&nbsp;bebas\n&nbsp;&nbsp;&nbsp;&nbsp;size:&nbsp;48\n&nbsp;&nbsp;-&nbsp;file:&nbsp;\"BebasNeue-Regular.ttf\"\n&nbsp;&nbsp;&nbsp;&nbsp;id:&nbsp;bebas20\n&nbsp;&nbsp;&nbsp;&nbsp;size:&nbsp;20 \n display:\n&nbsp;&nbsp;-&nbsp;platform:&nbsp;ssd1306_i2c\n&nbsp;&nbsp;&nbsp;&nbsp;model:&nbsp;\"SSD1306&nbsp;128x64\"\n&nbsp;&nbsp;&nbsp;&nbsp;reset_pin:&nbsp;D0\n&nbsp;&nbsp;&nbsp;&nbsp;address:&nbsp;0x3C\n&nbsp;&nbsp;&nbsp;&nbsp;id:&nbsp;my_display\n&nbsp;&nbsp;&nbsp;&nbsp;pages:\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-&nbsp;id:&nbsp;page1\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;lambda:&nbsp;|-\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;it.print(0,&nbsp;1,&nbsp;id(slk),&nbsp;\"CO2&nbsp;PPM\");\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(id(co2value).has_state())&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;it.printf(0,&nbsp;15,&nbsp;id(bebas),&nbsp;TextAlign::TOP_LEFT,&nbsp;\"%.0f\",&nbsp;id(co2value).state);\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} <\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Complete YAML file<\/h2>\n\n\n\n<p>To experiment, the complete YAML file can be downloaded <a href=\"https:\/\/www.galagaking.org\/nl\/download\/esp_co2-yaml-files\/\">here<\/a>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Captive Portal<\/h2>\n\n\n\n<p>If you do not know the WiFi network at compilation time, you may use a &#8216;captive portal&#8217;. If the given WiFi network was not found, your node will start as an access point with the given credentials. You can connect with your phone or PC, go to 192.168.4.1 and enter the right credentials. They will be stored until the next upload of your program.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">wifi:\n&nbsp;&nbsp;ssid:&nbsp;!secret&nbsp;wifi_ssid\n&nbsp;&nbsp;password:&nbsp;!secret&nbsp;wifi_key\n\nap:\n&nbsp;&nbsp;&nbsp;&nbsp;ssid:&nbsp;\"CO2&nbsp;meter\"\n&nbsp;&nbsp;&nbsp;&nbsp;password:&nbsp;\"1234abcd\"\n\ncaptive_portal: <\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Casing<\/h2>\n\n\n\n<p>Stefan has created a nice casing for this module. Details can be found <a href=\"https:\/\/www.thingiverse.com\/thing:3465447\">here<\/a>. In the written setup &#8216;ESPEasy&#8217; is used to connect the sensor. Feel free to use this, or the ESPHome implementation.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/cdn.thingiverse.com\/renders\/f5\/45\/08\/58\/c4\/5b5811ddb57c1c5589e3179e39c67de8_preview_featured.jpg\" alt=\"\"\/><\/figure>\n\n\n\n<p><\/p>","protected":false},"excerpt":{"rendered":"<p>Following the ESPHome introduction, we use the Winsen CO2 sensor to measure indoor CO2 level. For reference look at the datasheet. The sensor outputs the measured CO2 level in a PWM signal and a UART (serial) stream. We will use the UART interface in our example. Connection We need four connections: power (5V &#8211; 150mA, [&hellip;]<\/p>","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"enabled":false},"version":2}},"categories":[9],"tags":[2,3,4,7],"class_list":["post-69","post","type-post","status-publish","format-standard","hentry","category-workshop","tag-esp8266","tag-esphome","tag-mqtt","tag-nodered"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.galagaking.org\/nl\/wp-json\/wp\/v2\/posts\/69","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.galagaking.org\/nl\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.galagaking.org\/nl\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.galagaking.org\/nl\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.galagaking.org\/nl\/wp-json\/wp\/v2\/comments?post=69"}],"version-history":[{"count":0,"href":"https:\/\/www.galagaking.org\/nl\/wp-json\/wp\/v2\/posts\/69\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.galagaking.org\/nl\/wp-json\/wp\/v2\/media?parent=69"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.galagaking.org\/nl\/wp-json\/wp\/v2\/categories?post=69"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.galagaking.org\/nl\/wp-json\/wp\/v2\/tags?post=69"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}