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:
1
{
2
// Sets the 5 V switch ON
3
currentLoopBoard.ON(SUPPLY5V);
4
delay(100);
5
6
// Sets the 12 V switch ON
7
currentLoopBoard.ON(SUPPLY12V);
8
delay(100);
9
}
Copied!

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:
1
{
2
// Sets the 5 V switch OFF
3
currentLoopBoard.OFF(SUPPLY5V);
4
delay(100);
5
6
// Sets the 12 V switch OFF
7
currentLoopBoard.OFF(SUPPLY12V);
8
delay(100);
9
}
Copied!

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:
1
{
2
// Get the sensor value in integer format (0-1023)
3
int value = currentLoopBoard.readChannel(CHANNEL1);
4
USB.print(“Int value read from channel 1:);
5
USB.println(value);
6
7
// Get the sensor value as a voltage
8
float voltage = currentLoopBoard.readVoltage(CHANNEL1);
9
USB.print(“Voltage value read from channel 1:);
10
USB.print(voltage);
11
USB.println(“V”);
12
13
// Get the sensor value as a current in mA
14
float current = currentLoopBoard.readCurrent(CHANNEL1);
15
USB.print(“Current value read from channel 1:);
16
USB.print(current);
17
USB.println(“mA”);
18
}
Copied!
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:
1
{
2
// Get the sensor value as a current in mA
3
float current = currentLoopBoard.readCurrent(CHANNEL1, OFFSET);
4
USB.print(“Current value read from channel 1:);
5
USB.print(current);
6
USB.println(“mA”);
7
}
Copied!

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:
1
{
2
if (currentLoopBoard.isConnected(CHANNEL1))
3
{
4
// Get the sensor value as a current in mA
5
current = currentLoopBoard.readCurrent(CHANNEL1);
6
USB.print(“Current value read from channel 1:);
7
USB.print(current, 3);
8
USB.println(“mA”);
9
}
10
else
11
{
12
USB.println(“The sensor is not connected...);
13
}
14
}
Copied!
Last modified 1yr ago