Arduino debug temperature and humidity sensor AM2321

Source: Internet
Author: User

AM2321 is a digital temperature and humidity sensor in Guangzhou ausone electronics production. Although it is a domestic brand, its precision can be comparable with foreign mainstream temperature and humidity sensing IC.

    • Size: 11.3x7.8x4mm (L x W x H)
    • Package: 0.05 pitch PTH
    • Operating voltage: 2.6~5v
    • Interface: i²c, maximum rate 100kbps, with special timing requirements
    • Resolution: Temperature 0.1°c, relative humidity 0.1%rh
    • Accuracy: Temperature error +/-0.3°c at room temperature, relative humidity error +/-3%RH
    • Repeatability: Temperature +/-0.2°c, relative humidity +/-0.1%rh

In the ointment: compared with foreign precision products, AM2321 repeatability and Drift index is large, only manual welding, mass production will be very inconvenient.

Circuit connection

The AM2321 supports 5V work, connecting the power, ground, SCL, SDA four pins directly to the corresponding pins of the UNO board. The SDA signal line for the Arduino UNO,I2C bus corresponds to the A4 pin, and the SCL clock line corresponds to the A5 pin. After that, the SCL, SDA lines need to be connected to a 5V power supply with a pull-up resistor, which can be 4.7k or 10k.

Functional commissioning

The first commissioning took a lot of time, and finally the oscilloscope was taken care of. Some questions to keep in mind:

1. Address problem. Although the address in the manual is 0xb8 (0b1011), the device is actually a 7-bit address that should be expressed as 0x5c (0b1011100), The address in the code should also be written as 0x5c, otherwise it cannot communicate.

2. Timing problems when waking AM2321. The device does not return an ACK, and the last clock falls along to the stop signal to be spaced 0.8~3ms. This timing condition is not handled in the Arduino wire library, so the A4, A5 can only be set to Gpio, with bit-banging implementation. The Shiftout () function implements the serial output of the byte, and the rate is just about 100kbps.

3. A4, A5 pin between gpio and hardware i²c function switching problem. After the Wire.begin () function is called, the use of the Pinmode () or digitalwrite () function is not valid. It is found that the control register TWCR, which is set in the Wire.begin () function, requires the TWCR to be restored to the state before the call to Wire.begin () to operate the A4 and A5 in a gpio manner.

4. Read the timing problem when returning data. The manual requires the sending address to wait at least 30μs before the data can be read. This function is not supported in the wire library, but it is read directly from the library function (interval approx. 10μs) and no communication errors are found.

Reference code:

1 /*2 measurement of temperature and humidity using the AM2321 sensor3 Attention:4 The protocol AM2321 used is not a standard i²c.5 bit-banging is used-wake up the sensor,6 And then i²c functions is used to communicate.7 Connection:8 AM2321 UNO9 VDD <----------> GNDTen Gnd <----------> Gnd One SCL <----------> SCL (A5) A SDA <----------> SDA (A4) - */ -  the#include <Wire.h> -  - #defineaddress_am2321 0x5C//Not 0xb8 - #defineSign_write 0x00 + #defineSda_pin A4 - #defineScl_pin A5 +  A byteFuctioncode =0; at byteDatalength =0; - byteHumihigh =0; - byteHumilow =0; - byteTemphigh =0; - byteTemplow =0; - byteCrchigh =0; in byteCrclow =0; -  to intHumidity =0; + intTemperature =0; -UnsignedintCrccode =0; the  * byteBACKUPTWCR =0; $ Panax Notoginseng voidSetup () - { theSerial.begin (115200); + } A  the voidLoop () + { -     //Step 1. Wake up the sensor $ sendwakeup (); $BACKUPTWCR =TWCR; -  -     //Step 2. Send command the Wire.begin (); - wire.begintransmission (address_am2321);WuyiWire.write (0x03); theWire.write (0x00); -Wire.write (0x04); Wu wire.endtransmission (); -  AboutDelaymicroseconds ( the); $  -     //Step 3. Read data, and recover the TWCR register -Wire.requestfrom (address_am2321,8); -Fuctioncode =Wire.read (); ADatalength =Wire.read (); +Humihigh =Wire.read (); theHumilow =Wire.read (); -Temphigh =Wire.read (); $Templow =Wire.read (); theCrclow =Wire.read (); theCrchigh =Wire.read (); the  the     //Get the result -Humidity = (humihigh<<8) |Humilow; inTemperature = (temphigh<<8) |Templow; theCrccode = (crchigh<<8) |Crclow; the  AboutSerial.print (temperature/10.0,1); Serial.println ("' C"); theSerial.print (humidity/10.0,1); Serial.println ("\%RH"); the CHECKCRC (); the  +     //Recover the TWCR register -TWCR =BACKUPTWCR; the BayiDelay4000); the } the  - voidsendwakeup () - { the     //Set Pinmode the Pinmode (Scl_pin, OUTPUT); the Pinmode (Sda_pin, OUTPUT); the Digitalwrite (Scl_pin, high); - Digitalwrite (Sda_pin, high); the  the     //issue a START condition theDelaymicroseconds (5);94 Digitalwrite (Sda_pin, low); theDelaymicroseconds (5); the Digitalwrite (Scl_pin, low); theDelaymicroseconds (5);98  About     //Send Address+w -Shiftout (Sda_pin, Scl_pin, Msbfirst, ((address_am2321<<1) |sign_write));101 102     //send clock for ACK103Pinmode (Sda_pin, Input_pullup);//or INPUT mode104Delaymicroseconds (5); the Digitalwrite (Scl_pin, high);106Delaymicroseconds (5);107 Digitalwrite (Scl_pin, low);108 Pinmode (Sda_pin, OUTPUT);109 Digitalwrite (Sda_pin, low); the     111Delaymicroseconds ( +); the 113     //issue a STOP condition the Digitalwrite (Scl_pin, high); theDelaymicroseconds (5); the Digitalwrite (Sda_pin, high);117 }118 119 voidCHECKCRC ()//From the datesheet - {121     byteBackvalues[] ={fuctioncode, datalength, Humihigh,122 Humilow, Temphigh, templow};123UnsignedintCRC =0xFFFF;124     inti; the     intLen =6;126     intj =0;127      while(len--) -     {129CRC ^=Backvalues[j]; theJ + +;131          for(i=0; i<8; i++) the         {133             if(CRC &0x01)134             {135CRC >>=1;136CRC ^=0xa001;137             }138             Else139             { $CRC >>=1;141             }142         }143     } 144     if(CRC = =Crccode)145     {146Serial.println ("CRC checked.");147     }148     Else149     { MaxSerial.println ("CRC error!");151     } the}

Resources

Ausone official Website Information-AM2321 digital temperature and humidity sensor
ti-troubleshooting i²c Bus Protocol documentation on the processing of I²c debug issues, recommended

Arduino debug temperature and humidity sensor AM2321

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.