I have a bit of a breakthrough
I rewired the whole thing to remove the pull up resistor, the capacitor across the power supply and also simplify the layout to keep things away from each other. I have run a load of tests with varying results - some fairly stable and some all over the place. At the moment, I have a stable environment with a repeatable problem. As mentioned before, I take 5 readings each time and then output the average. Looking at the details though the first read is wrong but the other 4 are broadly the same (within 2 ADC values). E.g. 23721, 23884, 23885, 23884.
The loop code is
Code: Select all
I2C_ADC_write(1, ADS1115_ADC2_CONFIG);
I2C_ADC_data [0] = 0;
I2C_ADC_data [1] = 0;
I2C_ADC_read(0, I2C_ADC_data, 1);
ESP_LOGI(BBQ_THERMOMETER_TAG, "DoTask: I2C_ADC 2 [%d][%dmV]", I2C_ADC_data[0], (I2C_ADC_data[0]*4096)/32767);
lADCTotal += I2C_ADC_data[0];
lVoltageTotal += round((I2C_ADC_data[0]*4096)/32767);
vTaskDelay(500 / portTICK_PERIOD_MS); // was 1000 reduced for testing
With the read / write functions as
Code: Select all
esp_err_t I2C_ADC_write(uint8_t regNumber, uint16_t value)
{
esp_err_t err = ESP_OK;
uint8_t bytes[3]; // Need to send 3 bytes: first the register number, then 16 bits of data for the register.
bytes[0] = regNumber; // First byte written selects the register
bytes[1] = (uint8_t)(value >> 8); // high byte
bytes[2] = (uint8_t)value; // low byte
err = i2c_master_transmit(dev_handle, &bytes[0], sizeof(bytes), 1000/portTICK_PERIOD_MS);
vTaskDelay(100 / portTICK_PERIOD_MS);
return err;
}
esp_err_t I2C_ADC_read(uint8_t nRegNumb, uint16_t *data, int len)
{
// ESP_LOGI(I2C_ADC_TAG, "I2C_ADC_read: About to do the transmit");
esp_err_t r = i2c_master_transmit(dev_handle, &nRegNumb, 1, 1000/portTICK_PERIOD_MS); // WRITE the register number
if (r == ESP_OK)
{
// ESP_LOGI(I2C_ADC_TAG, "I2C_ADC_read: About to do the receive");
uint8_t bytes[2];
r = i2c_master_receive(dev_handle, &bytes[0], sizeof(bytes), 1000/portTICK_PERIOD_MS); // READ two bytes of data from the register selected
if (r == ESP_OK)
{
*data = (((uint16_t)bytes[0]) << 8) | bytes[1]; // big-endian -> little-endian
// ESP_LOGI(I2C_ADC_TAG, "I2C_ADC_read: bytes0[%d] bytes1[%d] data[%d]", bytes[0], bytes[1], (uint16_t) *data);
}
}
return r;
}
The pause in the write was to fix an earlier problem and originally I had it at 10. I increased it to 100 to see if that was the problem but no joy.