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.
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.
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.
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 :)
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.
This!
Just got my ch32v003 in SOP16 format, soldered one on a breakout yesterday. I want to port the stm32f0 flasher to a bluepill f103, if anybody wants to help…
Hop on the ch32v003fun Discord with any questions! That said – I’ve got my ESP32-S2 based programmers going so I’m cruising.
Discovered there was also a pico rp2040 based flasher, but looks like a BlackMagicProbe (BMP) with a GDB endpoint and a serial console:
https://github.com/aappleby/PicoRVD
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?
Just add a simple diode and resisitor to an analogue input as described here: https://www.homemade-circuits.com/how-to-use-diodes-transistors-ics-as-temperature-sensors/
I think much easier, but costs an I/O and adds significant costs to an $0.10 MCU ;)
What’s better than a graph with no unit ?
A giraffe with a unit?
B^)
I would be a bit scared about how consistent that behavior is from device to device…
Good thing the article mentions that.
I really hope they can instrument and quantify the accuracy of using this method. I looove stuff like this. ALSO, I wonder if case deflection changes significantly the measured values.
For anyone curious – Yes. Torsion causes significant impact, as much as the equivalent as 20* C!
I guess that means adding a thermistor would enable building a digital load cell, hehe