STM32F103C8T6 GY-906 MLX90614ESF 无线测温传感器模块的使用方法和代码驱动
文档结尾有代码下载链接
🎯 实现效果



🌟 传感器用途
1. 传感器原理
MLX90614ESF 基于黑体辐射定律实现非接触温度测量 —— 任何温度高于绝对零度(-273.15°C)的物体都会向外辐射红外线,辐射的能量强度与物体温度直接相关。传感器通过接收物体的红外辐射,反向计算出物体的温度。
其内部结构与信号处理流程决定了测温精度和稳定性,具体可分为 5 个关键环节:
- 核心硬件:传感器最核心的部件是集成在芯片内的热电堆(Thermopile),它是一种特殊的红外接收元件,由多个 “热电偶” 串联组成(通常 16~32 对)。工作逻辑:当物体的红外辐射穿过传感器的光学窗口照射到热电堆上时,热电堆的 “热端”(接收红外的一面)与 “冷端”(芯片内部参考温度面)会产生温度差。物理效应:根据 “塞贝克效应”,温度差会在热电堆两端产生微弱的直流电压信号(通常几十到几百微伏,μV 级别),且电压大小与红外辐射的能量强度(即物体温度)成正比。
- 信号调理:热电堆输出的电压信号极微弱,且易受环境噪声影响,无法直接用于计算。因此,芯片内集成了低噪声运算放大器和信号滤波电路:先通过放大器将 μV 级信号放大到 mV 级甚至 V 级,确保信号强度足够;再通过滤波电路(如低通滤波器)滤除高频噪声,保留与红外辐射相关的有效信号。
- 温度参考:热电堆的输出电压不仅与 “物体红外辐射” 相关,还会受 “传感器自身环境温度(Tamb)” 影响。因此,芯片内必须集成环境温度传感器:实时测量传感器自身的环境温度,将其作为 “参考基准”;后续计算时,通过硬件或软件算法抵消环境温度对热电堆信号的干扰,确保即使环境温度变化,测温结果仍准确。
- 模数转换与数据计算:经过放大、滤波和补偿后的信号仍是模拟信号,需要转化为数字信号才能被处理。芯片内的16 位高精度 ADC会将模拟电压信号转换为数字值,再传递给内置的8 位 MCU。MCU 的核心作用是应用校准算法与黑体辐射公式:调用存储在片内 EEPROM中的 “校准参数”;根据 “斯蒂芬 - 玻尔兹曼定律”,结合热电堆的数字信号和环境温度参考值,反向计算出物体的绝对温度;将绝对温度转换为常用的摄氏温度(°C = Tobj - 273.15),同时计算出环境温度(Tamb)。
- 数据输出:MCU 计算完成后,将 “物体温度” 和 “环境温度” 通过芯片的标准接口输出给外部设备。
2. 传感器用途
a.消费电子领域
- 家电控制:微波炉、烤箱的食物温度监测,空调的人体体温感应;
- 智能穿戴:智能手表、手环的体表温度监测;
- 家居安防:红外测温门锁。
b.医疗健康领域
- 非接触体温筛查:医院、商场的快速体温检测,儿童体温计;
- 设备温度监测:医疗仪器关键部件的温度监测。
c. 工业与自动化领域
- 设备故障诊断:电机、变频器、电路板的表面温度监测;
- 生产过程控制:食品加工中流水线的温度监测;
- 汽车电子:车载电池温度监测,发动机舱关键部件的温度预警。
d.特殊场景应用
- 冷链物流:冷藏箱、冷链车内部温度的实时监测;
- 火灾预警:配电室、数据中心的设备温度巡检;
- 宠物护理:宠物窝的温度监测。
🌟 传感器的介绍
GY-906 是基于 MLX90614ESF 芯片的模块化产品,集成红外探测器、信号调理电路及I2C 通信接口,无需额外外围元件即可工作,其核心特性与参数如下:
1. 关键参数
- 测量范围:物体温度 - 70℃ - 380℃,部分型号支持 - 70℃- 500℃;环境温度 - 40℃ - 125℃;
- 测量精度:物体温度在 - 20℃~120℃范围内 ±0.5℃,全量程 ±1℃;
- 分辨率:0.02℃;
- 测量距离比:10:1;
- 通信接口:I2C 总线,速率最高 100kHz,支持双地址,默认 0x5A,由 ADR 引脚控制;
- 功耗:工作电流典型值 1.5mA,待机电流<1μA;
- 工作电压:2.5V~3.6V;
- 封装:TO-39 金属封装,内置红外滤光片,工作温度 - 40℃~125℃。
2. 模块特点
- 高集成度:模块已集成 MLX90614ESF 芯片、上拉电阻及稳压电路,仅需 4 根线即可与单片机连接,大幅简化硬件设计;
- 低功耗:1.5mA 工作电流适合电池供电设备;
- 抗干扰能力:金属封装与内置滤光片可减少环境光对红外探测的干扰,确保测量稳定;
- 即插即用:无需复杂校准,上电后即可通过通信接口读取温度数据,适配主流单片机。
🎯 单片机连接硬件图



🌟 驱动思路
步骤 1:硬件初始化
- 首先进行 I2C 接口的初始化。配置 STM32F103C8T6 的 I2C,设置通信速率为 400kHz,使能 I2C 外设。在初始化过程中,需要配置相关的 GPIO 引脚为复用功能,设置 I2C 的时钟频率、地址模式等参数,确保 I2C 通信能够正常进行。接着进行传感器的上电检测。
步骤 2:传感器数据读取
- 命令发送:向 MLX90614ESF 发送 “读物体温度” 命令
- 数据接收:连续读取 2 字节原始数据(低字节 + 高字节),组合为 16 位数值;
步骤 3:数据处理
- 单位转换:原始数据单位为 0.01K(开尔文),转换为摄氏度公式:摄氏度 = (原始数据 × 0.01) - 273.15;
- 结果输出:将有效温度值通过 OLED 显示和串口打印。
🎯 单片机程序代码
main.c
#include "stm32f10x.h"
#include "string.h"
#include "stdio.h"
#include "delay.h"
#include "bsp_usart.h"
#include "oled.h"
#include "MLX90614.h"
float fTemperature; //温度
char OledBuff[512];
int main(void)
{
NVIC_PriorityGroupConfig (NVIC_PriorityGroup_2);
SysTick_Init(72); //系统时钟初始化
usart1_init(115200);//串口1初始化
usart2_init(115200);//串口1初始化
usart3_init(115200);//串口3初始化
printf("USART OK!\r\n");
OLED_Init();
OLED_ShowString(36,0,"MLX90614");
SMBus_Init();
while(1)
{
fTemperature = SMBus_ReadTemp();
printf("MLX90614 Temp:%.2f ℃\r\n",fTemperature);
OLED_ShowString(0,2,"MLX90614 Temp:");
sprintf(OledBuff,"%.2f ",fTemperature);
OLED_ShowString(0,4,OledBuff);
if(fTemperature<-270)
{
OLED_ShowString(0,6,"ERROR");
}
else
{
OLED_ShowString(0,6," ");
}
delay_ms(400);
}
}
MLX90614.c
/* Includes ------------------------------------------------------------------*/
#include "delay.h"
#include "MLX90614.h"
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
#define ACK 0
#define NACK 1
#define SA 0x00 //Slave address 单个MLX90614时地址为0x00,多个时地址默认为0x5a
#define RAM_ACCESS 0x00 //RAM access command
#define EEPROM_ACCESS 0x20 //EEPROM access command
#define RAM_TOBJ1 0x07 //To1 address in the eeprom
/* -------------------------------------------------------------------------------------*/
#define SMBUS_PORT GPIOB
#define SMBUS_SCK GPIO_Pin_14
#define SMBUS_SDA GPIO_Pin_13
#define RCC_APB2Periph_SMBUS_PORT RCC_APB2Periph_GPIOB
/* -------------------------------------------------------------------------------------*/
#define SMBUS_SCK_H() SMBUS_PORT->BSRR = SMBUS_SCK
#define SMBUS_SCK_L() SMBUS_PORT->BRR = SMBUS_SCK
#define SMBUS_SDA_H() SMBUS_PORT->BSRR = SMBUS_SDA
#define SMBUS_SDA_L() SMBUS_PORT->BRR = SMBUS_SDA
#define SMBUS_SDA_PIN() SMBUS_PORT->IDR & SMBUS_SDA //读取引脚电平
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/*******************************************************************************
* Function Name : SMBus_StartBit
* Description : Generate START condition on SMBus
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void SMBus_StartBit(void)
{
SMBUS_SDA_H(); // Set SDA line
SMBus_Delay(5); // Wait a few microseconds
SMBUS_SCK_H(); // Set SCL line
SMBus_Delay(5); // Generate bus free time between Stop
SMBUS_SDA_L(); // Clear SDA line
SMBus_Delay(5); // Hold time after (Repeated) Start
// Condition. After this period, the first clock is generated.
//(Thd:sta=4.0us min)
SMBUS_SCK_L(); // Clear SCL line
SMBus_Delay(5); // Wait a few microseconds
}
/*******************************************************************************
* Function Name : SMBus_StopBit
* Description : Generate STOP condition on SMBus
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void SMBus_StopBit(void)
{
SMBUS_SCK_L(); // Clear SCL line
SMBus_Delay(5); // Wait a few microseconds
SMBUS_SDA_L(); // Clear SDA line
SMBus_Delay(5); // Wait a few microseconds
SMBUS_SCK_H(); // Set SCL line
SMBus_Delay(5); // Stop condition setup time(Tsu:sto=4.0us min)
SMBUS_SDA_H(); // Set SDA line
}
/*******************************************************************************
* Function Name : SMBus_SendByte
* Description : Send a byte on SMBus
* Input : Tx_buffer
* Output : None
* Return : None
*******************************************************************************/
u8 SMBus_SendByte(u8 Tx_buffer)
{
u8 Bit_counter;
u8 Ack_bit;
u8 bit_out;
for(Bit_counter=8; Bit_counter; Bit_counter--)
{
if (Tx_buffer&0x80)
{
bit_out=1; // If the current bit of Tx_buffer is 1 set bit_out
}
else
{
bit_out=0; // else clear bit_out
}
SMBus_SendBit(bit_out); // Send the current bit on SDA
Tx_buffer<<=1; // Get next bit for checking
}
Ack_bit=SMBus_ReceiveBit(); // Get acknowledgment bit
return Ack_bit;
}
/*******************************************************************************
* Function Name : SMBus_SendBit
* Description : Send a bit on SMBus 82.5kHz
* Input : bit_out
* Output : None
* Return : None
*******************************************************************************/
void SMBus_SendBit(u8 bit_out)
{
if(bit_out==0)
{
SMBUS_SDA_L();
}
else
{
SMBUS_SDA_H();
}
SMBus_Delay(2); // Tsu:dat = 250ns minimum
SMBUS_SCK_H(); // Set SCL line
SMBus_Delay(6); // High Level of Clock Pulse
SMBUS_SCK_L(); // Clear SCL line
SMBus_Delay(3); // Low Level of Clock Pulse
// SMBUS_SDA_H(); // Master release SDA line ,
return;
}
/*******************************************************************************
* Function Name : SMBus_ReceiveBit
* Description : Receive a bit on SMBus
* Input : None
* Output : None
* Return : Ack_bit
*******************************************************************************/
u8 SMBus_ReceiveBit(void)
{
u8 Ack_bit;
SMBUS_SDA_H(); //引脚靠外部电阻上拉,当作输入
SMBus_Delay(2); // High Level of Clock Pulse
SMBUS_SCK_H(); // Set SCL line
SMBus_Delay(5); // High Level of Clock Pulse
if (SMBUS_SDA_PIN())
{
Ack_bit=1;
}
else
{
Ack_bit=0;
}
SMBUS_SCK_L(); // Clear SCL line
SMBus_Delay(3); // Low Level of Clock Pulse
return Ack_bit;
}
/*******************************************************************************
* Function Name : SMBus_ReceiveByte
* Description : Receive a byte on SMBus
* Input : ack_nack
* Output : None
* Return : RX_buffer
*******************************************************************************/
u8 SMBus_ReceiveByte(u8 ack_nack)
{
u8 RX_buffer;
u8 Bit_Counter;
for(Bit_Counter=8; Bit_Counter; Bit_Counter--)
{
if(SMBus_ReceiveBit()) // Get a bit from the SDA line
{
RX_buffer <<= 1; // If the bit is HIGH save 1 in RX_buffer
RX_buffer |=0x01;
}
else
{
RX_buffer <<= 1; // If the bit is LOW save 0 in RX_buffer
RX_buffer &=0xfe;
}
}
SMBus_SendBit(ack_nack); // Sends acknowledgment bit
return RX_buffer;
}
/*******************************************************************************
* Function Name : SMBus_Delay
* Description : 延时 一次循环约1us
* Input : time
* Output : None
* Return : None
*******************************************************************************/
void SMBus_Delay(u16 time)
{
delay_us(time);
}
/*******************************************************************************
* Function Name : SMBus_Init
* Description : SMBus初始化
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void SMBus_Init()
{
GPIO_InitTypeDef GPIO_InitStructure;
/* Enable SMBUS_PORT clocks */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SMBUS_PORT, ENABLE);
/*配置SMBUS_SCK、SMBUS_SDA为集电极开漏输出*/
GPIO_InitStructure.GPIO_Pin = SMBUS_SCK | SMBUS_SDA;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_OD;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(SMBUS_PORT, &GPIO_InitStructure);
SMBUS_SCK_H();
SMBUS_SDA_H();
}
/*******************************************************************************
* Function Name : SMBus_ReadMemory
* Description : READ DATA FROM RAM/EEPROM
* Input : slaveAddress, command
* Output : None
* Return : Data
*******************************************************************************/
u16 SMBus_ReadMemory(u8 slaveAddress, u8 command)
{
u16 data; // Data storage (DataH:DataL)
u8 Pec; // PEC byte storage
u8 DataL=0; // Low data byte storage
u8 DataH=0; // High data byte storage
u8 arr[6]; // Buffer for the sent bytes
u8 PecReg; // Calculated PEC byte storage
u8 ErrorCounter; // Defines the number of the attempts for communication with MLX90614
ErrorCounter=0x00; // Initialising of ErrorCounter
slaveAddress <<= 1; //2-7位表示从机地址
do
{
repeat:
SMBus_StopBit(); //If slave send NACK stop comunication
--ErrorCounter; //Pre-decrement ErrorCounter
if(!ErrorCounter) //ErrorCounter=0?
{
break; //Yes,go out from do-while{}
}
SMBus_StartBit(); //Start condition
if(SMBus_SendByte(slaveAddress))//Send SlaveAddress 最低位Wr=0表示接下来写命令
{
goto repeat; //Repeat comunication again
}
if(SMBus_SendByte(command)) //Send command
{
goto repeat; //Repeat comunication again
}
SMBus_StartBit(); //Repeated Start condition
if(SMBus_SendByte(slaveAddress+1)) //Send SlaveAddress 最低位Rd=1表示接下来读数据
{
goto repeat; //Repeat comunication again
}
DataL = SMBus_ReceiveByte(ACK); //Read low data,master must send ACK
DataH = SMBus_ReceiveByte(ACK); //Read high data,master must send ACK
Pec = SMBus_ReceiveByte(NACK); //Read PEC byte, master must send NACK
SMBus_StopBit(); //Stop condition
arr[5] = slaveAddress; //
arr[4] = command; //
arr[3] = slaveAddress+1; //Load array arr
arr[2] = DataL; //
arr[1] = DataH; //
arr[0] = 0; //
PecReg=PEC_Calculation(arr);//Calculate CRC
}
while(PecReg != Pec); //If received and calculated CRC are equal go out from do-while{}
data = (DataH<<8) | DataL; //data=DataH:DataL
return data;
}
/*******************************************************************************
* Function Name : PEC_calculation
* Description : Calculates the PEC of received bytes
* Input : pec[]
* Output : None
* Return : pec[0]-this byte contains calculated crc value
*******************************************************************************/
u8 PEC_Calculation(u8 pec[])
{
u8 crc[6];
u8 BitPosition=47;
u8 shift;
u8 i;
u8 j;
u8 temp;
do
{
/*Load pattern value 0x000000000107*/
crc[5]=0;
crc[4]=0;
crc[3]=0;
crc[2]=0;
crc[1]=0x01;
crc[0]=0x07;
/*Set maximum bit position at 47 ( six bytes byte5...byte0,MSbit=47)*/
BitPosition=47;
/*Set shift position at 0*/
shift=0;
/*Find first "1" in the transmited message beginning from the MSByte byte5*/
i=5;
j=0;
while((pec[i]&(0x80>>j))==0 && i>0)
{
BitPosition--;
if(j<7)
{
j++;
}
else
{
j=0x00;
i--;
}
}/*End of while */
/*Get shift value for pattern value*/
shift=BitPosition-8;
/*Shift pattern value */
while(shift)
{
for(i=5; i<0xFF; i--)
{
if((crc[i-1]&0x80) && (i>0))
{
temp=1;
}
else
{
temp=0;
}
crc[i]<<=1;
crc[i]+=temp;
}/*End of for*/
shift--;
}/*End of while*/
/*Exclusive OR between pec and crc*/
for(i=0; i<=5; i++)
{
pec[i] ^=crc[i];
}/*End of for*/
}
while(BitPosition>8); /*End of do-while*/
return pec[0];
}
/*******************************************************************************
* Function Name : SMBus_ReadTemp
* Description : Calculate and return the temperature
* Input : None
* Output : None
* Return : SMBus_ReadMemory(0x00, 0x07)*0.02-273.15
*******************************************************************************/
float SMBus_ReadTemp(void)
{
float temp;
temp = SMBus_ReadMemory(SA, RAM_ACCESS|RAM_TOBJ1)*0.02-273.15;
return temp;
}
/*********************************END OF FILE*********************************/
🎯 代码下载链接
https://download.csdn.net/download/qq_41954594/92130073
896

被折叠的 条评论
为什么被折叠?



