Hi everyone, recently I was able to code the soil pH sensor using aarudino mega 2560 by the help of this forum. I just want to know how to code if I am using two soil pH sensor.
This is the code for 1 soil pH sensor that is working:
#include <SoftwareSerial.h>
#define RE 43
#define DE 41
const uint32_t TIMEOUT = 500UL;
const byte PH[] = {0x02, 0x03, 0x00, 0x03, 0x00, 0x01, 0x74, 0x39};
byte values[11];
#define mod Serial1 // Use Serial1, Serial2, Serial3 for other hardware serial ports
void setup() {
Serial.begin(9600); // Use Serial for debugging
mod.begin(4800);
pinMode(RE, OUTPUT);
pinMode(DE, OUTPUT);
delay(500);
}
void loop() {
uint16_t val4;
Serial.println("Ph: ");
val4 = ph();
float Val4 = val4 * 0.1;
delay(250);
Serial.print(Val4);
Serial.println(" ph");
Serial.println("-----");
delay(250);
}
int16_t ph() {
uint32_t startTime = 0;
uint8_t byteCount = 0;
digitalWrite(DE, HIGH);
digitalWrite(RE, HIGH);
delay(10);
mod.write(PH, sizeof(PH));
mod.flush();
digitalWrite(DE, LOW);
digitalWrite(RE, LOW);
startTime = millis();
while (millis() - startTime <= TIMEOUT) {
if (mod.available() && byteCount < sizeof(values)) {
values[byteCount++] = mod.read();
printHexByte(values[byteCount - 1]);
}
}
Serial.println();
return (int16_t)(values[3] << 8 | values[4]);
}
void printHexByte(byte b) {
Serial.print((b >> 4) & 0xF, HEX);
Serial.print(b & 0xF, HEX);
Serial.print(' ');
}
Hi @solron31 ,
The ID needs to be different, should be able to change the ID default should be 1 but as we've seen maybe it's not.. 
Let me know the other device ID and I'll calculate the array for you..
in case you don't know, you can daisy chain them..
~q
Hi @qubits-us thanks for replying again
I bought the two soil pH sensor at the same time via online, and they only send me one datasheet for the two sensor
are they both set to ID 2??
meaning you can inter-change them and they both work with the current sketch??
~q
maybe something like..
#define RE 43
#define DE 41
#define SENSOR_COUNT 2
const uint32_t TIMEOUT = 500UL;
const byte PH[SENSOR_COUNT][8] = {{0x01, 0x03, 0x00, 0x03, 0x00, 0x01, 0x74, 0x0A},
{0x02, 0x03, 0x00, 0x03, 0x00, 0x01, 0x74, 0x39}};
byte values[11];
#define mod Serial1 // Use Serial1, Serial2, Serial3 for other hardware serial ports
void setup() {
Serial.begin(9600); // Use Serial for debugging
mod.begin(4800);
pinMode(RE, OUTPUT);
pinMode(DE, OUTPUT);
delay(500);
}
void loop() {
uint16_t val4;
for (int i = 0 ; i < SENSOR_COUNT; i++ ){
Serial.print("Ph"); Serial.print(i+1); Serial.print(":");
val4 = ph(i);
float Val4 = val4 * 0.1;
delay(250);
Serial.print(Val4);
Serial.println(" ph");
Serial.println("-----");
delay(250);
}
}
int16_t ph(int pNum) {
uint32_t startTime = 0;
uint8_t byteCount = 0;
digitalWrite(DE, HIGH);
digitalWrite(RE, HIGH);
delay(10);
mod.write(PH[pNum], sizeof(PH[pNum]));
mod.flush();
digitalWrite(DE, LOW);
digitalWrite(RE, LOW);
startTime = millis();
while (millis() - startTime <= TIMEOUT) {
if (mod.available() && byteCount < sizeof(values)) {
values[byteCount++] = mod.read();
printHexByte(values[byteCount - 1]);
}
}
Serial.println();
return (int16_t)(values[3] << 8 | values[4]);
}
void printHexByte(byte b) {
Serial.print((b >> 4) & 0xF, HEX);
Serial.print(b & 0xF, HEX);
Serial.print(' ');
}
assuming id's 1 and 2..
have fun.. ~q
if their both on ID 2..
sends this array to one of them, one time, should switch ID from 2 to 1..
const byte SetAddr1[] = {0x02, 0x06, 0x07, 0xD0, 0x00, 0x01, 0x48, 0xB4};
~q
Hi @qubits-us sorry for the super late reply,
I will try the code now and will make a feedback later.
Btw, I test the two sensor and found out that the ID of the other sensor is 01
Hi, is the connection of rs485 needs to be daisy chain?
thanks, the code now works