
Introduction: Industrial Sensors Made Easy
Want to connect 4-20mA Industrial Sensors with ESP32 PLC? You’re in the right place! This guide shows you exactly how to interface 4-20mA sensors with ESP32 PLC controllers for accurate, reliable data acquisition.
4-20mA sensors are the industry standard for measuring temperature, pressure, flow, and level in factories, water treatment plants, and automation systems. With ESP32, you get the same professional capability at a fraction of traditional PLC costs.
Why Use 4-20mA Sensors with ESP32?
Understanding 4-20mA Current Loop
The 4-20mA current loop is simple yet powerful. Instead of transmitting voltage, sensors send current signals:
- 4mA = Minimum reading (0%)
- 12mA = Mid-range (50%)
- 20mA = Maximum reading (100%)
Example: A pressure sensor measuring 0-100 PSI sends 4mA at 0 PSI and 20mA at 100 PSI.
The 4-20mA current loop is defined in ISA standards for industrial instrumentation. Follow NEMA installation guidelines for industrial environments. Reference IEC 61131 standards for industrial control systems.
Key Advantages
- Noise Immunity: Current signals aren’t affected by voltage drops or electrical noise, perfect for noisy industrial environments.
- Long Distance: Transmit signals hundreds of meters without degradation.
- Fault Detection: Readings below 4mA indicate sensor failure or disconnection—a critical safety feature.
- Universal Standard: Compatible with virtually all industrial equipment worldwide.
ESP32 PLC Benefits for Industrial Applications
Why Choose ESP32 Over Traditional PLCs?
- Cost Savings: Save 80-95% compared to traditional industrial PLCs while maintaining professional functionality.
- Built-in Connectivity: WiFi and Bluetooth included—no expensive communication modules needed.
- Easy Programming: Use familiar Arduino IDE instead of costly proprietary software.
- IoT Ready: Connect to cloud platforms, create web dashboards, and enable remote monitoring instantly.
- Compact Design: Small footprint saves panel space while offering powerful dual-core processing.
What You’ll Need
Arduino IDE and Resources
- New to Arduino? Check the Arduino getting started guide
- See the complete Arduino language reference for programming help.
Hardware Components
For Basic Setup:
- ESP32 development board or NORVI industrial controller
- 4-20mA industrial sensor (temperature, pressure, etc.)
- 165Ω resistor (1/4W, 1% tolerance, metal film)
- 24VDC power supply (for sensor)
- Breadboard or PCB for prototyping
- Connecting wires
Optional Protection:
- 100nF ceramic capacitor (noise filtering)
- TVS diode 5.6V (overvoltage protection)
- Optocoupler (electrical isolation)
Software Requirements
- Arduino IDE (free download)
- ESP32 board support package
- Basic Arduino programming knowledge
Electronic Component Datasheets
For advanced applications, check the INA196 current sense amplifier datasheet to ensure accurate current measurement. Learn about precision resistor selection for critical designs requiring stability and reliability. To enhance circuit protection, use TVS diodes for overvoltage protection and safeguard your system from transient events.
Component Suppliers
Purchase components from Digi-Key, Mouser, or Adafruit. Explore ESP32 development boards at SparkFun. Browse industrial 4-20mA transmitters at Omega Engineering.
The Simple Circuit: Converting Current to Voltage
Understanding the Conversion
ESP32’s analog pins read voltage (0-3.3V), but sensors output current (4-20mA). The solution? A simple resistor!
Ohm’s Law in Action:
Voltage = Current × Resistance
V = I × R
Understanding Ohm’s Law basics is essential for circuit design. New to electronics? Start with All About Circuits fundamentals. Learn more about analog signal conditioning techniques.
Choosing the Right Resistor
Using 165Ω Shunt Resistor:
At 4mA: V = 0.004A × 165Ω = 0.66V
At 12mA: V = 0.012A × 165Ω = 1.98V
At 20mA: V = 0.020A × 165Ω = 3.30V
Perfect! This gives us 0.66V to 3.30V—exactly within ESP32’s 0-3.3V range.
Basic Wiring Diagram
+24V Power Supply
|
[+ Sensor -]
|
[165Ω Resistor]
|
┌───┴───┐
│ │
ESP32 ADC GND
(GPIO 36)
Step-by-Step Connections:
- Connect sensor positive to 24V supply
- Connect sensor negative to resistor
- Connect other resistor end to ESP32 ground
- Connect junction (between sensor and resistor) to ESP32 GPIO 36
- Add 100nF capacitor between GPIO 36 and ground (optional but recommended)
Programming Your ESP32
Basic Code to Read Sensor
// Simple 4-20mA Sensor Reading
const int SENSOR_PIN = 36; // ADC pin
const float SHUNT_RESISTOR = 165.0;
void setup() {
Serial.begin(115200);
analogReadResolution(12); // 12-bit ADC
analogSetAttenuation(ADC_11db); // 0-3.3V range
}
void loop() {
// Read voltage
int rawADC = analogRead(SENSOR_PIN);
float voltage = (rawADC / 4095.0) * 3.3;
// Convert to current
float current_mA = (voltage / SHUNT_RESISTOR) * 1000.0;
Serial.print("Current: ");
Serial.print(current_mA, 2);
Serial.println(" mA");
delay(1000);
}
Converting to Real Values
Let’s convert current to actual sensor readings. Example for 0-100 PSI pressure sensor:
float convertToPressure(float current_mA) {
// Check for valid range
if (current_mA < 4.0 || current_mA > 20.0) {
return -999; // Error code
}
// Map 4-20mA to 0-100 PSI
float pressure = ((current_mA - 4.0) / 16.0) * 100.0;
return pressure;
}
void loop() {
int rawADC = analogRead(SENSOR_PIN);
float voltage = (rawADC / 4095.0) * 3.3;
float current_mA = (voltage / SHUNT_RESISTOR) * 1000.0;
float pressure_PSI = convertToPressure(current_mA);
if (pressure_PSI == -999) {
Serial.println("ERROR: Sensor fault!");
} else {
Serial.print("Pressure: ");
Serial.print(pressure_PSI, 1);
Serial.println(" PSI");
}
delay(1000);
}
Adding Wi-Fi and Web Dashboard
Make your system IoT-enabled with this simple web server code:
#include <WiFi.h>
#include <WebServer.h>
const char* ssid = "YourWiFi";
const char* password = "YourPassword";
WebServer server(80);
float currentReading = 0;
void setup() {
Serial.begin(115200);
// Connect to WiFi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("\nConnected!");
Serial.println(WiFi.localIP());
// Setup web page
server.on("/", []() {
String html = "<html><body style='font-family:Arial;text-align:center;'>";
html += "<h1>Pressure Monitor</h1>";
html += "<h2>" + String(currentReading, 1) + " PSI</h2>";
html += "<meta http-equiv='refresh' content='2'>";
html += "</body></html>";
server.send(200, "text/html", html);
});
server.begin();
}
void loop() {
server.handleClient();
// Read sensor
int rawADC = analogRead(SENSOR_PIN);
float voltage = (rawADC / 4095.0) * 3.3;
float current_mA = (voltage / SHUNT_RESISTOR) * 1000.0;
currentReading = convertToPressure(current_mA);
delay(100);
}
Access your sensor readings from any device by visiting the ESP32’s IP address in a web browser!
IoT and Cloud Platforms
Implement MQTT protocol for IoT communication. Connect to AWS IoT Core for cloud integration. Visualize sensor data with ThingSpeak IoT platform. Create automation flows using Node-RED.
ESP32 Official Documentation
The ESP32 features a versatile ADC (Analog to Digital Converter) that supports multiple channels, making it ideal for precise sensor data acquisition in industrial and IoT applications. To understand its performance in detail, you can learn more about ESP32 ADC specifications in the official documentation. For hands-on development, download the ESP32 Arduino Core to get started.
GitHub Repositories and Open Source
Find more examples in the ESP32 Arduino examples repository. Use the Adafruit ADS1115 library for 16-bit precision. Explore ESP32 IoT projects on GitHub for inspiration.
Troubleshooting Common Issues
Problem: No Reading (0mA)
Check:
- Sensor has power (24V connected?)
- Wires are properly connected
- No broken cables
- Sensor is functioning (test with multimeter)
Problem: Readings Are Noisy
Solutions:
- Add 100nF capacitor between ADC pin and ground
- Use shielded cable for sensor wiring
- Keep sensor wires away from power cables
- Add software averaging (average 10 readings)
Problem: Reading Stuck at 4mA
Check:
- Sensor is getting correct input (temperature/pressure changing?)
- Sensor is properly calibrated
- Sensor hasn’t failed (test with known good system)
Community Forums and Support
Get help from the community at Arduino Forum. Join discussions on r/esp32 subreddit. Find solutions to common issues on Stack Overflow ESP32 questions.
Best Practices and Tips
Installation Tips
DO:
- Use quality shielded cable for sensor connections
- Ground shield at controller end only (avoid ground loops)
- Mount ESP32 in proper enclosure (IP65+ for industrial use)
- Label all connections clearly
- Keep spare sensors for critical applications
DON’T:
- Run sensor cables parallel to power cables
- Ground both ends of shielded cable
- Exceed 3.3V on ESP32 ADC pins
- Forget to add protection (capacitors, TVS diodes)
- Skip calibration testing
Improving Accuracy
Software Filtering:
// Simple moving average filter
const int SAMPLES = 10;
float readings[SAMPLES];
int index = 0;
float getFilteredReading() {
readings[index] = readCurrentReading();
index = (index + 1) % SAMPLES;
float sum = 0;
for (int i = 0; i < SAMPLES; i++) {
sum += readings[i];
}
return sum / SAMPLES;
}
Hardware Tips:
- Use 0.1% precision resistors for critical applications
- Calibrate at operating temperature
- Use external ADC (ADS1115) for 16-bit resolution if needed
Safety and Compliance Resources
Review OSHA electrical safety standards. Understand UL 508 standards for industrial controls. Learn about CE marking requirements for European markets.
Real-World Applications
- Temperature Monitoring: Monitor oven temperature, HVAC systems, or process heating with RTD transmitters outputting 4-20mA.
- Pressure Control: Track hydraulic pressure, air pressure, or water pressure in real-time with pressure transmitters.
- Flow Measurement: Measure water flow, gas flow, or chemical flow rates for process control and monitoring.
- Level Detection: Monitor tank levels, silo levels, or reservoir levels for automated filling and alerts.
- Monitor tank levels, silo levels, or reservoir levels for automated filling and alerts.
Next Steps and Advanced Features
Once you have the basics working, consider these enhancements:
- Cloud Integration: Send data to AWS IoT, Azure, or Google Cloud for advanced analytics.
- MQTT Protocol: Enable IoT communication for integration with home automation or industrial systems.
- Data Logging: Add SD card to log historical data for trend analysis.
- Modbus Support: Communicate with SCADA systems and other industrial equipment.
- Multiple Sensors: Read 4-8 sensors simultaneously using multiple ADC pins.
- Alarm System: Send email/SMS alerts when readings exceed thresholds.
Conclusion
Connecting 4-20mA industrial sensors to ESP32 is straightforward and cost-effective. With just a resistor, some basic wiring, and simple code, you can build professional industrial monitoring systems at a fraction of traditional costs.
The combination of ESP32’s powerful processing, built-in WiFi, and compatibility with industry-standard sensors makes it perfect for:
- DIY industrial monitoring
- Educational projects
- Small-scale automation
- IoT sensor networks
- Prototyping before production systems
Key Takeaways:
- 4-20mA sensors work perfectly with ESP32 using a simple resistor
- 165Ω resistor converts 4-20mA to 0.66-3.3V for ESP32 ADC
- ESP32 saves 80-95% vs traditional PLCs
- Add Wi-Fi dashboards and cloud connectivity easily
- Follow best practices for reliable industrial operation
Frequently Asked Questions
How to Connect 4-20mA Industrial Sensors with ESP32 PLC: Simple Guide
Can I use any ESP32 board for industrial sensors?
Yes, but NORVI industrial controllers offer built-in 4-20mA inputs with isolation and protection, making installation easier and more reliable.
What’s the maximum cable length for 4-20mA sensors?
Typically, 500-1000 meters with proper cable. Current signals don’t degrade like voltage over distance.
Can I connect multiple sensors?
Absolutely! ESP32 has multiple ADC pins. Use separate 165Ω resistors for each sensor.
How accurate are ESP32 readings?
With proper calibration, expect ±0.1-0.5% accuracy – suitable for most industrial applications.
Do I need a special power supply?
Most 4-20mA sensors need 12-36VDC. A regulated 24VDC industrial power supply is recommended.