[SOLVED] 4-20 ma sensor reading

Hi to all, i'm new in the Arduino world, and now I must read a value from a 2 wire sensor and display it on a lcd like this http://tronixstuff.wordpress.com/2011/03/12/the-dfrobot-lcd4884-lcd-shield/

actually the only thing that Arduino read is 1023, the code is this:

#include <LCD4884.h>

void setup(){
lcd.LCD_init();
pinMode(7, OUTPUT);
{Serial.begin(9600);
pinMode(A5, INPUT);
analogReference(DEFAULT);
lcd.LCD_write_string(0,1,"Accensione...", MENU_NORMAL);}
delay(500);}

int val = 0;
int analogpin = 5;

char valadc[4] = "0";
char pres[4] = "0";

void loop()
{
val = analogRead(analogpin);
dtostrf(val, 4, 0, valadc);
Serial.println(val);
lcd.LCD_clear();
lcd.LCD_write_string(0,1, "Lettura ADC:", MENU_NORMAL);
lcd.LCD_write_string(0,2, valadc, MENU_NORMAL);
delay (100);
}

and the analog input is taken on a 250ohm resistor in series with the sensor, all the project work with 12V...

thanks to all!

Post a diagram of what is connected where - that way you will get a definitive answer, rather than guesses as to your problem.
Most (industrial standard) 4-20 units require at least 18 volts (preferably 24) to drive them properly.
Even if your problem is a software issue it would be beneficial to see your circuit hook-up diagram.

250 Ohm is very low.
Which sensor are you using ?

Krodal:
250 Ohm is very low.
Which sensor are you using ?

250 ohms is a very popular 'standard' value to use as it converts a 2-wire 4-20ma current loop to a easy to read 1-5vdc range, easily read with a arduino analog input pin. Again thing we need is a detailed wiring drawing of the complete current loop to Arduino including loop voltage source and ground wire details.

Lefty

Thanks for the reply, I've made a "schematic" of the connection:

however, I take current for the sensor from the DC IN of Arduino, and then I simply wire the middle of 2 resistor (sensor and 251 ohm) with one of the analog pin (the A0 pin is used by the monitor however, so I can only use A1-2-3-4-A5)

Thanks for your drawing!

This one is about that:
http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1292955787

This one has a picture, and there is a protection resistor of 10k:
http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1283882387

If the 250 Ohm was not connected by accident, the input pin might have been blown.
Can you add a protection resistor of 10k in series to the analog input, and use a different analog input?

The buffers in your code are too small.
This will mess up your variables on the stack:

char valadc[4] = "0";       // 4 characters
char pres[4] = "0";
...
dtostrf(val, 4, 0, valadc);  // width is 4 plus terminator is 5 !

A string is ended with a string-terminator, also called zero-terminator or '\0'.
I also prefer to initialize the string in the program (but that's just me, ignore it if you like).

During development, send it back as an integer.
You can use floats if that is working.

You need something like this:

char valadc[20];       // buffer, large enough
char pres[20];
...
  if (everything_is_ok)
  {
    val = analogRead(analogpin);
    Serial.println(val, DEC);
    // do some calculation: sensor = ((double) val - offset ) * gain
    dtostrf .....
  }
  else
  {
    strcpy (valadc, "INF");      // "INF", or "not valid", or "0" 
  }
}

Thanks to you for your help!!

I've read the two topics you linked (I had already seen them when looking for a solution to interface the sensor with Arduino), I will add the resistor as you suggested: it must be EXACTLY 10 Kohm or it can be 8Ko or 12Ko (for example)?

This evening I will try to fix this buffer problem, I sensed that something was wrong with DTOSTRF function.

During development, send it back as an integer.
You can use floats if that is working.

Explain this to me please

char valadc[20]; // buffer, large enough
char pres[20];

that's ok

if (everything_is_ok)
{

what is "everything_is_ok"? (what kind of checks I can do to say that's ok or not?)

Thanks a lot for the help!! :slight_smile:

Normally 1k to 10k is used for a protection resistor. Since you use 12V: (12-5) / 1k = 7mA maximum. So 1k would already be enough.

I ment, sending it with Serial.println as an integer.
The return value of analogRead is an integer (it's the raw value of the ADC, 0...1023), so just send that.
Once you get normal readings, you can do the calculation with floats.

The "everything_is_ok" was just an example. If you are able to detect if the sensor is working, you can add this. Nothing special, just something that came to mind.

You see, once you know a little more, it's not that hard.

4-20MA sensors, as mentioned above, usually require a loop power supply of 18 to 24 volts.

Has anyone been successful using a lower voltage like 12 volts? The sensor is powered from the loop, and may not work properly if the applied voltage is too low.

I have some nice low-cost 40 amp Solid-State-Relay form dimmers. They are phase control, but the control is 4-20ma and they have an internal resistance of about 1.2K. So that looks like 24 volts is needed to run them. They look this this:

Any easy solutions?? Adding a 24V supply to a project bumps the cost up quite a bit..

So I can use the first resistor I will find in my lab, that's good.
Yes, to do calculations with the raw value was my first target but when I encountered this "reading obstacles" I got stopped...

Mmm i don't think that there are ways (in this case) to know if it's all ok, but I've understood the hint, thanks!

This evening when I return home I will do some try.

The sensor seems to work properly even with 12 V, but maybe not full range... The try that I can do is to power all the system with something like 15V.... Arduino can support it? (I read that max is 12V, but it can work even with 20V)

P.S.: little job dome with arduino:

P.P.S.: sorry for my english, I only studied it in high school!

terryking228:
Any easy solutions?? Adding a 24V supply to a project bumps the cost up quite a bit..

No, but SSR's like that for 5V control are not very expensive.
Search google for [ssr-25*], they switch on at 3V and cost about 5 dollars.
You can use the output pin of the Arduino to switch such relays.

Hi, Yes those SSRs are good for ON-OFF but this one is a dimmer; different type.. But they LOOK similar..

terryking228:
but this one is a dimmer

Okay, now I understand.

I read that the industry standard is for 24V, although there are also 12V types today.
But with those dimmers, you have to use 24V.

In the Playground is no page for the 4-20mA sensors yet, someone should write it!

Hi, I've checked the code and changed the buffer of the char as suggested:

char valadc[20];
char presadc[20];
char pesoadc[20];
char pres[20];

and all seem to work properly :slight_smile:

Thanks to all for help!

Here a photo of the final work!

P.S.: for moderator, you can close the topic, I've solved my problem.

Well done. :slight_smile:

I think that topics are not closed, but you can change the Subject of your first post (the top post) and add [SOLVED] in front of it.

can some one share the simple code for reading the values 4-20ma output.

AMPS-N:
can some one share the simple code for reading the values 4-20ma output.

int reading = analogRead(A0);

Thanks for needfull help.

retrolefty:

AMPS-N:
can some one share the simple code for reading the values 4-20ma output.

int reading = analogRead(A0);