[4-20mA Sensor][Arduino R4]Troubles reading the voltage with analog read

The "cheat sheet"
https://docs.arduino.cc/tutorials/uno-r4-wifi/cheat-sheet/
says

analogReference(AR_INTERNAL)  (Built in reference of 1.5 V.)

usefully, the "datasheet" does not mention the ADC
https://docs.arduino.cc/resources/datasheets/ABX00087-datasheet.pdf

I'd suggest this circuit
image
which will give 1360mV for 20mA
then use the 1.5V reference as shown above

I've not been able to find any quality data on the ADC or the 1.5V reference

No, but the Renesas datasheet for the controller does. It's buried very deeply though:


https://www.renesas.com/us/en/document/dst/ra4m1-group-datasheet page 95

The reference voltage is 1.43V, give or take 70mV.

Thanks @rsmls

I cant believe this

I'm guessing this denotes the series resistance on the ADC input. This would be a different spec from the e.g. 100Meg input resistance of the ATMega328P.
image

The worst-case combination of the 9pF input capacitance and 6.7k input resistance would give an RC period corresponding to roughly 16MHz. Realistically, this would reduce max sampling frequency to 4MHz or so if large voltage swings are expected. Still sounds reasonably high.

Output impedance of the measured circuit/signal should also be included in the analysis of course.

I agree; but its very misleading to say its the input reistance.
I'm now testing the input resistance of an anlog input on a NANO.
I have the 3v3 pin connected directly to A6, and via a 300M resistor to A0.

image

I'm using the DEFAULT reference. SO the resolution is 5/1024 = 5mV
I'm averaging 1024 readings to improve resolution.
There is NO difference greater than 0.5 LSB between the averaged readings.
So the actual input resistance (if my calcs are right) is
I = dV / 300M and take dV as 5mV so I = 5mV /300M = 0.017 nA
Rin = 3.3V / 0.017nA = 194 Gohm - or more precisely OVER 200G

Yes, it is misleading/unclear/confusing. It's actually weird that there's no agreed-upon definition for a spec like this, as we have for many other ratings/specifications.

Whoot!

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 :confused: )

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:
image

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!

It's worth a shot. It's unclear what causes your outliers, but changing to a more stable reference and also filtering the input of the ADC will definitely remove a couple of potential noise sources. The additional resistor (20k in the diagram above) will moreover add protection and the zener suggested earlier is a good idea, too.

This will be fine.

You can do that as well; one doesn't exclude the other.

Dont miss out the series resistor that is there to protect the ADC.
The 20k and .047 arent critical, you can certainly use the 100nF (0.1uF) cap and a 10k or 100k.
If you stick with the 220 R you will need to use the DEFAULT reference.
Alternatively you could parallel two 100 ohm - to get 50 ohm - and use the INTERNAL reference, which would be better.

There WILL be a reason for this; it could be interference on the adc line - which the filter (R&C) will suppress.
It could also be changes in the "DEFAULT" reference voltage due to supply variation. Try putting your 100uF cap across the 5V & GND of your arduino.

Its better to find and fix the problem than hide it by filtering outliers.

Hello guys,

so today I was finally able to make another test, I used this circuit inspired by @johnerrington

I used 220ohm and 100 ohm so that I would get around 68ohm and then use the 1.5V internal ref, unfortunately the value isn't stable:

22:57:24.347 -> ADC <385> VOLTAGE [V] <0.56> CURRENT [mA] <8.30> DEPTH [cm] <134.43> DEPTH [%] <79>
22:57:25.438 -> ADC <388> VOLTAGE [V] <0.57> CURRENT [mA] <8.37> DEPTH [cm] <136.45> DEPTH [%] <80>
22:57:26.529 -> ADC <385> VOLTAGE [V] <0.56> CURRENT [mA] <8.30> DEPTH [cm] <134.43> DEPTH [%] <79>
22:57:27.577 -> ADC <385> VOLTAGE [V] <0.56> CURRENT [mA] <8.30> DEPTH [cm] <134.43> DEPTH [%] <79>
22:57:28.686 -> ADC <388> VOLTAGE [V] <0.57> CURRENT [mA] <8.37> DEPTH [cm] <136.45> DEPTH [%] <80>
22:57:29.781 -> ADC <384> VOLTAGE [V] <0.56> CURRENT [mA] <8.28> DEPTH [cm] <133.75> DEPTH [%] <78>
22:57:30.839 -> ADC <385> VOLTAGE [V] <0.56> CURRENT [mA] <8.30> DEPTH [cm] <134.43> DEPTH [%] <79>
22:57:31.962 -> ADC <385> VOLTAGE [V] <0.56> CURRENT [mA] <8.30> DEPTH [cm] <134.43> DEPTH [%] <79>
22:57:33.040 -> ADC <385> VOLTAGE [V] <0.56> CURRENT [mA] <8.30> DEPTH [cm] <134.43> DEPTH [%] <79>
22:57:34.146 -> ADC <388> VOLTAGE [V] <0.57> CURRENT [mA] <8.37> DEPTH [cm] <136.45> DEPTH [%] <80>
22:57:35.209 -> ADC <385> VOLTAGE [V] <0.56> CURRENT [mA] <8.30> DEPTH [cm] <134.43> DEPTH [%] <79>
22:57:36.309 -> ADC <385> VOLTAGE [V] <0.56> CURRENT [mA] <8.30> DEPTH [cm] <134.43> DEPTH [%] <79>
22:57:37.361 -> ADC <388> VOLTAGE [V] <0.57> CURRENT [mA] <8.37> DEPTH [cm] <136.45> DEPTH [%] <80>
22:57:38.491 -> ADC <388> VOLTAGE [V] <0.57> CURRENT [mA] <8.37> DEPTH [cm] <136.45> DEPTH [%] <80>
22:57:39.536 -> ADC <388> VOLTAGE [V] <0.57> CURRENT [mA] <8.37> DEPTH [cm] <136.45> DEPTH [%] <80>

What do you guys think? What could be the reason for this instability?

I was also wondering if I could use the 24V DC power supply used for the 4-20mA sensor to BOTH power the Arduino and the sensor. The one I'm using is this one: (45W)

An LED power supply is not suitable. For stable results you need a regulated power supply.
Have you looked at the specification for that unit? can you give a link to the data sheet?

In principle this works if you use a DC-DC step down buck converter to make e.g. 5V for the Arduino. Do not feed 24VDC into the Arduino board directly.

Depicted above in #30 is a regular SMPS. Note the output spec; it's not a constant current supply.

An LED power supply is not suitable. For stable results you need a regulated power supply.
Have you looked at the specification for that unit? can you give a link to the data sheet?

Unfortunately I've failed to find a datasheet for the power supply on the web, here a zoomed image:
image

and here the brand website which unfortunately isn't even fully in English NUOVA GERMANY Alimentatore Professionale 1.8A DC 24V Striscia Led Tras

the one I've got is the 45W version. Do you think the unstable reading is due to the power supply?

Thanks, I'll use a phone charger 5V 1A

There's no reason to suspect this power supply. It outputs 24VDC.

The problem with (other) LED power supplies is that they're generally constant current sources. This means they'll adjust their voltage (within a certain range) to keep the current through the load constant.

Your unit is not one of these; it's a regular SMPS with a fixed output voltage that just happens to be intended for use with e.g. LED strips.

That should work just fine, too.

Do you mean: constant voltage or: direct current??

1 Like

I meant exactly what I wrote. Current source - Wikipedia
I might have written just 'current source', but added the redundant 'constant' to emphasize that a typical LED driver holds current constant, by allowing voltage to vary. They are also DC (typically), and emphatically NOT constant voltage.

From Wikipedia: “An ideal current source generates a current that is independent of the voltage changes across it.”

What we want for our electronic devices is a voltage source that is independent of the current it delivers.

The difference is very important.

First of all: if it were a constant current source it would be a unique apparatus, worth mentioning explicitly.

Second: a constant current source would be useful to drive 1 LED or a number of LEDs in series, in which case the voltage is undetermined. (1.8A for a LED? Really?)

Third: it would be utterly unuseful for powering the circuit as shown in this thread.

Apparently I read the description differently than you do.

The first denomination is voltage, with a sign that indicates that is DC, not a well stabilized DC, but LEDs work well enough with that.

The second denomination is the maximum current it can deliver. (The actual current is determined by the load.)

This is confirmed by the description on the website, as far as I can read Italian.

Nuova DCsupply

1 Like

Not sure why you decided to use a vref of 1.5V or stick a big 10K resistor in there but both will make your system more susceptibleto to noise.
You should use the 5V reference and see post #8

Not sure, but I have a feeling you didn't read my posts above well. Go through them again.

Hi @jim-p

Didnt think this needed explaining further.

1: The 10k resisitor is to protect the input if the arduino supply fails. Also with the cap acts as a filter to reduce noise at the analog pin input.
(provided its connected properly across A0 and GND at the arduino)

2: we dont know the spec of the "5V" supply to the arduino is - so the instability COULD be due to an unstable reference voltage, if the "DEFAULT" reference is used. We overcome this by using a reference that is more stable - the INTERNAL reference.

and @rsmls - generally LED driver supplies are not regulated or filtered to the same standard as "proper" bench power supplies; they dont need to be for that application.

The output "spec" says DC 24V 1.8A
with no indication of regulation, ripple etc.
DC means the current only flows one way; not that the voltage never varies. and 1.8A also doesnt mean its a constant current supply.