Devices & Components
Arduino Nano
Adafruit RGB Color Sensor TCS34725
4-Bit LED Digital Tube Module
Hardware & Tools
pH test paper
Project description
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.
1//Color Sensor Type pH Meter 2 3#include <Wire.h>//I2C 4#include "Adafruit_TCS34725.h"//Color Sensor 5#include <TM74HC595Display.h>//7LED 6 7Adafruit_TCS34725 tcs = Adafruit_TCS34725(TCS34725_INTEGRATIONTIME_50MS, TCS34725_GAIN_4X); 8 9//7LED 10int SCLK = 7; 11int RCLK = 6; 12int DIO = 5; 13 14//7LED 15TM74HC595Display disp(SCLK, RCLK, DIO); 16unsigned char LED_0F[14];// 17 18void setup() { 19 // 20 LED_0F[0] = 0xC0; //0 21 LED_0F[1] = 0xF9; //1 22 LED_0F[2] = 0xA4; //2 23 LED_0F[3] = 0xB0; //3 24 LED_0F[4] = 0x99; //4 25 LED_0F[5] = 0x92; //5 26 LED_0F[6] = 0x82; //6 27 LED_0F[7] = 0xF8; //7 28 LED_0F[8] = 0x80; //8 29 LED_0F[9] = 0x90; //9 30 LED_0F[10] = 0x7F; //. 31 LED_0F[11] = 0xBF; //- 32 LED_0F[12] = 0x8C; //P 33 LED_0F[13] = 0x89; //H 34 35 disp.send(LED_0F[11], 0b1111);//---- 36 delay(1000);//1000ms 37 38 //PH 39 for(int i=0; i<=2999; i++){ 40 disp.send(LED_0F[12], 0b0100); 41 disp.send(LED_0F[13], 0b0010); 42 } 43 44 // 45 Serial.begin(9600); 46 Serial.println("Color View Test!"); 47 48 if (tcs.begin()) { 49 Serial.println("Found sensor"); 50 } 51 else { 52 Serial.println("No TCS34725 found ... check your connections"); 53 while (1); // halt! 54 } 55} 56 57void loop() { 58 //16RGB 59 uint16_t clear, red, green, blue; 60 61 tcs.getRawData(&clear, &red, &green, &blue); 62 //delay(1000);//1000ms 63 64 //RGB 65 Serial.print("C:\ "); Serial.print(clear); 66 Serial.print("\ R:\ "); Serial.print(red); 67 Serial.print("\ G:\ "); Serial.print(green); 68 Serial.print("\ B:\ "); Serial.print(blue); 69 70 // 71 uint32_t sum = clear; 72 float r, g, b; 73 r = red; r /= sum; 74 g = green; g /= sum; 75 b = blue; b /= sum; 76 r *= 256; g *= 256; b *= 256; 77 Serial.print("\ /"); 78 Serial.print("\ r:"); Serial.print((int)r); 79 Serial.print("\ g:"); Serial.print((int)g); 80 Serial.print("\ b:"); Serial.print((int)b); 81 82 //pH 83 float pH_r, pH_g, pH_b; 84 int pH; 85 86 //pH Function 87 pH_r = 0.0000001216*r*r*r-0.0001876*r*r+0.1009*r-11.62;//r 88 pH_g = 0.000000007332*g*g*g-0.00001259*g*g+0.01022*g+4.676;//g 89 pH_b = 0.000000005716*b*b*b-0.00001896*b*b+0.02183*b-1.428;//b 90 91 pH = (pH_r+pH_g+pH_b)*10/3; 92 93 //pH 94 for(int i=0; i<=999; i++){ 95 disp.digit4(pH, 3); 96 disp.send(LED_0F[10], 0b0010); 97 } 98 99 //pH 100 Serial.print("\ /"); 101 Serial.print("\ pH_r:"); Serial.print((int)pH_r); 102 Serial.print("\ pH_g:"); Serial.print((int)pH_g); 103 Serial.print("\ pH_b:"); Serial.print((int)pH_b); 104 Serial.print("\ pH:"); Serial.print(pH); 105 Serial.println(); 106 107} 108
Comments
Only logged in users can leave comments