Notifications
Clear all

IoT 3.3V I2C Pressure sensor with ESP32 for Swimming Pool Controller

4 Posts
2 Users
0 Reactions
235 Views
 Jaco
(@jaco)
Member
Joined: 4 months ago
Posts: 3
Topic starter  

I am new to Arduino and ESP32's. (I have done many VB and VBA projects, but nothing in C++). This project is to assist my Chemigem D10 Pool controller. The pool controller take readings for the chemical levels and then add Acid and Chlorine as required. This all work very well...until either, it runs out of acid or Chlorine or the Filter is clogged-up. So for this project I have an ultrasonic distance sensor to measure the chemical level as well as a none contact liquid sensors for the Acid Drum and the same for the Chlorine drum. The none contact sensors are easy digital IO sensors -so no issues. The A02YYUW Ultrasonic sensors are the UART type and thanks to your video, I got that going. The pressure sensor is an I2C sensor and the support from the supplier is non-existent. Since I am new to Arduino / ESP32's, this is a challenge. They provide sample code, but it is missing one of the header files. So I am trying to get it going based on my limited knowledge and the even more limited data.

These are the files provide.

I have used a I2C sniffer program to confirm that the address for the sensor is correct. I have used this to initiate the measurement

image

After  that I am stuck. Can somebody please help.

 


   
Quote
(@aliarifat)
Member
Joined: 10 months ago
Posts: 61
 

It looks like the provided code is written for HT66F0176, a Holtek microcontroller, not for Arduino/ESP32. For ESP32, you can try this:

#include <Wire.h>

#define I2C_ADDRESS 0x78  // 7-bit I2C address of the sensor
#define CMD_MEASURE 0xAC

void setup() {
  Serial.begin(115200);
  Wire.begin(); // SDA, SCL default to GPIO21, GPIO22 on ESP32
}

void loop() {
  if (triggerMeasurement()) {
    float pressure = 0, temperature = 0;
    if (readSensor(pressure, temperature)) {
      Serial.print("Pressure (Pa): ");
      Serial.print(pressure);
      Serial.print(" | Temperature (°C): ");
      Serial.println(temperature);
    } else {
      Serial.println("Failed to read sensor.");
    }
  } else {
    Serial.println("Measurement command failed.");
  }
  delay(1000); // Read every 1s
}

bool triggerMeasurement() {
  Wire.beginTransmission(I2C_ADDRESS);
  Wire.write(CMD_MEASURE);
  return Wire.endTransmission() == 0;
}

bool readSensor(float &pressure, float &temperature) {
  // Wait until sensor is ready
  uint8_t status = 0;
  for (int i = 0; i < 100; i++) {
    Wire.requestFrom(I2C_ADDRESS, 1);
    if (Wire.available()) {
      status = Wire.read();
      if (((status >> 5) & 0x01) == 0) break; // Not busy
    }
    delay(10);
  }

  // Read 6 bytes
  Wire.requestFrom(I2C_ADDRESS, 6);
  if (Wire.available() < 6) return false;

  uint8_t buffer[6];
  for (int i = 0; i < 6; i++) buffer[i] = Wire.read();

  // Pressure raw: 3 bytes
  uint32_t press_raw = ((uint32_t)buffer[0] << 16) |
                       ((uint32_t)buffer[1] << 8)  |
                       (uint32_t)buffer[2];

  // Temp raw: 2 bytes
  uint16_t temp_raw = ((uint16_t)buffer[3] << 8) |
                       (uint16_t)buffer[4];

  // Convert based on 0–1MPa calibration
  double CAL_L = -125000;
  double CAL_H = 1125000;
  pressure = ((double)press_raw / 16777216.0) * (CAL_H - CAL_L) + CAL_L;
  temperature = ((double)temp_raw / 65536.0) * 190.0 - 40.0;

  return true;
}

since you are using the ESP32 and working on IoT projects, here are some more ideas for you.

https://www.pcbway.com/blog/10/Making_IoT_projects_with_ESP32_____Innovation___Implementation_S2E6.html


   
ReplyQuote
 Jaco
(@jaco)
Member
Joined: 4 months ago
Posts: 3
Topic starter  

@aliarifat Thanks for your time to help. I have loaded the code on the ESP32 and there was no errors - very good start. The results are less promising. The Sensor is design to measure water pressure up to 1 MPa. As a bonus, it also provide the temperature of the water. When I run the code with the sensor just at atmospheric pressure, I was expecting a value of close to zero for the pressure and around 26°C (My workshop temperature). The values I am getting is -105kPa for the pressure and the Temperature is jumping from -20°C to 130°C. I did a very crude test on the pressure. It is very stabile around -104 986 Pa. If I blow in the sensor ( I know sounds silly 🙂 , but then the pressure go to -104 942 Pa. So I think it is just the calibration factor/ correction issue. The temperature is a different story, not sure what the go there is.


   
ReplyQuote
 Jaco
(@jaco)
Member
Joined: 4 months ago
Posts: 3
Topic starter  

Hi @aliarifat I had another look at the code yesterday. I have changed the following:

// Pressure raw: 3 bytes - Jaco change buffer[0] to buffer[1] and the same for the rest (0,1,2 to 1,2,3)
press_raw = (buffer[1] << 16) | (buffer[2] << 8) | buffer[3];

as well as :

// Temp raw: 2 bytes - Jaco change 3,4 to 4,5
temp_raw = (buffer[4] << 8) | buffer[5];

based on

image

 Is that correct, or am I missing something?

 

Regards

 

Jaco


   
ReplyQuote