First of all, thank you for your time reading this.
It is realy appreciated!
I measure the PH for my aquarium with follwing board and arduino nano:
The sketch provided by Reza works fine on its own but as soon as one of the libraries mentioned in the titel is also used the PH output goes to "inf"
Main code provided by Reza:
#include <Wire.h>
#define PHADDRESS 0x4D
int RoomTempI2CAddress = B1001011;
float volt4 = 0.95;
float volt7 = 0.67;
float calibrationTempC = 20;
void setup()
{
}
void loop()
{
Wire.begin(); //connects I2C
Serial.begin(9600);
Serial.print("avgMeasuredPH-");
SetRoomTemperataureResolutionBits(12);//12 bits room temp resolution in celcius
int sampleSize = 500;
double avgMeasuredPH = 0;
double avgRoomTempC = 0;
double avgPHVolts = 0;
double avgRoomTemperatureCompensatedMeasuredPH = 0;
double tempAdjusted4;
int x;
for(x=0;x< sampleSize;x++)
{
double phVolt = getPHVolts();
tempAdjusted4 = adjustPHBasedOnTemp(4,calibrationTempC);
double voltsPerPH = (abs(volt7-volt4)) / (7-tempAdjusted4);
double realPHVolt = (volt7 - phVolt);
double phUnits = realPHVolt / voltsPerPH;
double measuredPH = 7 + phUnits;
double roomTempC = getRoomTemperatureC();
double roomTempCompensatedMeasuredPH = adjustPHBasedOnTemp(measuredPH,roomTempC);
avgMeasuredPH+=measuredPH;
avgRoomTemperatureCompensatedMeasuredPH+=roomTempCompensatedMeasuredPH;
avgRoomTempC+=roomTempC;
avgPHVolts += phVolt;
}
avgMeasuredPH/=sampleSize;
avgRoomTemperatureCompensatedMeasuredPH/=sampleSize;
avgRoomTempC/=sampleSize;
avgPHVolts/=sampleSize;
Serial.print("avgMeasuredPH-");
Serial.print(avgMeasuredPH,4);
Serial.print(" roomTempCompensatedPH-");
Serial.print(avgRoomTemperatureCompensatedMeasuredPH,4);
Serial.print(" avgRoomtTempC-");
Serial.print(avgRoomTempC,4);
Serial.print(" avgPhVolts-");
Serial.print(avgPHVolts,4);
Serial.print(" 7CalVolts-");
Serial.print(volt7,4);
Serial.print(" 4CalVolts-");
Serial.print(volt4,4);
Serial.print(" 4CalTempAdjusted-");
Serial.println(tempAdjusted4,4);
delay(1000);
}
float adjustPHBasedOnTemp(float PH, float temp)
{
// http://www.omega.com/Green/pdf/pHbasics_REF.pdf
// When the temperature is other than 25degC and the ph is other than 7
// the temperature error is 0.03ph error/ph unit/10degC
// which means error = 0.03*(ph away from 7)*(tempdiffC/10)
float phDifference = abs(PH-7);
float tempDifferenceC = abs(temp-25);
float phAdjust = (0.03*phDifference)*(tempDifferenceC/10);
if(PH>7 && temp<25)
phAdjust=phAdjust;
if(PH>7 && temp>25)
phAdjust=phAdjust*-1;
if(PH<7 && temp>25)
phAdjust=phAdjust;
if(PH<7 && temp<25)
phAdjust=phAdjust*-1;
float tempAdjustedPH = PH + phAdjust;
return tempAdjustedPH;
}
double getPHVolts()
{
byte ad_high;
byte ad_low;
Wire.requestFrom(PHADDRESS, 2); //requests 2 bytes
while(Wire.available() < 2); //while two bytes to receive
ad_high = Wire.read();
ad_low = Wire.read();
double units = (ad_high * 256) + ad_low;
double volts = (units /4096)*3;
return volts;
}
double getRoomTemperatureC()
{
Wire.requestFrom(RoomTempI2CAddress,2);
byte MSB = Wire.read();
byte LSB = Wire.read();
int TemperatureSum = ((MSB << 8) | LSB) >> 4;
double celsius = TemperatureSum*0.0625;
return celsius;
}
void SetRoomTemperataureResolutionBits(int ResolutionBits)
{
if (ResolutionBits < 9 || ResolutionBits > 12) exit;
Wire.beginTransmission(RoomTempI2CAddress);
Wire.write(B00000001); //addresses the configuration register
Wire.write((ResolutionBits-9) << 5); //writes the resolution bits
Wire.endTransmission();
Wire.beginTransmission(RoomTempI2CAddress); //resets to reading the temperature
Wire.write((byte)0x00);
Wire.endTransmission();
}
In the past I tried to include SoftwareSerial (for some reason I can't remember) but skipped it since the sketch above stopped working.
This time I am trying to use the VirtualWire library (RF 433) on the same arduino but again the same issue I had before.
Tried the radiohead library (ask) as an alternative but same issue:
http://www.airspayce.com/mikem/arduino/RadioHead/index.html
Serial output stand alone:
avgMeasuredPH-avgMeasuredPH-6.9461 roomTempCompensatedPH-6.9465 avgRoomtTempC-27.7500 avgPhVolts-0.6750 7CalVolts-0.6700 4CalVolts-0.9500 4CalTempAdjusted-3.9550
Serial output when using once of the libraries:
avgMeasuredPH-avgMeasuredPH-inf roomTempCompensatedPH-inf avgRoomtTempC-27.8981 avgPhVolts-0.6755 7CalVolts-0.6700 4CalVolts-0.9500 4CalTempAdjusted-3.9550
I tried and tried but my skill alre lacking to solve this one.
Reza did not reply so I try my luck here.
Thank you in advance.