How To Use A PH Sensor With Arduino
How To Use A PH Sensor With Arduino
pH stands for ‘potential hydrogen’ and is a measure of the hydrogen ion concentration
in a solution. It indicates how acidic or basic a liquid is on a scale from 0 to 14. Pure
water has a neutral pH of 7. Acidic solutions have a lower pH while bases have a
higher pH.
The sensing part of a pH probe consists of a glass electrode and a reference electrode.
The glass electrode develops an electrical potential proportional to the hydrogen ion
activity as given by the Nernst equation. This potential is measured against the stable
potential of the reference electrode.
Out of these, glass electrode pH sensors are the most popular and commonly used
with Arduino.
Parts Required
To interface a pH sensor with Arduino, you will need the following components:
LCD/OLED display
SD card module for data logging
Isolator circuit for safety
Circuit Diagram
The basic circuit diagram for connecting a pH sensor to Arduino is shown below:
The pH probe generates a very small voltage (in mV range) proportional to the
hydrogen ion concentration. This needs to be amplified to scale it to the 0-5V range of
the Arduino analog inputs.
An op-amp IC like LM358 can provide the necessary amplification or buffering. The
10k resistor helps set the gain to about 200X to amplify the 0-1000mV sensor range to
0-5V DC range for the Arduino.
The Arduino can then read this amplified pH voltage on one of its analog input pins to
measure the pH of the solution.
5. Wire the op-amp output to an analog input pin on the Arduino such as A0.
6. Power the op-amp IC if needed by connecting the power and ground pins to
5V and GND respectively.
7. Optionally, you can add an LCD display, SD module, etc to the available
Arduino pins.
8. Insert the pH probe in the solution to be tested.
This completes the sensor interfacing circuitry. Make sure all connections are secure
before powering up the Arduino board.
Arduino Sketch
The Arduino software needs to read the analog voltage, map it to a pH value based on
the calibration curve, and display/log the results.
//pH Sensor Arduino Code const int phPin = A0; //pH sensor connected to
analog pin A0 float phValue; //to hold pH value void setup()
{ Serial.begin(9600); //calibrate pH meter function calibrateSensor(); }
void loop() { phValue = readpH(); //read pH value Serial.print("pH: ");
Serial.println(phValue); delay(1000); } //Function to calibrate
sensor void calibrateSensor() { //calibration codes //store calibration
points //map voltages to pH values } //Function to read pH float
readpH(){ //read analog voltage //map voltage to pH based on
calibration return ph; //return pH value }
Modify the calibration logic and reference voltage to pH mapping based on your
specific sensor calibration. This will give you accurate real-time pH measurements
that can be displayed or datalogged.
To display the pH value on an LCD module, simply print the phValue to the LCD in
the loop() function:
This will continuously log the pH measurements to the SD card for later analysis.
Conclusion
No, you can use any standard laboratory pH probe with BNC connector. Just get a
BNC to banana plug adapter to match its pins to the breadboard. There are also
special waterproof Arduino compatible pH probes available.
Yes, Arduino can interface to many types of electrochemical sensors apart from pH,
including CO2, dissolved oxygen, nitrogen, and more. Similar circuits with amplifiers
and calibration are needed to adapt their signals for Arduino.
We all know PH is an essential thing for drinking water. If you don’t take care of this
crucial ingredient, you might ruin your entire meal in a matter of seconds. This article
will teach you how to build an Arduino Ph sensor as a beginner project for someone
who doesn’t know what they’re doing.
What is an Arduino?
We do this project using the Arduino Uno and the Arduino 1.0.2 IDE (integrated
development environment).
PCB Manufacturing & Assembly Services https://www.raypcb.com/
RAYMING PCB & ASSEMBLY
– Ph probe ($50)
Before you get started, make sure you set up the Arduino kit by following the
instructions that came with it. This takes approximately 1 hour and 30 minutes to do.
However, if you’re in a hurry, it’s possible to do this in 20 minutes by following this
tutorial.
What is pH?
The pH scale is logarithmic, and it measures the negative base ten logarithms of the
activity of hydrogen ions in a solution.
The pH scale is from 0 to 14, with seven being neutral, less than seven acidic, and
greater than seven is alkaline.
Every solution has a pH that a simple electrode can measure. For example, lemon
juice has a pH of 2-3, and vinegar has a pH range from 4-6. Clean water may have a
pH between 6.5 and 8, depending on the area you live.
A downside to this value is that it’s not used for precise recipes when cooking.
A probe
A probe is an electronic device that allows you to measure the amount of voltage or
current flowing through a circuit. For example, we will be using a Ph probe when
measuring PH. This sensor lets us know if the water contains acids or bases.
The PH probe has two wires: one red and one black. The blue wire connects to 5V,
and the black wire connects to the Ground(GND). The Red wire is what you use to
measure the PH. It’s a very sensitive probe that can measure the pH value. You
connect it to the Arduino, and in a concise period, it will tell you if the solution has an
acidic or basic value.
There are many different models of Ph Sensor for Arduino. Unfortunately, many of
them appear similar. But, if you want to do a good job and make sure your sensor will
last for a long time, you should go with one from Rayming PCB & Assembly and get
this one.
You can connect the sensor to the Arduino board using two wires. Once the
connection is complete, you can start testing the sensor. You should do this by using a
small piece of bread. The amount of sugar in bread is close to human skin, so it is
easy to see how the sensor will react.
One can adjust the Ph probe to any other values as well. You need to tell it which
value you want, and it will give it back to you (the value). You can do this by using a
simple piece of bread.
Testing the sensor after connecting it to the Arduino is essential before using other
solutions. You should always let the sensor rest for around 24 hours to stabilize and
work properly. You will have to do this again when you connect it to a new circuit
later.
The Ph probe requires between 3.5V and 5V to read the solution’s pH value properly.
Therefore, to monitor the pH level of your Arduino project, you will need to use
a voltage regulator or a voltage divider.
This project will use two transistors and two resistors to get the proper readings from
different circuits or sensors. For example, if you want to measure the temperature on
your Arduino board, you can get that information with a thermistor. However, the
readings for both temperatures and pH depend on the circuit’s current level and,
therefore, on the value used in your soil ph sensor Arduino.
To build this little project, you will need to gather a few components and supplies.
Here is the list:
Safety
Before you start building your sensor, make sure to read these instructions carefully.
Your project will not be waterproof. Ensure that you place the device on a flat, safe
surface.
When moving or storing your Arduino circuit, always unplug the sensor from the
Arduino to make sure it doesn’t short-circuit on something.
If you are unsure about this project, don’t hesitate to ask someone about electronics
and programming. You will learn more, and your project and knowledge will grow
bigger.
Other than that, almost everything is as simple as a ‘Plug and Play’ installation. If
anything fails to work correctly, try restarting the Arduino IDE. If it still doesn’t work,
check all the connections again to ensure there aren’t any loose wires touching other
components or parts of the circuit.
You can choose to make the Arduino board a stand-alone device so that you don’t
need an enclosure. But, we think it’s better to use an enclosure because it gives you a
safer way to store your sensor or Arduino board in the future. But, of course, you
could also use the box that comes with your Arduino kit.
First, you will have to cut out two holes for the LCD module, the mobile
phone camera, and the micro SD card slot.
You should place the LCD on the bottom of the enclosure. Make sure you leave
enough room for the mobile phone camera and micro SD card slot.
Supply a hole at the right size for the LCD screen, and make a hole on the back of
your enclosure so that you can place a screw to fix it in place.
Place your finished product on top of another piece of acrylic sheet or plexiglass that
is slightly larger than your enclosure. Then, cut it to the same size as a saw. Once you
finish both pieces, drill two holes for the mobile phone camera and one for the micro
SD card slot.
Don’t worry if you mess up while making these holes. You can always take your
enclosure apart and fix all these problems. Do this by using a drill bit that is slightly
smaller than your cord and then cut all of these holes with a rotary tool, or you can
use a saw if you want to make smoother cuts.
Install three components, two transistors, and one resistor for the Arduino pH sensor.
Here is a quick explanation of these components:
1) First, we will install the 220-ohm resistor from the LCD module side so that you
can use an external power source. You also want to connect this to your Arduino
board’s positive (red) side.
2) Next, connect the LCD module’s ground to the Arduino board’s ground.
3) To install the two transistors, you will use a breadboard. First, make sure that you
place the transistor in each circuit correctly.
4) You will also have to add an extra ground wire between the transistors and the
breadboard since they don’t share a common ground with the Arduino board. Finally,
connect the transistor and resistor to GND on the Arduino board.
The Ph probe should be installed like the picture above to connect the wires to your
Arduino board. The GND wire should be connected to one of the Arduino’s pins and
should go in between both transistors to be grounded.
You can wire the other wire (from the Ph probe) directly with one of the transistors
(the transistor without an extra ground wire).
Finally, you can install the LCD module using two wires. You need to connect one
pin to the Arduino board and the other to the transistor that shares a ground with the
Arduino. You can use a breadboard for this if you want, but it is much easier just by
connecting both circuits directly.
You have already installed all the components in your housing, and now you have to
connect them. For this step, make sure you follow the circuit diagram I created for
this project. This diagram will find details about every component’s location and
where you should connect it to.
Just connect the parts that are highlighted in green using jumper wires. You can bend
the wires to make them fit in between the housing and your Arduino board without
causing any harm to them or their circuit.
Now you should be able to plug and play! Feel free to try out all of your sensors’
different settings and see how it works.
Once you have your Arduino pH circuit assembled, you can now place your sensor in
a safe environment to see how well it works. However, you don’t want to put it in the
water yet because you haven’t installed the software to let your sensor know its pH
level.
You can control the water temperature by sliding the potentiometer while controlling
the voltage by holding down the “set” button.
You can download the Arduino code for the project from here. It’s a sketch that you
can use to control your sensor.
You will have to install and run the Arduino IDE on your computer. You will also
have it on your mobile phone for setting up, uploading, and testing sensors in the
future.
Once you have finished installing everything, open up your Arduino IDE on your
computer, select ‘File/Open and select the code you downloaded from our page.
The code contains the description of each sensor on the Arduino board, and you can
easily change it to suit your needs.
Now connect your Arduino UNO to your computer with a USB cord and then click on
‘File/Upload’ this will send the code to your sensor so that you can start testing it out.
Click ‘Tools/serial monitor’ This will open up a terminal in which you can test your
sensor! Type “M50” in the terminal to heat the water at 50 degrees Celsius.
After that, check out the display, and you will notice that the LCD screen is currently
on, and it says: “Temp 1.0” on top of it.
If you type “M10” in your terminal, you will notice that the temperature is now 10
degrees Celsius hotter, and the LCD screen will now say “Temp 2.0”.
Then type “M20” to see that the temperature has risen to 20 degrees Celsius.
Finally, we can test our pH sensor and see how well this sensor works! Type “pH” in
the terminal, and the LCD screen will say “2.0”.
That’s how you can use this Arduino pH sensor to monitor the levels of your
environment.
You can calibrate this sensor so that it will be able to tell the exact pH level that is in
your environment. For this part, you will need two common solutions in a range of
1-14 pH. In this case, we used a solution at five and another at 10.
Our solution at five pH was pink, and our solution at ten pH was purple. So we mixed
these two solutions, and our sensor read “7”. Which means you calibrated the sensor
at 7.
You can do this step multiple times to see how well your sensor calibrates and reads
the pH level in your environment.
After calibrating the sensor, you can use it with Arduino electronics. Here is a code
snippet that you can use to see how well your sensor works:
This code will turn on (red LED on) the LED connected to your LED strip (VCC) and
display “Temp” on the LCD screen. You can change these values in the sketch to suit
your needs!
Now again, open up your Arduino IDE and upload this sketch onto your Arduino
board.
This will allow your pH sensor to bridge your sensors and Arduino board. With this,
you can control many different sensors from one device!
Now take this same code, but change the text and change it so that it says “pH”
instead of “Temp.”
Then connect your pH source (a five pH) to your Arduino board. Then download a
sketch from here. This will let your Arduino board be able to read your pH sensor!
Notice that when you download and upload the program, the LED light will turn blue
and red when it recognizes that the sensor and Arduino need a connection.
Now type “pH” into your terminal, and you should see this screen:
That’s how easy it is to use this DIY pH sensor from Arduino electronics. You can
now use it as a simple probe for your other sensors in our project.
We tested the pH sensor in an open environment. First, we used a clear jar and filled it
up with distilled water so that there was no conductivity of the water, and we stirred
for about 30 seconds. Next, we put a piece of pH paper on top of the solution and
connected an Arduino board using a USB cable.
Then, we took the sensor apart and connected it to our lab equipment. We tested the
voltage output from the sensor, compared it to a known value, and found that there
was about 0.1 volts difference between both of them. We then compared the results to
the pH table online and found that the readings were correct!
We used distilled water again to have no water conductivity in the jar. Next, we used
a hard water solution and poured it into the jar. We then put some pH paper on top of
the water and connected the other end to our Arduino board. Then we took our pH
sensor apart, stripped off its casing, and put it into the hard water solution. From there,
we tested both outputs from the Arduino and lab equipment.
To our surprise, both of them were about 0.02-volt difference which is acceptable for
our sensor since it is an analog voltage output device. Unfortunately, we tested one
previous version of the pH sensor in hard water, and it didn’t give us a reliable result,
so we needed to replace it with this one since it is more precise.
We used the same setup again, but we used a saltwater solution, about 0.4 volts
difference from our analog output device. Both outputs were still within an acceptable
range, and we tested both of them using a previous version of the pH sensor in
saltwater, and it also gave us similar results.
There are a few common errors that we can find when building this Arduino pH
sensor from scratch. Here are some of them:
java.lang.StackOverflowError
This error happens when the code you are trying to upload doesn’t work properly.
This might be because you did not implement some of your library’s functions in your
sketch. To fix this problem, comment out unnecessary codes and compile and upload
again.
If your sketch is too large for the flash memory, you might get this error message,
which means your sketch is too big to fit in the flash memory. To fix this problem,
comment out unnecessary codes and compile and upload again. If it still doesn’t work,
you can use another Arduino IDE instead of using the default one that comes with
Arduino boards.
When you compile your code and upload it on your Arduino board, you might get the
“Unsatisfied Link Error” message box. This means that there is a library that you need
to add to the Arduino IDE before trying to compile and upload again. But, of course,
you can always go to this page and download this library into your Arduino IDE to
use it in your project.
This error happens when you try to upload a sketch, but nothing happens on your
board. This might be because there is something wrong with the code you are trying
to upload. First, try removing all the comments from your file and then re-compile it
again. If that doesn’t work, you could use this other Arduino board instead.
When you upload sketches onto your Arduino board, you might get a message box
telling you that “Serial port is already in use.” This might be because your IDE tried
to upload on a serial port and failed because it was not connected. To fix this, try to
restart your Arduino IDE by closing it and re-open the IDE.
Launch4j Error
Sometimes, when you compile your code, a Java error comes up and tells you that the
Launch4j cannot run. This is because your Arduino board is not detected by your
computer since there might be a problem with the serial connection between your
computer and the Arduino board. Connect your Arduino board with a new USB cable
to fix this problem.
This error happens when you try to connect an Arduino board to your computer, but
your Arduino board doesn’t appear in the list of recognized devices. To fix this,
remove the IDE, and after you reboot your computer and then re-plug it into the USB
port, this should help.
Sometimes, the code you put into your Arduino IDE doesn’t work when you compile
it. This might be because you need to put in the PIN before starting. To fix this
problem, comment out the “Serial. begin()” line by putting a “#” at the beginning of
that line and then upload the program onto your board again.
Your board may be out of sync with the Arduino IDE. If you can’t upload any sketch
onto your board, try resetting and restarting it by disconnecting the power and
reconnecting it to a new USB cable. This should sync it up to Arduino IDE to upload
sketches onto your board.
This might be because your Arduino board is not compatible with this type of
hardware. If you have an Arduino Uno that you cannot use, you might consider
finding a different one.
Conclusion
Finally, we have finished our first pH sensor Arduino project. We started by looking
at the basic parts that we will need for this project: an Arduino Uno and a pH sensor.
After reading about how these sensors work, we decided to use the DS18B20, easy to
find and cheap. Fortunately, most of the parts we used were available on Amazon, so
there was no need to look everywhere to get what we needed.
After building our pH sensor, we tested both possible scenarios using hard and
saltwater. We found that both outputs were still within an acceptable range from the
common range of pH values. Both values were around seven and below eight, which
means our sensor gave us a correct output.
Finally, we learned about some common errors and how to fix them for your Arduino
project.
Related Posts:
2. Which is the best to use between Arduino Leonardo and Arduino UNO
https://www.raypcb.com/arduino-ph-sensor/