Converting sensor output to 4-20mA

Hi,

I am controlling a pressure sensor using an Arduino Mega 2560.
Arduino behaves as a master and the communication is via RS485 half duplex and the protocol will be Modbus RTU.
The program attached works fine for me.

#include <SimpleModbusMaster.h>
#include <stdio.h>


/////////////////// Port information ///////////////////
#define baud 9600
#define timeout 200                                                                                                                                                                                                    
#define polling 1000 // the scan rate
#define retry_count 5

// used to toggle the receive/transmit pin on the driver
#define TxEnablePin 8 

#define LED 9

// The total amount of available memory on the master to store data
#define TOTAL_NO_OF_REGISTERS 2

// This is the easiest way to create new packets
// Add as many as you want. TOTAL_NO_OF_PACKETS
// is automatically updated.
enum
{
  PACKET1,
  TOTAL_NO_OF_PACKETS // leave this last entry
};

// Create an array of Packets to be configured
Packet packets[TOTAL_NO_OF_PACKETS];

// Masters register array
unsigned int readRegs[4];   //Data read from the slave will be stored in this array 


float pressure;
float thead;
float press;

/calibration parameters
const float c= 0.025;    
const float d= 0.072;
const float e= 0.422;
const float f= 0.431;



void setup()
{
  
  // Initialize each packet
  Serial.begin(9600);
  Serial1.begin(9600);
  
  modbus_construct(&packets[PACKET1], 250, READ_HOLDING_REGISTERS, 0x100, 4, 0);
  modbus_configure(&Serial1, baud, SERIAL_8N1, timeout, polling, retry_count, TxEnablePin, packets, TOTAL_NO_OF_PACKETS,readRegs);   // Initialize the Modbus Finite State Machine
                                                                                                                          
  pinMode(LED,OUTPUT); 
}


void loop()
{
  
  delay (100);
  modbus_update();
  
  digitalWrite(LED, HIGH);  
  delay(100);   
  digitalWrite(LED, LOW);
  
  //Shift and Cast pressure to float   
  unsigned long temp1 = (unsigned long) readRegs[0] << 16 | readRegs[1];   
  pressure = *(float*)&temp1;
  
  digitalWrite(LED, HIGH); 
  delay(100); 
  digitalWrite(LED, LOW);
  
  //Shift and Cast value tempretaure to float   
  unsigned long temp2 = (unsigned long) readRegs[2] << 16 | readRegs[3];
  thead = *(float*)&temp2;
   
 //Calcultion of press
  p = pressure;
  p2 = pow (p,2);
  p3 = pow (p,3);
  
  
  press = c * p3 + d * p2 + e * p + f;
  
  Serial.print("\nRequestspack: ");
  Serial.println(packets[PACKET1].requests);  
  Serial.print("Successful requestspack1: ");    
  Serial.println(packets[PACKET1].successful_requests);
  Serial.print("Failed requestspack1: ");  
  Serial.println(packets[PACKET1].failed_requests);  
  Serial.print("Reg[0]: ");   
  Serial.println(readRegs[0]);  
  Serial.print("Reg[1]: ");  
  Serial.println(readRegs[1]); 
  Serial.print("pressure:");  
  Serial.println (pressure,4);   
  Serial.println("");   // new line 
  Serial.print("Reg[2]: "); 
  Serial.println(readRegs[2]); 
  Serial.print("Reg[3]: "); 
  Serial.println(readRegs[3]);
  Serial.print ("thead: "); 
  Serial.println (thead,4);
  Serial.println("");   // new line
  Serial.print ("press: "); 
  Serial.println (press,4);
  Serial.println("");   // new line
     
}

As you can see, I use the Serial1 for the the sensor's communication and Serial to monitor the sensor's outputs with the pc display.

The next step for me is to convert the format of the outputs 4-20mA. Do you have any suggestions about how to do that from the HW and FW points of view?

Thank you in advance.

Assuming you want the Mega to convert the output to 4-20mA, use something like this...

or like this...

Hi Dave,

Thanks for the links.

I would like to pass from my actual system to the final system you can see attached.
The system has too guaranteed very high accuracy in the the conversion to 4-20mA.

I was wondering if it is better to choose a board with a 16-bit DAC like this:

instead of 14-bit DAC like this:

4-20mA T Click - Add-on board with XTR116 4-20mA transmiter.

What do you think about?

...sorry I made a mistake in posting the links.

Here you are the right ones.

12-bit DAC(your advice):

16-bit DAC:

The 12 - bit transmitter is probably good enough for most applications. You have a 4096-step resolution for a 4 µA step size. Good enough for you?

If you really need the 0.25 µA resolution of the 16-bit version, or need the four channels, and you have the budget, by all means go for it.

Of course a 16-bit transmitter is no use if your receiver is only 10-bit or so.

The sensor I am using is from 0-10bar and it has an accuracy of 0.02%FS.

This means that the maximum variation on the signal is 2mbar. The sensor is powered at 5V so a variation of 2mbar corresponds to 1mV and the converter has to be able to measure this value.

Looking to the resolution of a 12-bit DAC: 5V/2^12=1.22mV....I won't measure the small variation...

For this reason, I was thinking to use a 16-bit DAC. I don't need 4-channels, but I haven't found anything different...

Every suggestions is much appreciated.

You are focussing on getting the maximum resolution out to your system - without answering the question of what resolution you really need for your process to work properly.

If you really need a perfect transmission of your 0.02% resolution, by all means go for the 16-bit one. If a slightly lower resolution is good enough, you can save yourself a nice chunk of money.

And a very important issue that you didn't address: can your receiver even read the signal that accurately?

Thank you for the suggestions.

I will buy the 16-bit DAC, to be sure to have the best transmission I can.