I've been playing around with an AHT10 temperature and humidity sensor, and although it's working, it has extra characters at the end of the printout. Not sure how to get rid of them.
Here's the original example:
#include <Wire.h>
// AHT10 I2C address
#define AHT10_ADDRESS 0x38
// AHT10 registers
#define AHT10_INIT_CMD 0xE1
#define AHT10_MEASURE_CMD 0xAC
#define AHT10_RESET_CMD 0xBA
void setup() {
Wire.begin(); // Initialize I2C
Serial.begin(9600); // Start serial communication at 9600 baud rate
// Initialize AHT10 sensor
Wire.beginTransmission(AHT10_ADDRESS);
Wire.write(AHT10_INIT_CMD);
Wire.endTransmission();
delay(20); // Wait for sensor initialization
}
void loop() {
// Trigger a measurement
Wire.beginTransmission(AHT10_ADDRESS);
Wire.write(AHT10_MEASURE_CMD);
Wire.write(0x33);
Wire.write(0x00);
Wire.endTransmission();
delay(100); // Measurement time
// Read the data (6 bytes)
Wire.requestFrom(AHT10_ADDRESS, 6);
if (Wire.available() == 6) {
uint8_t data[6];
for (int i = 0; i < 6; i++) {
data[i] = Wire.read();
}
// Convert the data to actual humidity and temperature
unsigned long humidity_raw = ((unsigned long)data[1] << 12) | ((unsigned long)data[2] << 4) | (data[3] >> 4);
unsigned long temp_raw = (((unsigned long)data[3] & 0x0F) << 16) | ((unsigned long)data[4] << 8) | data[5];
float humidity = humidity_raw * (100.0 / 1048576.0);
float temperature = (temp_raw * (200.0 / 1048576.0)) - 50;
// Output the results to serial monitor
Serial.print("Humidity: ");
Serial.print(humidity);
Serial.println("%");
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.println("C");
}
delay(2000); // Delay between measurements
}
And here's my edited version adding my LCD:
#include <Wire.h>
#include <LiquidCrystal.h>
// create the LCD
LiquidCrystal lcd(30, 31, 32, 33, 34, 35);
// set up backlight
int bkl = 45; // backlight pin
byte bklIdle = 100; // PWM value for backlight at idle
byte bklOn = 255; // PWM value for backlight when on
int bklDelay = 10000; // ms for the backlight to idle before turning off
unsigned long bklTime = 0; // counter since backlight turned on
// AHT10 I2C address
#define AHT10_ADDRESS 0x38
// AHT10 registers
#define AHT10_INIT_CMD 0xE1
#define AHT10_MEASURE_CMD 0xAC
#define AHT10_RESET_CMD 0xBA
void setup() {
Wire.begin(); // Initialize I2C
pinMode(bkl, OUTPUT);
lcd.begin(20, 4);
digitalWrite(bkl, HIGH);
// Initialize AHT10 sensor
Wire.beginTransmission(AHT10_ADDRESS);
Wire.write(AHT10_INIT_CMD);
Wire.endTransmission();
delay(20); // Wait for sensor initialization
}
void loop() {
// Trigger a measurement
Wire.beginTransmission(AHT10_ADDRESS);
Wire.write(AHT10_MEASURE_CMD);
Wire.write(0x33);
Wire.write(0x00);
Wire.endTransmission();
delay(100); // Measurement time
// Read the data (6 bytes)
Wire.requestFrom(AHT10_ADDRESS, 6);
if (Wire.available() == 6) {
uint8_t data[6];
for (int i = 0; i < 6; i++) {
data[i] = Wire.read();
}
// Convert the data to actual humidity and temperature
unsigned long humidity_raw = ((unsigned long)data[1] << 12) | ((unsigned long)data[2] << 4) | (data[3] >> 4);
unsigned long temp_raw = (((unsigned long)data[3] & 0x0F) << 16) | ((unsigned long)data[4] << 8) | data[5];
float humidity = humidity_raw * (100.0 / 1048576.0);
float temperature = (temp_raw * (200.0 / 1048576.0)) - 50;
// Output the results to serial monitor
lcd.setCursor(0, 0);
lcd.print("Humidity: ");
lcd.print(humidity, 0);
lcd.println("%");
lcd.setCursor(0, 1);
lcd.print("Temperature: ");
lcd.print(temperature, 0);
lcd.println("C");
}
delay(2000); // Delay between measurements
}
And here's the display:
Any ideas?
