Last active
July 18, 2020 16:03
-
-
Save adegard/72df137eeb38394eff8eca59d3128bd7 to your computer and use it in GitHub Desktop.
Soil Moisture sensor calibration Arduino ESP 32
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
// Calibration Capacitive Soil Moisture Sensor with ESP32 | |
// use Serial Plotter of Arduino per determinare wet/dry values | |
// Reduse sensor tension to 1,63 volts : I used 2 resistors of 100 Ohms see diagram in post | |
// https://forum.micropython.org/viewtopic.php?t=4487 | |
// 2020 | |
*/ | |
#include <Arduino.h> //for vscode | |
#include <SPI.h> | |
#include <Wire.h> | |
const int AirValue = 770; //you need to replace this value with Value_1 | |
const int WaterValue = 840; //you need to replace this value with Value_2 | |
const int SensorPin = 32; | |
int soilMoistureValue = 0; | |
int soilmoisturepercent=0; | |
int numMeasure = 10; // Number of measurements to average. | |
// This function reads the soil moisture sensor numAve times and returns the average. | |
long readSoil(int numAve) | |
{ | |
long ADCValue = 0; | |
for ( int i = 0; i < numAve; i++ ) { | |
delay(250); // Wait milliseconds for sensor to settle. | |
ADCValue += analogRead( SensorPin ); // Read the value from sensor. | |
} | |
ADCValue = ADCValue / numAve; | |
soilmoisturepercent = map(ADCValue, AirValue, WaterValue, 0, 100); | |
//return ADCValue; | |
if(soilmoisturepercent > 100) { soilmoisturepercent =100;} | |
if(soilmoisturepercent < 0) { soilmoisturepercent =0;} | |
return soilmoisturepercent; // Return the moisture value. | |
} | |
void setup() { | |
Serial.begin(115200); // open serial port, set the baud rate to 9600 bps | |
} | |
void loop() | |
{ | |
soilMoistureValue = readSoil( numMeasure ); //put Sensor insert into soil | |
Serial.println(soilMoistureValue); | |
delay(250); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment