Bath-Biodevices-Without-Borders / Bath-Biodevices-Without-Borders/System

Improve function that read turbidity

Open
#6 0 comments 0 reactions 0 assignees View on GitHub
enhancement
Dominant language
C++
Stars
1
Forks
2
PR merge metrics
No merged PRs in 30d

Description

Improve how the microcontroller reads data from the turbidity sensor. Currently, the function responsible for this task operates like so:

The function takes one parameter: an integer `numberOfReadings` which represents the number of readings to take from the sensor.

The function starts by initializing a floating-point variable `TurbiditySensorVoltage` to 0. This variable will be used to store the voltage reading from the sensor.

Next, the function calculates the number of samples to take by dividing 10000 by the number of readings and rounding up to the nearest integer. This is done using the ceil() function.

The function then enters a loop that iterates samples times. Inside the loop, the function reads the voltage from the sensor by calling `analogRead(turbPin)`. This returns a raw sensor reading between 0 and 1023, which is then converted to a voltage between 0 and 5 volts. The converted voltage is added to `TurbiditySensorVoltage`.

After the loop, the function calculates the average voltage by dividing `TurbiditySensorVoltage` by samples.

The function then rounds the average voltage to six decimal places. This is done by multiplying the voltage by 10^6, rounding to the nearest integer using `roundf()`, and then dividing by 10^6.

Next, the function checks if the average voltage is less than 2.5 volts. If it is, the function returns a turbidity of 3000 NTU (Nephelometric Turbidity Units). This is because the sensor's voltage range is from 2.5 to 4.5 volts, and a voltage less than 2.5 volts corresponds to a turbidity of 3000 NTU.

If the average voltage is not less than 2.5 volts, the function calculates the turbidity in NTU using a quadratic equation. This equation was likely derived from the sensor's datasheet or through calibration. Note that this equation is highly susceptible to environmental changes and can cause the resulting value to fluctuate frequently. This equation therefore needs to be investigated.

The function then adjusts the calculated NTU value using a linear equation. This equation was also likely derived from the sensor's datasheet or through calibration.

Finally, the function checks if the adjusted NTU value is less than 0. If it is, the function sets it to 0. This is because turbidity cannot be negative. The function then returns the adjusted NTU value.

In summary, this function is used to read the turbidity of water using a turbidity sensor. It does this by taking multiple voltage readings from the sensor, averaging them, and converting the average voltage to a turbidity value in NTU.

Contributor guide

No contributing guide indexed for this repository

Research direction

Locate the turbidity-reading function and trace its analogRead(turbPin) sampling and voltage-to-NTU conversion. Check the sensor datasheet and existing calibration assumptions before deciding how the quadratic and linear equations should be evaluated; done means an agreed, validated conversion that handles the stated voltage and non-negative NTU constraints.

Written by the indexing model from the issue text.

Assessment

Tech stack
arduino, cpp
Domain
embedded-iot
Issue type
Refactor
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.