0% found this document useful (0 votes)
24 views5 pages

Coding Kalibrasi PH Air

This document contains the code for an Arduino sketch that measures pH levels using an analog pH sensor. It takes pH readings every 20 milliseconds and calculates a running average. Every 800 milliseconds it prints the voltage, pH value, and toggles an LED. It also controls a relay to activate an electrolysis process when the pH is above 7 for 30 seconds, and deactivates it after 10 seconds below 7.

Uploaded by

gbrl.wcksn
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views5 pages

Coding Kalibrasi PH Air

This document contains the code for an Arduino sketch that measures pH levels using an analog pH sensor. It takes pH readings every 20 milliseconds and calculates a running average. Every 800 milliseconds it prints the voltage, pH value, and toggles an LED. It also controls a relay to activate an electrolysis process when the pH is above 7 for 30 seconds, and deactivates it after 10 seconds below 7.

Uploaded by

gbrl.wcksn
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

#define SensorPin A0 //pH meter Analog output to Arduino Analog Input 0

#define Offset 1.25 //deviation compensate

#define LED 13

#define samplingInterval 20

#define printInterval 800

#define ArrayLenth 40 //times of collection

int Relaypin = 3;

#define elektrolisis_bekerja 30000

#define elektrolisis_berenti 10000

int pHArray[ArrayLenth]; //Store the average value of the sensor feedback

int pHArrayIndex = 0;

static float pHValue, voltage;

void setup(void)

pinMode(LED, OUTPUT);

pinMode(Relaypin, OUTPUT);

Serial.begin(9600);

Serial.println("pH meter experiment!"); //Test the serial monitor

void loop(void)

static unsigned long samplingTime = millis();

static unsigned long printTime = millis();

1
if (millis() - samplingTime > samplingInterval)

pHArray[pHArrayIndex++] = analogRead(SensorPin);

if (pHArrayIndex == ArrayLenth)pHArrayIndex = 0;

voltage = avergearray(pHArray, ArrayLenth) * 5.0 / 1024;

pHValue = 3.5 * voltage + Offset;

samplingTime = millis();

if (millis() - printTime > printInterval) //Every 800 milliseconds, print a numerical, convert the
state of the LED indicator

Serial.print("Voltage:");

Serial.print(voltage, 2);

Serial.print(" pH value: ");

Serial.println(pHValue, 2);

digitalWrite(LED, digitalRead(LED) ^ 1);

printTime = millis();

relay();

double avergearray(int* arr, int number) {

int i;

2
int max, min;

double avg;

long amount = 0;

if (number <= 0) {

Serial.println("Error number for the array to avraging!/n");

return 0;

if (number < 5) { //less than 5, calculated directly statistics

for (i = 0; i < number; i++) {

amount += arr[i];

avg = amount / number;

return avg;

} else {

if (arr[0] < arr[1]) {

min = arr[0]; max = arr[1];

else {

min = arr[1]; max = arr[0];

for (i = 2; i < number; i++) {

if (arr[i] < min) {

amount += min; //arr<min

min = arr[i];

3
} else {

if (arr[i] > max) {

amount += max; //arr>max

max = arr[i];

} else {

amount += arr[i]; //min<=arr<=max

}//if

}//for

avg = (double)amount / (number - 2);

}//if

return avg;

void relay() {

static unsigned long relaybekerja = millis();

static unsigned long relaymati = millis();

if (millis() - relaybekerja > elektrolisis_bekerja) {

if (pHValue >= 7 ) {

Serial.println("ELEKTROLISIS BEKERJA");

digitalWrite(Relaypin, LOW); // Sends high signal

Serial.println(pHValue);

4
relaybekerja = millis();

else {

if (millis() - relaymati > elektrolisis_berenti) {

Serial.println("ELEKTROLISIS BERHENTI");

digitalWrite(Relaypin, HIGH); // Sends high signal

Serial.println(pHValue);

relaymati = millis();

You might also like