hminowa
Published © GPL3+

Color Sensor Type pH Meter

Goodbye calibration! Digitization of pH test paper! This is a pH meter that reads the color of the pH test paper.

IntermediateShowcase (no instructions)5,223
Color Sensor Type pH Meter

Things used in this project

Hardware components

Arduino Nano R3
Arduino Nano R3
×1
Adafruit RGB Color Sensor TCS34725
×1
4-Bit LED Digital Tube Module
×1

Hand tools and fabrication machines

pH test paper

Story

Read more

Code

Color Sensor Type pH Meter

Arduino
The pH function needs to be adjusted according to the distance between color sensor and pH test paper.
//Color Sensor Type pH Meter

#include <Wire.h>//I2C
#include "Adafruit_TCS34725.h"//Color Sensor
#include <TM74HC595Display.h>//7LED

Adafruit_TCS34725 tcs = Adafruit_TCS34725(TCS34725_INTEGRATIONTIME_50MS, TCS34725_GAIN_4X);

//7LED
int SCLK = 7;
int RCLK = 6;
int DIO = 5;

//7LED
TM74HC595Display disp(SCLK, RCLK, DIO);
unsigned char LED_0F[14];//

void setup() {
  //
  LED_0F[0] = 0xC0; //0
  LED_0F[1] = 0xF9; //1
  LED_0F[2] = 0xA4; //2
  LED_0F[3] = 0xB0; //3
  LED_0F[4] = 0x99; //4
  LED_0F[5] = 0x92; //5
  LED_0F[6] = 0x82; //6
  LED_0F[7] = 0xF8; //7
  LED_0F[8] = 0x80; //8
  LED_0F[9] = 0x90; //9
  LED_0F[10] = 0x7F; //.
  LED_0F[11] = 0xBF; //-
  LED_0F[12] = 0x8C; //P
  LED_0F[13] = 0x89; //H

  disp.send(LED_0F[11], 0b1111);//----
  delay(1000);//1000ms

  //PH
  for(int i=0; i<=2999; i++){
    disp.send(LED_0F[12], 0b0100);
    disp.send(LED_0F[13], 0b0010);
  }

  //
  Serial.begin(9600);
  Serial.println("Color View Test!");

  if (tcs.begin()) {
    Serial.println("Found sensor");
  }
  else {
    Serial.println("No TCS34725 found ... check your connections");
    while (1); // halt!
  }
}

void loop() {
  //16RGB
  uint16_t clear, red, green, blue;

  tcs.getRawData(&clear, &red, &green, &blue);
  //delay(1000);//1000ms

  //RGB
  Serial.print("C:\t"); Serial.print(clear);
  Serial.print("\tR:\t"); Serial.print(red);
  Serial.print("\tG:\t"); Serial.print(green);
  Serial.print("\tB:\t"); Serial.print(blue);

  //
  uint32_t sum = clear;
  float r, g, b;
  r = red; r /= sum;
  g = green; g /= sum;
  b = blue; b /= sum;
  r *= 256; g *= 256; b *= 256;
  Serial.print("\t/");
  Serial.print("\tr:"); Serial.print((int)r);
  Serial.print("\tg:"); Serial.print((int)g);
  Serial.print("\tb:"); Serial.print((int)b);

  //pH
  float pH_r, pH_g, pH_b;
  int pH;
  
  //pH Function
  pH_r = 0.0000001216*r*r*r-0.0001876*r*r+0.1009*r-11.62;//r
  pH_g = 0.000000007332*g*g*g-0.00001259*g*g+0.01022*g+4.676;//g
  pH_b = 0.000000005716*b*b*b-0.00001896*b*b+0.02183*b-1.428;//b

  pH = (pH_r+pH_g+pH_b)*10/3;

  //pH
  for(int i=0; i<=999; i++){
    disp.digit4(pH, 3);
    disp.send(LED_0F[10], 0b0010);
  }

  //pH
  Serial.print("\t/");
  Serial.print("\tpH_r:"); Serial.print((int)pH_r);
  Serial.print("\tpH_g:"); Serial.print((int)pH_g);
  Serial.print("\tpH_b:"); Serial.print((int)pH_b);
  Serial.print("\tpH:"); Serial.print(pH);
  Serial.println();

}

Credits

hminowa

hminowa

0 projects • 4 followers

Comments