nanoFramework RS485 Temperature, Humidity & CO2 Sensor

As part of this series of samples comparing Arduino to nanoFramework to .NET IoT Device “Proof of Concept (PoC) applications, the next couple of posts use a SenseCAP CO2, Temperature and Humidity Sensor SKU101991029.

I cut one of the cables of a spare Industrial IP68 Modbus RS485 1-to-4 Splitter/Hub to connect the sensor to the breakout board.

This sensor has an operating voltage of 5V ~ 24V so it can be powered by the 5V output of a RS485 Breakout Board for Seeed Studio XIAO (SKU 113991354)

The red wire is for powering the sensor with a 12V power supply so was tied back so it didn’t touch any of the other electronics.

public static void Main()
{
   Debug.WriteLine("Modbus Client for Seeedstudio Temperature Humidity and CO2 sensor SKU101991029");

   Configuration.SetPinFunction(Gpio.IO06, DeviceFunction.COM2_RX);
   Configuration.SetPinFunction(Gpio.IO05, DeviceFunction.COM2_TX);
   Configuration.SetPinFunction(Gpio.IO03, DeviceFunction.COM2_RTS);

   DateTime warmupCompleted = DateTime.UtcNow;

   // Modbus Client
   using (var client = new ModbusClient("COM2"))
   {
      try
      {
         Debug.WriteLine("Reading CO2 Sensor Warmup duration");

         // Read warm-up time (seconds) from 0x0021
         var warmupReg = client.ReadHoldingRegisters(SlaveAddress, RegWarmup, 1);
         ushort warmupSeconds = unchecked((ushort)warmupReg[0]);

         Debug.WriteLine($"Sensor warm-up:{warmupSeconds}sec");

         warmupCompleted += TimeSpan.FromSeconds(warmupSeconds);
      }
      catch (Exception ex)
      {
         Debug.WriteLine($"Warm-up read failed (continuing): {ex.Message}");
      }

      while (true)
      {
         try
         {
            var regs = client.ReadHoldingRegisters(SlaveAddress, RegCO2, 3);
            short rawTemp = regs[RegTemperature];
            double tempC = rawTemp / 100.0; // Signed 16 - bit, value = °C * 100

            // regs[2] = Humidity. Unsigned 16-bit, value = %RH * 100
            ushort rawRh = unchecked((ushort)regs[RegHumidity]);
            double rhPercent = rawRh / 100.0; // Humidity. Unsigned 16-bit, value = %RH * 100

            if (DateTime.UtcNow > warmupCompleted)
            {
               // regs[0] = CO2 (ppm)
               ushort rawCO2 = unchecked((ushort)regs[RegCO2]);

               int co2Ppm = rawCO2; // already ppm
               Debug.WriteLine($"T:{tempC:F2}°C, RH:{rhPercent:F2}%, CO2:{co2Ppm} ppm");
            }
            else
            {
               Debug.WriteLine($"T:{tempC:F2}°C, RH:{rhPercent:F2}%");
            }
         }
         catch (Exception ex)
         {
            Debug.WriteLine($"Read failed: {ex.Message}");
         }

         Thread.Sleep(60000);
      }
   }
}

The nanoFramework Modbus Library based application worked first time but implementing the CO2 Sensor warm-up time took a couple of attempts.

I did consider trying to fit the Seeed Studio XIAO ESP32-S3 inside the SenseCAP CO2, Temperature and Humidity Sensor but the electronics had been sprayed with a corrosion resistant coating. Connecting (rather than via a breakout board) the VCC+, VCC-, universal asynchronous receiver-transmitter(UART) and transmit enable would have been difficult.

I tried Copilot to clean up the image but it didn’t go well

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.