Error reading Pico W adc4 onboard temp sensor after using DMA on adc1

Hello I am using the Pico W to display meters and miniscopes and all is working fine. I also read the onboard temperature sensor using ADC4 as below:

from machine import ADC
adc4 = machine.ADC(4) # Pico temp sensor

def ReadTemperature(): # does not work after using ADC-DMA sampling
volt = (adc4.read_u16() * 3.3)/65535
temperature = 27 - (volt - 0.706)/0.001721
return round(temperature, 1)

print (‘\nPico Temperature =’, ReadTemperature())

This works perfectly and gives the correct value, eg 30 deg.

I can use ADC0, 1, 2 for sampling sensors and the above always returns the correct results.

However, when I sample using DMA techniques on ADC1 (could be when using any ADC?) to get fast sampling into a buffer to display on an LCD to produce a scope display, after I have exited the scope routine and read the temperature again it gives a false reading of 95 or thereabouts.

If I restart, it produces the correct results until after the scope display. It is repeatedable.

I do not know what causes the fault after DMA and wondered if any of you had come across this problem. From this forum I am aware of having to use a different technique when reading ADC3 (= VSys/3) using a Pico W as the W version uses the same GPIO as ADC3 so the pad has to be saved and restored and so wondered if ADC4 has some similar issue when using DMA.

Thanks for looking, Steve

There is a whole section on DMA and ADC in the RP2040 datasheet: 4.9.3.5. Maybe it helps to have look. Seems a bit complicated what you have to do in which order to get things right.

Yes, it does seem complicated and I have ground to a halt!
Thanks

I have found a simple fix, shared in case others come across the problem.

TO READ TEMPERATURE on ADC4 when using ADC-DMA sampling reset adc setting each access

because ADC-DMA sampling on another channel corrupts ADC4 setting

def ReadADC4_Temperature(): # If using ADC-DMA sampling this ADC setting gets corrupted
adc4 = machine.ADC(4) # so always redefine each time it is used
volt = (adc4.read_u16() * 3.3)/65535
temperature = 27 - ((volt - 0.706)/0.001721)
return round(temperature, 1)

Could anyone let me know how to fix the code text formatting when code is entered in the forum, thanks

You can use the button in the editor labeled “</>” (CTRL-e) for inline code-formatting or just include everything in three backward ticks at the beginning or end for block code-formatting. Third option: indent the complete code four spaces, leaving an empty line above and below.