Pages: [1]   Go Down
Author Topic: Interfacing with Vernier pH probe  (Read 831 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm currently working on a project that involves outputting the pH reading in the serial monitor.

Here's the code:
Code:
int potPin = 0;
float phSense = 0;

void setup()
{
  Serial.begin(9600);
  Serial.println("PH sensor ");
  analogReference(INTERNAL);
}

void printTenths(int value) {
// prints a value of 123 as 12.3
  Serial.print(value / 10);
  Serial.print(".");
  Serial.println(value % 10);
}

void loop() {
  int span = 20;
  int aRead = 0;
 
  for (int i = 0; i < span; i++) {
    aRead = aRead+analogRead(potPin);
  }

  aRead = aRead / 20;
  phSense = ((0.0592*1.1*aRead)/1024)*10;
  // convert voltage to pH
  Serial.print("Analog in reading: ");
  Serial.print(long(aRead));
  // print pH value on serial monitor
  Serial.print(" - Calculated pH Level: ");
  printTenths(long(phSense));
  delay(500);
 }
btw, I got this code from a book and modified it a little

As for the pins connection to arduino, I've directly connected pin 2 to GND, pin 5 to 5V, and pin 6 to A0.

Here's the output from the serial monitor:



As you can see, there is no output. I've checked my connections and nothing's loose or anything. As for the code, it was previously used for displaying temp on the serial monitor but I've modified it for my pH sensor. I'm wondering what's wrong here. Is it the connections or is it the code? Do I need to use other pins of the pH sensor (seeing that I've only used three of them).


For further reference on the probe that I'm using:
Logged

New Jersey
Online Online
Faraday Member
**
Karma: 50
Posts: 3426
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You have two problems: when you divide by 1024, the compiler will (I think) perform integer division and drop the fractional part. Since the result has only a fractional part, it'll become zero. Use 1024.0 instead. It looks like your cast to long in the print will do the same thing - get rid of it.
« Last Edit: March 11, 2012, 07:33:40 am by wildbill » Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 180
Posts: 8102
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You have chosen the INTERNAL (1.1v) reference.  The pH=7 output of the probe is 1.75v so unless you are measuring a high pH you will always get 1023 counts.

According to the user manual (http://www.vernier.com/files/manuals/ph-bta.pdf): "The pH Sensor will produce a voltage of 1.75 volts in a pH 7 buffer. The voltage will increase by about 0.25 volts for every pH number decrease. The voltage will decrease by about 0.25 volts/pH number as the pH increases.

That means that the range of your probe outputs should be 0V (pH 14) to 3.5V (pH 0).  To cover that range you will want a reference voltage of at least 3.5V.  Try setting analogRefrence() to DEFAULT (5V).
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 170
Posts: 12482
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


You must divide by 1023 as that is the max value of the analog read. (effect using 1024 = -0.1%)

Furthermore refactored some code including printing the floating point value as that can be done simpler

including the tips of John wasser

(not tested)
Code:
int potPin = 0;
float phSense = 0;
float refvoltage = 1.1;

void setup()
{
  Serial.begin(9600);
  Serial.println("PH sensor ");
  analogReference(INTERNAL);
}

void loop()
{
  int samples = 20;
  int aRead = 0; 
 
  for (int i = 0; i < samples ; i++)
  {
    aRead += analogRead(potPin);
  }
  float voltage = 5.0 * aRead/ (1023 * samples);  // assuming 5V reference
 
  phSense = 14 - voltage/0.25;
 
  // convert voltage to pH
  Serial.print("Analog in reading: ");
  Serial.print(aRead);
 
  // print pH value on serial monitor
  Serial.print(" - Calculated pH Level: ");
  Serial.println(phSense, 1); // 1 = one decimal, 2 = two decimals (default),etc   // removed the /10
  delay(500);
 }
Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks johnwasser and robtillaart. It works perfectly now.

@johnwasser: I was confused which output to use. Thanks for pointing that out.
@robtillaart: thanks for the refactoring! smiley-grin although it took me a while to understand it since I'm not well versed with programming but your modification really helped a lot.
Logged

Pages: [1]   Go Up
Jump to: