Water Quality Sensors
Turbidity Sensor
To get the turbidity sensor to work, we plugged the sensor into the digital port of the XIAO Seeed studio rp2040 expansion board, and tried to use the code provided from the website. This did not work, so we had to try some different code, we found the other code, and then were able to make the sensor work. This senses how clear the water is, and how far the light can travel without being diffused.
![](https://www.google.com/images/icons/product/drive-32.png)
Temperature Sensor
This sensor was quite easy to use and is used to measure the temperature of the water. To use it plug it into the digital port of the expansion board, and place the silver end of the probe into the water to begin reading values. We found some simple code online, and it worked quite well. The values outputted are in Celsius so be wary of that.
![](https://www.google.com/images/icons/product/drive-32.png)
TDS Sensor
To use this sensor, we again plugged it into the digital port of the expansion board. We found code online, and it worked first try. When testing the sensor we found that the sensor maxed out at around 155 ppm, discovering this by adding lots of salt after the initially salt we added. The value of normal charlotte tap water was around 85 ppm.
![](https://www.google.com/images/icons/product/drive-32.png)
Water Quality Values
Sensor Code
To explain the code I asked chat GPT the prompt "Explain this code to a 6th grader" and it gave the following for each section of code
Water Turbidity Sensor:
Analog Signal Output, the output value will decrease when in liquids with a high turbidity
Working Code:
void setup() {
Serial.begin(9600);
}
void loop() {
int sensorValue = analogRead(A0);
float voltage = sensorValue * (3.3 / 1024.0);
Serial.println ("Sensor Output (V):");
Serial.println (voltage);
Serial.println();
delay(1000);
}
Explanation:
Loop Function: This is the main part of the code that runs continuously after the setup. It keeps looping over and over.
analogRead(A0): The Arduino reads the analog voltage from pin A0. Analog means it can read a range of values, not just on or off like a digital signal.
float voltage = sensorValue * (3.3 / 1024.0): The raw sensor value is converted to a voltage value. The Arduino's analog-to-digital converter (ADC) gives values between 0 and 1023 for voltages between 0 and 3.3 volts. So, this line converts that raw value to an actual voltage.
Serial.println("Sensor Output (V):"): It prints a message to the serial monitor saying "Sensor Output (V):".
Serial.println(voltage): It prints the voltage value to the serial monitor. This is the actual result of the analog reading.
Serial.println(): This line adds an empty line in the serial monitor to make it more readable.
delay(1000): It pauses the program for 1000 milliseconds (1 second) before starting the next loop. This is done to slow down the output and make it more manageable to read on the serial monitor.
This prints the voltage that changes depending on the turbidity. This technically measures the changes in turbidity, but I’m not sure if this is the reading water turbidity. Connects to analog pin
Temperature Sensor:
https://wiki.seeedstudio.com/One-Wire-Temperature-Sensor-DS18B20/
NOTE: YOU HAVE TO DOWNLOAD THIS LIBRARY AND THIS LIBRARY THEN DO SKETCH > INCLUDE LIBRARY > ADD ZIP LIBRARY —--- and add each of the two
Working Code(Connect to any digital pin):
// Include the libraries we need
#include <OneWire.h>
#include <DallasTemperature.h>
#define ONE_WIRE_BUS 26
OneWire oneWire(ONE_WIRE_BUS);
// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);
void setup(void)
{
Serial.begin(9600);
Serial.println("Dallas Temperature IC Control Library Demo");
sensors.begin();
}
void loop(void)
{
Serial.print("Requesting temperatures...");
sensors.requestTemperatures(); // Send the command to get temperatures
Serial.println("DONE");
float tempC = sensors.getTempCByIndex(0);
if(tempC != DEVICE_DISCONNECTED_C)
{
Serial.print("Temperature for the device 1 (index 0) is: ");
Serial.println(tempC);
}
else
{
Serial.println("Error: Could not read temperature data");
}
}
Explanation:
Libraries: The code includes special sets of pre-written instructions (libraries) that help it talk to a particular type of temperature sensor.
Pin Definition: The code says where the temperature sensor is physically connected to the microcontroller. Think of it like specifying which plug on a computer is being used.
Initialization: The code sets up communication with both the computer (through Serial communication) and the temperature sensor. It's like saying "Hello" to both and making sure they understand each other.
Loop: The main part of the code that keeps repeating. It constantly asks the temperature sensor, "What's the temperature right now?" It's a continuous conversation.
Temperature Reading: It reads the temperature from the sensor and checks if the reading is valid. If everything is okay, it shares the temperature with the computer. If something goes wrong, it reports an error.
In simpler terms, this code is a conversation between a computer and a temperature sensor. The computer is regularly asking the sensor, "Hey, what's the temperature?" and then letting us know what it finds out. It's like a continuous check on the current temperature.
TDS sensor:
Working code(any analog pin):
#define SERIAL Serial
#define sensorPin A0
int sensorValue = 0;
float tdsValue = 0;
float Voltage = 0;
void setup() {
SERIAL.begin(9600);
}
void loop() {
sensorValue = analogRead(sensorPin);
Voltage = sensorValue*5/1024.0; //Convert analog reading to Voltage
tdsValue=(133.42/Voltage*Voltage*Voltage - 255.86*Voltage*Voltage + 857.39*Voltage)*0.5; //Convert voltage value to TDS value
SERIAL.print("TDS Value = ");
SERIAL.print(tdsValue);
SERIAL.println(" ppm");
delay(1000);
}
Explanation:
Macros Definition (using #define):
SERIAL Serial: This line creates an alias SERIAL for the standard Serial object. It's just a way to make the code more readable.
sensorPin A0: This line defines a constant named sensorPin with the value A0. It makes the code more modular by assigning a name (sensorPin) to a specific pin on the Arduino (A0).
Variable Declarations:
int sensorValue = 0;: It declares an integer variable sensorValue and initializes it to 0. This variable will store the raw analog reading from the sensor.
float tdsValue = 0;: It declares a floating-point variable tdsValue and initializes it to 0. This variable will store the calculated Total Dissolved Solids (TDS) value.
float Voltage = 0;: It declares a floating-point variable Voltage and initializes it to 0. This variable will store the voltage calculated from the analog sensor reading.
Setup Function:
SERIAL.begin(9600);: It initializes serial communication with a baud rate of 9600 bits per second. This allows communication between the Arduino and an external device, typically a computer.
Loop Function:
sensorValue = analogRead(sensorPin);: Reads the analog voltage from the pin specified by sensorPin (A0 in this case) and stores the result in sensorValue.
Voltage = sensorValue * 5 / 1024.0;: Converts the raw analog reading to voltage. The analog-to-digital converter (ADC) produces values between 0 and 1023 for voltages between 0 and 5 volts.
tdsValue = (133.42 / Voltage * Voltage * Voltage - 255.86 * Voltage * Voltage + 857.39 * Voltage) * 0.5;: Calculates the Total Dissolved Solids (TDS) value based on a formula that involves the voltage. This formula is specific to the sensor being used.
SERIAL.print("TDS Value = ");: Prints a message to the serial monitor.
SERIAL.print(tdsValue);: Prints the calculated TDS value to the serial monitor.
SERIAL.println(" ppm");: Prints "ppm" (parts per million) and moves to the next line in the serial monitor.
delay(1000);: Pauses the program for 1000 milliseconds (1 second) before starting the next iteration of the loop. This delay is added to make the output more manageable and readable.
Gathering the water image gallery