Library functions

Library constructor

To start using Waspmote 4-20 mA library, an object from class currentLoop must be created. This object, called currentLoopBoard, is created inside Waspmote 4-20 mA library and it is public to all libraries. It is used through this guide to show how Waspmote 4-20 mA library works. When creating this constructor, all the variables are defined with an initial value by default.

Switching the board on

The 4-20 mA Board includes a 12 V output that can be used to supply sensors, and can be controlled from the library functions, by a digital pin of Waspmote. The electronic measurement circuits use the 5 V power (so it is always mandatory to switch this option on), and is necessary to switch on this power supply before getting data from the sensors.

On the other hand, it is only necessary to switch the 12 V on when we want to power 3rd party sensors.

Example of use:

{
    // Sets the 5 V switch ON
    currentLoopBoard.ON(SUPPLY5V);
    delay(100);

    // Sets the 12 V switch ON
    currentLoopBoard.ON(SUPPLY12V);
    delay(100);
}

See an example of use here: https://development.libelium.com/4-20-ma-01-current-loop-basic/

Switching the board off

This function can be used to switch OFF the power supplies of the 4-20 mA Board. The 12 V and 5 V power supplies must be switched off separately as shown in the next example.

Example of use:

{
    // Sets the 5 V switch OFF
    currentLoopBoard.OFF(SUPPLY5V);
    delay(100);

    // Sets the 12 V switch OFF
    currentLoopBoard.OFF(SUPPLY12V);
    delay(100);
}

Reading data

The 4-20 mA library includes the necessary functions to read data in several formats. The 4-20 mA standard sends the sensor information as a current, and this information can be transformed in voltage with a simple conversion function.

Example of use:

{
    // Get the sensor value in integer format (0-1023)
    int value = currentLoopBoard.readChannel(CHANNEL1);
    USB.print(“Int value read from channel 1: “);
    USB.println(value);

    // Get the sensor value as a voltage
    float voltage = currentLoopBoard.readVoltage(CHANNEL1);
    USB.print(“Voltage value read from channel 1: “);
    USB.print(voltage);
    USB.println(“V”);

    // Get the sensor value as a current in mA
    float current = currentLoopBoard.readCurrent(CHANNEL1);
    USB.print(“Current value read from channel 1: “);
    USB.print(current);
    USB.println(“mA”);
}

Sometimes, it is necessary to introduce a correction factor to correct offset deviations in the measurement process. The next function can be used for this:

{
    // Get the sensor value as a current in mA
    float current = currentLoopBoard.readCurrent(CHANNEL1, OFFSET);
    USB.print(“Current value read from channel 1: “);
    USB.print(current);
    USB.println(“mA”);
}

See an example of use here: https://development.libelium.com/4-20-ma-01-current-loop-basic/

Current loop state

One of the most important features of the 4-20 mA standard is the possibility of detecting a broken wire or failed instrument. The 4-20 mA library includes a function to detect the current state of the line.

Example of use:

{
 if (currentLoopBoard.isConnected(CHANNEL1))
   {
     // Get the sensor value as a current in mA
     current = currentLoopBoard.readCurrent(CHANNEL1);
     USB.print(“Current value read from channel 1: “);
     USB.print(current, 3);
     USB.println(“mA”);
    }
 else
 {
  USB.println(“The sensor is not connected...”);
 }
}

See an example of use here: https://development.libelium.com/4-20-ma-02-current-loop-connection-state/

Last updated