Hello Arduino Users!
In this tutorial, we will learn how to light up a RGB LED and power a DC motor using a pH sensor. This tutorial is part of my team’s water quality project. For our project, we will be monitoring sewage outflows using four different water quality sensors (including a pH sensor). We will be creating an aeration wastewater treatment system (powered using a DC motor) and will employ LEDs to signal when the aeration treatment is switched on.
Before getting into the tutorial, let’s briefly explore what a pH sensor does. A pH sensor measures the pH of water-based solutions to determine whether the solution is acidic or basic (alkaline). pH sensors detect pH values by measuring the hydrogen-ion activity of the solution. pH values range from 0-14. Neutral solutions like pure water have a pH value of 7. Solutions that have values less than 7 are deemed acidic and those with values greater than 7 are deemed basic (alkaline). (Source: Sensorland.com)
To learn about DC motors and RGB LEDs check out these links – Modmypi.com, Adafruit.com
Now let’s go ahead and set up our tutorial. We will need the following parts :
- Breadboard x 1
- 9V or 12V battery x 1
- Jumper wires x 12
- 330 ohm resistors x 4
- Transistor x 1
- Diode x 1
- RGB LED x 1
- DC motor x 1
- DFRobot analog pH sensor kit x 1
- Arduino UNO x 1
- 4 clear glass or plastic containers containing solutions with different pH levels. For this tutorial I have used tap water, salt water, sherry vinegar and diet coke as my test solutions. I leave it up to you to decide what type and how many solutions you want to test.
Note: The DFRobot analog pH sensor kit comes with several components namely a pH electrode, a BNC connector and a pH meter board that has an in-built blue LED to indicate when the pH sensor is on.
Next, we will wire the circuit as per the diagram below. Also, refer the circuit connections instructions provided after the diagram. The instructions will help you understand the wiring in the circuit diagram.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
* Circuit connections instructions * DFRobot analog pH sensor kit: * pH sensor ______ pH meter ______ BNC connector * BNC connector (black wire) ______ - Breadboard GND * BNC connector (red wire) ______ + Breadboard power * BNC connector (blue/green wire) ______ Arduino analog Pin A0 * - Breadboard GND ______ - Arduino GND pin * + Breadboard power ______ + Arduino 5V pin * RGB LED: * Pin 9 ______ 330 ohm resistor ______ + RGB LED (Red pin) * Pin 10 ______ 330 ohm resistor ______ + RGB LED (Green pin) * Pin 11 ______ 330 ohm resistor ______ + RGB LED (Blue pin) * DC motor: * Transistor emitter ______ - Breadboard GND * Transistor base ______ 330 ohm resistor ______ Pin 3 * Transistor collector ______ DC motor GND (black wire) and diode anode * Diode cathode ______ 5V and DC motor power (red wire) |
This is what my completed circuit looked like.
In this tutorial I have used the code provided in the DFRobot product website to run the pH sensor. I have added some additional lines of code that will allow us to use the readings from the pH sensor to light up the RGB LED and turn on the DC motor. The RGB LED turns blue when the pH value detected is less than 3, green if the value is greater than or equal to 3 but less than 7 and red if the value is greater than or equal to 7. The DC motor switches on when the pH value is greater than or equal to 7.
Copy the code provided below into your Arduino IDE.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
/* * Title: Tutorial on using pH sensor to turn on RGB LED and DC motor * Author: Sagari Datta * Date: Novemeber 14, 2018 * Reference: https://www.dfrobot.com/wiki/index.php/PH_meter(SKU:_SEN0161) */ //Declaring global variables //setting pH meter Analog output to Arduino Analog Input 0 #define SensorPin A0 //Setting an offset for deviation compensate #define Offset 0.00 //Setting pin 13 as LED #define LED 13 //Defining a sampling interval of 20 milliseconds #define samplingInterval 20 //Defining a printing interval of 800 milliseconds #define printInterval 800 //Defining an array for recording collection times #define ArrayLenth 40 //Integer variable for storing the average value of the sensor feedback int pHArray[ArrayLenth]; //Setting the ph array index to 0 int pHArrayIndex = 0; //Setting global variables for the color pins (red, green and blue) in the RGB LED int redPin = 9; int greenPin = 10; int bluePin = 11; // Creating global variable for the DC motor using a pwm pin int motorPin = 3; //Creating global variables for motor speed int Speed1 = 150; int Speed2 = 0; void setup(void){ //Setting LED pin (pin 13) as output pinMode(LED,OUTPUT); //Configuring RGB LED color pins (9,10 and 11) to output mode pinMode(redPin, OUTPUT); pinMode(greenPin, OUTPUT); pinMode(bluePin, OUTPUT); // Setting up the motor pin (pin 3) to be an output pinMode(motorPin, OUTPUT); //Note: The SensorPin (analog input pin, A0) is automatically set to input mode //Setting up serial connection Serial.begin(9600); //Debugging statement using the serial monitor Serial.println("pH meter experiment!"); } void loop(void){ //Creating local variables for time, pHValue and voltage static unsigned long samplingTime = millis(); static unsigned long printTime = millis(); static float pHValue,voltage; //Calculating pHValue 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(); //Turning on RGB LED and DC motor based on pHValue readings if (pHValue >= 7){ //Make RGB LED display color red digitalWrite(redPin, LOW); // turning pin 9 off digitalWrite(greenPin, LOW); // turning pin 10 off digitalWrite(bluePin, HIGH); // turning pin 11 on //Switch on DC motor analogWrite(motorPin, Speed1); // turning pin 3 on } if (pHValue >= 3 && pHValue < 7){ //Make RGB LED display color yellow digitalWrite(redPin, LOW); // turning pin 9 off digitalWrite(greenPin,LOW); // turning pin 10 off digitalWrite(bluePin, LOW); // turning pin 11 off //Switch off DC motor analogWrite(motorPin, Speed2); // turning pin 3 off } if (pHValue >= 0 && pHValue < 3){ //Make RGB LED display color blue digitalWrite(redPin, HIGH); // turning pin 9 on digitalWrite(greenPin, LOW); // turning pin 10 off digitalWrite(bluePin, LOW); // turning pin 11 off //Switch off DC motor analogWrite(motorPin, Speed2); // turning pin 3 off } } //Every 800 milliseconds, print a numerical, convert the state of the LED indicator if(millis() - printTime > printInterval){ showColors(); Serial.print("Voltage:"); Serial.print(voltage,2); Serial.print(" pH value: "); Serial.println(pHValue,2); digitalWrite(LED,digitalRead(LED)^1); printTime=millis(); } } double avergearray(int* arr, int number){ int i; 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]; } 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; } |
Take a minute to ensure that all the wiring is as it should be and then run the code. Once you’ve run the code, open the serial monitor. You will see the voltage and pH values displayed.
Your serial monitor should look something like this.
Before we go any further, let’s first calibrate the pH sensor. Open the lid of the pH sensor and dip the sensor into a neutral solution. Record the pH value and calculate the difference between the recorded value and the pH value of 7. Use this difference value as the ‘offset’ value in the code.
Once you’ve set the offset values you can dip your pH sensor into other solutions. It is important to note that if you want to get accurate pH readings, you will have to calibrate the sensor every time you test a new solution (especially if you’re moving from acidic to basic or vice versa). For this tutorial’s demo video (see below) I have dispensed with calibrating while testing different solutions because I’m interested in showing how the pH sensor, RGB LED and DC motor work together. The DFRobot website has some great information on how to calibrate the pH sensor. Click here to check it out.
Demo video
And there you have it! You can tweak the code to test how many ever solutions you want and turn on the RGB LED and DC motor in whatever way you like.
Hope you enjoyed this tutorial!