I have had some of these tank level sensors for some time which have been connected to an Arduino ADC pin via a simple current to voltage circuit and then into my previous automation software Homeseer. This worked ok but there was some noise, and the tank level varied by 2 or 3% everytime the level was read.
I now would like to get the tank levels into Home Assistant.
I bought one of these boards
and it came with this arduino code. I don’t know how to get this into an esphome component. Can anyone help please?
Cheers!
#include <Arduino.h>
#include <stdint.h>
#include <Wire.h>
#include "ADC.h"
//! I2C address of the module.
//! Configured by tying the A1, A0 pins to HI( High ) or LO( Low ). Or to be left open for Float.
// I2C Address // A1 A0
#define ADC_I2C_ADDRESS 0x14 // Low Low
// #define ADC_I2C_ADDRESS 0x16 // Low High
// #define ADC_I2C_ADDRESS 0x15 // Low Float
// #define ADC_I2C_ADDRESS 0x26 // High Low
// #define ADC_I2C_ADDRESS 0x34 // High High
// #define ADC_I2C_ADDRESS 0x27 // High Float
// #define ADC_I2C_ADDRESS 0x17 // Float Low
// #define ADC_I2C_ADDRESS 0x25 // Float High
// #define ADC_I2C_ADDRESS 0x24 // Float Float
// Global variables
static int16_t speed_mode = SLOW; //!< The ADC Speed Mode settings. Change it to "FAST" for higher data rate.
static float adc_vref = 2.5; //!< The ADC reference voltage
static uint8_t rejection_mode = ADC_R50; //!< The ADC rejection mode
static uint8_t i2c_address = ADC_I2C_ADDRESS; //!< I2C address in 7 bit format for part
static uint16_t eoc_timeout = 300; //!< Timeout in ms
static float adc_offset = 0;
//! Lookup table to build the command for single-ended mode
const uint16_t BUILD_COMMAND[4] = { ADC_CH0, ADC_CH1, ADC_CH2, ADC_CH3 };
void setup()
{
Wire.begin(); // wake up I2C bus
Serial.begin(9600); // Initialize the serial port to the PC
}
void loop()
{
uint8_t acknowledge = 0;
acknowledge |= read_analog_input();
if (acknowledge)
Serial.println(F("***** I2C ERROR *****"));
Serial.print(F("\n*************************\n"));
}
//! @return 0 if successful, 1 is failure
int8_t read_analog_input()
{
uint8_t adc_command_high; // The ADC command high byte
uint8_t adc_command_low; // The ADC command low byte
int32_t adc_code = 0; // The ADC code
float adc_voltage = 0; // The ADC voltage
float current_mA = 0; // 4-20mA current
while (1)
{
uint8_t ack = 0;
adc_command_high = BUILD_COMMAND[0]; // Build ADC command for channel 0
adc_command_low = rejection_mode| speed_mode;
ack |= adc_read( i2c_address, adc_command_high, adc_command_low, &adc_code, eoc_timeout ); // Throws out last reading
for (int8_t x = 0; x <= 3; x++)
{
adc_command_high = BUILD_COMMAND[(x + 1) % 4];
ack |= adc_read( i2c_address, adc_command_high, adc_command_low, &adc_code, eoc_timeout );
adc_voltage = adc_code_to_voltage( adc_code, adc_vref) + adc_offset + adc_vref/2;
current_mA = adc_voltage * 10;
if (!ack)
{
Serial.print(F(" ****"));
Serial.print(F("CH"));
Serial.print(x);
Serial.print(F(": "));
Serial.print(F("Received Code: 0x"));
Serial.print(adc_code, HEX);
Serial.print("; Current: ");
Serial.print(current_mA, 4);
Serial.print(F("mA\n"));
}
else
{
Serial.print(F(" ****"));
Serial.print(F("CH"));
Serial.print(x);
Serial.print(F(": "));
Serial.print(F("Error in read"));
return 1;
}
}
Serial.print(F("\n"));
delay(1000);
}
return(0);
}