0% found this document useful (0 votes)
45 views20 pages

DIY CO2 Sensor with ESP8266 Guide

This document provides instructions to build a sensor that can measure CO2, temperature, and humidity levels and display the measurements. It includes a shopping list of required components, wiring diagrams, and steps to assemble the sensor, load firmware onto the microcontroller, and configure it to display readings and send data to other systems like Domoticz or ThingSpeak. Contact information is provided if anyone wants to contribute to improving the instructions.

Uploaded by

Miraç Ölmez
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views20 pages

DIY CO2 Sensor with ESP8266 Guide

This document provides instructions to build a sensor that can measure CO2, temperature, and humidity levels and display the measurements. It includes a shopping list of required components, wiring diagrams, and steps to assemble the sensor, load firmware onto the microcontroller, and configure it to display readings and send data to other systems like Domoticz or ThingSpeak. Contact information is provided if anyone wants to contribute to improving the instructions.

Uploaded by

Miraç Ölmez
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Instruct ons to bu ld your own CO2, temperature and hum d ty sensor w th a d splay.

The sensor
s able to d splay the measurements. It can also send the measurements to another system, for
nstance domot cz or th ngspeak.

<If you w sh to contr bute to th s document, please contact me.>

Shopp ng l st

Item Pr ce (+/-) P cture

MH-Z19B CO2 sensor (choose one w th a cable) € 16.50

Temperature sensor (p ck one, BME280 or HTU21D): € 2.00

BME280 (temp / hum d ty / barometer)

Temperature sensor (p ck one, BME280 or HTU21D): € 2.00

HTU21D (temp / hum d ty)

ESP8266 m crocontroller € 2.00


0.96” I2C 128x64 d splay ( ncl PCB) € 1.85
Make sure the PCB measures 27.5mm * 27.8mm

Hous ng (can be 3d pr nted v a € 5.00


[Link] [Link]/en/3d-pr nt ng-serv ce/ )

5V USB power supply

M cro USB kabel (w th all w res connected1)

4* 10cm dupont F-F € 0.50 *

8* 1P dupont shell p n head € 0.50 *

8* 2.54mm female dupont w re term nal connector € 0.50 *


* these th ngs are not fully consumed by th s project

Shopp ng l st on al express: l nk

Tools
● Cable str pper
● Cr mper (l ke Eng neer PA-09 or IWISS IWS-2820M)
● Hobby kn fe
● Solder ng ron + solder

Bu ld nstruct ons
1. Solder the p ns on the ESP8266 board. Ensure that the p ns st ck out on the top of the
board (where most of the ch ps are), so solder them from the back s de. Note that when

1
Some cables don’t have all w res connected, they can only be used to charge a dev ce. The data l nes
are not connected.
you ntend to use t w th a breadboard the p ns are soldered on the other s de;

2. Solder the p ns on the d splay. Ensure that the p ns st ck out on the back s de (oppos te
of the d splay);
3. Solder the p ns on the temperature / hum d ty / barometer sensor (BME280 (R) or
HTU21D (L)). Ensure that the p ns st ck out on the oppos te s de of the sensor;
4. Cut the cable of the MH-Z19B halfway. Cr mp the dupont connectors to w res 3 (black), 4
(red) , 5 (blue) and 6 (green). The other w res can be shortened down to the connector;
5. Cut the 10cm dupont cables to 4cm + 6cm length. Use the follow ng color code: VCC =
red, ground = black, SCL = yellow, SDA = green. Str p the ends and connect them
together nto one dupont connector. Use a 4P dupont plast c shell wh ch needs to be
part ally cut off to make room for the 2 w res. Th s s tr cky;

6. Insert the 4 dupont connectors (wh ch have 2 w res connected) nto the 4p dupont plast c
shell. Make sure the order s correct for the p ns of the sensor (the p cture below was for
a HTU21D, for the BME280 the yellow and green w res need to be sw tched round);

7. Connect the MH-Z19b cable to the ESP8266 (see table below);


8. Connect the 4cm end of the 4 dupont connectors to the ESP8266 (see table below);

9. Flash the ESP8266 w th easy esp software (see nstruct ons below);
10. Conf gure the ESP8266 (see nstruct ons below);
11. Place the ESP8266 n the case;
12. Put the 4P dupont connector n the rear s de of the case;
13. Connect the 6cm end of 4 dupont connectors on the d splay. Make sure the order s
correct;
14. Connect the BME280 / HTU21D to the rear of the case;
15. Place the MH-Z19b n the l d of the case. Connect the MH-Z19b to the cable;

16. Place the l d on the case. It’s des gned to be a t ght f t and w ll requ re some persuad ng;
17. Sensor s read for use. Connect the m cro-usb cable to power the sensor.
P nouts
MH-Z19b CO2 sensor

MH-Z19B ESP8266 Color

P n 3 GND P n GND black

Pn4Vn P n VIN (5V) red

P n 5 RX P n D1 (GPIO-5) blue

P n 6 TX P n D2 (GPIO-4) green
Opt on 1: the BME280 s connected to the I2C bus, together w th the d splay.
BME280 ESP8266 Color

VIN P n 3V3 (3.3V) Red

GND P n GND Black

SCL P n D7 (GPIO-13) Yellow

SDA P n D6 (GPIO-12) Green

Opt on 2: the HTU21D s connected to the I2C bus, together w th the d splay.
HTU21D ESP8266 Color

VIN P n 3V3 (3.3V) Red

GND P n GND Black

SDA P n D6 (GPIO-12) Green

SCL P n D7 (GPIO-13) Yellow

The d splay s connected to the I2C bus, together w th the BME280 / HTU21D sensor.
D splay ESP8266 (v a T/H sensor) Color

GND P n GND Black

VIN P n 3V3 (3.3V) Red

SCL P n D7 (GPIO-13) Yellow

SDA P n D6 (GPIO-12) Green


F rmware and sett ngs

ESP Easy
Install the dr vers mac / w ndows for the CH340G ch p (mac 1 + 2 / w ndows v deo nstruct on).
After nstall ng the dr vers the COM port w ll only be v s ble when you connect the ESP8266
successfully to the PC. If t does not work try a d fferent USB cable f rst because not all cables
seem to work.

The ESP Easy software for the ESP8266 can be downloaded here. Download the
ESPEasy_mega-<date>.z p f le and unz p on your computer. Follow the nstruct ons on th s
page. Flash the normal vers on for the ESP8266 w th 4MB (of more) memory:
ESP_Easy_mega-<date>_normal_ESP8266_4096.b n

Not all releases seem to work, the release 2018.12.31 and 2019.06.07 are conf rmed to work.

Hardware Sett ngs:


I2C nterface:
GPIO ⇿ SDA: GPIO-12 (D6, green)
GPIO ⇾ SCL: GPIO-13 (D7, yellow)

Dev ces:
1: Dev ce: Gases - CO2 MH-Z19
Name: CO2
Enabled: <checked>
GPIO ← TX:: GPIO-4 (D2)
GPIO → RX:: GPIO-5 (D1)
Auto Base Cal brat on: Normal
F lter: Sk p Unstable
Send to controller <empty>
Value 1: PPM 0 dec mals
Value 2: Temperature 2 dec mals
Value 3: U 2 dec mals
2a: Dev ce: Env ronment - BMx280
Name: BME280
Enabled: <checked>
I2C Address: 0x76 (118) - (default)
Alt tude: <your alt tude>
Temperature offset: 02
2
I’ve set m ne to -43.
IDX: 235
Interval: 60 sec
1: T 2 Dec mals
2: H 2 Dec mals
3: P 2 Dec mals
2b: Dev ce: Env ronment - SI7021/HTU21D
Name: HTU21D
Enabled: <checked>
Resolut on: Temp 14 b ts / RH 12 b ts
IDX: 235
Interval: 60 sec
1: T %value% -3.8 3 2 Dec mals
2: H 2 Dec mals
3: Dev ce: D splay - OLED SSD1306/SH1106 Framed
Name: D splay
Enabled: <checked>
Controller: SSD1306
I2C address: 0x3c (default)
Rotat on: Rotated
L nes per frame: 1
Scroll: Very slow
L ne 1 (opt on a): [BME280#T] {D}
L ne 1 (opt on b): [HTU21D#T] {D}
L ne 2 (opt on a): [BME280#H] %
L ne 2 (opt on b): [HTU21D#H] %
L ne 3: [CO2#PPM] ppm
D splay button: none
D splay t meout: 0
Contrast: Med um
Interval: 5 sec

Tools → Advanced
Use NTP: <checked>
T mezone Offset (UTC +): 604
DST: <checked>

3
See: Bas cs: Cal brat on and Accuracy
4
CET t mezone s 60 m nutes ahead of UTC
Domot cz Integrat on

The “Rules” tab sn’t v s ble by default. To make t v s ble: Tools tab: → Advanced → Rules
<checked>

Rules, Rule set 1:


on CO2#PPM do
SendToHTTP
domot [Link],80,/[Link]?type=command&param=udev ce& dx=228&nvalue=[CO2#PPM]
endon

In Domot cz create two dummy dev ces n the Setup → Hardware tab. G ve each a name, for
nstance “K tchen CO2” and “K tchen T+H”. In the “create v rtual sensors” d alog, g ve them the
same names and select “a r qual ty” for the CO2 dev ce and (opt on a) “Tem+Hum+Baro” for the
BME280 or (opt on b) “Tem+Hum” for the HTU21D dev ce.

Got to Setup → Dev ces, add the two dev ces by cl ck ng the con “add dev ce”. Also wr te down
the dx values of the dev ces. Those dx values are requ red n the sett ngs on the ESP8266.

On the ESP8266 the follow ng needs to be setup to send the data to Domot cz:

Controllers:
1: Protocol: Domot cz HTTP
Locate controller: Use Hostname
Controller Hostname: domot [Link]
Controller Port: 80
Controller User: <empty>
Controller Password: <empty>
Enabled: <checked>
Th ngSpeak ntegrat on
Th ngspeak s MatLabs data-logg ng platform for data v sual sat on. It s free for non-commerc al
use. Th ngV ew s a great App to v ew h stor cal data. The Th ngSpeak web- nterface just shows
the last hour.

Beware!
The data s stored n the USA and there s no guarantee that you w ll stay the owner of
the data nor are you sure that anyone else can use/see t. For show ng counters
publ cally t s a fantast c tool though. CO2-data s HIGHLY pr vacy sens t ve: you can see
how many people are sleep ng n a bedroom w th n m nutes!

Good to understand the data collect on process:

The Easy ESP sends the data to the Th ngSpeak buckets based on the IDX + ncremental
value.

So the TS value 1 = IDX 1 sensor 1 (PPM)


So the TS value 2 = IDX 1 sensor 2 (Temp sensor 0 d g ts, worthless)
So the TS value 3 = IDX 1 sensor 3 (upt me)
So the TS value 4 = IDX 4 sensor 1 (temperature, 2 dec mals)
So the TS value 5 = IDX 4 sensor 2 (hum d ty, 2 dec mals)
So the TS value 6 = IDX 4 sensor 3 (Pressure, 2 dec mals)

Sett ng up the data-logg ng s de


- Make an account on th [Link], log n
- Create a channel, the name dent f es your sensor and th s s just a label
- the channel number s ass gned to you
- Create the 6 data f elds accord ng to the follow ng scheme: (these are just labels)
- Share the data publ cally f you l ke (th s enables the Publ c V ews)

Get the wr te API-key


(as the sensor s the one push ng data to Th ngSpeak)

Th s completes the data-logg ng s de, now the sensor s de.


- Log n to the ESP Easy nterface
- Create a Th ngSpeak "controller"
- Important : m n mum send nterval 15000 ms
- The Th ngHTTP Name s the bucket where stuff gets stored
- Paste the API-key
- Enable the upload
Dev ces sett ng:
- Set all sensors of the BME280 to two dec mals (otherw se the graphs look bumpy on the
Th ngSpeak-s de)
Please note the IDX numbers (as d scussed n the ntroduct on):

And f nally:
- Tools / advanced / Message nterval

Now try play ng around w th mak ng "w dg t" graphs:

Enjoy! (Lars)
ESP Home
Note: for nstruct ons on us ng ESPHome, please follow th s l nk.
P n Def n t on:
● VCC: 3.3V
● GND: Ground
● CLK: Clock s mulat on
● CS: ch p select s gnal
● SDI: data nput, s m lar to MOSI
● D / C: command / data sw tch

You might also like