Hi!
Im trying to combine capacitive moisture sensors and soil NPK sensors in 1 using an Arduino MEGA2560. Right now, im trying to get both NPK sensors to be working.
as you can see, only one of the NPK sensors is working while the other isn't. However, if I test them individually, they both work. Can someone help me with this?
#include <SoftwareSerial.h>
#include <Wire.h>
// Define the pins for the first NPK sensor
#define RE 8
#define DE 7
// Define the pins for the second NPK sensor
#define RE2 6
#define DE2 5
#define sensorPin A1
#define sensorPin2 A2
// 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};
// Arrays to store NPK values
byte values1[7];
byte values2[7];
// Create two instances of SoftwareSerial
SoftwareSerial mod1(10, 11); // RX, TX for first sensor
SoftwareSerial mod2(12, 13); // RX, TX for second sensor
void setup() {
// Set the baud rate for the Serial port
Serial.begin(9600);
// Set the baud rate for the first SoftwareSerial object
mod1.begin(9600);
// Set the baud rate for the second SoftwareSerial object
mod2.begin(9600);
// Define pin modes for RE and DE
pinMode(RE, OUTPUT);
pinMode(DE, OUTPUT);
pinMode(RE2, OUTPUT);
pinMode(DE2, OUTPUT);
delay(500);
}
void loop() {
// Read values from the first NPK sensor
byte val1 = nitrogen(DE, RE, mod1, values1);
delay(250);
byte val2 = phosphorous(DE, RE, mod1, values1);
delay(250);
byte val3 = potassium(DE2, RE2, mod2, values2);
delay(250);
// Read values from the second NPK sensor
byte val4 = nitrogen(DE2, RE2, mod2, values2);
delay(250);
byte val5 = phosphorous(DE2, RE2, mod2, values2);
delay(250);
byte val6 = potassium(DE2, RE2, mod2, values2);
delay(250);
int value = analogRead(sensorPin);
int value2 = analogRead(sensorPin2);
// Print values to the serial monitor
Serial.print("NPK 1 - Nitrogen: ");
Serial.print(val1);
Serial.println(" mg/kg");
Serial.print("NPK 1 - Phosphorous: ");
Serial.print(val2);
Serial.println(" mg/kg");
Serial.print("NPK 1 - Potassium: ");
Serial.print(val3);
Serial.println(" mg/kg");
Serial.print("NPK 2 - Nitrogen: ");
Serial.print(val4);
Serial.println(" mg/kg");
Serial.print("NPK 2 - Phosphorous: ");
Serial.print(val5);
Serial.println(" mg/kg");
Serial.print("NPK 2 - Potassium: ");
Serial.print(val6);
Serial.println(" mg/kg");
Serial.print("Analog output 1: ");
Serial.println(value);
Serial.print("Analog output 2: ");
Serial.println(value2);
delay(2000);
}
byte nitrogen(int DE_pin, int RE_pin, SoftwareSerial& mod, byte* values) {
digitalWrite(DE_pin, HIGH);
digitalWrite(RE_pin, HIGH);
delay(10);
if (mod.write(nitro, sizeof(nitro)) == 8) {
digitalWrite(DE_pin, LOW);
digitalWrite(RE_pin, LOW);
for (byte i = 0; i < 7; i++) {
values[i] = mod.read();
Serial.print(values[i], HEX);
}
Serial.println();
}
return values[4];
}
byte phosphorous(int DE_pin, int RE_pin, SoftwareSerial& mod, byte* values) {
digitalWrite(DE_pin, HIGH);
digitalWrite(RE_pin, HIGH);
delay(10);
if (mod.write(phos, sizeof(phos)) == 8) {
digitalWrite(DE_pin, LOW);
digitalWrite(RE_pin, LOW);
for (byte i = 0; i < 7; i++) {
values[i] = mod.read();
Serial.print(values[i], HEX);
}
Serial.println();
}
return values[4];
}
byte potassium(int DE_pin, int RE_pin, SoftwareSerial& mod, byte* values) {
digitalWrite(DE_pin, HIGH);
digitalWrite(RE_pin, HIGH);
delay(10);
if (mod.write(pota, sizeof(pota)) == 8) {
digitalWrite(DE_pin, LOW);
digitalWrite(RE_pin, LOW);
for (byte i = 0; i < 7; i++) {
values[i] = mod.read();
Serial.print(values[i], HEX);
}
Serial.println();
}
return values[4];
}
im not the best at arduino... its been 2 weeks since I've started so please bear with me
(both npk sensors have their own 12v power supply)