An SBC-Based Project
In this article, Derek helps you gain deeper understanding of 4-20 mA current loop devices and process control systems. He looks at some history, explains why things are the way they are, looks at simple example components of a process control system (sensor, transmitter, receiver) and works through a practical example with working code.
Anybody who has experience in process control knows that 4-20 mA current loop devices dominate the industry. These devices are built to transmit analog signal information through varying amounts of current, which is then read and interpreted by a receiver unit. The receiver can then be configured to display the sensor data in a human friendly format and/or perform some action. This is fundamental for automating processes in simple or complex systems.
Why 4-20 mA? The history on how 4-20 mA came about is quite interesting—going back to pre-electronic process control systems based on a 3-18 psi linear measurement scale. The low-end value of 4 mA was chosen because the necessary equipment consumes about 3 mA—so, a desire to add some wiggle drove the industry to choose 4 mA. And 0 mA wasn’t chosen as the lower value so that line faults could be detected if it falls below 3.8 mA. The high end of 20 mA was chosen because anything over 30 mA is very dangerous to humans—and because 20 is a nice multiple of 4 [1].
Taking a look at an overly simplified example, let’s imagine a water pump scenario where a hot water valve must be automatically opened or closed depending on water temperature in a pipe carrying mixed hot and cold water. A temperature sensor is installed in the mixed water pipe, transmitting an analog signal (most likely a signal measured in resistance). The wires from the sensor are then connected to a transmitter which converts the analog signal to a 4-20 mA current signal.
This transmitter is then connected to a single board computer (SBC) with on-board relays that has been programmed to read the 4-20 mA current signal of temperature, and convert it into units of degrees Celsius. The program continually reads the temperature value, and when it becomes greater than 41ºC, the program will energize the relay connected to an electronic valve actuator, thus closing the hot water valve until the temperature has returned to nominal value. All this without any human interaction, using simple, inexpensive and low power hardware.
Let’s Build Something
Now with basic 4-20 mA current loop system principles out of the way, let’s have some fun and deepen our understanding so we can actually begin to build something! We’re going to further explore reading temperature data using a sensor and an SBC. So, grab your digital multimeter and let’s get rolling!
— ADVERTISMENT—
—Advertise Here—
Project Requirements:
● RTD PT100 temperature sensor [2]● PT100 4-20 mA transmitter, -50 to 100ºC, 24 VDC [3]● SBC or microcontroller (MCU) with ADC port. The TS-7680 [4] is used in this article because it has built-in 4-20 mA support, but the principles can be applied globally. The SBC or MCH can have ADC ports with 4-20 mA loop support or not
● Digital multimeter with current measurement (mA); Optionally, temperature measurements to confirm temperature conversion
● Small Philips and flathead screwdrivers for screw terminals
● Hookup wire
The Sensor: We’ll be using a three-wire resistance temperature detector (RTD) temperature sensor (Figure 1). More specifically, we’ll be using a PT100, which means the resistance at 0ºC is 100 Ω and measures from -200ºC to 600ºC. We’ve chosen a three-wire RTD sensor mainly because of its min and max range, accuracy, availability, packaging types and lead wire range. We can install this sensor in a pipe a long distance away from a facility without accuracy loss.
The third wire in an RTD is used to compensate for the resistance added by the length of the wires. This allows for maximum cable lengths up to 200 feet. If longer cable lengths are required, then it is best to use a temperature transmitter—like we talk about in a moment—which converts temperature into a current output or digital signal. Pro Tip: The main players in temperature sensors are thermocouples, thermistors, RTDs, and one-wire (DS18B20). Naturally, each have their advantages and disadvantages [5]. Think about your application requirements when choosing.
One advantage of using an RTD sensor is that it has a linear temperature vs resistance output [6]. That means that it’s easy to calculate a slope intercept formula to calculate final, human-readable values. Adventurous folks who want to build their own circuit (in other words, voltage divider or Wheatstone bridge) instead of buying a transmitter will appreciate the linearity. As for the rest of us, we’ll use a transmitter.
The Transmitter: In our application so far, we’ve determined that we want to use a 4-20 mA current loop signal. We’ve also determined that an RTD PT100 sensor suits our application nicely. Now, we need to find a transmitter that will take an analog signal from the sensor and translate it into a nice 4-20 mA current loop signal. Let’s consider our water temperatures. Water will be flowing through a pipe, so let’s figure we won’t be measuring anything below 0ºC (freezing). Let’s also figure we won’t be sending anything more than 90ºC through it (water boilers for home heating are typically 80ºC). A quick trip to the store will yield a PT100, 4-20 mA transmitter with a -50 to 100ºC range and three-wire input (Figure 2). It will need to be supplied with 24 VDC [7], which is very common in process control systems.
We’re almost there. Now, we need to choose a receiver in order to read and convert the 4-20 mA signal into human friendly values and then do something with them.
The Receiver: The final piece of equipment in our application is the receiver. Its main job is to make sense of the 4-20 mA signal by digitizing it so that we can program it to perform some action based on the signal. Actions include things like displaying the temperature in ºC or ºF on a display, toggling DIO based on set thresholds, energizing relays for system control, serving up a web application for the world to see (think Internet of Things, IoT or IIoT) or some combination of all these things. The receiver could be a simple circuit (555 chip, transistors, voltage divider, relay), an MCU (Arduino) or am SBC.
It just so happens that the Technologic Systems’ TS-7680 [8] falls into this application very nicely, with 4× ADC inputs supporting 4-20 mA current loop (Figure 3). You can conveniently power it with the same 24 VDC supply as the transmitter. It also has a lot of digital output channels available for controlling external relays or devices, along with a load of other interfaces like Modbus, CAN, RS-232, RS-485 and more. The feature that captures my attention is the networking abilities, including dual Ethernet, Wi-Fi and Bluetooth. The TS-7680 is well suitable for many interesting applications [9] and meets the requirements for this project article.
If you choose to use some other receiver device, just make sure it has an ADC port. If the ADC port does not support 4-20 mA current loops, don’t fret. It’s actually quite simple to add support using a single load resistor for measurement. By placing a 250 Ω resistor between GND and ADC, we can interpret the 4-20 mA output as a voltage drop across the resistor (Figure 4).
— ADVERTISMENT—
—Advertise Here—

Why 250 Ω? Let’s apply Ohm’s Law: V = IR, and assume the max voltage we want to supply to our ADC channel is 5 VDC. We know the max current is 20 mA, so we can now apply the formula to come up with a resistance value: R = V/I = 5/0.020 = 250 Ω. This works out nicely for the low end as well, since V = IR = 0.004 × 250 = 1 V. This equates to a very nice 1 VDC to 5 VDC range. However, going back to fault detection when current is below or above 4 and 20 mA, we may opt for a lower resistance value, like 225 Ω (0.9 VDC to 4.5 VDC) or, in the case of the TS-7680’s built-in load resistors, 240 Ω (0.96 VDC to 4.8 VDC).
Connecting the Pieces
Sensor and Transmitter: Connecting our temperature sensor to it is easy enough. The two red wires are common and the clear wire is what we’ll call the sensor signal. The clear wire will be attached to the far right terminal while the two red wires will be attached to the remaining terminals in that row (Figure 5). Hooking the transmitter up to power might make your brain twitch a little. Instead of negative wire to negative terminal and positive to positive, we’re going to be connecting the negative terminal in line with a receiver in order to measure the current.
Let’s start out simple and use a digital multimeter (DMM) to prove the concept and get our minds wrapped around it. So, the positive wire of your 24 VDC power supply will go to the positive terminal as usual, but the negative wire will be connected to the COM port of your DMM. Then, you’ll connect the mA port of your DMM to the negative terminal (Figure 6 and Figure 7).
This is the exciting part! Grasp the sensor fully with your hand and watch the mA measurement rise with the temperature of your hand. Dunk the sensor into a mug full of ice water and watch it drop. This validates everything we’ve been working on so far! Now that we have proof of concept out of the way and we’ve validated our sensor and transmitter, let’s get the receiver, in our case the TS-7680 SBC, hooked up.
Transmitter and Receiver: Just as we connected our multimeter inline to the 4-20 mA current loop transmitter, we’re going to do the same for the ADC channel of the receiver. Remember, it’s here in the receiver that we’re going to convert the analog signal into a digital one so that we can translate it into a meaningful number in ºC and then do something with it.
Without going into analog signal theory too much, you can easily have the transmitter and receiver installed on opposite sides of several football fields and still connect them using a shielded wire (to prevent EMI and essentially turning the long wires into an antenna). So long as there is still voltage potential at the receiver for the 240 Ω resistor, the receiver can measure the voltage drop. For example, using 24 AWG wire [10] and 24 VDC, that turns out to be about 35,000 feet (19 VDC drop leaving 5 VDC potential at the 240 Ω resistor of the receiver).
Since we’re working with the TS-7680 in this guide, we’ll be taking advantage of the variable input voltage and supply it and the transmitter with 24 VDC from a single source. We only need to run a single wire to the AN-0 pin on the bottom connector (Figure 9). This simplifies wiring for the case of our TS 7680, but we might find that there are a lot of systems that do not have a variable input voltage as high as 24 VDC. More commonly, it’ll be 5 VDC or 12 VDC. So, let’s see what a separate power supply would look like in Figure 10.
Here’s a pitfall to consider: Keep in mind, using separate supplies in this way requires them to be able to be grounded together. Using separate isolated supplies can cause a ground potential which can give incorrect readings at best, or damage equipment at worst. It would be best to use a supply that’s able to offer multiple voltage taps with the same reference ground.
There. Now, we have two power supplies—one for the receiver and one for the transmitter. We could opt for a 12 VDC to 24 VDC converter [11] as well, but we’ll abstract that away for now. At this point, we’re ready to dive into programming the receiver, but while we’re still on the topic of hooking everything up, Figure 11 shows an example of how we’d hook up multiple transmitters. Okay! Now, we’re ready to move on to even more exciting stuff: programming the receiver.
Programming the Receiver
If you’re not following along with a TS- 7680 or similar Technologic Systems’ product, that’s okay because this article is generic enough to apply to other boards. Just keep your product’s manual handy! For those of you who are anxious to see the code, you may jump to the TS-7680 4-20 mA Current Loop Example Code gist on GitHub [12]. I’ll explain it in a little more detail below.
Read the ADC Value: Get your receiver into a state where you can program it. For the TS- 7680 running Linux, this means powered on and connected to a serial console (see also TS-7680 getting started guide [13]). Chances are, your receiver came with some example code for how to read in values from the ADC channels. For the TS-7680, we’ll be using the mx28adcctl.c
example code [14] within the TS-7680 Utility Sources repository on GitHub [15] as a base.
Copy or download the example code onto your receiver and update it with the correct pin locations. Since the code will likely be reading voltages, you might also want to add a conversion back to milliamps or microamps based on the load resistor you used for measurement. The TS-7680 has a 240 Ω load resistor between AN-0 and GND, so the conversion from milliamps to microamps looks like:
uA = (((meas_mV)*1000)/240); // Ohms law: I = V/R
Compile your program and test it! Since we have our measurements from our multimeter, we can do a sanity check that our ADC value is good. On the TS-7680, we’ve named this program getadc
(getadc.c, Makefile
) [12]. This is what the output looks like:
root@ts7680:~/getadc# ./getadc 0
mV: 2718
uA: 11325
— ADVERTISMENT—
—Advertise Here—
This is very cool, because so much has been building up to this moment! We’re very close to finishing up by converting these raw numbers into something meaningful. Tip: You could stop here if you don’t care what the units are and want to use the 4-20 mA signal directly, but it does help to have meaningful, human-readable values represented.
Convert Signal to Meaningful Units: The final piece of code! Instinctively, I separated the getadc.c code [12] from the system control code called rtdTemp.sh [12]. You could tie these two into the same program if you wanted, but I find it easier to maintain modular code. Plus, you can use the other ADC ports for different sensors, each requiring their own conversions and set of instructions but all rely on the getadc.c
code to get there.
Right to the point, the 4-20 mA to temperature conversion formula you’ll want to use is:
tempC = (mA - 9.333) / .107
Why? How? Think back to our high school days. We have a linear temperature vs current scale thanks to our sensor and transmitter choices. We want the slope intercept form of this line so we can solve for X (temperature) given Y (mA). Using a spreadsheet, we can calculate this using INTERCEPT()
and SLOPE()
functions knowing -50ºC is 4 mA and 100ºC is 20 mA. Go to [17] and take a look at the RTD PT100 4-20 mA Transmitter Temperature Conversion spreadsheet I used to get this formula if you’re still curious or want to verify it. Make a copy of it and modify it to fit your needs.
We’ll be applying that formula in our system control script, rtdTemp.sh
, which will read the mA value from the output of getadc
, convert it to ºC (and ºF), and display it. Here’s what the conversion looks like in code:
#!/bin/bash
uA=$(getadc 0 | awk ‘FNR == 2 {print $2}’)
mA=$(echo “scale=3; $uA / 1000” | bc)
tempC=$(echo “scale=1; ($mA - 9.333) / .107” | bc)
tempF=$(echo “scale=1; ($tempC * 9/5) + 32” | bc)
echo “Temp (C): $tempC”
echo “Temp (F): $tempF”
We’re using bc
because it supports floating point math whereas eval
does not. Running the script will yield the output:
root@ts7680:~/getadc# ./rtdTemp.sh
Temp (C): 20.1
Temp (F): 68.1
Momentous! We’re seeing the meaningful value for the first time! Congrats and great job so far!
Take some time and verify your output. Do the values make sense and are they accurate? Use other thermometers you might have to do sanity checks (Figure 12). For example, use the temperature measurement from your DMM. Grab cups of water (hot, cold and luke warm) and stick the PT100 sensor and DMM sensor in each of them. Are they getting the same values? I also grabbed a kitchen thermometer to get a third opinion. I found that the DMM value was about 2ºC higher than what the kitchen thermometer and the PT100 sensor was reporting, so it’s good to have another opinion handy. From here, we can continue smoothly sailing through the rest of our application, wherever it may take you!
Congratulations! We’ve now reached the end of our example application of working with a 4-20 mA current loop sensor device to get temperature data. At this point, you should be ready to move onto the next component in your application, whether that be hooking the receiver up to a relay to open or close a valve or setting up a web application to display temperatures in JSON format for other systems to consume. Whatever it is, you’re well on your way! Pick your favorite compiled (C/C++) or scripting language (Python, Node.js, Bash and such) and keep moving forward.
Wrap up
This article should have left you with a deeper understanding of 4-20 mA current loop devices and process control systems. We took a look at some history, why things are the way they are, looked at simple example components of a process control system (sensor, transmitter, receiver) and even worked through a practical example with working code.
There’s still more to learn, and if you’re hungry for it, take a look at the excellently written and illustrated “Back to Basics: The Fundamentals of 4-20 mA Current Loops” series on predig.com [18]. Also, be sure and review Technologic Systems’ product line, because most of its product is fit for industrial applications like process control systems with multiple ADCs, onboard relays, industry standard connectors, industrial temperature range and more. Now, go enjoy planning and building out the rest of your control system!
For detailed article references and additional resources go to:
www.circuitcellar.com/article-materials
References [1] through [18] as marked in the article can be found there
RESOURCES
Technologic Systems | www.embeddedarm.com
PUBLISHED IN CIRCUIT CELLAR MAGAZINE • November 2019 #352 – Get a PDF of the issue
Sponsor this ArticleA small-town Montana boy, born and raised, with a passion for programming
and embedded systems. Derek Hildreth has been working for Technologic
Systems (in various capacities) since 2010 starting as an intern embedded
engineer. Off the clock, he’s an avid skier and backpacker who slays double
black diamonds, conquers mountain peaks, and just generally loves life at
10,000 feet. What he loves most about his career at Technologic Systems is
the extended family cultural feel and the opportunity to wear many hats as
responsibilities change.