analogique_0_10v.ino
Opta analog inputs read 0-10 V in millivolts. This sketch runs an 8-sample moving average to smooth noise, then scales a standard 4-20 mA sensor through a sense resistor, including open-loop fault detection.
Hardware
An STMicroelectronics Opta PLC (real-time STM32H747 + secure i.MX RT1052 companion) programmed with the Arduino PLC environment, 24 V DIN-rail supply. 0-10 V sensor, or a 4-20 mA loop with a 250 Ω sense resistor (0-5 V across it) into AI1 and GND.
Wiring
Sensor output to the AIx terminal, 0 V to analog GND. For 4-20 mA: transmitter → 250 Ω resistor → AIx, ground shared with the PLC.
How it works
AnalogIn.read returns millivolts; the circular average damps background noise. Readings above 19.5 mA flag an open loop (sensor fault).
Running it
Install the Arduino PLC boards manager, select “Opta PLC”, wire USB/DFU and upload. The sketch runs on the real-time STM32H747 side.
Variants
Rescale for a true 0-10 V sensor (map 0..10000 mV), log to the embedded web server or push over MQTT.
analogique_0_10v.ino
// Opta PLC : acquisition AI1 (0-10 V) avec moyenne glissante + seuil.
#include "PLCContainer.h"
PLCContainer PLC;
const int CANAL = OPTA_AI_1; // entrée analogique 1 (0-10 V)
const int FENETRE = 8; // moyenne sur 8 mesures
long tampon[FENETRE];
int n = 0;
unsigned long t0 = 0;
// Capteur 4-20 mA typique avec résistance de lecture 250 Ω :
// 4 mA -> 1 V ; 20 mA -> 5 V. Conversion en mA ci-dessous.
float vers_mA(int millivolts) {
return (millivolts / 250.0f) * 10.0f;
}
void setup() {
Serial.begin(115200);
PLC.begin();
for (int i = 0; i < FENETRE; i++) tampon[i] = PLC.AnalogIn.read(CANAL);
}
void loop() {
if (millis() - t0 < 250) return; // 4 mesures/s
t0 = millis();
tampon[n] = PLC.AnalogIn.read(CANAL); // renvoie des millivolts
n = (n + 1) % FENETRE;
long somme = 0;
for (int i = 0; i < FENETRE; i++) somme += tampon[i];
long moy = somme / FENETRE;
float mA = vers_mA(moy);
Serial.printf("AI1 = %ld mV -> %.2f mA", moy, mA);
if (mA > 19.5f) Serial.print(" ALERTE capteur en défaut (boucle ouverte)");
Serial.println();
}