76 lines
1.4 KiB
Arduino
Raw Normal View History

2022-01-06 12:08:13 +01:00
//
// FILE: AM2315_test.ino
// AUTHOR: Rob Tillaart
// PURPOSE: Demo for AM2315 I2C humidity & temperature sensor
//
#include "AM2315.h"
#include "Wire.h"
AM2315 sensor(&Wire);
void setup()
{
Serial.begin(115200);
2023-10-15 14:17:19 +02:00
Serial.println();
2022-01-06 12:08:13 +01:00
Serial.println(__FILE__);
Serial.print("AM2315_LIB_VERSION: ");
Serial.println(AM2315_LIB_VERSION);
Serial.println();
2023-10-15 14:17:19 +02:00
Wire.begin();
while (sensor.begin() == false)
{
Serial.print(millis());
Serial.println("\tCould not connect to sensor.");
delay(1000);
}
2022-01-06 12:08:13 +01:00
delay(2000);
Serial.println("Type,\tStatus,\tHumidity (%),\tTemperature (C)");
delay(100);
}
void loop()
{
if (millis() - sensor.lastRead() >= 2000)
{
2023-01-21 15:19:08 +01:00
// READ DATA
2022-01-06 12:08:13 +01:00
Serial.print("AM2315, \t");
int status = sensor.read();
switch (status)
{
case AM2315_OK:
Serial.print("OK,\t");
break;
case AM2315_ERROR_CHECKSUM:
Serial.print("Checksum error,\t");
break;
case AM2315_ERROR_CONNECT:
Serial.print("Connect error,\t");
break;
case AM2315_MISSING_BYTES:
Serial.print("Bytes error,\t");
break;
default:
2023-01-21 15:19:08 +01:00
Serial.print("error <");
Serial.print(status);
Serial.print(">,\t");
2022-01-06 12:08:13 +01:00
break;
}
2023-01-21 15:19:08 +01:00
// DISPLAY DATA, sensor has only one decimal.
2022-01-06 12:08:13 +01:00
Serial.print(sensor.getHumidity(), 1);
Serial.print(",\t");
Serial.println(sensor.getTemperature(), 1);
}
}
2023-01-21 15:19:08 +01:00
// -- END OF FILE --