初学者
10 分钟

使用SHT11和STM32F410RB实现可靠的湿度和温度测量

从环境监测到热管理

SHT1x Click with Nucleo 64 with STM32F410RB MCU

已发布 10月 08, 2024

点击板

SHT1x Click

开发板

Nucleo 64 with STM32F410RB MCU

编译器

NECTO Studio

微控制器单元

STM32F410RB

在环境条件直接影响功能、产品质量或用户舒适度的应用中,这是一种非常宝贵的解决方案。

A

A

硬件概览

它是如何工作的?

SHT1x Click基于Sensirion的湿度和温度传感器SHT11。SHT11是一种强大而可靠的传感器,即使在超出正常范围的条件下,一旦条件稳定,它也可以重新校准自身。该传感器在推荐的正常温度范围内(-20°C至100°C)运行时性能最佳。长期暴露于正常范围之外的条件可能会暂时偏移RH信号。在返回正常温度范围后,传感器将自行缓慢恢复到校准状态。此外,请注意,长时间暴露于极端条件可能会加速老 化。尽管典型的相对湿度分辨率为12位,温度读数为

14位,但SHT11上的两个传感器都无缝地耦合到14位ADC。相对湿度传感器使用独特的电容式传感器元件,而带隙传感器测量温度。SHT11在精密湿度室中单独校准,并且校准系数存储在板载OTP存储器中。SHT1x Click通过mikroBUS™插座上的I2C接口与主机MCU通信,通信速度高达1MHz。SHT1x Click没有复位引脚;如果与传感器的通信中断,可以通过信号 序列重新设置传感器。虽然SHT1x Click不能直接测量露点,但可以使用湿度和温度读数计算露点。这是可

能的,因为同一块单片芯片测量湿度和温度。但是,重要的是要注意,传感器对光不敏感,长时间暴露于强烈的紫外辐射可能会导致外壳随时间的推移而劣化。此Click板可以通过PWR SEL跳线选择3.3V或5V逻辑电压级别进行操作。这样,既支持3.3V又支持5V的MCU可以正确使用通信线路。此外,此Click板配备了一个包含易于使用的函数和示例代码的库,可用作进一步开发的参考。

SHT1x Click hardware overview image

功能概述

开发板

Nucleo-64 搭载 STM32F410RB MCU 提供了一种经济高效且灵活的平台,供开发者探索新想法并原型设计他们的项目。该板利用 STM32 微控制器的多功能性,使用户能够为他们的项目选择最佳的性能与功耗平衡。它配备了 LQFP64 封装的 STM32 微控制器,并包含了如用户 LED(同时作为 ARDUINO® 信号)、用户和复位按钮,以及 32.768kHz 晶体振荡器用于精确的计时操作等基本组件。Nucleo-64 板设计考虑到扩展性和灵活性,它特有的 ARDUINO® Uno

V3 扩展连接器和 ST morpho 扩展引脚头,提供了对 STM32 I/O 的完全访问,以实现全面的项目整合。电源供应选项灵活,支持 ST-LINK USB VBUS 或外部电源,确保在各种开发环境中的适应性。该板还配备了一个具有 USB 重枚举功能的板载 ST-LINK 调试器/编程器,简化了编程和调试过程。此外,该板设计旨在简化高级开发,它的外部 SMPS 为 Vcore 逻辑供电提供高效支持,支持 USB 设备全速或 USB SNK/UFP 全速,并内置加密功能,提升了项目的功效

和安全性。通过外部 SMPS 实验的专用连接器、 用于  ST-LINK 的 USB 连接器以及 MIPI® 调试连接器,提供了更多的硬件接口和实验可能性。开发者将通过 STM32Cube MCU Package 提供的全面免费软件库和示例得到广泛支持。这些,加上与多种集成开发环境(IDE)的兼容性,包括 IAR Embedded Workbench®、MDK-ARM 和 STM32CubeIDE,确保了流畅且高效的开发体验,使用户能够充分利用 Nucleo-64 板在他们的项目中的能力。

Nucleo 64 with STM32C031C6 MCU double side image

微控制器概述 

MCU卡片 / MCU

default

建筑

ARM Cortex-M4

MCU 内存 (KB)

128

硅供应商

STMicroelectronics

引脚数

64

RAM (字节)

32768

你完善了我!

配件

Click Shield for Nucleo-64 配备了两个专有的 mikroBUS™ 插座,使得所有的 Click board™ 设备都可以轻松地与 STM32 Nucleo-64 开发板连接。这样,Mikroe 允许其用户从不断增长的 Click boards™ 范围中添加任何功能,如 WiFi、GSM、GPS、蓝牙、ZigBee、环境传感器、LED、语音识别、电机控制、运动传感器等。您可以使用超过 1537 个 Click boards™,这些 Click boards™ 可以堆叠和集成。STM32 Nucleo-64 开发板基于 64 引脚封装的微控制器,采用 32 位 MCU,配备 ARM Cortex M4 处理器,运行速度为 84MHz,具有 512Kb Flash 和 96KB SRAM,分为两个区域,顶部区域代表 ST-Link/V2 调试器和编程器,而底部区域是一个实际的开发板。通过 USB 连接方便地控制和供电这些板子,以便直接对 Nucleo-64 开发板进行编程和高效调试,其中还需要额外的 USB 线连接到板子上的 USB 迷你接口。大多数 STM32 微控制器引脚都连接到了板子左右边缘的 IO 引脚上,然后连接到两个现有的 mikroBUS™ 插座上。该 Click Shield 还有几个开关,用于选择 mikroBUS™ 插座上模拟信号的逻辑电平和 mikroBUS™ 插座本身的逻辑电压电平。此外,用户还可以通过现有的双向电平转换器,使用任何 Click board™,无论 Click board™ 是否在 3.3V 或 5V 逻辑电压电平下运行。一旦将 STM32 Nucleo-64 开发板与我们的 Click Shield for Nucleo-64 连接,您就可以访问数百个工作于 3.3V 或 5V 逻辑电压电平的 Click boards™。

Click Shield for Nucleo-64 accessories 1 image

使用的MCU引脚

mikroBUS™映射器

NC
NC
AN
NC
NC
RST
NC
NC
CS
NC
NC
SCK
NC
NC
MISO
NC
NC
MOSI
Power Supply
3.3V
3.3V
Ground
GND
GND
NC
NC
PWM
NC
NC
INT
NC
NC
TX
NC
NC
RX
I2C Clock
PB8
SCL
I2C Data
PB9
SDA
Power Supply
5V
5V
Ground
GND
GND
1

“仔细看看!”

Click board™ 原理图

SHT1x Click Schematic schematic

一步一步来

项目组装

Click Shield for Nucleo-64 accessories 1 image hardware assembly

从选择您的开发板和Click板™开始。以Nucleo 64 with STM32F410RB MCU作为您的开发板开始。

Click Shield for Nucleo-64 accessories 1 image hardware assembly
Nucleo 64 with STM32F401RE MCU front image hardware assembly
LTE IoT 5 Click front image hardware assembly
Prog-cut hardware assembly
Board mapper by product8 hardware assembly
Necto image step 2 hardware assembly
Necto image step 3 hardware assembly
Necto image step 4 hardware assembly
Necto image step 5 hardware assembly
Necto image step 6 hardware assembly
Clicker 4 for STM32F4 HA MCU Step hardware assembly
Necto No Display image step 8 hardware assembly
Necto image step 9 hardware assembly
Necto image step 10 hardware assembly
Debug Image Necto Step hardware assembly

软件支持

库描述

这个库包含 SHT1x Click 驱动程序的 API。

关键函数:

  • sht1x_output_sda - 设置引脚为输出模式。

  • sht1x_input_sda - 设置引脚为输入模式。

  • sht1x_sda_high - 设置 SDA 高电平的功能。

开源

代码示例

完整的应用程序代码和一个现成的项目可以通过NECTO Studio包管理器直接安装到NECTO Studio 应用程序代码也可以在MIKROE的GitHub账户中找到。

/*!
 * \file 
 * \brief SHT1x Click example
 * 
 * # Description
 * This Click measures temperature and humidity.
 *
 * The demo application is composed of two sections :
 * 
 * ## Application Init 
 * Initialization driver enables GPIO.
 * 
 * ## Application Task  
 * This example demonstrates the use of SHT1x Click board by measuring 
   temperature and humidity, and displays the results on USART terminal.
 * 
 * 
 * \author MikroE Team
 *
 */
// ------------------------------------------------------------------- INCLUDES

#include "board.h"
#include "log.h"
#include "sht1x.h"

// ------------------------------------------------------------------ VARIABLES

static sht1x_t sht1x;
static log_t logger;

static sht1x_cfg_t cfg;

static uint8_t i;
static uint8_t data_val;
static uint8_t err;
static uint8_t msb;
static uint8_t lsb;
static uint8_t checksum;
static uint16_t t;
static uint16_t h;
static float value;
static uint8_t uc_sens_err;
static int16_t int_temp;
static int16_t int_humi;
static float humidity;
static float temperature;

// ------------------------------------------------------ APPLICATION FUNCTIONS

void sht1x_trans_start ( )
{
    sht1x_output_sda ( &sht1x, &cfg );

    sht1x_sda_high( &sht1x );

    sht1x_input_sda ( &sht1x, &cfg );

    sht1x_scl_low( &sht1x );

    Delay_us( 1 );

    sht1x_scl_high( &sht1x );

    Delay_us( 1 );

    sht1x_output_sda ( &sht1x, &cfg );

    sht1x_sda_low( &sht1x );

    Delay_us( 1 );

    sht1x_scl_low( &sht1x );

    Delay_us( 3 );

    sht1x_scl_high( &sht1x );

    Delay_us( 1 );

    sht1x_input_sda ( &sht1x, &cfg );

    Delay_us( 1 );

    sht1x_scl_low( &sht1x );
}

uint8_t sht1x_read_byte( uint8_t ack )
{
    i = 0x80;
    data_val = 0;

    sht1x_output_sda ( &sht1x, &cfg );

    sht1x_sda_high( &sht1x );

    sht1x_input_sda ( &sht1x, &cfg );

    sht1x_scl_low( &sht1x );

    while( i )
    {
        sht1x_scl_high( &sht1x );
        Delay_us( 1 );
        if ( sht1x_get_sda( &sht1x ) == 1 )
        {
            data_val = ( data_val | i );
        }
        sht1x_scl_low( &sht1x );
        Delay_us( 1 );
        i = ( i >> 1 );
    }

    sht1x_output_sda ( &sht1x, &cfg );

    if( ack )
    {
        sht1x_sda_low( &sht1x );
    }
    else
    {
        sht1x_sda_high( &sht1x );
    }

    sht1x_scl_high( &sht1x );
    Delay_us( 3 );
    sht1x_scl_low( &sht1x );
    Delay_us( 1 );

    sht1x_sda_high( &sht1x );

    sht1x_input_sda ( &sht1x, &cfg );

    return data_val;
}

uint8_t sht1x_write_byte( uint8_t value )
{
    i = 0x80;
    err = 0;

    sht1x_output_sda ( &sht1x, &cfg );

    while( i )
    {
        if ( i & value )
        {
            sht1x_sda_high( &sht1x );
        }
        else
        {
            sht1x_sda_low( &sht1x );
        }

        sht1x_scl_high( &sht1x );
        Delay_us( 3 );
        sht1x_scl_low( &sht1x );
        Delay_us( 3 );
        i = ( i >> 1 );
    }

    sht1x_sda_high( &sht1x );
    sht1x_input_sda ( &sht1x, &cfg );

    sht1x_scl_high( &sht1x );
    Delay_us( 3 );
    if ( sht1x_get_sda( &sht1x ) == 1 )
	{
	    err = 1;
	}
    Delay_us( 1 );
    sht1x_scl_low( &sht1x );

    return err;
}

uint8_t sht1x_measure( uint16_t *p_val, uint8_t mode )
{
    i = 0;
    *p_val = 0;
    
    sht1x_trans_start(  );

    if( mode )
    {
        mode = SHT1X_MEAS_HUMI;
    }
    else
    {
        mode = SHT1X_MEAS_TEMP;
    }

    if( sht1x_write_byte( mode ) )
    {
        return( 1 );
    }

    sht1x_input_sda ( &sht1x, &cfg );

    while( i < 240 )
    {
        Delay_ms ( 3 );
        if ( sht1x_get_sda( &sht1x ) == 0 )
        {
            i = 0;
            break;
        }
        i++;
    }

    if( i )
    {
        return( 2 );
    }

    msb = sht1x_read_byte( SHT1X_ACK );
    lsb = sht1x_read_byte( SHT1X_ACK );
    checksum = sht1x_read_byte( SHT1X_NACK );

    *p_val = ( msb << 8 ) | lsb ;

    return( 0 );
}

void sht1x_read_results ( float *f_t, float *f_rh )
{
    uc_sens_err = 0;

    uc_sens_err = sht1x_measure( &t, 0 );
    int_temp = ( int16_t )( sht1x_calc_temp( &sht1x, t ) * 10 );

    uc_sens_err = sht1x_measure(&h, 1);
    int_humi = ( int16_t )( sht1x_calc_humi( &sht1x, h, t ) * 10 );

    value = ( float )int_temp;
    *f_t = value / 10;
    value = ( float )int_humi;
    *f_rh = value / 10;
}

uint8_t sht1x_rd_stat_reg ( uint8_t *p_val )
{
    checksum = 0;

    sht1x_trans_start( );
    if( sht1x_write_byte( SHT1X_STAT_REG_R ) )
    {
        return 1;
    }
    *p_val = sht1x_read_byte( SHT1X_ACK );
    checksum = sht1x_read_byte( SHT1X_NACK );

    return 0;
}

uint8_t sht1x_wr_stat_reg ( uint8_t value )
{
    sht1x_trans_start();
    if( sht1x_write_byte( SHT1X_STAT_REG_W ) )
    {
        return 1;
    }
    if( sht1x_write_byte( value ) )
    {
        return 1;
    }

    return 0;
}

void sht1x_connection_reset ( )
{
    sht1x_output_sda ( &sht1x, &cfg );

    sht1x_sda_high( &sht1x );

    sht1x_input_sda ( &sht1x, &cfg );

    sht1x_scl_low( &sht1x );

    for( i = 0; i < 9; i++ )
    {
        sht1x_scl_high( &sht1x );
        Delay_us( 3 );
        sht1x_scl_low( &sht1x );
        Delay_us( 3 );
    }

    sht1x_trans_start( );
}

uint8_t sht1x_soft_reset ( )
{
    sht1x_connection_reset( );

    return ( sht1x_write_byte( SHT1X_SOFT_RESET ) );
}

void application_init ( void )
{
    log_cfg_t log_cfg;
    
    /** 
     * Logger initialization.
     * Default baud rate: 115200
     * Default log level: LOG_LEVEL_DEBUG
     * @note If USB_UART_RX and USB_UART_TX 
     * are defined as HAL_PIN_NC, you will 
     * need to define them manually for log to work. 
     * See @b LOG_MAP_USB_UART macro definition for detailed explanation.
     */
    LOG_MAP_USB_UART( log_cfg );
    log_init( &logger, &log_cfg );
    log_info(&logger, "---- Application Init ----");

    //  Click initialization.

    sht1x_cfg_setup( &cfg );
    SHT1X_MAP_MIKROBUS( cfg, MIKROBUS_1 );
    sht1x_init( &sht1x, &cfg );
}

void application_task ( void )
{
    sht1x_read_results( &temperature, &humidity );
    log_printf( &logger, " Temperature: %.2f ", temperature );
    log_printf( &logger, " C \r\n" );
  
    log_printf( &logger, " Humidity: %.2f ", humidity );
    log_printf( &logger, " \r\n" );
    
    Delay_ms ( 1000 );
}

int main ( void ) 
{
    /* Do not remove this line or clock might not be set correctly. */
    #ifdef PREINIT_SUPPORTED
    preinit();
    #endif
    
    application_init( );
    
    for ( ; ; ) 
    {
        application_task( );
    }

    return 0;
}

// ------------------------------------------------------------------------ END

额外支持

资源

喜欢这个项目吗?

'购买此套件' 按钮会直接带您进入购物车,您可以在购物车中轻松添加或移除产品。