First of all, before using the Arduino Mega 2560, I used the Arduino UNO to try the NPK sensor. The Arduino UNO has a wiring schematic like the picture below:
When using the Arduino UNO there are no problems at all. The program code can run properly and produce NPK measurement results. However, when I changed the board to Arduino Mega 2560, a problem arose, namely the value that appeared became 255. On the Serial monitor, initially it appeared FFFFFFFFFF and continued to issue 255. Meanwhile, on the Arduino UNO, numbers like 12351231 initially appeared, then continued with the NPK measurement value.
I've tried changing pins 2 and 3 to pins 18,19 then 16,17 then 14,15. However, it still produces output 255. Is there a solution to my problem. Thank you
HI, i need help. First of all, before using the Arduino Mega 2560, I used the Arduino UNO to try the NPK sensor. The Arduino UNO has a wiring schematic like the picture below:
When using the Arduino UNO there are no problems at all. The program code can run properly and produce NPK measurement results. However, when I changed the board to Arduino Mega 2560, a problem arose, namely the value that appeared became 255. On the Serial monitor, initially it appeared FFFFFFFFFF and continued to issue 255. Meanwhile, on the Arduino UNO, numbers like 12351231 initially appeared, then continued with the NPK measurement value.
I've tried changing pins 2 and 3 to pins 18,19 then 16,17 then 14,15. However, it still produces output 255. Is there a solution to my problem. Thank you
The most likely explanation is that your code isn't receiving any data back from the sensor. Part of the problem is that this bit of code:
That bit of code (or code very similar) can be found all over the web and it is simply wrong! It doesn't check to see if there is a response from the sensor. It just assumes that there is a response instantly available and blindly reads in values. The software serial port is trying to tell you that there is no data by returning -1 - which is 0xFF - for every mod.read call, which is why you see lots of FFFFFFFF being printed.
You don't need to use any software serial code on a MEGA2560 - it has 4 hardware serial ports. Use Serial1 for your modbus comms and the pins TX1 and RX1.
My other piece of advice would be to move away from the horrible canned modbus messages and use a proper modbus library which will help in the long run. This library should work for you:
Oh and you may find that the code you think is working on your UNO isn't quite doing what you think it is. You may find that the parameter values you print out are mixed up - i.e. the value you think is Nitrogen is actually Phosphorous, and the value for Phosphorus is actually Potassium etc.
Please create one topic only and carefully select the forum category. Please do not add your question to the end of someone else's topic. If you continue to do either of these things you risk a time out from the forum. I have merged your 2 topics here.