Help with Code Please

Hi guys, for a school project, I am trying to use the analog pH sensor and have an LED turn on when the pH reads below 7.35 and turn off when the pH is above 7.35. I am new to Arduino, I got the LED, sensor, and Arduino to turn on, but I think my code is the issue because the light does not turn off when I put the sensor in a basic solution. Here is my code, do you guys have any tips on what I can change?

#define pHmeter INPUT
void setup() {
  // put your setup code here, to run once:

  pinMode(A0, INPUT);
  pinMode(13, OUTPUT);

void loop() {
  // put your main code here, to run repeatedly:

  int sensorValue = 7;

  if (pHmeter < 7.35) {
    digitalWrite(13, HIGH);
  } else {
    digitalWrite(13, LOW);
  delay(10); // Delay a little bit to improve simulation performance

Remove this line -> pinMode(A0, INPUT);

You probably need to do some math to covert what the analog reading comes in as to what the PH value really is.

-jim lee

Hi Maggie,

welcome to the forum.

karma for posting code as a code-section. Well done.
Your sketch shows that you have put own effort into it. That is very important to get help in this forum.
It seems to be your first attempt to make the code work. As you described it doesn't do it yet.

So here are tips how to improve your programming-skills:

A fundamental important technique to analyse not working code is to use serial-output which can be seen on the serial monitor.
Below you see an example of how serial output can be added to your code. The additional lines print the real values of your variable "pHMeter" to the serial monitor.

There are multiple errors in your code. In this version one error is corrected. pHmeter has to ba a variable not a #define-constant. All other errors are left inside. upload this code-version into your (which Arduino?) open the serial monitor and adjust baud to 115200

then look at the serial output. activate the serial-monitor by pressing shift-Ctrl-m or main-menu tools-serial monitor
If you have any questions just post them. Even the most simple questions. Just ask as concrete questions as your demonstration of willingness to learn.

const byte pHMeterPin = A0; // use selfexplaining names instead of comments
const byte myLEDPin   = 13; // use selfexplaining names instead of comments

int pHmeter;  // define pHmeter as a variable
void setup() {

  pinMode(pHMeterPin, INPUT);  // use selfexplaining names instead of comments
  pinMode(myLEDPin, OUTPUT);   // use selfexplaining names instead of comments


void loop() {
  int sensorValue = 7; // not (yet) used

  pHmeter == analogRead(pHMeterPin);
  Serial.print("value of variable pHmeter is #"); // text needs double-hyphens 
  Serial.print(pHmeter);                          // print value variables without double-hyphens
  Serial.print("#");                              // put a #-sign right before and after the value to make visible what belongs to the value  

  if (pHmeter < 7.35) {
    digitalWrite(myLEDPin, HIGH);
  } else {
    digitalWrite(myLEDPin, LOW);
  delay(500); // Delay half a second to slow down serial output 

best regards Stefan

Please start by posting the datasheet for your sensor. A bare pH sensor will have an extremely high output impedance. You just can't connect one directly to a processors A2D input. The input's (relatively) low input impedance will load down the circuit so much that it will be impossible to get usable measurements. You need a special buffer amplifier with a very high input impedance.

Assuming your sensor setup has such an amplifier (we'll know when we see the datasheet), what is it's output range? Meaning for a pH value between 0 and 15, how does it it's output voltage vary? That needs to be scaled to the range allowed by your processor's A2D input. Depending on what board you're using (please specify ... Arduino Uno?), that may require external circuitry.

Finally, the analogInput() function does not return floating point values (like 7.35). It returns a whole number between 0 and 1023 (typically). 0 represents 0v input level. 1023 represents the A2D's reference voltages. That's typically the processor's supply voltage (3.3v or 5v). That's why we need to know what board you're using.

With the above information, you'll then be able to scale the 0 to 1023 value into the pH the sensor is reading.

Hi guys,
Thank you all for your help and for being so kind to a beginner. I attached the data sheet for the pH sensor as @gfvalvo suggested, and I do have an Arduino Uno. I copy and pasted the code that @StefanL38 post (thank you for that) in the Arduino sketchbook (I believe that's what it is called?) and this may be a dumb question but once I click the upload arrow does it automatically send the code to the Arduino and start the loop? Thanks!

IMG_8475.jpeg.pdf (1.72 MB)

That's not a very useful "datasheet". It only appears to be a bare sensor. Please post a schematic diagram of how you have it connected.

pHmeter == analogRead(pHMeterPin);

= for assignment of value

You are not making a comparison where you use ==

Remove this line -> pinMode(A0, INPUT);

It's not a problem to leave that line as it is, but it isn't necessary.

Hi Maggi,

If you click the upload arrow the code gets compiled and then uploaded to the arduino.

The windows where you write the code is called "Arduino-IDE" IDE is short for Integrated Development Environment

The whole IDE is more than just the window you write the code in. There is the serial monitor, serial plotter, library manager, board-manager etc.

Did you open the serial monitor to watch what gets printed?
The pH-sensor has an impedance specified < 250 MOhm. The datasheet says only less than 250 MOhm.
Anyway sounds like the impedance is high. Impedance here means resistance. A high impedance means as soon as something that "cosnumes" electricity from the sensor is connected to the sensor the sensors output-voltage will drop reasonably. This means measuring the wrong voltage = wrong pH-value.

Only exception is connecting the pH-sensor to a device that does "consume" only an extremly small amount of electricity. Such a device has a very high input-impedance (very high input-resistance). This resistance has to be in the Giga-Ohm-area. Therefore you need an operational amplifier. Huge word but in most cases easy to work with. Easiest circuit is called voltage-follower.

best regards Stefan