Arduino I2C + 温湿度传感器AM2321#

(2015.5.17:本日志的内容有所更新,参见《使用Arduino Wire Library读取温湿度传感器AM2321》。)

AM2321是广州奥松电子生产的数字式温湿度传感器。虽是国产品牌,其精度也可以与国外的主流温湿度传感IC媲美。

img

  • 尺寸:11.3x7.8x4mm(长x宽x高)

  • 封装:0.05 pitch PTH

  • 工作电压:2.6~5V

  • 功耗:测量时0.5mA,休眠状态10μA

  • 接口:I2C,最大速率100kbps;或单总线通讯

  • 分辨率:温度0.1°C,相对湿度0.1%RH

  • 精度:出厂前已校正,室温时温度误差+/-0.3°C,相对湿度误差+/-3%RH(皆典型值)

  • 重复性:温度+/-0.2°C,相对湿度+/-0.1%RH

美中不足:与国外同精度产品相比,AM2321的重复性和漂移指标偏大;功耗偏高;只能手动焊接,给产品量产带来不便。

电路连接#

AM2321支持5V工作,将电源、地、SCL、SDA四个管脚直接与UNO板子的对应管脚相连。对于Arduino UNO,I2C总线的SDA信号线对应A4管脚,SCL时钟线对应A5管脚。之后,SCL、SDA线需要通过上拉电阻连接到5V电源,电阻值可取4.7k或10k。

img

功能调试#

第一次调试花了不少时间,最终借助示波器才搞定。需留意的几个问题:

  1. I2C地址问题。虽然手册里写的地址是0xB8(0b10111000),但实际上器件是采用的7位地址,应该表述成0x5C(0b1011100),代码中的地址也应写成0x5C,否则无法通信。

  2. 唤醒AM2321时的时序问题。器件不回ACK,且最后一个时钟下降沿到发stop信号需间隔0.8~3ms。这个时序条件在Arduino的Wire库中没有处理的函数,因此只能将A4、A5设置成GPIO,利用bit-banging实现。shiftOut()函数可以实现字节的串行输出,且速率刚好也是100kbps左右。[注:后面发现即使没有这个等待时间,传感器也能正常工作,诡异。]

  3. A4、A5管脚在GPIO和硬件I2C之间的功能切换问题。在调用Wire.begin()函数之后,再使用pinMode()或digitalWrite()函数就无效了。发现在Wire.begin()函数中设置了I2C的控制寄存器TWCR,需将TWCR恢复到调用Wire.begin()前的状态,才可以用GPIO的方式操作A4、A5。

  4. 读返回数据时的时序问题。手册要求发送地址后,需要等待至少30μs后才能读取数据。这个功能在Wire库里也不支持,但直接用库里的函数(间隔约10μs)读取,没有发现有通信错误的问题。

  5. 传感器发送数据之后,会触发下一次温湿度测量,测量结果供下次数据读取。因此连续读取两次才能获得当前的温湿度值,即:第一次读取的是上一次测量的值,第二次读取的才是当前测量值。两次读取的最小间隔为2秒。

测试代码#

/*
Measurement of temperature and humidity using the AM2321 sensor
Attention:
    The protocol AM2321 used is not a standard i2c.
    Bit-banging is used to wake up the sensor,
    and then i2c functions are used to communicate.
Connection:
AM2321           UNO
VDD <--------->  5V
GND <--------->  GND
SCL <---------> SCL(A5)
SDA <---------> SDA(A4)
*/

#include <Wire.h>

#define ADDRESS_AM2321 0x5C //not 0xB8
#define SIGN_WRITE 0x00
#define SDA_PIN A4
#define SCL_PIN A5

byte fuctionCode = 0;
byte dataLength = 0;
byte humiHigh = 0;
byte humiLow = 0;
byte tempHigh = 0;
byte tempLow = 0;
byte crcHigh = 0;
byte crcLow = 0;

int humidity = 0;
int temperature = 0;
unsigned int crcCode = 0;

byte backupTWCR = 0;

void setup()
{
    Serial.begin(115200);
}

void loop()
{
    //step 1. wake up the sensor
    SendWakeUp();
    backupTWCR = TWCR;

    //step 2. send command
    Wire.begin();
    Wire.beginTransmission(ADDRESS_AM2321);
    Wire.write(0x03);
    Wire.write(0x00);
    Wire.write(0x04);
    Wire.endTransmission();

    delayMicroseconds(1500);

    //step 3. read data, and recover the TWCR register
    Wire.requestFrom(ADDRESS_AM2321, 8);
    fuctionCode = Wire.read();
    dataLength = Wire.read();
    humiHigh = Wire.read();
    humiLow = Wire.read();
    tempHigh = Wire.read();
    tempLow = Wire.read();
    crcLow = Wire.read();
    crcHigh = Wire.read();

    //get the result
    humidity = (humiHigh<<8) | humiLow;
    temperature = (tempHigh<<8) | tempLow;
    crcCode = (crcHigh<<8) | crcLow;

    Serial.print(temperature/10.0, 1);    Serial.println(" `C");
    Serial.print(humidity/10.0, 1);    Serial.println(" \%RH");
    CheckCRC();

    //recover the TWCR register, e.g. disable the I2C bus
    TWCR = backupTWCR;

    delay(4000);
}

void SendWakeUp()
{
    //set pinmode
    pinMode(SCL_PIN, OUTPUT);
    pinMode(SDA_PIN, OUTPUT);
    digitalWrite(SCL_PIN, HIGH);
    digitalWrite(SDA_PIN, HIGH);

    //issue a START condition
    delayMicroseconds(5);
    digitalWrite(SDA_PIN, LOW);
    delayMicroseconds(5);
    digitalWrite(SCL_PIN, LOW);
    delayMicroseconds(5);

    //send ADDRESS+W
    shiftOut(SDA_PIN, SCL_PIN, MSBFIRST, ((ADDRESS_AM2321<<1) | SIGN_WRITE));

    //send clock for ack
    pinMode(SDA_PIN, INPUT_PULLUP);// or INPUT mode
    delayMicroseconds(5);
    digitalWrite(SCL_PIN, HIGH);
    delayMicroseconds(5);
    digitalWrite(SCL_PIN, LOW);
    pinMode(SDA_PIN, OUTPUT);
    digitalWrite(SDA_PIN, LOW);

    delayMicroseconds(1000);

    //issue a STOP condition
    digitalWrite(SCL_PIN, HIGH);
    delayMicroseconds(5);
    digitalWrite(SDA_PIN, HIGH);
}

void CheckCRC() //from the datesheet
{
    byte backValues[] = {fuctionCode, dataLength, humiHigh, \
        humiLow, tempHigh, tempLow};
    unsigned int crc = 0xFFFF;
    int i;
    int len = 6;
    int j = 0;
    while (len--)
    {
        crc ^= backValues[j];
        j++;
        for (i=0; i<8; i++)
        {
            if (crc & 0x01)
            {
                crc >>= 1;
                crc ^= 0xA001;
            }
            else
            {
                crc >>= 1;
            }
        }
    }
    if (crc == crcCode)
    {
        Serial.println("CRC checked.");
    }
    else
    {
        Serial.println("CRC Error!");
    }
}

[注] 后面发现,第一步唤醒时即使直接用Wire库中的beginTransmission()和endTransmission()函数即可,即使没有手册中要求的0.8~3ms等待时间,传感器也能正常运行。从示波器上看,传感器不回ACK,硬件I2C等待的时间仅10us左右,却不影响工作。看来手册的描述有问题。使用Wire Library标准库来读取AM2321,可以参照另一篇日志

参考资料#