CO2-100 עם Arduino, ESP32, Raspberry Pi ו-Python

איך מחברים את CO2-100 חיישן פחמן דו-חמצני מומס - RS485 Modbus למיקרו-בקר או למחשב שלכם וקוראים ממנו ב-RS485 Modbus RTU. הטבלאות הן אלה שבדף הנתונים, ושתי דוגמאות הקוד מורצות מול מסגרת הדוגמה שבדף הנתונים לפני שהעמוד מתפרסם.

עמוד המוצר דף נתונים (PDF) דף נתונים (Markdown)

CO2-100 חיישן פחמן דו-חמצני מומס - RS485 Modbus

חיבור ה-CO2-100

החיישן הוא התקן RS485, ולכן צריך משדר-מקלט בינו לבין הלוח שלכם. במחשב או ב-Raspberry Pi זה מתאם USB ל-RS485. במיקרו-בקר זה מודול מסוג MAX485 על יציאה טורית חומרתית. הזינו את החיישן מספק שנמצא בטווח המתח שבטבלה, וחברו את האדמה שלו לאדמה של המשדר-מקלט.

Color Function Description
Red V+ Power Supply (9-27V DC)
Black GND Power Ground
Yellow RS485-A Data+ (T/R+)
White RS485-B Data- (T/R-)

דוגמת Python (Raspberry Pi, Linux, macOS, Windows)

הסקריפט צריך רק את pyserial. הוא בונה בעצמו את בקשת ה-Modbus, בודק את ה-CRC של התשובה ומדפיס קריאה אחת כל חמש שניות. ב-Windows העבירו את יציאת ה-COM כארגומנט.

#!/usr/bin/env python3
"""Read the Agrinovo CO2-100 over RS485 Modbus RTU.

Works on a Raspberry Pi or any computer with a USB to RS485 adapter.
Install the serial library once:  pip install pyserial
Run:  python3 co2_100.py /dev/ttyUSB0
"""
import struct
import sys
import time

import serial

PORT = sys.argv[1] if len(sys.argv) > 1 else "/dev/ttyUSB0"
ADDRESS = 0x01  # the probe's Modbus address


def crc16(frame: bytes) -> bytes:
    crc = 0xFFFF
    for byte in frame:
        crc ^= byte
        for _ in range(8):
            crc = (crc >> 1) ^ 0xA001 if crc & 1 else crc >> 1
    return struct.pack("<H", crc)


def read_registers(port, address, function, start, count):
    request = struct.pack(">BBHH", address, function, start, count)
    port.reset_input_buffer()
    port.write(request + crc16(request))
    reply = port.read(5 + 2 * count)
    if len(reply) >= 5 and reply[1] == function | 0x80:
        raise IOError(f"probe returned Modbus error {reply[2]}")
    if len(reply) != 5 + 2 * count or crc16(reply[:-2]) != reply[-2:]:
        raise IOError("no valid reply from the probe")
    return reply[3:-2]


def read_probe(port):
    data = read_registers(port, ADDRESS, 0x03, 0x0000, 10)
    return {
        "co2_ppm":       struct.unpack(">f", data[2:4] + data[0:2])[0],
        "electrode_mv":  struct.unpack(">f", data[6:8] + data[4:6])[0],
        "temperature_c": struct.unpack(">f", data[18:20] + data[16:18])[0],
    }


if __name__ == "__main__":
    with serial.Serial(PORT, 9600, bytesize=8, parity="N", stopbits=1, timeout=1) as port:
        while True:
            print(read_probe(port))
            time.sleep(5)

דוגמת Arduino (ESP32, Mega ולוחות אחרים עם יציאה טורית שנייה)

הסקיצה לא משתמשת באף ספרייה. ב-ESP32 היא משתמשת ב-Serial2 על הפינים שמוגדרים בראש הקובץ, ובלוחות AVR ב-Serial1. DE ו-RE של המשדר-מקלט מתחברים לפין יציאה אחד.

// Agrinovo CO2-100 over RS485 Modbus RTU.
// For a board with a spare hardware serial port (ESP32, Mega, Leonardo, Due)
// and a MAX485-type transceiver. Power the probe from its own supply, as the
// wiring table says, and join the grounds.

const uint8_t ADDRESS = 0x01;  // the probe's Modbus address
const int DE_PIN = 4;          // transceiver DE and RE tied together
const int RX_PIN = 16;         // ESP32 only
const int TX_PIN = 17;         // ESP32 only

#if defined(ESP32)
#define RS485 Serial2
#else
#define RS485 Serial1
#endif

uint16_t crc16(const uint8_t *frame, size_t length) {
  uint16_t crc = 0xFFFF;
  for (size_t i = 0; i < length; i++) {
    crc ^= frame[i];
    for (int bit = 0; bit < 8; bit++) {
      crc = (crc & 1) ? (crc >> 1) ^ 0xA001 : crc >> 1;
    }
  }
  return crc;
}

// Builds a float from its four bytes, most significant first.
float float32(uint8_t b3, uint8_t b2, uint8_t b1, uint8_t b0) {
  uint32_t bits = ((uint32_t)b3 << 24) | ((uint32_t)b2 << 16) | ((uint32_t)b1 << 8) | b0;
  float value;
  memcpy(&value, &bits, sizeof value);
  return value;
}

// Reads `count` registers into `data`. Returns false when the probe does not
// answer or the checksum is wrong.
bool readRegisters(uint8_t function, uint16_t start, uint16_t count, uint8_t *data) {
  uint8_t request[8] = {ADDRESS, function, (uint8_t)(start >> 8), (uint8_t)start,
                        (uint8_t)(count >> 8), (uint8_t)count, 0, 0};
  uint16_t crc = crc16(request, 6);
  request[6] = crc & 0xFF;
  request[7] = crc >> 8;

  while (RS485.available()) RS485.read();
  digitalWrite(DE_PIN, HIGH);
  RS485.write(request, 8);
  RS485.flush();
  digitalWrite(DE_PIN, LOW);

  uint8_t reply[5 + 2 * 16];
  size_t expected = 5 + 2 * count;
  if (RS485.readBytes(reply, expected) != expected) return false;
  uint16_t received = reply[expected - 2] | ((uint16_t)reply[expected - 1] << 8);
  if (reply[1] != function || crc16(reply, expected - 2) != received) return false;
  memcpy(data, reply + 3, 2 * count);
  return true;
}

void setup() {
  Serial.begin(115200);
  pinMode(DE_PIN, OUTPUT);
  digitalWrite(DE_PIN, LOW);
#if defined(ESP32)
  RS485.begin(9600, SERIAL_8N1, RX_PIN, TX_PIN);
#else
  RS485.begin(9600);
#endif
  RS485.setTimeout(1000);
}

void loop() {
  uint8_t data[2 * 10];
  if (!readRegisters(0x03, 0x0000, 10, data)) {
    Serial.println("no reply");
    delay(5000);
    return;
  }
  Serial.print("co2_ppm=");
  Serial.print(float32(data[2], data[3], data[0], data[1]), 2);
  Serial.print(" electrode_mv=");
  Serial.print(float32(data[6], data[7], data[4], data[5]), 1);
  Serial.print(" temperature_c=");
  Serial.print(float32(data[18], data[19], data[16], data[17]), 1);
  Serial.println();
  delay(5000);
}

מה הדוגמאות קוראות

שתי הדוגמאות שולחות בקשה אחת, פונקציה 0x03, 10 רגיסטרים החל מ-0x0000, ומפענחות את הערכים האלה:

ערך יחידה רגיסטר קידוד
co2_ppm ppm 0x0000 float של 32 סיביות בשני רגיסטרים, המילה הנמוכה ראשונה
electrode_mv mV 0x0002 float של 32 סיביות בשני רגיסטרים, המילה הנמוכה ראשונה
temperature_c °C 0x0008 float של 32 סיביות בשני רגיסטרים, המילה הנמוכה ראשונה

כלי Modbus אחרים

אותה בקשה עובדת מכל מאסטר Modbus RTU: pymodbus, minimalmodbus, Node-RED, LabVIEW, בקר מתוכנת (PLC) או דאטה לוגר של צד שלישי עם יציאת Modbus. השתמשו בהגדרות התקשורת הטורית, בקוד הפונקציה ובכתובות הרגיסטרים שלמעלה.

איסוף נתונים בלי לכתוב קוד

בקר Omni Genesis קורא את ה-CO2-100 ושולח את הקריאות לענן Agrinovo ברשת הסלולרית, ומשם אפשר לייצא אותן או לקרוא אותן דרך ה-API של הענן.

מתוך דף הנתונים

Specifications

Parameter Specification
CO₂ Range 0.2-2200 ppm (configurable to 20000 ppm)
Resolution 0.01 ppm
Accuracy ±5% FS
Temperature Range -10 to 110°C
Temperature Accuracy ±0.5°C
Temperature Compensation Automatic (NTC10K) or manual
Working pH Range 4.8-5.2
Supply Voltage 9-27 VDC
Power Consumption ≤0.5W
Isolation 2500 Vrms
Housing PPS, NPT 3/4” front and back thread
Dimensions Ø30 mm, 220 mm length

Communication Settings

Parameter Value
Protocol Modbus-RTU
Baud Rate 9600 bps (default)
Data Bits 8
Parity None
Stop Bits 1
Default Address 0x01

Data Format

  • Type: 32-bit Single Precision Float (IEEE-754)
  • Byte Order: Word-swapped, big-endian within each word

Each measurement spans two registers. Assemble the float from the second register (high word) followed by the first register (low word). Supported baud rates: 1200, 2400, 4800, 9600, 19200.

Register Map

Measurement Registers (Function 0x03, float32)

Address (Hex) Description Range Unit
0x0000-0x0001 CO₂ Concentration 0-20000 ppm
0x0002-0x0003 Electrode Signal -750 to 750 mV
0x0008-0x0009 Temperature -10 to 110 °C

Parameter Registers (Function 0x03 read / 0x06 write)

Address (Hex) Description Range / Values Default
0x0019 Calibration Status Bitfield (read) -
0x001C Electrode Efficiency 70.0-130.0% (÷10) 100.0%
0x001D Calibrated Points 0-5 0
0x001E Device Address 1-247 (255 = broadcast) 1
0x001F Baud Rate 0:1200 1:2400 2:4800 3:9600 4:19200 3 (9600)
0x0020 Temp Comp Type 0: Manual, 1: Automatic 1
0x0021 Temp Setpoint / Offset -10.0 to 110.0°C (×10) 25.0°C / 0.0°C
0x0023 Ion Valence 0: Unset, 1: Monovalent, 2: Divalent -

Information Registers (Function 0x03 read)

Address (Hex) Description Notes
0x0040 Work Mode 0x0010 Measure, 0x0050 Setup, 0x0060 Calibration
0x0043 Calibration Status / Operation Read status, write standard code
0x0045 Model Number 0x1210
0x0046 Software Version BCD
0x0047 Hardware Version BCD
0x0048-0x0049 Serial Number BCD

Reading Data

Read CO₂ + Electrode Signal + Temperature

Read 10 registers from 0x0000:

Request:  01 03 00 00 00 0A C5 CD
Response: 01 03 14 00 00 41 20 33 33 42 C8 00 00 00 00
          00 00 00 00 E3 E8 41 C7 43 0C

Decoding (word-swapped float32):

Value Registers Reordered Result
CO₂ 00 00 / 41 20 41 20 00 00 10.00 ppm
Electrode Signal 33 33 / 42 C8 42 C8 33 33 100.1 mV
Temperature E3 E8 / 41 C7 41 C7 E3 E8 25.0°C

Address Configuration

Change Address (0x01 → 0x02)

Write to register 0x001E:

01 06 00 1E 00 02 68 0D

Note: Communication will be lost until reconnecting with the new address.

Broadcast Discovery

Use address 255 (0xFF) with only one device connected.

Calibration

The sensor supports up to 5 calibration points. Calibration is performed over RS485 by placing the electrode in a standard solution and writing the matching standard code to the calibration register (0x0043).

Standard Solution Codes

Standard Code
0.1 ppm 0x0002
1 ppm 0x0004
10 ppm 0x0008
100 ppm 0x0010
1000 ppm 0x0020

Calibration Procedure

  1. Place the electrode in the standard solution.
  2. Write the standard code to register 0x0043. Example (1 ppm point):
01 06 00 43 00 04 79 DD
  1. Poll register 0x0043 to read calibration status:
Value Meaning
0 Success (returned to measurement mode)
1 In progress (wait and re-read)
2 Incorrect standard solution value
3 Signal unstable or out of range after 180s
4 Sensor performance out of allowed range
  1. Repeat for additional points.

Clear Calibration

Write 0x7FFF to register 0x0043 to clear all calibration data.

Factory Reset

Enter setup mode, then restore defaults:

01 06 00 40 00 50 88 22   (enter setup mode)
01 06 00 41 7F FF B9 AE   (restore defaults and restart)

Revision History

Rev Date Changes
1.0 June 2026 Initial release
1.1 August 2026 Layout revision
1.2 October 2026 New layout; corrected Modbus example CRC values

Contact

Agrinovo, https://agrinovo.io/products, [email protected], +972 54 688 8148 (direct / WhatsApp). Specifications subject to change without notice.

שאלות על החיווט או על הפרוטוקול?

ספרו לנו באיזה לוח או דאטה לוגר אתם משתמשים ומה צריך למדוד.