CO2 Sensor With Modbus RTU and 24V With D1 Mini
by Nils123456 in Circuits > Sensors
8 Views, 0 Favorites, 0 Comments
CO2 Sensor With Modbus RTU and 24V With D1 Mini
I wanted to build a CO2 sensor using the Sensirion SCD41 CO2 sensor, which provides accurate readings between 400 - 5000 ppm. The sensor should be able to speak Modbus RTU and receive 24V to include it in an industrial setup.
Before proceeding to PCB layouting I wanted to test all components one-by-one and see whether my ESPHome code was working as expected with all components.
I had already created some D1 Mini shields before for other things that I was working on, so I had them available. If you want to reproduce this tutorial or parts of it, you can easily obtain them on Tindie.
Supplies
I have used:
- CO2 shield (with Sensirion SCD41 sensor)
- RS485 shield (UART to RS485)
- 6-36V power converter shield
- Aliexpress D1 Mini v4.0
- Aliexpress enclosure
- Aliexpress SP1312 connector (5pin)
- 3D printed lid (attached)
- Aliexpress active cole filter
Downloads
I started out with soldering the shields to header pins such that I could stack them easily. The ones in the middle got long header pins, while the bottom shield (in my case the CO2 shield) got short header pins soldered. On top I attached the D1 Mini.
This is how it looks when they are all stacked up.
Next, I flashed the D1 Mini with the required code that allowed me to read the values via Modbus RTU. I have been using ESPHome for that, which simply requires some YAML code to compile. The system does not allow the upload of YAML files, just download the file and remove the ".txt" extension.
The YAML code can be executed via esphome:
Please be aware that a simple USB adapter might not provide sufficient power for the SCD41 sensor to function properly. Hence, after flashing you should provide it with the 24V power supply instead of continuing with the USB power.
Since we are stacking multiple shields, we reduce the I2C frequency to 10kHz to work reliably.
Connection parameters: Slave 1, 9600,1,N,8
The Modbus registers were defined as follows:
0x0000 - CO2 value in ppm
0x0001 - temp value
0x0002 - humidity value
0x07D0 - slave address
0x07D1 - baud rate
While the first three are read only registers (read holding register), the slave address and baud rate can be written as well (read/write holding register). These changes are persistent.
Downloads
Since the electronics were working already (at least reading sensor values and powering the stacked shields), I started to drill a 13mm hole into the center of one of the sides of the enclosure. These enclosures are compatible with the Misitemita project. Quite handy if you need to 3d print some electronics holder for the inside.
I soldered the five wires required (24V+, 24V-, SGND, RS485+, RS485-) to the 5pin connector and inserted it into the previously drilled hole.
Afterwards, I simply connected the wires (one of which is a PH2.0 connector) to their respective ends.
Lastly, I used my 3D printer to print a custom lid with openings and a holder in the back to attach an carbon filter to avoid dust particles entering the case. Simply cut the filter material to the desired length.
That´s it. I can now connect the CO2 sensor to a Modbus RTU line carrying 24V and read the sensor values (CO2, temp, humidity) from any Modbus RTU master reliably with up to 1km cable length.