feat: 全量同步 254 个常用的 Arduino 扩展库文件

This commit is contained in:
yczpf2019
2026-01-24 16:05:38 +08:00
parent c665ba662b
commit 397b9a23a3
6878 changed files with 2732224 additions and 1 deletions

View File

@@ -0,0 +1,140 @@
#include <stdint.h>
#include <math.h>
#include <Arduino.h>
#include <Wire.h>
#include "RL_AHT21.h"
// Specify the constants for water vapor and barometric pressure.
#define WATER_VAPOR 17.62f
#define BAROMETRIC_PRESSURE 243.5f
Sensor_CMD eSensorCalibrateCmd[3] = {0x1B, 0x1C, 0x1E};
Sensor_CMD eSensorNormalCmd[3] = {0xBE, 0x00, 0x00};
Sensor_CMD eSensorMeasureCmd[3] = {0xAC, 0x30, 0x00};
Sensor_CMD eSensorResetCmd = 0xBA;
boolean GetRHumidityCmd = true;
boolean GetTempCmd = false;
/******************************************************************************
* Global Functions
******************************************************************************/
AHT21Class::AHT21Class() {
}
boolean AHT21Class::begin(unsigned char _AHT21_address)
{
AHT21_address = _AHT21_address;
Serial.begin(9600);
//Serial.println("\x54\x68\x69\x6E\x61\x72\x79\x20\x45\x6C\x65\x74\x72\x6F\x6E\x69\x63\x20\x41\x48\x54\x31\x30\x20\x4D\x6F\x64\x75\x6C\x65\x2E");
delay(100);
Wire.begin(AHT21_address);
Wire.beginTransmission(AHT21_address);
Wire.write(eSensorCalibrateCmd, 3);
Wire.endTransmission();
//Serial.println("https://thinaryelectronic.aliexpress.com");
delay(500);
if((readStatus()&0x18) == 0x18)
return true;
else
{
return false;
}
}
/**********************************************************
* GetHumidity
* Gets the current humidity from the sensor.
*
* @return float - The relative humidity in %RH
**********************************************************/
float AHT21Class::GetHumidity(void)
{
float value = readSensor(GetRHumidityCmd);
if (value == 0) {
return 0; // Some unrealistic value
}
return value * 100 / 1048576;
}
/**********************************************************
* GetTemperature
* Gets the current temperature from the sensor.
*
* @return float - The temperature in Deg C
**********************************************************/
float AHT21Class::GetTemperature(void)
{
float value = readSensor(GetTempCmd);
return ((200 * value) / 1048576) - 50;
}
/**********************************************************
* GetDewPoint
* Gets the current dew point based on the current humidity and temperature
*
* @return float - The dew point in Deg C
**********************************************************/
float AHT21Class::GetDewPoint(void)
{
float humidity = GetHumidity();
float temperature = GetTemperature();
// Calculate the intermediate value 'gamma'
float gamma = log(humidity / 100) + WATER_VAPOR * temperature / (BAROMETRIC_PRESSURE + temperature);
// Calculate dew point in Celsius
float dewPoint = BAROMETRIC_PRESSURE * gamma / (WATER_VAPOR - gamma);
return dewPoint;
}
/******************************************************************************
* Private Functions
******************************************************************************/
unsigned long AHT21Class::readSensor(boolean GetDataCmd)
{
unsigned long result,temp[7];
Wire.beginTransmission(AHT21_address);
Wire.write(eSensorMeasureCmd, 3);
Wire.endTransmission();
delay(300);
Wire.requestFrom(AHT21_address, 6);
for(unsigned char i = 0; Wire.available() > 0; i++)
{
temp[i] = Wire.read();
}
if(GetDataCmd)
{
result = ((temp[1] << 16) | (temp[2] << 8) | temp[3]) >> 4;
}
else
{
result = ((temp[3] & 0x0F) << 16) | (temp[4] << 8) | temp[5];
}
return result;
}
unsigned char AHT21Class::readStatus(void)
{
unsigned char result = 0;
Wire.requestFrom(AHT21_address, 1);
result = Wire.read();
return result;
}
void AHT21Class::Reset(void)
{
Wire.beginTransmission(AHT21_address);
Wire.write(eSensorResetCmd);
Wire.endTransmission();
delay(20);
}

View File

@@ -0,0 +1,51 @@
/*
AHT21 - A Humidity Library for Arduino.
Supported Sensor modules:
AHT21-Breakout Module - https://www.aliexpress.com/item/33002710848.html
Created by Thinary Eletronic at Modern Device on April 2019.
* This file is part of Thinary_AHT21.
*
* Thinary_AHT21 is free software: you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
* as published by the Free Software Foundation, either version 3 of
* the License, or(at your option) any later version.
*
* Sodaq_SHT2x is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with Thinary_AHT21. If not, see
* <http://www.gnu.org/licenses/>.
*/
#ifndef RL_AHT21_H
#define RL_AHT21_H
#include <stdint.h>
typedef unsigned char Sensor_CMD;
class AHT21Class
{
private:
unsigned long readSensor(boolean GetDataCmd);
unsigned char AHT21_address;
public:
AHT21Class();
boolean begin(unsigned char _AHT21_address = 0x38);
float GetHumidity(void);
float GetTemperature(void);
float GetDewPoint(void);
unsigned char readStatus(void);
void Reset(void);
};
#endif