Please, anyone can share the code for this sensor to work in esp32 ?
Have a search of the forums for NPK sensor. There are quite a few discussions - usually for one of the AVR microcontrollers (Nano, MEGA2560 etc). There are a few that involve an ESP32 where the person has posted code that they were using.
You need to specify which ESP32 board you have, as well as which RS485 module you are using. Also, there are several NPK sensor configurations out there. You will need to share the datasheet for your particular sensor with us in order to receive further guidance.
i am using esp32 dev module and module rs485 to ttl max485
'''
#define analogInPin 34
#define RE 4
#define DE 2
//LCD
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16,2);
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};
byte values[11];
//pH
int bacaSensorPH = 0;
float nilaipH = 0.0;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
Serial2.begin(4800);
pinMode(RE, OUTPUT);
pinMode(DE, OUTPUT);
pinMode(34, INPUT);
lcd.init();
lcd.backlight();
}
void loop() {
// put your main code here, to run repeatedly:
byte val1,val2,val3;
val1 = nitrogen();
delay(250);
val2 = phosphorous();
delay(250);
val3 = potassium();
delay(250);
int bacaSensorPH = analogRead(34);
nilaipH = (-0.0193*bacaSensorPH)+7.7851;
lcd.setCursor(0,0);
lcd.print("pH: ");
lcd.print(nilaipH);
lcd.setCursor(8,0);
lcd.print("N : ");
lcd.print(val1);
lcd.setCursor(0,1);
lcd.print("P : ");
lcd.print(val2);
lcd.setCursor(8,1);
lcd.print("K : ");
lcd.print(val3);
Serial.print("pH: ");
Serial.print(bacaSensorPH);
Serial.print("pHterbaca: ");
Serial.print(nilaipH);
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(1000);
}
byte nitrogen(){
digitalWrite(DE,HIGH);
digitalWrite(RE,HIGH);
delay(10);
if(Serial2.write(nitro,sizeof(nitro))==8){
Serial2.flush();
digitalWrite(DE,LOW);
digitalWrite(RE,LOW);
for(byte i=0;i<7;i++){
//Serial.print(mod.read(),HEX);
values[i] = Serial2.read();
Serial.print(values[i],HEX);
}
Serial.println();
}
return values[4];
}
byte phosphorous(){
digitalWrite(DE,HIGH);
digitalWrite(RE,HIGH);
delay(10);
if(Serial2.write(phos,sizeof(phos))==8){
Serial2.flush();
digitalWrite(DE,LOW);
digitalWrite(RE,LOW);
for(byte i=0;i<7;i++){
//Serial.print(mod.read(),HEX);
values[i] = Serial2.read();
Serial.print(values[i],HEX);
}
Serial.println();
}
return values[4];
}
byte potassium(){
digitalWrite(DE,HIGH);
digitalWrite(RE,HIGH);
delay(10);
if(Serial2.write(pota,sizeof(pota))==8){
Serial2.flush();
digitalWrite(DE,LOW);
digitalWrite(RE,LOW);
for(byte i=0;i<7;i++){
//Serial.print(mod.read(),HEX);
values[i] = Serial2.read();
Serial.print(values[i],HEX);
}
Serial.println();
}
return values[4];
}
'''
when I upload my code an error like this :
Arduino: 1.8.19 (Windows Store 1.8.57.0) (Windows 10), Board: "ESP32 Dev Module, Disabled, Disabled, Default 4MB with spiffs (1.2MB APP/1.5MB SPIFFS), 240MHz (WiFi/BT), QIO, 80MHz, 4MB (32Mb), 921600, Core 1, Core 1, None, Disabled"
WARNING: library LiquidCrystal_I2C-1.1.2 claims to run on avr architecture(s) and may be incompatible with your current board which runs on esp32 architecture(s).
Sketch uses 281185 bytes (21%) of program storage space. Maximum is 1310720 bytes.
Global variables use 22912 bytes (6%) of dynamic memory, leaving 304768 bytes for local variables. Maximum is 327680 bytes.
esptool.py v4.5.1
Serial port COM4
Connecting....
Chip is ESP32-D0WD-V3 (revision v3.0)
Features: WiFi, BT, Dual Core, 240MHz, VRef calibration in efuse, Coding Scheme None
Crystal is 40MHz
MAC: 7c:87:ce:2f:82:a0
Uploading stub...
Running stub...
Stub running...
Changing baud rate to 921600
Changed.
Configuring flash size...
Flash will be erased from 0x00001000 to 0x00005fff...
Flash will be erased from 0x00008000 to 0x00008fff...
Flash will be erased from 0x0000e000 to 0x0000ffff...
Flash will be erased from 0x00010000 to 0x00054fff...
Compressed 18960 bytes to 13073...
Writing at 0x00001000... (100 %)
Wrote 18960 bytes (13073 compressed) at 0x00001000 in 0.3 seconds (effective 476.9 kbit/s)...
File md5: 81da22449acfb2451d39eaa91ecd2570
Flash md5: ca487cb988daf83d6c88b9e7cfc453d3
MD5 of 0xFF is 8bd688d357221ba9ccc4b19b1c32b8b8
A fatal error occurred: MD5 of file does not match data in flash!
A fatal error occurred: MD5 of file does not match data in flash!
Hi there
Go through it
The standard MAX485 module is designed to work with 5V logic levels. I believe that your ESP32 uses 3.3V logic levels. You should feed the RO output through a potential divider to reduce the 5V down to 3.3V.
You should start with a simple sketch that requests 1 parameter and prints it to the serial port to begin with.
i've used esp32 with 5v voltage
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.