Local Controller (UNO)
Local Controller (UNO)
The Arduino Uno acts as the Modbus Master in this architecture. It is responsible for initiating requests to the NPK sensor via the RS485 bus, processing the raw hexadecimal response, and converting it into human-readable units (mg/kg).
Configuration & Pin Mapping
To ensure the controller communicates correctly with the MAX485 transceiver, verify the following pin assignments in the NPK_Sensor_v0_7uno.ino sketch:
| Pin | Function | Description |
| :--- | :--- | :--- |
| D2 | RE_DE_PIN | Controls the direction of RS485 data (High = Transmit, Low = Receive). |
| D10 | RX_PIN | Software Serial Receive (connects to MAX485 RO). |
| D11 | TX_PIN | Software Serial Transmit (connects to MAX485 DI). |
Data Polling Loop
The controller operates on a cyclic polling mechanism. By default, the sketch performs the following sequence in the loop() function:
- Request Phase: The Uno sends a predefined Hexadecimal command (Modbus RTU frame) to the sensor.
- Wait Phase: The controller toggles the
RE_DE_PINtoLOWand waits for the sensor to respond. - Parsing Phase: The incoming byte stream is validated against the expected Modbus frame length.
- Conversion: Raw values are extracted from the high/low byte registers and printed to the Serial Monitor.
User-Adjustable Parameters
Users can modify the following constants at the top of the sketch to suit their specific deployment:
// Polling interval in milliseconds (e.g., 3000ms = 3 seconds)
const unsigned long POLLING_DELAY = 3000;
// Communication Baud Rate (Must match the NPK sensor default)
const long RS485_BAUDRATE = 9600;
// Calibration Offsets (Adjust these if values are consistently biased)
const float OFFSET_N = 0.0;
const float OFFSET_P = 0.0;
const float OFFSET_K = 0.0;
Serial Interface Output
Once the sketch is uploaded, the controller provides real-time logging via the Hardware Serial port (USB). Open the Serial Monitor at 9600 Baud to view the structured data:
--- Reading Soil Nutrients ---
Nitrogen (N): 21 mg/kg
Phosphorus (P): 15 mg/kg
Potassium (K): 45 mg/kg
-----------------------------
Error Handling
The local controller includes basic error checking for the data bus. If the Serial Monitor reports 0 for all values or "Timeout," check the following:
- A/B Polarity: Swap the A and B wires on the RS485 bus.
- Power Supply: Ensure the NPK sensor is receiving its required voltage (usually 5V-30V DC, depending on the model), as the Uno's 5V rail may be insufficient for some industrial sensors.
- Direction Control: Ensure the
RE_DE_PINis physically jumpered to both the RE and DE pins on the MAX485 module.