Hello,
Since the Arduino Nano RP2040 doesn't support SoftwareSerial, I need help writing a serial communication program using its hardware UART ports. Here's an example of how you can modify the code from the link you provided to use the hardware UART:
I am working on modifying this code: Measure Soil Nutrient using Arduino & Soil NPK Sensor
to read NPK an sensor from the Nano rp2040
Here is the recommended Pseudocode from Arduino:
#include <Arduino.h>
#include <Wire.h>
#include <SimpleModbusMaster.h>
// Define the NPK sensor's Modbus address
#define SENSOR_ADDRESS 1
// Define the hardware UART port to use
#define SERIAL_PORT Serial1
// ModbusMaster object
ModbusMaster mod;
// Variables to store sensor readings
uint16_t npkValue = 0;
void setup() {
// Initialize the hardware UART port
SERIAL_PORT.begin(9600);
// Initialize the Modbus communication
mod.begin(SENSOR_ADDRESS, SERIAL_PORT);
// Set the Modbus timeout and baud rate
mod.setTimeOut(1000);
mod.setBaudrate(9600);
}
void loop() {
// Read the NPK sensor value
int8_t result = mod.readHoldingRegisters(0x0001, 1); // Read one register starting from address 0x0001
// Check if the reading was successful
if (result == mod.ku8MBSuccess) {
npkValue = mod.getResponseBuffer(0); // Get the value from the response buffer
// Process the NPK value as needed
Serial.print("NPK Value: ");
Serial.println(npkValue);
} else {
// Failed to read the NPK value
Serial.println("Failed to read NPK value");
}
// Delay before the next reading
delay(1000);
}
And here is the code I am trying to adjust without using the <SoftwareSerial.h> library:
#include <SoftwareSerial.h>
#include <Wire.h>
// Define RS485 pins for RE and DE to switch between transmit and receive mode
#define RS485_RE 8
#define RS485_DE 7
// Modbus RTU requests for reading NPK values
const byte nitro[] = {0x01, 0x03, 0x00, 0x1e, 0x00, 0x01, 0xe4, 0x0c};
const byte phos[] = {0x01, 0x03, 0x00, 0x1f, 0x00, 0x01, 0xb5, 0xcc};
const byte pota[] = {0x01, 0x03, 0x00, 0x20, 0x00, 0x01, 0x85, 0xc0};
// A byte array to store NPK values
byte values[11];
// SoftwareSerial object to communicate with the RS485 module
SoftwareSerial modbus(2, 3); // RX, TX
void setup() {
// Start serial communication with the computer
Serial.begin(9600);
// Start serial communication with the RS485 module
modbus.begin(9600);
// Set RS485 pins as outputs
pinMode(RS485_RE, OUTPUT);
pinMode(RS485_DE, OUTPUT);
// Turn off RS485 receiver and transmitter initially
digitalWrite(RS485_RE, LOW);
digitalWrite(RS485_DE, LOW);
// Wait for the RS485 module to initialize
delay(500);
}
void loop() {
// Read NPK values and print them to the serial monitor
Serial.print("Nitrogen: ");
Serial.print(readValue(nitro));
Serial.println(" mg/kg");
Serial.print("Phosphorous: ");
Serial.print(readValue(phos));
Serial.println(" mg/kg");
Serial.print("Potassium: ");
Serial.print(readValue(pota));
Serial.println(" mg/kg");
// Wait for 2 seconds before reading values again
delay(2000);
}
// Sends a Modbus RTU request and reads the response to get a value
byte readValue(const byte* request) {
// Set RS485 module to transmit mode
digitalWrite(RS485_RE, HIGH);
digitalWrite(RS485_DE, HIGH);
// Send Modbus RTU request to the device
modbus.write(request, sizeof(request));
// Set RS485 module to receive mode
digitalWrite(RS485_RE, LOW);
digitalWrite(RS485_DE, LOW);
// Wait for the response to be received
delay(10);
// Read the response into the values array
byte responseLength = modbus.available();
for (byte i = 0; i < responseLength; i++) {
values[i] = modbus.read();
}
// Return the value from the response
return values[3] << 8 | values[4];
}
Any help is much appreciated!
\Thanks!