How to read 4-20mA industrial sensors with ESP32 safely
Learn how to connect industrial 4 to 20 milliamp current sensors to ESP32 microcontrollers safely using signal conditioning circuits, precision resistors, and operational amplifiers.
Summary
- The four to twenty milliamp current loop is an industrial standard due to its high immunity against noise over long wire runs.
- The microcontroller analog-to-digital converter only reads voltage, requiring current conversion via an external resistor.
- Operational amplifiers help isolate and scale the voltage range to protect the sensitive microcontroller pin.
- High-precision resistors prevent reading drift caused by thermal variations and component aging.
- Proper electrical isolation prevents severe industrial voltage surges from destroying the microcontroller board.
Why the four to twenty milliamp current loop rules the industry
In industrial automation, the real world is noisy, filled with heavy motors turning on and off, generating invisible electromagnetic sparks. If we tried to send the raw voltage from a sensor over dozens of meters of thin wire to a control room, the signal would arrive completely distorted by interference along the path. That is why industry prefers working with an electrical current that ranges from four to twenty milliamperes. Unlike voltage, which suffers drops and noise easily, electrical current remains stable even when the wiring is long or passes through noise-laden environments.
In practice, this means a four milliamp signal represents zero or the starting point of the sensor measurement scale, such as zero degrees Celsius or zero bar of pressure. Meanwhile, the maximum value of twenty milliamperes represents the top of the scale, such as one hundred degrees Celsius or ten bar. If the sensor drops to exactly zero milliamperes, the automation system instantly knows that a cable broke or the equipment failed, which would be impossible to detect if the lower limit were zero volts. To better understand how to protect electronic systems against severe electromagnetic interference in the physical environment, it is worth checking our tips on why Wi-Fi drops and how to track RF interference in radio frequency and data cabling.
The main challenge when connecting industrial sensors to microcontrollers
Although industrial sensors operate on robust current loops, modern microcontrollers like the ESP32 are delicate devices that operate at low voltages with 3.3-volt logic circuits. The board's analog-to-digital converter — the internal component that translates electrical quantities into numbers understood by code — does not understand electrical current directly. It only measures electrical potential difference, meaning the voltage across its input pins. Therefore, to read an industrial current signal, an engineer must solve a classic physical translation problem.
The most straightforward way to perform this translation is to insert a component called a burden resistor or precision resistor into the current path. When current flows through this resistor, it creates a voltage drop according to Ohm's Law. The big secret here lies in choosing the correct value for this resistor and its tolerance, because any cheap or thermally unstable component can generate false readings in the production process. For those looking to build a complete monitoring hub at home or in a factory, this project perfectly complements energy measurement setups, as discussed in our article on how to measure home energy with ESP32 and PZEM.
Calculating the ideal precision resistor for the circuit
To convert the four to twenty milliamp current range into a voltage range that the ESP32 can read safely without damage, we need to perform a simple math calculation. The ESP32 normally accepts analog voltages between zero and 3.3 volts. If we design the circuit so that the maximum flow of twenty milliamperes generates exactly 3.0 volts across our resistor, we will have a safety margin against unwanted saturation.
Applying the physics formula where resistance equals voltage divided by current, we divide three volts by zero point zero twenty amperes, which results in one hundred and fifty ohms. A high-precision one hundred and fifty-ohm resistor — preferably with a zero point one percent tolerance and a low temperature coefficient — will make the four milliamp signal generate zero point six volts, while twenty milliamperes will generate a clean three volts at the microcontroller input.
The essential role of operational amplifiers in signal conditioning
Simply placing a resistor at the ESP32 input solves the basic problem, but leaves the circuit vulnerable to electrical surges and impedance issues. This is where operational amplifiers come in, popularly known in electronics as op-amps. They act as intelligent and protective barriers between the violent industrial world and the sensitive microcontroller, adjusting gain and filtering out unwanted high-frequency noise.
The operational amplifier can be configured as a voltage follower or a signal conditioning circuit. In practice, it ensures that the ESP32 input impedance does not steal current from the sensor loop, which would cause severe measurement errors. Furthermore, if a short circuit occurs on the external line, the amplifier acts as a shield, sacrificing itself or cutting the spike before it reaches the microcontroller analog port. If you encounter short-circuit issues on boards while assembling these protection circuits, you can check the practical guide on how to find short circuits on boards with a thermal camera to save your prototypes on the workbench.
Assembling the protection circuit and analog reading stage
Building a safe circuit requires following a logical assembly sequence to avoid polarity errors or accidental shorts on the breadboard or final printed circuit board. Follow the necessary steps to structure the interface safely:
- Completely turn off all power sources and the industrial bus before starting any physical soldering or connection work on the workbench.
- Connect the one hundred and fifty-ohm precision resistor in series with the industrial sensor negative return and the common circuit ground.
- Insert an operational amplifier powered by a stable power supply to isolate and condition the signal generated across the precision resistor.
- Add Zener-type protection diodes at the ESP32 analog input to clamp any voltage that accidentally exceeds the 3.3-volt limit.
- Perform a bench test by injecting known currents into the loop using an adjustable power supply and validate the values read by the code on the serial monitor.
Functional code for reading and conversion on the ESP32
After ensuring the physical safety of the circuit with resistors and amplifiers, the next step is writing the code in the Arduino development environment. The code below reads the analog port, applies a simple moving average to stabilize digital noise, and converts the raw value into the actual physical unit of the sensor.
const int sensorPin = 34; // ESP32 analog pin connected to the conditioning circuit
const float maxVoltage = 3.0; // Maximum voltage calculated across the precision resistor
const int maxReading = 4095; // 12-bit resolution of the ESP32 converter
void setup() {
Serial.begin(115200);
pinMode(sensorPin, INPUT);
}
void loop() {
long totalReadings = 0;
int samples = 50;
// Collect multiple samples to smooth out electrical noise
for (int i = 0; i < samples; i++) {
totalReadings += analogRead(sensorPin);
delay(2);
}
float rawAverage = (float)totalReadings / samples;
float voltage = (rawAverage * maxVoltage) / maxReading;
// Convert measured voltage to current in milliamperes (4-20mA)
// Considering a 150-ohm resistor: V = I * R => I = V / R
float currentMa = (voltage / 150.0) * 1000.0;
// Map current to the physical process unit (e.g., 0 to 100 degrees)
float processValue = (currentMa - 4.0) * (100.0 / 16.0);
Serial.print("Voltage: ");
Serial.print(voltage, 2);
Serial.print(" V | Current: ");
Serial.print(currentMa, 2);
Serial.print(" mA | Process: ");
Serial.print(processValue, 2);
Serial.println(" C");
delay(1000);
}Final thoughts on the robustness of industrial projects
Integrating the ESP32 into industrial environments using the four to twenty milliamp current standard is an excellent way to reduce costs without sacrificing operational reliability. However, project success depends directly on the care taken with the analog input electronics, prioritizing high thermal stability resistors and well-designed operational amplifiers.
By implementing proper surge and electrical noise protection barriers, you ensure that a low-cost microcontroller can run for years in harsh environments without freezing or suffering premature burnout. Extensively testing the hardware on the workbench before final field installation is the differentiator that separates an amateur prototype from a robust, professional automation system.