I have a simple conductivity sensor setup for measuring soil moisture. The sensor uses this circuit: https://www.faludi.com/2006/11/02/moisture-sensor-circuit/
I’m using an Arduino Nano and one of the .96 OLed screens. I have the Nano configured to use A2 as the sensor input and A4 & A5 as SDA & SDL to drive the screen.
The strange part is that when I have the sensor pins apart, the Arduino reads a strange pattern like so (serial output):
Raw: 0
24%
Raw: 626
32%
Raw: 297
31%
Raw: 0
25%
Raw: 566
31%
Raw: 474
34%
Raw: 0
The Raw reading is the data from analogRead(), the percentage is from a mapping 0-1023 to 0-100
Any idea why the sensor reads like this?
I have a feeling that there’s something wrong with the code that I can’t see.
Steve
Adding full code.
#include <Adafruit_ssd1306syp.h>
#include <math.h>
#define SDA_PIN A4
#define SCL_PIN A5
const int analogInPin = A2;
float sensorValue = 0;
int moist = 0;
Adafruit_ssd1306syp display(SDA_PIN,SCL_PIN);
void setup() {
Serial.begin(9600);
delay(1000);
display.initialize();
}
float readMoisture(){
float tmpVal=0;
for (int i=0; i <= 25; i++){
tmpVal+=analogRead(analogInPin);
delay(10);
}
return (tmpVal / 25);
}
void loop() {
// put your main code here, to run repeatedly:
sensorValue = readMoisture();
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(20,0);
display.println("Moisture");
display.setTextColor(WHITE);
display.setCursor(38,18);
display.println("Level");
Serial.print("Raw: ");
Serial.println(sensorValue);
moist=map(sensorValue, 0, 1023, 0, 100);
Serial.print(moist);
Serial.println("%");
if (moist < 10) {
display.setCursor(48,40);
}else if(moist < 100){
display.setCursor(40,40);
}else{
display.setCursor(32,40);
}
display.print(moist);
display.println("%");
display.update();
delay(1000);
display.clear();
}