Skip to content

Instantly share code, notes, and snippets.

@maxpromer
Last active May 5, 2026 17:53
Show Gist options
  • Select an option

  • Save maxpromer/353f4e538df7cf35f4b0f082792d780b to your computer and use it in GitHub Desktop.

Select an option

Save maxpromer/353f4e538df7cf35f4b0f082792d780b to your computer and use it in GitHub Desktop.
#include <Arduino.h>
#include <Wire.h>
#include <IOXESP32_4-20mA_Receiver.h>
Receiver4_20 sensor(&Wire, 0x45); // Default 0x45
const int raw_in_0cm = 6485; // Enter Raw value in level 0CM
const int raw_in_20cm = 7521; // Enter Raw value in level 20CM
void setup() {
Serial.begin(115200);
Wire.begin(); // Start I2C
while (!sensor.begin()) { // Start ADC
Serial.println("Not found hardware :(");
delay(2000);
}
}
void loop() {
delay(100);
if (sensor.measure()) { // Update analog value from ADC
int16_t raw = sensor.raw(); // Read raw value (-32768 to 32767)
float current = sensor.current(); // Read current in mA unit
float level_cm = (float)(raw - raw_in_0cm) * 20.0f / (raw_in_20cm - raw_in_0cm); // Map (float)
level_cm = constrain(level_cm, 0, 500); // limit 0 - 500CM
Serial.printf("Raw=%5d Level=%.1f CM\n", raw, level_cm);
} else {
Serial.println("Hardware error !");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment