My Cart

4-20 mA Sensor Board Tutorial for Arduino, Raspberry Pi and Intel Galileo

Difficulty Level: Intermediate -


The 4-20 mA Current Loop Board is one of the most robust sensor signaling standard. Current loops are ideal for data transmission because of their inherent insensitivity to electrical noise. In a 4-20 mA current loop, all the signaling current flows through all components; the same current flows even if the wire terminations are less than perfect. All the components in the loop drop voltage due to the signaling current flowing through them. The signaling current is not affected by these voltage drops as long as the power supply voltage is greater than the sum of the voltage drops around the loop at the maximum signaling current of 20 mA.

Transmitting sensor information via a current loop is particularly useful when the information has to be sent to a remote location over long distances (500 meters, or more). The loop operation is straightforward: a sensor output voltage is first converted to a proportional current, with 4 mA normally representing the sensor zero-level output, and 20 mA representing the sensor full-scale output. Then, a receiver at the remote end converts the 4-20 mA current back into a voltage which in turn can be further processed by a computer or display module.

4-20 mA Current Loop
Type Analog
Media Twisted pair        
No. of channels        4
Distance 900 m
Supply 5-24V

This list includes some of the more common uses of the standard:

  • Sensors and Instruments
  • Remote Transducers
  • Monitoring Processes
  • Data transmission in industrial ambients

1. Features

The 4-20 mA Board has 4 input channels which allow converting signals that come with this industrial standard. This shield works with type 2, 3 and 4 current transmitters. Each channel can be configured for working with these types of connections. If the jumper is retired, the receptor will be configured as float (type 4 connection).

Electrical Features

The 4-20 mA Board includes a 24 V output that can be used to supply sensors. The majority of 4-20 mA sensors work in the 9-24 V range. Before connecting a sensor to the 4-20 mA Board, you must be sure that the sensor is in this range of power supply. If you are going to use all channels, the consumption of the board will increase. Is important to know, that 4-20 mA Board is not designed to supply industrial devices such as PLC's and thermostats.

2. The board

The 4-20 mA PCB

The 4-20 mA Board over Arduino

Get Arduino

Get the 4-20 mA Sensor Board

The 4-20 mA Board over Raspberry Pi

Get Raspberry Pi

Get the Raspberry Pi to Arduino shields connection bridge

Get the 4-20 mA Board

Warnings:

  • The 4-20 mA Board is not designed to supply industrial devices such as PLC's and thermostats. It must be used only to supply sensors.
  • If you are going to use the 4-20 mA Board with Raspberry Pi, you must supply your sensors with your own power supply.
  • The 4-20 mA board uses the digital 5 pin to enable/disable the 24 V output.
  • In Arduino, the analog pins 5 and 4 are used by the i2c bus, so it is not possible to use them at the same time.
  • In Raspberry Pi, the analog pins 5 and 4 are used by the i2c bus, and therefore by the ADC, so it is not possible to use them at the same time and it is necessary to bend the A4 and A5 pins on the 4-20mA card away and reconnect these to the A4 and A5 pins on the connection bridge, which are located on the separate vertical connector.

3. The Library

The 4-20 mA Board counts with a C++ library that lets you manage it in a simple way. This library offers an simple-to-use open source system.

In order to ensure the same code is compatible in all the platforms (Arduino, Raspberry Pi and Intel Galileo) we use the ArduPi libraries which allows developers to use the same code. Detailed info can be found here:

Using the library with Arduino

The 4-20 mA Board includes a high level library functions for an easy manage. Before start using this functions you should download the library. This zip includes all the files needed in several folders.

Download the 4-20 mA library for Arduino

Libraries are often distributed as a ZIP file or folder. The name of the folder is the name of the library. Inside the folder will be the .cpp files, .h files and often a keywords.txt file, examples folder, and other files required by the library.

To install the library, first quit the Arduino application. Then uncompress the ZIP file containing the library. For installing libraries , uncompress zip file. It should contain three folder called _4_20mA. Drag these folders into this folder (your libraries folder). Under Windows, it will likely be called "My Documents\Arduino\libraries". For Mac users, it will likely be called "Documents/Arduino/libraries". On Linux, it will be the "libraries" folder in your sketchbook.

The library won't work if you put the .cpp and .h files directly into the libraries folder or if they're nested in an extra folder. Restart the Arduino application. Make sure the new library appears in the Sketch->Import Library menu item of the software.

Using the library with Raspberry Pi

The 4-20 mA library for Raspberry Pi requires the ArduPi library and both libraries should be in the same path.

Download the 4-20 mA library for Raspberry Pi

Creating a program that uses the library is as simple as putting your code in this template where it says "your Arduino code here"

Code:
//Include 4-20 mA library (it includes arduPi)



/********************************************************
*  IF YOUR ARDUINO CODE HAS OTHER FUNCTIONS APART FROM  *
*  setup() AND loop() YOU MUST DECLARE THEM HERE        *
* ******************************************************/

/*************************
* YOUR ARDUINO CODE HERE *
*************************/

int main (){
  setup();
  while(1)
  {
    loop();
  }
  return(0);
}
    

Compilation of the program can be done in two ways:

  • Compiling separately the 4-20 mA and the arduPi libraries, and using them for compiling the program in a second step:
g++ -c arduPi.cpp -o arduPi.o
g++ -c currentLoop.cpp -o currentLoop.o
g++ -lpthread -lrt user-currentLoop-app.cpp arduPi.o currentLoop.o -o user-currentLoop-app
    
  • Compiling everything in one step:
g++ -lpthread -lrt user-currentLoop-app.cpp arduPi.cpp currentLoop.cpp -o user-currentLoop-app
    

Executing your program is as simple as doing:

sudo ./user-currentLoop-app
    

Using the library with Intel Galileo

The 4-20 mA Board includes a high level library functions for an easy manage. Before start using this functions you should download the library. This zip includes all the files needed in several folders.

Download the 4-20 mA library for Intel Galileo

Libraries are often distributed as a ZIP file or folder. The name of the folder is the name of the library. Inside the folder will be the .cpp files, .h files and often a keywords.txt file, examples folder, and other files required by the library.

To install the library, first quit the Arduino application. Then uncompress the ZIP file containing the library. For installing libraries , uncompress zip file. It should contain three folder called _4_20mA. Drag these folders into this folder (your libraries folder). Under Windows, it will likely be called "My Documents\Arduino\libraries". For Mac users, it will likely be called "Documents/Arduino/libraries". On Linux, it will be the "libraries" folder in your sketchbook.

The library won't work if you put the .cpp and .h files directly into the libraries folder or if they're nested in an extra folder. Restart the Arduino application. Make sure the new library appears in the Sketch->Import Library menu item of the software.

General 4-20 mA functions

General functions

  // The constructor of the class
  currentLoop();

  // Powers ON the 4-20 mA 24V Supply
  void ON(void);

  // Powers OFF the 4-20 mA 24V Supply
  void OFF(void);

  // Get the sensor value in integer format
  int readChannel(uint8_t channel);

  // Get the sensor value as a voltage
  float readVoltage(uint8_t channel);

  // Get the sensor value as a current in mA
  float readCurrent(uint8_t channel);

  // Get the sensor value as a current in mA and introduce
  // a correction offSet factor
  float readCurrent(uint8_t channel ,float offSet);

  // It checks if the current loop is well connected
  uint8_t isConnected(uint8_t channel);
    

4. Connecting Sensors

The majority of 4-20 mA sensors work in the 9-24 V range. Before connecting a sensor to the 4-20 mA Board, you must be sure that the sensor is in this range of power supply. If you are going to use all channels, the consumption of the board will increase. Arduino board can be supplied with an external power supply. The recommended input voltage is 7-12 V. If you need more voltage, you can use the DC-DC converter integrated in the 4-20 mA Board. This converter provides a 24 V out, and can be used to supply 4-20 mA sensors. Is important to know, that the 4-20 mA board is not designed to supply industrial devices such as PLC's and thermostats.

To get this 24 V, is necessary to supply the Arduino board with a minimum voltage of 9 volts.

You can use the functions ON()/OFF() to enable/disable the 24 V output.

It is advisable to check that the DC-DC converter is working properly. You can measure the output voltage using a multimeter.

NOTE: This feature is not available in Raspberry Pi. The 4-20 mA sensors must be supplied externally.

Connecting sensors

In this part of the tutorial we are going to connect a 4-20 mA sensor and supply it with 24 V output. This sensor is type 2 transmitter. Type 2 transmitters are energized by the current loop, where the supply voltage is included in the receptor. The transmitter is floating and the ground is in the receptor, so is necessary to put the connect the corresponding jumper.

Upload the example code included with the library

Arduino / Intel Galileo

Code:
/*  
 *  4-20 mA Sensor Board
 *  
 *  Copyright (C) Libelium Comunicaciones Distribuidas S.L. 
 *  http://www.libelium.com 
 *  
 *  This program is free software: you can redistribute it and/or modify 
 *  it under the terms of the GNU General Public License as published by 
 *  the Free Software Foundation, either version 3 of the License, or 
 *  (at your option) any later version. 
 *  a
 *  This program is distributed in the hope that it will be useful, 
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of 
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 *  GNU General Public License for more details.
 *  
 *  You should have received a copy of the GNU General Public License 
 *  along with this program.  If not, see http://www.gnu.org/licenses/. 
 *  
 *  Version:           1.0
 *  Design:            David GascĂłn 
 *  Implementation:    Ahmad Saad
 */

// Include this library for using current loop functions.
#include <currentLoop.h >

#define CHANNEL CHANNEL4

void setup()
{
  
  // Switch ON the 24V DC-DC converter
  sensorBoard.ON();

  // Inits the Serial for viewing data in the serial monitor
  Serial.begin(115200);
  delay(100);
  Serial.println("Arduino 4-20mA board switched ON...");
}


void loop()
{
  // Get the sensor value in int format (0-1023)
  int value = sensorBoard.readChannel(CHANNEL);
  Serial.print("Int value read from channel 1 : ");
  Serial.println(value);

  // Get the sensor value as a voltage in Volts
  float voltage = sensorBoard.readVoltage(CHANNEL);
  Serial.print("Voltage value rad from channel 1 : ");
  Serial.print(voltage);
  Serial.println("V");

  // Get the sensor value as a curren in mA
  float current = sensorBoard.readCurrent(CHANNEL);
  Serial.print("Current value read from channel 1 : ");
  Serial.print(current);
  Serial.println("mA");


  Serial.println("***************************************");
  Serial.print("\n");

  delay(2000);
}
    

Raspberry Pi

Code:
/*  
 *  4-20 mA Sensor Board
 *  
 *  Copyright (C) Libelium Comunicaciones Distribuidas S.L. 
 *  http://www.libelium.com 
 *  
 *  This program is free software: you can redistribute it and/or modify 
 *  it under the terms of the GNU General Public License as published by 
 *  the Free Software Foundation, either version 3 of the License, or 
 *  (at your option) any later version. 
 *  a
 *  This program is distributed in the hope that it will be useful, 
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of 
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 *  GNU General Public License for more details.
 *  
 *  You should have received a copy of the GNU General Public License 
 *  along with this program.  If not, see http://www.gnu.org/licenses/. 
 *  
 *  Version:           1.0
 *  Design:            David GascĂłn 
 *  Implementation:    Ahmad Saad
 */

//Include this library for using current loop functions.
#include "currentLoop.h"
#define CHANNEL CHANNEL4


void setup()
{
  //Inits the Serial for viewing data in the serial monitor.


  //Switch ON the 24V DC-DC converter
  sensorBoard.ON();

  delay(100);
  printf("Arduino 4-20 mA board switched ON...");
  printf("\n");
}

void loop()
{
  if (sensorBoard.isConnected(CHANNEL))
  {
    // Get the sensor value in int format (0-1023).
    int value = sensorBoard.readChannel(CHANNEL);
    printf("Int value read from channel 1 : %d",value);
    printf("\n");

    // Get the sensor value as a voltage in Volts.
    float voltage = sensorBoard.readVoltage(CHANNEL);
    printf("Voltage value rad from channel 1 : %f",voltage);
    printf(" V");
    printf("\n");

    // Get the sensor value as a curren in mA.
    float current = sensorBoard.readCurrent(CHANNEL);
    printf("Current value read from channel 1 : %f",current);
    printf(" mA");
    printf("\n");


    printf("***************************************");
    printf("\n");

  }
  else {

    printf("The sensor is not connected...");
    printf("\n");

  }

  //Delay after reading.
  delay(2500);

}

int main (){
  setup();
  while(1){
    loop();
  }
  return (0);
}
    

In the serial monitor you should be that the sensor is transmitting 4mA. The 4mA "bottom of span" signal allows the receiver to detect a broken wire or failed instrument. The constant-current feature of a current loop cancels out voltage drop errors due to long wiring runs (of course this would also be true if you selected different current values for zero and span).

Device Compatibility

This shield is tested and fully compatible with the following boards:

Arduino Boards

  • Arduino Uno

Raspberry Pi Boards

  • Raspberry Pi
  • Raspberry Pi (Model B+)
  • Raspberry Pi 2
  • Raspberry Pi 3