Hello everyone I'm having a big of a snag in my project and I'm really confused at what could be going on and how to rectify the issue.
I'm trying to use both an analog pH sensor that works off of 5v and a DS18B20 temperature sensor with the OneWire library on an ESP32. The pH probe works great and I've been logging away for days, but today was the day that my temperature sensor arrived in the mail and when I hooked it up my pH readings just steadily gave me a "6.45" with my calibration I assume that it is a "7".
I've tried tons of different creative ways to fix this, I even tried to power the DS18B20 using a separate power supply but everything seems to mess up the analog signal with my pH sensor.
Here is my code:
#include <WiFi.h>
#include <WiFiMulti.h>
#include <OneWire.h>
#include <DallasTemperature.h>
// Dump //
int dumpCount = 0;
int dumpLimit = 500;
// WiFi Setup //
WiFiMulti WiFiMulti;
WiFiClient client;
const uint16_t port = 5000; // SurfBox Server port
const char * host = "192.168.1.188"; // SurfBox Server host
String line;
// pH Sensor //
float calibration = 1.0;
const int analogInPin = A0;
int sensorValue = 0;
unsigned long int avgValue;
float b;
int buf[10],temp;
// Temperature Sensor //
#define ONE_WIRE_BUS 15
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
DeviceAddress tempSensor;
void setup() {
Serial.begin(115200);
// Temperature Sensor //
if (!sensors.getAddress(tempSensor, 0));
sensors.setResolution(tempSensor, 12);
sensors.begin();
// Connect to WiFi //
WiFiMulti.addAP("Fios-LBPBU", "end9823wet8602meat");
while(WiFiMulti.run() != WL_CONNECTED) {
Serial.print(".");
delay(500);
}
// Connect to SurfBox Server //
if (!client.connect(host, port)) {
Serial.println("connection failed");
Serial.println("wait 5 sec...");
delay(5000);
return;
}
}
void loop() {
// pH Probe Logic //
for(int i=0;i<10;i++) {
buf[i]=analogRead(analogInPin);
delay(30);
}
for(int i=0;i<9;i++) {
for(int j=i+1;j<10;j++) {
if(buf[i]>buf[j]) {
temp=buf[i];
buf[i]=buf[j];
buf[j]=temp;
}
}
}
avgValue=0;
for(int i=2;i<8;i++)
avgValue+=buf[i];
float pHVol=(float)avgValue*1.5/4095/6;
float phValue = -5.70 * pHVol + calibration;
Serial.print("pH = ");
Serial.println(14.0 - (phValue * -1));
delay(500);
// Temperature Sensor Logic //
sensors.requestTemperatures();
delay(100);
Serial.println("Temperature = "+ String(sensors.getTempFByIndex(0)));
delay(500);
// Send data to SurfBox Server //
client.println("p 0 newr " + String(sensors.getTempFByIndex(0)));
line = client.readStringUntil('\r');
Serial.println(line);
delay(100);
client.println("p 1 newr " + String(14.0 - (phValue * -1)));
line = client.readStringUntil('\r');
Serial.println(line);
if (dumpCount < dumpLimit) {
client.println("p 1 dump");
line = client.readStringUntil('\r');
Serial.println(line);
dumpCount++;
} else if (dumpCount == dumpLimit) {
client.println("p 1 clearhist");
line = client.readStringUntil('\r');
Serial.println(line);
dumpCount = 0;
}
}
If someone could please explain even in the slightest bit what could be going on