Hackster is hosting Hackster Holidays, Ep. 6: Livestream & Giveaway Drawing. Watch previous episodes or stream live on Monday!Stream Hackster Holidays, Ep. 6 on Monday!
Seth MillerLandon KetchieJacob Brannon
Published

Water Sensor Project

This project is the creation of a device that can measure the depth, pH, and temperature of water using three IoT devices and sensors.

AdvancedFull instructions provided15 hours195
Water Sensor Project

Things used in this project

Hardware components

Adafruit 10k Precision Epoxy Thermistor
×1
Teyleten Robot PH Value Data Detection and Acquisition Sensor Module Acidity and Alkalinity Sensor Monitoring and Control ph0-14 for Arduino
×1
Waterproof Ultrasonic Module JSN-SR04T Water Proof Integrated Distance Measuring Transducer Sensor for Arduino
×1
Photon 2
Particle Photon 2
×3
Solderless Breadboard Half Size
Solderless Breadboard Half Size
×3
Resistor 1k ohm
Resistor 1k ohm
×1

Software apps and online services

ThingSpeak API
ThingSpeak API
Particle Build Web IDE
Particle Build Web IDE

Story

Read more

Schematics

pH Sensor Schematic

Schematic of the pH sensor connected to the Particle Argon (Particle Photon 2 was used).

pH Sensor Circuit Build

pH Sensor built.

Depth Sensor Schematic

Depth Sensor Fritzing Schematic

Depth Sensor Circuit Build

Depth Sensor Circuit Built

Temperature Sensor Schematic

Temperature Sensor Schematic

Temperature Circuit Build

Temperature Circuit Build

Code

Temperature

C/C++
#include <thingspeak-webhooks.h>
#include "Particle.h"
#include <math.h>

const int thermistorPin = A2;
const int numReadings = 10; // Number of readings to average

int readings[numReadings];    // Store the recent readings
int readIndex = 0;            // Index for storing new readings
int total = 0;                // Running total of readings
float averageTemperature = 0; // Averaged temperature value
int led;

int depth = 0;

void distance_event(const char* event, const char* data) {
  depth = atoi(data);
    if(depth < 7) {
    digitalWrite(led,HIGH);
    delay(1000);
    digitalWrite(led, LOW);
    delay(1000);
  } else if (depth > 15) {
    digitalWrite(led,HIGH);
    delay(1000);
    digitalWrite(led, LOW);
    delay(1000);
  }
}

void setup() {
  Serial.begin(9600);
  Particle.function("getTemperature", getTemperature);
  Particle.connect();
  Particle.subscribe("depth", distance_event, MY_DEVICES);
}

void loop() {
  // Read the analog value from the thermistor
  int rawReading = analogRead(thermistorPin);

  // Smooth out fluctuations using a moving average
  total -= readings[readIndex];       // Subtract the oldest reading
  readings[readIndex] = rawReading;    // Store the new reading
  total += readings[readIndex];       // Add the new reading
  readIndex = (readIndex + 1) % numReadings; // Move to the next index

  // Calculate the average temperature
  averageTemperature = calculateTemperature(total / numReadings);

  // Publish the averaged temperature to the Particle Cloud
  Particle.publish("water_temperature", String(averageTemperature));

  // Print raw and averaged temperature to the Serial Monitor for debugging
  Serial.println("Raw Reading: " + String(rawReading));
  Serial.println("Average Temperature: " + String(averageTemperature, 2));

  // Wait for some time before reading the temperature again
  delay(5000);
}

float calculateTemperature(int rawReading) {
  // Convert the raw analog reading to resistance
  float voltage = rawReading * (3.3 / 4095.0);
  float resistance = (3.3 / voltage - 1) * 10000.0;

  // Use the Steinhart-Hart equation for accurate temperature conversion
  float steinhart = log(resistance / 10000.0) / 3950.0 + 1.0 / (273.15 + 25.0); // Room temperature in Kelvin
  steinhart = 112 + (1.0 / steinhart - 273.15); // Convert Kelvin to Celsius

  return steinhart;
}

int getTemperature(String command) {
  // Respond to the Particle Cloud request with the current averaged temperature
  return static_cast<int>(averageTemperature);
}

Depth

C/C++
// Particle Photon Code for JSN-SR04T Water Proof Ultrasonic Sensor
#include <thingspeak-webhooks.h>
#include <Particle.h>

#define TRIG_PIN D2  // Connect the Trig pin of the sensor to D2 on the Photon
#define ECHO_PIN D3  // Connect the Echo pin of the sensor to D3 on the Photon

int pH_Value;
int led = D7;

void ph_Value(const char* event, const char* data){
 pH_Value=atoi(data);   
  if(pH_Value < 7.0) {
  digitalWrite(led,HIGH);
  delay(1000);
  digitalWrite(led, LOW);
  delay(1000);
  } else if(pH_Value > 7.6) {
  digitalWrite(led,HIGH);
  delay(1000);
  digitalWrite(led, LOW);
  delay(1000);
  }
}

void setup() {
  Serial.begin(9600);
  pinMode(TRIG_PIN, OUTPUT);
  pinMode(ECHO_PIN, INPUT);
  Particle.subscribe("pH_Value", pH_Value, MY_DEVICES)
}

void loop() {
  
  // Read the echo pulse duration
  long duration = pulseIn(ECHO_PIN, HIGH);

  // Calculate distance in centimeters
  float distance = 9.81202 + duration * 0.034 / 2;

  // Print distance to Serial (optional)
  Serial.print("Distance: ");
  Serial.print(distance);
  Serial.println(" cm");

  // Publish the distance value to the Particle Dashboard
  Particle.publish("distance_event", String(distance));

  delay(5000);

}

pH

C/C++
// This #include statement was automatically added by the Particle IDE.
#include <thingspeak-webhooks.h>
#include "Particle.h"


// Define the pin connected to the analog output of the pH sensor
int pHSensorPin = A0;
float pHValue;
float sensorValue;
int temp = 0.0; // Initialize with a default value

void temperature(const char* event, const char* data) {
  temp = atoi(data);
}

void setup() {
    Serial.begin(9600);
    pinMode(pHSensorPin, INPUT);
    Particle.subscribe("Temperature", temperature, MY_DEVICES);
}

void loop() {
    // Read the analog value from the pH sensor
    int sensorValue = analogRead(pHSensorPin);

    // Convert the analog value to pH using your sensor's calibration
    float pHValue = convertToPH(sensorValue);

    // Publish the pH value to the Particle Cloud
    Particle.publish("pH_value", String(pHValue));

    // Wait for some time before the next reading
    delay(10000);
}

// Replace this function with the actual conversion formula from your pH sensor's datasheet
float convertToPH(int sensorValue) {
    // Example conversion formula (replace this with the actual formula)
    // pH = some_coefficient * sensorValue + temperature_correction
    pHValue = 7.0 + (sensorValue - 512) * 0.00015 + temp * 0.0133;
    return pHValue;
}

Credits

Seth Miller

Seth Miller

1 project • 2 followers
Landon Ketchie

Landon Ketchie

0 projects • 1 follower
Jacob Brannon

Jacob Brannon

0 projects • 1 follower

Comments