Adding Temperature Sensor Functionality To The CH32V003 MCU

As cheap as the WCH CH32V003 MCU is, its approximately $0.10 price tag looks far less attractive when you need to start adding on external ICs for missing basic features, such as temperature measurement. This is a feature that’s commonly found on even basic STM32 MCUs. Fear not though, as [eeucalyptus] shows, you can improvise a working solution by finding alternative sources that can act as a thermometer.

Plot of the temperature measurement using the improvised CH32V003 -based temperature sensor. (Credit: eeucalyptus)
Plot of the temperature measurement using the improvised CH32V003 -based temperature sensor. (Credit: eeucalyptus)

The CH32V003 is a low-end, 32-bit RISC-V-based MCU by the China-based Nanjing Qinheng Microelectronics, commonly known abbreviated as ‘WCH’, and featured on Hackaday previously. Although it features a single-core, 48 MHz CPU, its selection of peripherals is fairly basic:

So how do you create an internal temperature sensor using just this? [eeucalyptus] figured that all that’s needed is to measure the drift between two internal clocks – such as the LSI and HSI – as temperatures change and use this to calibrate a temperature graph.

Unfortunately, the LSI isn’t readily accessible, even through the Timer peripheral. This left the AWU (automatic wake-up unit) which also uses the LSI as a clock source. By letting it go to sleep and wake up after N LSI cycles, the AWU enabled indirect access to the LSI.

Internal diagram of the CH32V003 MCU. (Credit: WCH)
Internal diagram of the CH32V003 MCU. (Credit: WCH)

After calibrating against room temperature (~22 °C) and ice water (0 °C), a temperature plot was obtained, which could conceivably be somewhat accurate. As [eeucalyptus] warns, this is a kind of calibration that likely differs per MCU, and no attempt to quantify the absolute accuracy of this method has been made yet. Even so, as a crude temperature measurement, it might just be good enough.

15 thoughts on “Adding Temperature Sensor Functionality To The CH32V003 MCU

  1. Even the STM32 built-in temperature sensor has really poor accuracy, like +- 5°C. Adding NTC resistor to ADC pin costs 2 cents and gets you to +-1°C or better.

    Still a cool hack, though, and useful if you can’t modify the hardware :)

    1. 2 cents wow, you can 100 1N914 or 1N4148s for that and most uC pins have pull-ups
      Many uC’s have a temp sensor and don’t mention it or bury it deep in the data sheet. Even the ATMEGA328 has a temp sensor – not very accurate but it will tell you if the chips is heading out of spec.

  2. WDOG also runs with LSI as clock source and as per datasheet can be used as an independent timeout source handled in software. I may be biased since I used the same thing on an ATMega32u4 years ago for a crude RNG (RC oscillator noise vs XTAL), but isn’t the answer here not to ask a timer to have LSI as a source, but to use WDOG as the timer?

Leave a Reply

Please be kind and respectful to help make the comments section excellent. (Comment Policy)

This site uses Akismet to reduce spam. Learn how your comment data is processed.