Skip to content

Contributed by WildboarG, organized by Ai-Thinker

Let's Monitor the Ai-WB2 Device Temperature Together

Let's Monitor the WB2 Device Temperature Together


Description


When you open any terminal device, people usually like to run neofetch first to print the device status, such as memory, disk, GPU driver, kernel, boot time, temperature and so on. Monitoring device status is a good habit. It prevents abnormal device status from causing permanent damage.

Our WB2 also comes with temperature detection, so we can use the ADC to sample the internal temperature of the device, feed it back to our operating system or make it alarm, to avoid the chip burning out due to high temperature (especially in summer)

ADC


ADC (Analog-to-Digital Converter) is an electronic component that converts analog signals (such as temperature, pressure, light, etc.) into digital signals. Its main role is to enable a microcontroller to process analog signals from the physical world and analyze, control or display them through digital processing. Here are several common uses of the ADC:

  • The voltage output by temperature sensors (such as thermistors and thermocouples) changes with temperature; read the voltage with the ADC, then calculate the temperature value through digital processing.
  • The voltage change of a photoresistor can be read through the ADC and converted into light intensity data.
  • The analog signal output by a sound sensor or microphone needs to be converted into a digital signal through the ADC, then used for audio processing and digital audio processing (e.g., speech recognition, noise cancellation), etc.
  • A potentiometer can be used to adjust volume, brightness, etc.; the ADC reads the analog output of the potentiometer and converts it into a digital value for the system to use.
  • In fields such as wireless communication, medical instruments, and industrial measurement, the ADC is used to collect analog signals (such as sensor data) and perform data analysis, calculation and other operations.
  • In motor control systems, the ADC reads the voltage or current signal of the motor to adjust the motor's speed or direction.

ADC functional block diagram

This is the internal structure of the WB2 ADC:

  1. Front-end input channel selector
  2. Programmable gain amplifier
  3. ADC sampling module
  4. Data processing module
  5. FIFO

WB2 analog channel count:

  • 12 external analog channels (maximum)
  • 2 internal DAC channels
  • 1 VBAT channel
  • 1 internal temperature channel

Mapping between external channels and GPIO

Common APIs


Functions

Click to expand full code
c
int

hosal_adc_init
(
hosal_adc_dev_t
 *adc)

初始化ADC接口准备用于采样的 ADC 硬件接口

Return value

  • 0 Success
  • EIO (if any step encounters an error)

Parameters

  • adc: adc device block
Click to expand full code
c
int

hosal_adc_add_channel
(
hosal_adc_dev_t
 *adc,
uint32_t
 channel)

添加向ADC设备添加ADC通道

Return value

  • 0 Success
  • EIO (if any step encounters an error)

Parameters

  • adc: adc device block
  • channel: the channel for ADC sampling
Click to expand full code
c
int

hosal_adc_remove_channel
(
hosal_adc_dev_t
 *adc,
uint32_t
 channel)

移除ADC通道

Return value

  • 0 Success
  • EIO (if any step encounters an error)

Parameters

  • adc: adc device block
  • channel: the channel to remove from the ADC
Click to expand full code
c
hosal_adc_dev_t
 *
hosal_adc_device_get
(
void
)

获取ADC句柄

Parameters

  • other The ADC device was obtained successfully
  • NULL Failure
Click to expand full code
c
int

hosal_adc_value_get
(
hosal_adc_dev_t
 *adc,
uint32_t
 channel,
uint32_t
 timeout)

取单个ADC采样结果

Return value

  • other ADC data obtained successfully
  • -1 Failure

Parameters

  • adc: ADC device handle
  • channel: ADC channel
  • timeout: timeout period
Click to expand full code
c
int

hosal_adc_tsen_value_get
(
hosal_adc_dev_t
 *adc)

采集WB2设备温度

Return value

  • other ADC data obtained successfully
  • -1 Failure

Parameters

  • adc: ADC device handle
Click to expand full code
c
int

hosal_adc_sample_cb_reg
(
hosal_adc_dev_t
 *adc,
hosal_adc_cb_t
 cb)

注册ADC回调函数

Return value

  • 0 Success
  • EIO Failure

Parameters

  • adc: ADC device handle
  • cb: a non-null pointer is a sample callback handler; a NULL pointer in cb is used to send the unregister operation. The adc must be the same pointer as the one passed to hosal_adc_sample_cb_reg. If the ADC data is ready in hardware or memory (DMA), the upper layer must be notified by calling cb

For more, click API

Example


Next, let's configure the ADC and use the internal temperature sampling channel to monitor the device.

  1. First, allocate an ADC block device (or handle) and fill in the parameters to be set
Click to expand full code
c
static

hosal_adc_dev_t
 adc0 = {
//申请句柄并初始化

      .cb =
NULL
,
      .config ={
              .mode = HOSAL_ADC_ONE_SHOT,
              .pin =
12
,

//这里针脚可以任意选只要是ADC的就行

              .sampling_freq =
340
,
          },
      .dma_chan =
0
,
      .p_arg =
NULL
,
      .port =
0
,
  };
  1. Then initialize this ADC block device
Click to expand full code
c
hosal_adc_init(&adc0);
//初始化ADC
  1. Add an adc channel
Click to expand full code
c
hosal_adc_add_channel(&adc0,
14
);
  1. Then enable the adc This is not implemented in the official API, but the adc is already started in the read function
  2. Wait a moment, then you can read the digitized ADC value
Click to expand full code
c
temp = hosal_adc_tsen_value_get(&adc0);
// 获取板子温度

完整代码

Click to expand full code
c
#
include

<FreeRTOS.h>

#
include

<blog.h>

#
include

<hosal_adc.h>

#
include

<stdio.h>

#
include

<string.h>

#
include

<task.h>

/**********   BL602  ************
 *    channel0   ----->     gpio12
 *    channel1   ----->     gpio4
 *    channel2   ----->     gpio14
 *    channel3   ----->     gpio13
 *    channel4   ----->     gpio5
 *    channel5   ----->     gpio6
 *    channel7   ----->     gpio9
 *    channel9   ----->     gpio10
 *    channel10  ----->     gpio11
 *    channel11  ----->     gpio15
 */

#
define
 GPIO_ADC_PIN 11

#
define
 ADC_TEMPCHANNEL 14

void

main
(
void
)
 {

static

hosal_adc_dev_t
 adc0 = {
      .cb =
NULL
,
      .config =
          {
              .mode = HOSAL_ADC_ONE_SHOT,
              .pin =
12
,
              .sampling_freq =
340
,
          },
      .dma_chan =
0
,
      .p_arg =
NULL
,
      .port =
0
,
  };

  hosal_adc_init(&adc0);
  hosal_adc_add_channel(&adc0, ADC_TEMPCHANNEL);

for
 (;;) {

int
 temp = hosal_adc_tsen_value_get(&adc0);
// 获取板子温度

    blog_info(
"BOARD temp = %d C\r\n"
, temp);

    vTaskDelay(pdMS_TO_TICKS(
5000
));
  }
}

Then flash it into the board.

Error!!!

Looking at the API implementation, I found a bunch of conditional compilation around CONF_ADC_ENABLE_TSEN

Click to expand full code
c
#
ifdef
 CONF_ADC_ENABLE_TSEN

#
define
 ADC_CHANNEL_MAX 11
/*CHANNEL14 FOR Tsen*/

#
else

#
define
 ADC_CHANNEL_MAX 10

#
endif

Here it tells us that the channel used for the internal temperature is 14, which is why I added ADC channel 14 just now.

It seems this feature only takes effect when the macro is enabled, so add it manually.

Click to expand full code
c
#
define
 CONF_ADC_ENABLE_TSEN 1

Then compile again successfully and check via the serial port

The internal temperature can now be read

Released under the MIT License. Build Time 2026-09-11 14:52:23