добавил ещё несколько примеров для I2C

This commit is contained in:
Ogneyar 2024-08-21 15:49:49 +03:00
parent 996e316403
commit 045e4c6c78
5 changed files with 157 additions and 3 deletions

View File

@ -1,9 +1,7 @@
/* /*
An SD card connected via SPI is used for this test. An SD card connected via SPI is used for this test.
The example is taken from the arduino SD library. The example uses the SD library, it must be installed first.
https://github.com/arduino-libraries/SD
*/ */
#include <SPI.h> #include <SPI.h>

View File

@ -0,0 +1,59 @@
#include <Arduino.h>
#include <Wire.h>
#include "AHT10.h"
static const uint8_t AHT10_ADDR = 0x38;
bool AHT10::begin() {
Wire.beginTransmission(AHT10_ADDR);
Wire.write(0xE1);
Wire.write(0x08);
Wire.write(0x00);
return Wire.endTransmission() == 0;
}
bool AHT10::reset() {
Wire.beginTransmission(AHT10_ADDR);
Wire.write(0xBA);
return Wire.endTransmission() == 0;
}
bool AHT10::measure(float *temp, float *hum) {
uint8_t data[6];
uint32_t d;
Wire.beginTransmission(AHT10_ADDR);
Wire.write(0xAC);
Wire.write(0x33);
Wire.write(0x00);
if (Wire.endTransmission())
return false;
delay(75);
if ((Wire.requestFrom(AHT10_ADDR, (uint8_t)6) != 6) || (Wire.readBytes(data, 6) != 6))
return false;
if (temp) {
d = ((uint32_t)(data[3] & 0x0F) << 16) | ((uint32_t)data[4] << 8) | data[5];
*temp = (float)d * 200 / 1048576 - 50;
}
if (hum) {
d = ((uint32_t)data[1] << 12) | ((uint32_t)data[2] << 4) | (data[3] >> 4);
*hum = (float)d * 100 / 1048576;
}
return true;
}
float AHT10::getTemperature() {
float result;
if (! measure(&result, NULL))
result = NAN;
return result;
}
float AHT10::getHumidity() {
float result;
if (! measure(NULL, &result))
result = NAN;
return result;
}

View File

@ -0,0 +1,15 @@
#ifndef __AHT10_H
#define __AHT10_H
#include <Wire.h>
class AHT10 {
public:
static bool begin();
static bool reset();
static bool measure(float *temp, float *hum);
static float getTemperature();
static float getHumidity();
};
#endif

View File

@ -0,0 +1,32 @@
/*
The AHT10 temperature and humidity sensor connected to I2C is used for this test.
*/
#include "AHT10.h"
void setup() {
Serial.begin(115200);
Serial.println();
Wire.begin();
if (! AHT10::begin()) {
Serial.println(F("AHT10 not detected!"));
Serial.flush();
}
}
void loop() {
float temp, hum;
delay(2000);
if (AHT10::measure(&temp, &hum)) {
Serial.print(F("Temperature is "));
Serial.print(temp);
Serial.print(F(" C, humidity is "));
Serial.print(hum);
Serial.println('%');
} else {
Serial.println(F("AHT10 read error!"));
}
}

View File

@ -0,0 +1,50 @@
/*
The BMP280 temperature and pressure sensor connected to I2C is used for this test.
The example uses the Adafruit_BMP280 library, it must be installed first.
*/
#include <Wire.h>
#include <Adafruit_BMP280.h>
Adafruit_BMP280 bmp;
void setup() {
Serial.begin(115200);
while( ! Serial )
;
Serial.println("BMP280 run test in terminal");
unsigned status;
status = bmp.begin(BMP280_ADDRESS_ALT, BMP280_CHIPID);
if (!status) {
Serial.println("Could not find a valid BMP280 sensor!");
while(1)
;
}
/* Default settings from datasheet. */
bmp.setSampling(Adafruit_BMP280::MODE_NORMAL, /* Operating Mode. */
Adafruit_BMP280::SAMPLING_X2, /* Temp. oversampling */
Adafruit_BMP280::SAMPLING_X16, /* Pressure oversampling */
Adafruit_BMP280::FILTER_X16, /* Filtering. */
Adafruit_BMP280::STANDBY_MS_500); /* Standby time. */
}
void loop() {
Serial.print(F("Temperature = "));
Serial.print(bmp.readTemperature());
Serial.println(" *C");
Serial.print(F("Pressure = "));
Serial.print(bmp.readPressure());
Serial.println(" Pa");
Serial.print(F("Approx altitude = "));
Serial.print(bmp.readAltitude(1013.25)); /* Adjusted to local forecast! */
Serial.println(" m");
Serial.println();
delay(2000);
}