I’ve had a hydroponic plant for two years now, and everything went well until this summer. The new flowers, that were supposed to be pink, were green with a few shades of pink. So I searched on Google and I found out that the optimal water pH value for my plant is from 5.5 and 5.8. Well, guess what? I was using tap water for the plant and its pH is 7! So, I decided to buy a pH corrector solution and build my own system to monitor the pH of the water by exploiting the tiny machine learning.Setup
For this project I decided to use Arduino Nano 33 BLE Sense as the central unit, since I've used it in the HarvardX TinyML course. The circuit is simple: a pH sensor (with a water pH probe connected with it) is wired to an Arduino analog input and 3 leds are driven as digital outputs. After I uploaded the sketch to the Arduino, I had to calibrate the sensor. I dipped a pH indicator paper in a glass of tap water to know its pH, and it was 7. Then, I dipped the probe in the same glass and I adjusted the sensor trimmer close to the probe connector until I read values around 7 on the serial monitor. For the specifc sensor I've used, it was usefull adjust also the trimmer close to the sensor pins, to improve the sensibility. For this project I chose three pH edge values:
- pH 4: pH values lower then 5 is too low for the plant
- pH 5: pH values from 5 to 6 are optimal for the plant
- pH 7: pH values around 7 is too high for the plant
To have a solution with pH 4, I've put some lemon juice in the glass of tap water and then I've measured the pH with the indicator paper to verify that it was around 4, so I dipped the probe in the glass and it sensed values around 4, that made me sure that the sensor was well calibrated. For the pH 5 solution, I took another glass of tap water and I put in it a pH corrector to reduce its pH. Both the indicator paper and the pH sensor showed a value around 5, so I decided to use this solution for the hydroponic plant. The three leds in the circuits indicates the pH level:
- red led: turns on when pH value is around 4
- green led: turns on when pH value is around 5
- yellow led: turns on when pH value is around 7
To train the tinyML model I've used the Edge Impulse platform. I've trained the model based on the three edge values, for each of them I've acquired data for five minutes, in order to cover most of the little fluctuations around the value.
After the data collection I've proced to the impulse design, adding the functional blocks I needed.
The "Time series data" block splits the collected data in several windows of the selected size (in this case 2000ms). The "Spectral Analysis" block filters the signal and then performs the spectral analysis. The "Classification" block is the neural network that uses the results of the spectral analysis to learn how to discriminate between the three datasets. To create the impulse, I've clicked on "Save Impulse", then went to "Spectral Features" on the left menu to see the spectral analysis results and clicked on "Save Parameters".
After that, a new window appeared that allowed to generate the features for the neureal network.
Then, by clickink on "NN Classifier" on the left menu, I entered in the neural network configuration enviroment. I've chose to train the network for 500 epochs, in order to achieve a satisfactory level of model accuracy.
The neural network was able to distinguish between the datasets, so I could proced to the next step: add th "Anomaly Detection" block to the impulse design, that allows to classify as anomalies data that the neural netwotk can't identify.
The "Anomaly Detection" section on the left menu allowed me to select the features of which I wanted to detect the anomalies.
To test the model I've proced to the live classification and I've collected data for five minutes, from the ph 5 solution. The model was able to identify that the collected data belonged to the ph 5 dataset. Since the sensed values were included in the datasets, no anomaly was detected.
The model has proven reliable, so I've deployed it to the Arduino. The "Deployment" section of the left menu helped me to do that. I could chose to deploy the impulse as a library or as a firmware, I've decided to create an Arduino library, so I could included it into my sketch.
On the Arduino IDE
To include the model library in the Arduino IDE, I went on Sketch -> Include Library -> Add.ZIP, then I include the library in my sketch and add few lines to the code in order to run the model to the Arduino Nano 33 BLE Sense.