Created
November 17, 2024 06:02
-
-
Save EDISON-SCIENCE-CORNER/3cdc56b6e11184da402031122ad13888 to your computer and use it in GitHub Desktop.
dht11 with voltage monitor
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <DHT11.h> | |
| DHT11 dht11(4); | |
| int analogInPin = A0; // Analog input pin | |
| int sensorValue; // Analog Output of Sensor | |
| int temperature = 0; | |
| int humidity = 0; | |
| float calibration = 0.23; // Check Battery voltage using multimeter & add/subtract the value | |
| int bat_percentage; | |
| void setup() | |
| { | |
| Serial.begin(115200); | |
| ; | |
| } | |
| void loop() | |
| { | |
| int result = dht11.readTemperatureHumidity(temperature, humidity); | |
| if (result == 0) { | |
| Serial.print("Temperature: "); | |
| Serial.print(temperature); | |
| Serial.print(" °C\tHumidity: "); | |
| Serial.print(humidity); | |
| Serial.println(" %"); | |
| } | |
| sensorValue = analogRead(analogInPin); | |
| float voltage = (((sensorValue * 3.3) / 1024) * 2 - calibration); //multiply by two as voltage divider network is 100K & 100K Resistor | |
| bat_percentage = mapfloat(voltage, 2.8, 4.2, 0, 100); //2.8V as Battery Cut off Voltage & 4.2V as Maximum Voltage | |
| if (bat_percentage >= 100) | |
| { | |
| bat_percentage = 100; | |
| } | |
| if (bat_percentage <= 0) | |
| { | |
| bat_percentage = 1; | |
| } | |
| Serial.print("Analog Value = "); | |
| Serial.print(sensorValue); | |
| Serial.print("\t Output Voltage = "); | |
| Serial.print(voltage); | |
| Serial.print("\t Battery Percentage = "); | |
| Serial.println(bat_percentage); | |
| delay(1000); | |
| } | |
| float mapfloat(float x, float in_min, float in_max, float out_min, float out_max) | |
| { | |
| return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment