Quick Start
Quick Start
Follow these steps to get your NPK sensor reading data on the Serial Monitor in under 5 minutes.
1. Hardware Connection
Connect the RS485 module (MAX485) to your Arduino and the NPK sensor as follows:
| Arduino Pin | MAX485 Pin | Description | | :--- | :--- | :--- | | 5V | VCC | Power Supply | | GND | GND | Ground | | D2 | DE & RE | Direction Control (Jumpered together) | | RX (Pin 10*) | RO | Receiver Output | | TX (Pin 11*) | DI | Driver Input |
RS485 Bus Side:
- A+ (MAX485) $\leftrightarrow$ A (NPK Sensor)
- B- (MAX485) $\leftrightarrow$ B (NPK Sensor)
- VCC/GND (NPK Sensor) $\leftrightarrow$ External Power Supply (typically 9V-24V)
*Note: Pin assignments may vary based on your SoftwareSerial configuration in the sketch.
2. Software Preparation
- Install Arduino IDE: Ensure you have the latest version of the Arduino IDE installed.
- Required Libraries: Open the Library Manager (Tools > Manage Libraries) and install:
ModbusMaster(by Doc Walker)SoftwareSerial(Built-in)
- Open the Sketch: Download this repository and open the
.inofile.
3. Configuration
Before uploading, verify the following constants in the code to match your hardware:
// --- Configuration Section ---
const int RE_DE_PIN = 2; // Digital pin for RS485 direction control
const int SENSOR_ADDRESS = 1; // Default Modbus ID of the NPK sensor
const long BAUD_RATE = 9600; // Standard baud rate for NPK sensors
// If using SoftwareSerial
SoftwareSerial swSerial(10, 11); // RX, TX
4. Upload and Monitor
- Connect your Arduino to your computer via USB.
- Select your board and port under the Tools menu.
- Click Upload (Right Arrow icon).
- Open the Serial Monitor (Ctrl+Shift+M).
- Set the baud rate to 9600 (or the rate defined in
Serial.begin).
5. Verify Output
If the connection is successful, you will see the soil nutrient values printed every few seconds:
--- Reading Sensor Data ---
Nitrogen (N): 14 mg/kg
Phosphorus (P): 8 mg/kg
Potassium (K): 121 mg/kg
Status: Success
---------------------------
Troubleshooting
- Zero Values: Check if the sensor has sufficient external power (most require >9V).
- Timeout Errors: Swap the A and B wires on the RS485 bus.
- Garbage Text: Ensure the Serial Monitor baud rate matches
Serial.begin().