Code for PH Sensor DFRobot

Hi There,
Need help in getting code for my PH sensor. Managed to work out DHT sensors Dallas 1-wire sensor and UltraSonic Sensor. But struggling to get the DFRobot PH sensor going. Found this code but getting junk result, the values don’t change. Either my sensor is faulty or more likely I’m using the wrong code. Can someone point me in the right direction.
Much Appreciated…

https://www.dfrobot.com/product-1025.html

esphome:
  name: fish
  platform: ESP32
  board: nodemcu-32s

# Enable logging
logger:

# Enable Home Assistant API
api:
ota:
  password: "72e60d20083f62ae61b3aa2f685c360a"
wifi:
  ssid: "xxxxxxx"
  password: "xxxxxxx"
  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Fish Fallback Hotspot"
    password: "lGgPMsXkk4uG"

captive_portal:
sensor:  
     # https://esphome.io/components/sensor/adc.html
  - platform: adc
    pin: GPIO35
    id: ph
    name: "pH Sensor"
    update_interval: 1s
    unit_of_measurement: pH
    # https://esphome.io/components/sensor/index.html#sensor-filters
    filters:
      - median:
          window_size: 7
          send_every: 4
          send_first_at: 3
      # Measured voltage -> Actual pH (buffer solution)
      - calibrate_linear:
          - 0.59 -> 7.0
          - 0.71 -> 4.0

Please format your code correctly.

2 Likes

And for God’s sake point us to the device, or a datasheet, or something?

I have made the updates as requested… By the looks of the constructive criticism so far. The code I copied might not be that far off…
So I have also ordered another ph sensor, just in case the one I’m using is faulty. If not at the very least I have a spare…
I thought this request would be a common project. Not asking for someone to re-invent the wheel. Just hoped that their would be someone that has already completed a similar project and help out a well intention noob.

The sample code multiplies the voltage by 3.5 and adds a calibration offset.

pHValue = 3.5*voltage+Offset;

Your code OTOH seems to have a negative slope

- calibrate_linear:
          - 0.59 -> 7.0
          - 0.71 -> 4.0

ie as voltage rises, the pH falls.

I can’t see where the specs of what voltage maps to what pH, other than the sample code. I am also aware that the ADC voltage range of an arduino is possibly different to an esp32.

Found variety of problems. For those newbies that need some help. My main problem was that the output values of PH Sensor was to high. Needed a resistor 4.7K to pull down the voltage. I basically followed this dude. But removed the POT and replaced with resistor. As I believe over time it would give me problems…


esphome:
  name: greenhouse
  platform: ESP32
  board: nodemcu-32s

# Enable logging
logger:
# Enable Home Assistant API
api:
ota:
  password: "ac37dd3649728ed22e02a9f3d4845db5"

wifi:
  ssid: "xxxxx"
  password: "xxxxxx"

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Greenhouse Fallback Hotspot"
    password: "Qr0ezNj9c2iK"

captive_portal:


i2c:
  sda: GPIO19
  scl: GPIO18
  scan: true
dallas:
  - pin: 32
sensor: 
#=================================================
  - platform: dht
    pin: GPIO3
    temperature:
      name: "TempDHT"
      filters:
       - offset: 0.0      
    humidity:
      name: "HumDHT"
      filters:
       - offset: -20.0      
#   model: DHT22
    update_interval: 30s
#===================================================
  - platform: dallas
    address: 0xBB01202631DC4628
    name: "Tank_1"
  - platform: dallas
    address: 0x7001202654251E28
    name: "Tank_2"
#===================================================    
  - platform: bme280
    temperature:
      id: temp
      name: "Temp"
      filters:
       - offset: 0.0
      oversampling: 16x
    humidity:
      id: hum
      name: "Hum"
      filters:
       - offset: 0.0
    pressure:
      name: "Pressure"
    address: 0x77
    update_interval: 30s
#===================================================  

  # https://esphome.io/components/sensor/adc.html
  - platform: adc
    pin: GPIO36
    id: ph
    name: "pH Sensor"
    update_interval: 10s
    unit_of_measurement: pH
    # https://esphome.io/components/sensor/index.html#sensor-filters
    filters:
      - median:
          window_size: 7
          send_every: 4
          send_first_at: 3
      # Measured voltage -> Actual pH (buffer solution)
      - calibrate_linear:
          - 0.78 -> 7.0    #voltage value when entering sensor in PH of 7 solution
          - 1.036 -> 4.0  #voltage value when entering sensor in PH of 4 solution
1 Like

The nodeMCU ADC input is restricted to 1Volt. However the DFrobot output max is Vcc. Use a voltage divider to translate the Vcc to 1 Volt.
Also the pH sensor requires temperature compensation. Keep this in mind for refined calibration.
I have to say that I have found the DF robot pH sensor output to jump around quite a bit. So I have also introduced an averaging window.

This is chip dependent. An ESP 32 can measure up to 3.12v.

In our tests, the usable ADC range was from ~0.075V to ~3.12V (with the attenuation: auto setting),

1 Like

Could you share you wiring diagram I am facing the same issue on getting this pH sensor to work with my D1 mini.

The sensor is made for an Arduino. It needs 5v power, the voltage output from the sensor ranges from 0-4.5v. An esp can only read 0-3.2v. You can use 2 resistors as a voltage divider to bring the signal from the sensor down to 0-3.3v range.

image

A very crude example, green/black is ground, red is 5v, purple/yellow back to the esp. R2 resistor (above) on the left.

1 Like

It looks like version 2 of these sensors support 3.2 to 5.5v, so you shouldn’t need to make the above adjustments.

I’m looking to buy DFRobot industrial ph sensor pro v2 - SEN0169-V2 | DFRobot Electronics

Are people in this chat happy with the combo esphome - DFRobot?

I have their water pressure sensor. It’s rated to 232 psi it maxes @150 psi. It’s not even close to it’s rating. I use it in an aeroponics system and only to sense if there is water pressure.

I was going to use it to regulate the pressure in the line but because of the above, I don’t trust it. I’m using a dumb mechanical pressure switch instead.

That said I would use their Ph for maybe a swimming pool but definitely not for my plants. I use an atlas ezo for my ph. There is also a lower cost version, their gravity line.

Thanks for the info @Mikefila !
It’s possible to see all the wire connections? I’m in the same situation but i don’t have electronic knowledge.

You’ll need to find the adc pin on your esp depending on what kind it is, see links below. The signal from the probe goes to that pin. The other 2 are power and ground

1 Like

Thanks! Working following your instrucctions!

Hello friends,
I’ve also been “tinkering” with my pool control for a few weeks.
But I also come up against my limits here and keep struggling with the problem that my pH sensor is not working properly. At least that’s my feeling.
I was very happy to find this post and also the reference with the 2K and 1K ohm resistor.
At least that’s how I managed to get my value “right” when I put the sensor in the 4.0 and 7.0 liquid.
4.0 == 0.075
7.0 == 0.73

but when I “test” my pool water, it shows me a value of 8.4pH (but that can’t be the case) and even if I put the sensor in normal tap water, I have a value of at least 8.3 (that can’t be the case either)

I connected the sensor to 3.3V and to 5V
sometimes I try to calibrate with “lambda”, sometimes with “calibrate_linear”

For example, if I connect the sensor to 5V and use the two resistors (2K and 1K) I get a value of 1.05100 when the probe is in 7.0 liquid. but that can’t be…

I would be happy if someone here got their sensor to work properly and could help me with something.

Hi!
I’m in similar situation… I have the PH sensor with a esp32 and apparently everything works, the only problem I have is that the values ​​fluctuate a lot. You can see a picture:
image

If I use a tester on the sensor output, I see that it always oscillates at 0.4 vols (in a 5v range)
when “translated” to 3.3v it should have a lot more fluctuations

I don’t know if it’s a problem with the sensor or the board or I if doing something wrong.

This is the sensor (aliexpress) that I’m using.

I was able to get the DFRobot pH sensor v2 working with my esp32 by creating a custom component and using the library here: https://github.com/GreenPonik/DFRobot_ESP_PH_BY_GREENPONIK

I calibrated the sensor manually before adding to home assistant. I may add calibration services that can be called from home assistant in the future. For now the custom component is only exposing the ph sensor.

Here is the custom component I created: Custom sensor component for DFRobot Ph sensor · GitHub

It also uses a Dallas water temperature sensor for more accurate readings. If you don’t have this sensor you could hard code the temperature value.

To install you must have the following entries in your config file:

esphome:
  includes:
    - custom_dfrobot_ph_sensor.h
  libraries:
    - eeprom
    - onewire
    - DallasTemperature
    - file:///config/esphome/custom/DFRobot_ESP_PH
sensor:
  - platform: custom
    lambda: |-
      auto my_sensor = new CustomDFRobotPhSensor();
      App.register_component(my_sensor);
      return {my_sensor};
  
    sensors:
      name: "DFRobot Ph Sensor"
      accuracy_decimals: 2
      unit_of_measurement: " "

You must place the ph sensor library files in: /config/esphome/custom/DFRobot_ESP_PH/

and the custom component file should be located at: /config/esphome/custom_dfrobot_ph_sensor.h

1 Like

Hi, this is great!

I havent finished my hottub project yet - but went with the calibrate linear option as couldnt work out how to use the code provided by DF with esphome (im not a programmer - more of a tinkerer)… But now this is available - im open to changing it to work this way… and then it can use the temperature compensation etc. too…

On that note… is there a way to change what youve done - to work with an ADS1115 board over i2c? I see in your code your setting the pin - but is it possible to set it to a multiplexer pin on the ADS board?

Greenponik has an example with an ADS1115 here: https://github.com/GreenPonik/DFRobot_ESP_PH_WITH_ADC_BY_GREENPONIK