I have a PI PICO W which I am developing code for amongst which is a TMP36 temperature sensor The code I have reused form other thermostat projects but its suddenly giving the odd false reading.
#define CHIP_TEMP_IO 28 #define CHIP_ADC 2 #define RELAY_IO 21 #define IN 0 #define OUT 1 #define ON 1 #define OFF 0
void initialise_hardware() { adc_init(); adc_gpio_init(CHIP_TEMP_IO); gpio_init(RELAY_IO); gpio_set_dir(RELAY_IO,OUT); } void set_relay(int flag) { cyw43_arch_gpio_put(CYW43_WL_GPIO_LED_PIN, flag); // set the lamp gpio_put(RELAY_IO,flag); //set the relay } float read_temperature() { static int index; static int flag; //initialised to zero at startup static uint32_t rawbuf[32]; const float conversion_factor = 3.3f / (1<<12); float result; float last; int i; uint32_t raw; adc_select_input(CHIP_ADC); raw = adc_read(); //wtf? printf ("Raw=%0X\n",raw);
result=((float)raw)*conversion_factor; return (100*result-54); // the pcb warms them up by about 4 degrees }
I am reading the temperature every two seconds as in while (1) { sleep_ms(2000); flag=(flag? OFF:ON); set_relay(flag); temperature = read_temperature(); printf("Temperature is %.1f C\n",temperature); }
about one time in ten the output jumps to about 20°C less than expected e.g.
Raw=3BE Temperature is 23.2 C Raw=3BE Temperature is 23.2 C Raw=3BE Temperature is 23.2 C Raw=29B Temperature is -0.3 C /************************ Raw=3C0 Temperature is 23.3 C Raw=3C0 Temperature is 23.3 C Raw=3BF Temperature is 23.3 C Raw=3BF Temperature is 23.3 C Raw=3BF Temperature is 23.3 C Raw=3C1
I have added decoupling to the TMP36 supply which is fed from the ADC
3.3v supply which seems to be a steady 3.29V according to my DVM.
Do I have a bad PICO W? TMP 36 or what?
Other PICOs with the same chip don't do this... The only difference is that this PICO is running different and a lot more wifi/server code Is it possible that some interrupt is - er - interrupting the ADC read with dire results?
I cannot but think this is a hardware issue.