HCAM2315 - Library for AM2315 Temperature & Humidity devices

Useful guides, libraries, and example sketches to support our Arduino based products.
Post Reply
admin
Site Admin
Posts: 866
Joined: Sun Aug 05, 2012 4:02 pm

HCAM2315 - Library for AM2315 Temperature & Humidity devices

Post by admin » Fri Jan 26, 2018 3:28 pm

[IMAGE TBA]

Image


Arduino library for the AM2315 digital temperature and humidity senor.
Products currently supported by this library:

Aosong AM2315 I2C Digital Temperature & Humidity Sensor (HCSENS0043) available from hobbycomponents.com


You will need to download (please log in to download the library) and unzip this library to the Arduino development environments library area.

On Windows:
My Documents\Arduino\libraries\

On Mac:
Documents/Arduino/libraries/

Linux:
Usually found within the users home area under /Arduino/libraries/



Using the HCAM2315 library

To use the library just include the HCAM2315.h header file and then create an instance of the library. E.g:

Code: Select all

#include <HCSHT3x .h>
HCAM2315 HCAM2315(I2C_ADD);

Where I2C_Add is the I2C address of the AM2315.


To initialise the library add the following line to the setup() section of your sketch:

Code: Select all

HCAM2315.init();


The following functions are available with this library:


void HCAM2315.Read(void)

Performs a read of the AM2315 and updates the currently stored values for temperature and humidity. The new values read back from the sensor can then be obtained using the
Temp(), and/or Hum() library functions.


Example:

Code: Select all

HCAM2315.Read();



float HCAM2315.Temp(void)

Returns a floating point value containing the temperature in Centigrade which was previously read using the Read() library function.

Example:

Code: Select all

float Temp = HCAM2315.Temp();


float HCAM2315.Hum(void)

Returns a floating point value containing the relative humidity in % which was previously read using the Read() library function.

Example:

Code: Select all

float Humidity = HCAM2315.Hum();



bool HCAM2315.CheckCRC(void)

Checks if the last data read back using the Read() library function has a valid CRC.

Returns a boolean value where:
true = CRC is valid
false = CRC is invalid

Example:

Code: Select all

if(HCAM2315.CheckCRC())
{
	Temp = HCAM2315.Temp_oC();
	Humidity = HCAM2315.Hum();
}


Image

Code: Select all

/* FILE:    HCAM2315_Library_Example
   DATE:    24/01/18
   VERSION: 0.1
   AUTHOR:  Andrew Davies
   
24/01/18 version 0.1: Original version

This example sketch uses the HCAM2315 library to read the current temperature and humidity
from a AM2315 device. The sketch repeatedly reads the sensor once a second and outputs the 
result to the serial UART. 

Hobby Components (HobbyComponents.com) products currently supported by this library:

Aosong AM2315 I2C Digital Temperature & Humidity Sensor (HCSENS0043)

More information about the library can be found in the software section of our support 
forum here:

http://forum.hobbycomponents.com/software


You may copy, alter and reuse this code in any way you like, but please leave
reference to HobbyComponents.com in your comments if you redistribute this code.
This software may not be used directly for the purpose of selling products that
directly compete with Hobby Components Ltd's own range of products.
THIS SOFTWARE IS PROVIDED "AS IS". HOBBY COMPONENTS MAKES NO WARRANTIES, WHETHER
EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ACCURACY OR LACK OF NEGLIGENCE.
HOBBY COMPONENTS SHALL NOT, IN ANY CIRCUMSTANCES, BE LIABLE FOR ANY DAMAGES,
INCLUDING, BUT NOT LIMITED TO, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES FOR ANY
REASON WHATSOEVER.
*/


#include <HCAM2315.h>       //Include the HCAM2315 library.

#define I2CADD 0x5C         //I2C address of the AM2315.

HCAM2315 HCAM2315(I2CADD);  //Create an instance of the library.

void setup() 
{
  Serial.begin(9600);       //Initiliase the Arduino serial library.
  
  HCAM2315.init();          //Initiliase the library.

}

void loop() 
{
  //Trigger a temperature & humidity measurement and read back the results.
  HCAM2315.Read();  

  //If there are no errors output the results to the serial UART.
  if(HCAM2315.CheckCRC())
  {
    Serial.print("Temp (oC): ");
    Serial.print(HCAM2315.Temp());
    Serial.print("\tHumidity (%RH): ");
    Serial.println(HCAM2315.Hum());
  }else
  {
    Serial.println("CRC ERROR!");
  }

  //Wait a second before taking another measurement.
  delay(1000);
}



Image
HCAM2315.zip


Diagrams, libraries, and example code are provided as an additional free service by Hobby Components and are not sold as part of this product. We do no provide any guarantees or warranties as to their accuracy or fitness for purpose.

Descriptions and diagrams on this page are copyright Hobby Components Ltd and may not be reproduced without permission.
You do not have the required permissions to view the files attached to this post.

RetroBoy
Posts: 73
Joined: Sat Feb 26, 2022 11:29 am
Location: U.K.

Re: HCAM2315 - Library for AM2315 Temperature & Humidity devices

Post by RetroBoy » Sun Jul 31, 2022 1:01 pm

Hi Andrew,

I noticed that the address of this/these units is ' 0x5C ' - is that changeable?

andrew
Site Admin
Posts: 1374
Joined: Sun Aug 05, 2012 4:15 pm

Re: HCAM2315 - Library for AM2315 Temperature & Humidity devices

Post by andrew » Mon Aug 01, 2022 8:48 am

I'm afraid it's fixed to 0x5C on these sensors which means you can only have one on the same I2C bus
Comments made by this poster do not necessarily reflect the views of Hobby Components Ltd.

RetroBoy
Posts: 73
Joined: Sat Feb 26, 2022 11:29 am
Location: U.K.

Re: HCAM2315 - Library for AM2315 Temperature & Humidity devices

Post by RetroBoy » Mon Aug 01, 2022 6:50 pm

Hi Andrew,

Thanks for the Answer. Unfortunately means I will have to think of ' other ways ' to reduce code size; my 2004 LCDs use 0x5A - 0x5D.
Had to change as DS18B20 uses 0x28 as Family Code. Even though I use OneWire for the DS18s, for some reason, it still got confused between 0x28 for a 2004 and the DS18. As soon as I changed 2004s to a different 'ADR Range' worked perfectly.

Thanks S.

Post Reply

Return to “Arduino”