# Read (digital/analog)values out of 12-24 volts(50mA) pressure sensor

Good day, everyone!

I've been wanting to read values out of industry standard sensors for quite some time now and finally I got my hand on a DP2-20 Pressure Sensor.

I searched on interfacing with 24v sensors here in the forum and found no luck in finding a schematic diagram I could use. (There are links to a 'picture' but it was an error404)

This is the post most relevant to my topic: (convert my reading to 0-5V) Interfacing with 24v 4-20ma analog devices

Sir @deSilva says here:

You place a 220 ohms resistor between + and - of the line current. Connect - to ground and + through a 10k resistor to an analog pin.

and Sir @Grumpy_Mike explains here how to get the 220:

Its is a sensor that produces current from a minimum of 4mA to a maximum of 20mA. When it is at its maximum you want the arduino to see 5V. So ohms law says that if you have 20mA through a 220R resistor then it has to have a voltage of 20 * 220 = 4400 mV or 4.4V across it.

Here is what I think the connection should look like based on the description:

and this is how I computed R1: 50 * 100 = 5000 mV or 5V ~ R1=100

Are my assumptions correct? :drooling_face: If so, could you help me read the digital output of the sensor?

I'm using arduino UNO and SUNX DP2-20 Digital Pressure Sensor.

After quickly looking through the manual, it appears that the DP2-20 pressure sensor is not the 4-20mA type, it has a terminal designated "Analog Voltage Output" and shows 1-5V, so the conversion to 5V signal is already done in the sensor.

Note that the 1-5V signal is compatible with the Arduino, but you will loose the lower 0-1V or 20% of the ADC range. In the Arduino sketch, you could use the map function to scale the reading as desired, but note that the resolution (mV/step in ADC reading) will be 20% higher than a signal with 0-5V input range.

If you need full ADC resolution in your project, then a single op-amp could be added that level-shifts (to 0-4V) then amplifies the signal to 0-5V.

Please check the output signal to ensure that it's already compatible with a 5V system (Arduino).

Thank you Sir dlloyd!

Here is the Note from the datasheet:

``````The analog voltage output is not incorporated with a short-circuit
protection circuit. Do not directly connect a power supply or a
capacitive load. When using the analog voltage output, take
care to connect external equipment of proper input impedance.
Also, when a cable extension is used, voltage drop due to cable
resistance should be taken into account
``````

This is the code I’m currently using. I’m just converting the signal to volts. I’m still searching on how to compute for the kpa.

``````int sensorPin  = A0;

void setup()
{
Serial.begin(9600);
}

void loop()
{
int val = 0;
for(int i = 0; i < 50; i++){
delay(1);
}
val /=50;
float volts = (val/1024.0)*5;
Serial.print(val);
Serial.print(" - ");
Serial.println(volts);
delay(500);
}
``````

and here is the serial output:

``````527 - 2.57
519 - 2.53
479 - 2.34
557 - 2.72
443 - 2.16
493 - 2.41
492 - 2.40
431 - 2.10
534 - 2.61
434 - 2.12
``````

If you need full ADC resolution in your project, then a single op-amp could be added that level-shifts (to 0-4V) then amplifies the signal to 0-5V.

Could you recommend what op amp to use?

Also, I want to read comparative output (NPN open-collector transistor) of the sensor. It says on the datasheet that it has a maximum sink of 100mA. Should I use a protector here? or plug directly to the arduino?

Sorry for the questions, this is the first pressure sensor I laid hands on.

Also, I want to read comparative output (NPN open-collector transistor) of the sensor. It says on the datasheet that it has a maximum sink of 100mA. Should I use a protector here? or plug directly to the arduino?

For reading the analog voltage output of the sensor (1-5V), R2 as shown in your diagram is not required and is probably overloading this signal. Also, R1 is only providing providing a load on the 24V supply ... where did you get the calculation or requirement for this resistor?

For connecting the NPN comparative output, you can connect directly to a digital input on the Arduino configured with INPUT_PULLUP.

For reading the analog voltage output of the sensor (1-5V), R2 as shown in your diagram is not required and is probably overloading this signal. Also, R1 is only providing providing a load on the 24V supply … where did you get the calculation or requirement for this resistor?

I removed the resistor and added INPUT_PULLUP in my code.

Circuit Setup:

Current Code:

``````int analogPin  = A0;
int digitalPin1 = 12;
int digitalPin2 = 11;

void setup()
{
Serial.begin(9600);

pinMode(analogPin, INPUT);
pinMode(digitalPin1, INPUT_PULLUP);
pinMode(digitalPin2, INPUT_PULLUP);
}

void loop()
{
int in = 0;
for(int i = 0; i < 50; i++){
delay(1);
}
in /=50;

float p = (in/1024.0)*5;

Serial.print(in);
Serial.print(" - ");
Serial.println(p);

Serial.print("1: ");
Serial.println(out1);
Serial.print("2: ");
Serial.println(out2);

delay(100);
}
``````

Serial Display:

``````218 - 1.06
1: 1
2: 0
218 - 1.06
1: 1
2: 0
218 - 1.06
1: 1
2: 0
218 - 1.06
1: 1
2: 0
218 - 1.06
1: 1
2: 0
218 - 1.06
1: 1
2: 0
218 - 1.06
1: 1
2: 0
``````

The reading is now stable. Thank very much Sir dlloyd!

Before I add the op amp that level-shifts (to 0-4V) then amplifies the signal to 0-5V, can you help me compute the pressure?

I can’t find a computation in the datasheet so googled voltage to pressure conversion and found this site → processonline.com

If P = (101kPa / 12V) * ( analogReading / k), where can I find k?

The DP2-20 Pressure Sensor's analog output is 1 to 5V over rated pressure range (0-101.3kPa) and the Arduino 10-bit ADC's range is 0-1023. Its just a direct linear relationship where:

``````[glow=teal,2,300]Pressure (kPa)    Analog Volts    Arduino ADC[/glow]
0.000                1            205
25.325                2            409
50.650                3            614
75.975                4            818
101.300                5           1023
``````

The data sheet gives conversion factors for other pressure units.

Your results indicate that no pressure was applied (218 reading). If so, the sensor could be calibrated using it's zero-point adjustment so that the Arduino reads 205.

With this direct connection, you have 818 ADC steps = 6.11mV per step. With an op-amp, there would be 1023 ADC steps = 4.89mV per step (20% improved resolution).

If the resolution is OK as is (6.11mV per step), you could use Arduino's map() function to convert (map) the range to 0-1023 and you won't need to use an external op-amp.

Woah! :astonished: that's a lot of information

Ok, so..

For the table:

0-101.3kPa is the pressure range 1-5volts is the analog output, and in 205-1023 the 205 is calculated using 1V=(analogReading / 1024.0) * 5.

To get the ADC steps --> 1023 - 205 = 818

To get the voltage per step --> 5V / 818 steps = 6.11mV

You're good at this. Thank you for making me understand! :)

I uploaded this code for the voltage to pressure conversion:

``````void setup() {
Serial.begin(9600);
}

void loop()
{
val = map(val, 219, 1023, 0, 101.3);

Serial.println(val);
}
``````
• I used 219 instead of 205 because 219 is where my sensor displays zero --even if I calibrate the sensor.

Serial output:

``````-3
-3
-3
-3
-3
``````

The only problem is it only outputs integer values. Says here:

"The map() function uses integer math so will not generate fractions, when the math might indicate that it should do so. Fractional remainders are truncated, and are not rounded or averaged."

or am I computing it wrong?

``````void setup() {
Serial.begin(9600);
}

const float kPaConstant = 101.3 / 1023;
float kPa = 0.0;

void loop()
{
kPa = map(analogRead(0), 219, 818, 0, 1023) * kPaConstant;

Serial.println(kPa, 4);
}
``````

Thank you, again, for the input!

I tried your code :) Can I ask, why fromLow:219 and fromHigh:818 in map(analogRead(0), 219, 818, 0, 1023)?

Note that the 1-5V signal is compatible with the Arduino, but you will loose the lower 0-1V or 20% of the ADC range. In the Arduino sketch, you could use the map function to scale the reading as desired, but note that the resolution (mV/step in ADC reading) will be 20% higher than a signal with 0-5V input range.

but the 0.0-0.8 has a very weird reading maybe it's because I use 218 instead of 205.

``````Sensor Reading   vs   Serial Output

-0.0                         -0.5

0.0                         -0.3

0.4                          0.1
0.5                          0.3
0.6                          0.5
0.7                          0.6
0.8                          0.8

1.6                          1.8
2.3                          2.5
3.5                          4.4
4.0                          5.1
``````

What op amp should I use?

To me, the output looks OK. It's only off 0.5 count. Notice that the serial output climbs up at a higher rate than the corresponding sensor reading. The original values for map were to just simulate what you would get if an op-amp were used and it's output used the full 0-5V range of the ADC.

If you would like to match the readings as connected, use:

Level shift only:

``````kPa = map(analogRead(0), 205, 818, 0, (1023-205)) * kPaConstant;
``````

Level shift with offset:

``````kPa = map(analogRead(0), 219, 818, 0, (1023-205)) * kPaConstant;
``````

There are many possible op-amps that could be used. It should be 5V rail-rail type, however I couldn't recommend one as I've never used this type. I would try getting matched readings and see how it works under higher pressures first. You may find that an op-amp isn't required.

EDIT:

``````const float kPaConstant = 101.3 / (1023-205);
``````

Ok. I’ll give you an update after I test it.

Thank you very much!