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

**URL:** <https://forums.pimoroni.com/t/error-reading-pico-w-adc4-onboard-temp-sensor-after-using-dma-on-adc1/24187>\
**Category:** Support\
**Created:** [February 15, 2024, 1:43pm UTC](https://forums.pimoroni.com/t/error-reading-pico-w-adc4-onboard-temp-sensor-after-using-dma-on-adc1/24187 "2024-02-15T13:43:36Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![SteveA](https://sea2.discourse-cdn.com/flex016/user_avatar/forums.pimoroni.com/stevea/32/9294_2.png) [@SteveA](https://forums.pimoroni.com/u/SteveA)\
**Post date:** [February 15, 2024, 1:43pm UTC](https://forums.pimoroni.com/t/error-reading-pico-w-adc4-onboard-temp-sensor-after-using-dma-on-adc1/24187/1 "2024-02-15T13:43:36Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![bablokb](https://avatars.discourse-cdn.com/v4/letter/b/ee59a6/32.png) [@bablokb](https://forums.pimoroni.com/u/bablokb)\
**Post date:** [February 15, 2024, 5:20pm UTC](https://forums.pimoroni.com/t/error-reading-pico-w-adc4-onboard-temp-sensor-after-using-dma-on-adc1/24187/2 "2024-02-15T17:20:37Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![SteveA](https://sea2.discourse-cdn.com/flex016/user_avatar/forums.pimoroni.com/stevea/32/9294_2.png) [@SteveA](https://forums.pimoroni.com/u/SteveA)\
**Post date:** [February 15, 2024, 5:41pm UTC](https://forums.pimoroni.com/t/error-reading-pico-w-adc4-onboard-temp-sensor-after-using-dma-on-adc1/24187/3 "2024-02-15T17:41:15Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![SteveA](https://sea2.discourse-cdn.com/flex016/user_avatar/forums.pimoroni.com/stevea/32/9294_2.png) [@SteveA](https://forums.pimoroni.com/u/SteveA)\
**Post date:** [February 17, 2024, 3:32pm UTC](https://forums.pimoroni.com/t/error-reading-pico-w-adc4-onboard-temp-sensor-after-using-dma-on-adc1/24187/4 "2024-02-17T15:32:32Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![bablokb](https://avatars.discourse-cdn.com/v4/letter/b/ee59a6/32.png) [@bablokb](https://forums.pimoroni.com/u/bablokb)\
**Post date:** [February 19, 2024, 9:00am UTC](https://forums.pimoroni.com/t/error-reading-pico-w-adc4-onboard-temp-sensor-after-using-dma-on-adc1/24187/5 "2024-02-19T09:00:53Z")

</div>

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.
