Good news!
Since I had no idea how to solve the problem I did a remake of circuit from 0 using new cables and guess what... it now works!
This is the code I've used to do some readings:
#define ANALOG_PIN A2
#define SENSOR_RANGE 500.0 // Depth measuring range 500cm (for water)
#define VREF 5.0 // ADC's reference voltage
#define CURRENT_INIT 4.0 // Current @ 0m in mA
#define MAX_DEPT 200.0 // cm
#define RESISTOR 220.0 // ohm
void setup() {
Serial.begin(9600);
pinMode(ANALOG_PIN, INPUT);
}
void loop() {
getDeptPercentage();
delay(1000);
}
int getDeptPercentage() {
int16_t sensor_adc = analogRead(ANALOG_PIN);
float voltage = sensor_adc * (VREF / 1023.0); // in V
float current = (voltage / RESISTOR) * 1000; // in mA
float depthCentimeters = (current - CURRENT_INIT) * (SENSOR_RANGE / 16.0); // 20ma - 4am = 16mA
if (depthCentimeters < 0.0)
depthCentimeters = 0.0;
int16_t depth_percentage = (depthCentimeters / MAX_DEPT) * 100;
Serial.print("ADC <");
Serial.print(sensor_adc);
Serial.print("> VOLTAGE [V] <");
Serial.print(voltage);
Serial.print("> CURRENT [mA] <");
Serial.print(current);
Serial.print("> DEPTH [cm] <");
Serial.print(depthCentimeters);
Serial.print("> DEPTH [%] <");
Serial.print(depth_percentage);
Serial.println(">");
return depth_percentage;
}
here some output:
22:07:18.268 -> ADC <373> VOLTAGE [V] <1.82> CURRENT [mA] <8.29> DEPTH [cm] <133.96> DEPTH [%] <66>
22:07:19.325 -> ADC <373> VOLTAGE [V] <1.82> CURRENT [mA] <8.29> DEPTH [cm] <133.96> DEPTH [%] <66>
22:07:20.440 -> ADC <372> VOLTAGE [V] <1.82> CURRENT [mA] <8.26> DEPTH [cm] <133.26> DEPTH [%] <66>
22:07:21.517 -> ADC <373> VOLTAGE [V] <1.82> CURRENT [mA] <8.29> DEPTH [cm] <133.96> DEPTH [%] <66>
22:07:22.587 -> ADC <372> VOLTAGE [V] <1.82> CURRENT [mA] <8.26> DEPTH [cm] <133.26> DEPTH [%] <66>
22:07:23.700 -> ADC <373> VOLTAGE [V] <1.82> CURRENT [mA] <8.29> DEPTH [cm] <133.96> DEPTH [%] <66>
22:07:24.762 -> ADC <373> VOLTAGE [V] <1.82> CURRENT [mA] <8.29> DEPTH [cm] <133.96> DEPTH [%] <66>
22:07:25.885 -> ADC <375> VOLTAGE [V] <1.83> CURRENT [mA] <8.33> DEPTH [cm] <135.35> DEPTH [%] <67>
22:07:26.972 -> ADC <373> VOLTAGE [V] <1.82> CURRENT [mA] <8.29> DEPTH [cm] <133.96> DEPTH [%] <66>
22:07:28.064 -> ADC <373> VOLTAGE [V] <1.82> CURRENT [mA] <8.29> DEPTH [cm] <133.96> DEPTH [%] <66>
22:07:29.111 -> ADC <374> VOLTAGE [V] <1.83> CURRENT [mA] <8.31> DEPTH [cm] <134.65> DEPTH [%] <67>
22:07:30.199 -> ADC <373> VOLTAGE [V] <1.82> CURRENT [mA] <8.29> DEPTH [cm] <133.96> DEPTH [%] <66>
22:07:31.325 -> ADC <373> VOLTAGE [V] <1.82> CURRENT [mA] <8.29> DEPTH [cm] <133.96> DEPTH [%] <66>
22:07:32.404 -> ADC <372> VOLTAGE [V] <1.82> CURRENT [mA] <8.26> DEPTH [cm] <133.26> DEPTH [%] <66>
22:07:33.498 -> ADC <372> VOLTAGE [V] <1.82> CURRENT [mA] <8.26> DEPTH [cm] <133.26> DEPTH [%] <66>
(I hate the fact that in Arduino IDE something as simple as copying and pasting the serial monitor output doesn't work well )
I've used the same circuit that I posted at the beginning:
At the moment I don't have a 68 ohm resistor, I only have 10 ohm, 100 ohm, 220 ohm, 1K ohm, 10K ohm, 100K ohm, 1M ohm so I'm still using the 5V reference.
Sometimes I get some outliers, for example:
22:08:56.125 -> ADC <372> VOLTAGE [V] <1.82> CURRENT [mA] <8.26> DEPTH [cm] <133.26> DEPTH [%] <66>
22:08:57.233 -> ADC <392> VOLTAGE [V] <1.92> CURRENT [mA] <8.71> DEPTH [cm] <147.15> DEPTH [%] <73>
22:08:58.327 -> ADC <371> VOLTAGE [V] <1.81> CURRENT [mA] <8.24> DEPTH [cm] <132.57> DEPTH [%] <66>
Do you guys think the circuit posted by @johnerrington:
could cut them out thanks to the 1.5V reference and the capacitor? (I don't have a 0.047uF capacitor, at the moment I've: 22pF ceramic, 100nF ceramic, 10uF electrolytic, 100uF electrolytic)
I was thinking to do it via software by collecting N values and get the average in order to reduce the spikes
Sorry for the long post, thanks all for the help!