Coding to map 4-20 mA pressure sensor

Hi,

I have 24VDC pressure sensor with output 4-20mA. My pressure sensor is rated for 4mA for 0 PSIG and 20mA for 20000 PSIG.

I followed the attached picture for the diagram, except I use 220 ohm from my arduino starter kit. I use 24VDC power supply to the sensor.

With the code below:
"Raw" gave me reading between 150-180. This number does not make sense to me.

Can anyone please advise how to code this to get PSIG reading from the sensor (0-20000 PSIG)?

Here is my code:

void setup() {
Serial.begin (9600);
pinMode(A0, INPUT);

}

void loop() {
int raw = analogRead (A0);
Serial.print (raw);
delay (100);
}

Thanks in advance

If you use a 220 Ohm resistor instead of the desired 250 Ohm your voltages would be 0.88 V at 0 PSI (4 mA) and 4.4 V at 20,000 PSI (20 mA). That should get you raw readings from 180 to 901 or about 28 PSI per step.

You have to run the 4 - 20 through a 250 Ohm resistor, that will give you 1 - 5V, connect that to an analog pin with a 27k resistor in series, DO NOT connect the signal DIRECTLY to a pin, if something goes wrong and 24V gets connected to a pin, bye bye Arduino.

Thank you for the replies.

Hi Johnwasser,

void setup() {
Serial.begin (9600);
pinMode(A0, INPUT);

}

void loop() {
int pressure = map(analogRead (A0),0, 1023, 0, 20000));
Serial.print (pressure);
delay (100);
}

Will this map the pressure reading?

Hi 756E6C,
I attached a new diagram. Is this what you suggested?

Thank you

 pinMode(A0, INPUT);This line is not needed for analog inputs.

int pressure =  map(analogRead (A0),0, 1023, 0, 20000));Close. Remember that your input range should be about 180 to 901, not 0 to 1023.

1 Like

Yes, that should work, any time you have a potential voltage greater than VCC + 0.6V, you should have a current limiting resistor in series to limit current below 1mA, in your case, 30V max - 5.6 = 24.6 / 0.0009 Amps = 27.1 k. Suppose the connection from the bottom of your resistor to GND was broken, the sensor, trying to drive 4mA into an open circuit would jack the voltage up to max, near 30V would be applied to A0 but the 27k would limit current to a safe 0.9 mA.

Thank you, It works!!!!