Controlling ADC with new I2C version

MicroController
Posts: 2045
Joined: Mon Oct 17, 2022 7:38 pm
Location: Europe, Germany

Re: Controlling ADC with new I2C version

Postby MicroController » Sat Feb 08, 2025 5:30 pm

2. The ESP is powered from the USB port of my laptop and the ADC from the 3.3v port on the dev board.
Same here.

Circuit looks ok. Breadboard (and associated wires) however... also not famous for reliable connections.

(Hint: You may not need the extra pull-ups on SDA and SCL, the module has 10K pull-ups on board. And it already has at least two capacitors on the supply to the ADS. Another 100nF won't do harm, but it's not necessary; and, as you noticed, it can be one more source of potential problems.)

leenowell
Posts: 155
Joined: Tue Jan 15, 2019 1:50 pm

Re: Controlling ADC with new I2C version

Postby leenowell » Sat Feb 08, 2025 6:42 pm

I have a few probes (one of a different make) and they all seem to slowly reduce resistance when measured so assume this is just a "feature" of them which we need to work around. So that leaves 2 issues.
1. the random spikes. Wonder if moving to a soldered board may resolve that?
2. the time it takes to start giving good readings. This I no suggestions for :)

When you say the capacitor may not be needed, do you mean the one on the power supply? I seem to recall the spec saying I needed that to cater for the spike in current needed when it was processing. The one on the ADC pin seems to remove a lot of the noise we saw?

Out of interest I drew a graph of the individual values from the test with the probe and the capacitor (i.e. every reading rather than the averaged ones before) and that seems to suggest I am getting highly fluctuating results which the averaging is masking.
Screenshot from 2025-02-08 18-43-44.png
Screenshot from 2025-02-08 18-43-44.png (198.68 KiB) Viewed 1546 times

MicroController
Posts: 2045
Joined: Mon Oct 17, 2022 7:38 pm
Location: Europe, Germany

Re: Controlling ADC with new I2C version

Postby MicroController » Sat Feb 08, 2025 7:22 pm

Yes, the ADS module already has the required capacitor(s) on the supply voltage side the of the ADS1115; so one of the capacitors (the top one) in your diagram is not needed.

leenowell
Posts: 155
Joined: Tue Jan 15, 2019 1:50 pm

Re: Controlling ADC with new I2C version

Postby leenowell » Sun Feb 09, 2025 12:48 pm

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.

leenowell
Posts: 155
Joined: Tue Jan 15, 2019 1:50 pm

Re: Controlling ADC with new I2C version

Postby leenowell » Sun Feb 09, 2025 2:15 pm

The other odd thing is that if I power it from the USB port of my laptop it is stable but if I power it from my phone charger it is unstable again. Adding the capacitor across the power supply seems to help although still not as good as using the laptop. Also taking it out of the phone charger and straight into the laptop the laptop reads around 8 ADC higher!!

MicroController
Posts: 2045
Joined: Mon Oct 17, 2022 7:38 pm
Location: Europe, Germany

Re: Controlling ADC with new I2C version

Postby MicroController » Sun Feb 09, 2025 5:54 pm

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.
Still converting at 128 SPS?

leenowell
Posts: 155
Joined: Tue Jan 15, 2019 1:50 pm

Re: Controlling ADC with new I2C version

Postby leenowell » Sun Feb 09, 2025 6:07 pm

Yes still at 128 sps

MicroController
Posts: 2045
Joined: Mon Oct 17, 2022 7:38 pm
Location: Europe, Germany

Re: Controlling ADC with new I2C version

Postby MicroController » Mon Feb 10, 2025 7:45 am

leenowell wrote:
Sun Feb 09, 2025 2:15 pm
The other odd thing is that if I power it from the USB port of my laptop it is stable but if I power it from my phone charger it is unstable again.
Yeah, good power supply is important. After all, we're operating at the micro-Volt range of analog signals here.
If noise from the (switch-mode) power supply is an issue, one or more capacitors between Vcc and GND can indeed help. In this case, it may be best to filter power at the 5V side (pre-LDO), e.g. with something like 100nf+1uF+10uF in parallel.

leenowell
Posts: 155
Joined: Tue Jan 15, 2019 1:50 pm

Re: Controlling ADC with new I2C version

Postby leenowell » Mon Feb 10, 2025 9:15 am

The board is a dev board powered via USB so assume I would need to put the 3 capacitors on the 3.3v supply from the board to the ADC? I did some further testing last night and it ranged from very good (+- 1 with no spikes) to being all over the place. I am wondering if it is the breadboard causing half the problems so was thinking of soldering the circuit onto a board to see if that helps?

MicroController
Posts: 2045
Joined: Mon Oct 17, 2022 7:38 pm
Location: Europe, Germany

Re: Controlling ADC with new I2C version

Postby MicroController » Mon Feb 10, 2025 9:19 am

leenowell wrote:
Mon Feb 10, 2025 9:15 am
The board is a dev board powered via USB so assume I would need to put the 3 capacitors on the 3.3v supply from the board to the ADC?
Should work too. (You can also filter the 5V (USB) supply of the dev board at the board's 5V pin.)

Thinking about it, filtering the 3.3V between ESP and ADC may actually be better since this may also reduce more of the noise caused by the ESP itself.
was thinking of soldering the circuit onto a board to see if that helps?
Certainly worth trying.

Who is online

Users browsing this forum: No registered users and 135 guests