I have been working on a project where NPK sensor(5v-30v), Arduino Uno, RS485 to ttl converter is used i have given the connection and use the code
#include <SoftwareSerial.h>
// 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 variable used to store NPK values
byte values[8];
SoftwareSerial mod(2, 3);
void setup() {
// Set the baud rate for the Serial port
Serial.begin(4800);
// Set the baud rate for the SerialSoftware object
mod.begin(4800);
delay(500);
}
void loop() {
// Read values
byte val1,val2,val3;
val1 = nitrogen();
delay(250);
val2 = phosphorous();
delay(250);
val3 = potassium();
delay(250);
// Print values to the serial monitor
Serial.print("Nitrogen: ");
Serial.print(val1);
Serial.println(" mg/kg");
Serial.print("Phosphorous: ");
Serial.print(val2);
Serial.println(" mg/kg");
Serial.print("Potassium: ");
Serial.print(val3);
Serial.println(" mg/kg");
delay(2000);
}
byte nitrogen(){
mod.write(nitro,8);
delay(100);
for(byte i=0;i<7;i++){
Serial.print(mod.read(),HEX);
values[i] = mod.read();
Serial.print(values[i],HEX);
}
Serial.println();
return values[4];
}
byte phosphorous(){
mod.write(phos,8);
for(byte i=0;i<7;i++){
values[i] = mod.read();
Serial.print(values[i],HEX);
}
Serial.println();
return values[4];
}
byte potassium(){
mod.write(pota,8);
for(byte i=0;i<7;i++){
values[i] = mod.read();
Serial.print(values[i],HEX);
}
Serial.println();
return values[4];
}
connection
RS485 to ttl converter TX to Arduino TX, RX to RX
A to NPK A, B to NPK b, 12v 2 A power to NPK,
RS485 to ttl converter 5V to arduino uno 5v and GND to GND
the result is coming as 255 continously.
please help
rs485 to ttl used
RS485 to TTL converter
Description :
With this board you can connect your microcontroller serially that’s far away from each other than this module interfaces RS-485 to TTL or vice-versa.It has Auto Direction control, making it easier to use as RS-232/TTL Serial Interface replacement. RS485 is used for Serial Communications over longer distances. The converter is designed for office and industrial applications (non-isolated), and provides superior characteristics/features normally found only on more expensive units.
Specification & Features :
• Compatibility: converts rs485 to ttl and vice- versa. (Transceiver Facility )
• Interface RS485 side: 2-pin (A+,B-) terminal connector.
• Interface TTL side:4-pin burg stick (5v,GND,tx,rx) .
• Transmission rate: 300 - 115200bps
• Transmission distance: RS485 max. 1200m,
• Working mode: asynchronous, half-duplex, differential transmission.
• Operating Voltage - 5volts DC