I buy my DHT11 from this shop
https://www.jaycar.com.au/arduino-compatible-temperature-and-humidity-sensor-module/p/XC4520
I could not get any data from the sensor, only got nan . I have check my connection plenty of times. I also check my code and use many example code from internet and example from arduino IDE. All the result came out as nan
When i measure voltage across the Vcc and ground of sensor , it shows 5V supply which is good because sensor DHT11 could accept both 3.3 -5V . However , when i measure the voltage across digital pin and ground it only show 2.16V which not I expect because to read the DHT11 , the digital pin voltage should be larger than 3 V . Could you please give me some advice on this?
temperature.h
#ifndef temperature_h
#define temperature_h
extern float temperature_battery;
class temperature {
public:
void begin();
void calculate_temperature();
};
#endif
temperature.cpp
#define turn_on_fan_pin 14
const int DHT_PIN = 17;
float temperature_battery ;
DHTesp dht;
void temperature::begin(){
dht.setup(DHT_PIN, DHTesp::DHT11);
}
void temperature::calculate_temperature(){
TempAndHumidity data = dht.getTempAndHumidity();
temperature_battery = data.temperature;
Serial.println("my temperature");
Serial.println ( temperature_battery);
if (temperature_battery > 25)
{
digitalWrite( turn_on_fan_pin, HIGH);
}
else {
digitalWrite( turn_on_fan_pin, LOW);
}
}
charge.cpp
#include "charge.h"
#include <Arduino.h>
#include <Wire.h>
#include <Adafruit_ADS1015.h>
Adafruit_ADS1115 ads(0x48);
#define voltage_read_pin 34
#define cut_off_pin 16
float total_cell_charge;
float cell[3];
int percentage_value;
float current_cell[3];
float total_current;
int count_time = 0 ;
volatile boolean interrupt_timer0;
hw_timer_t * timer0 = NULL;
portMUX_TYPE timerMux0 = portMUX_INITIALIZER_UNLOCKED;
void IRAM_ATTR onTimer0() {
portENTER_CRITICAL_ISR(&timerMux0);
interrupt_timer0 = true;
portEXIT_CRITICAL_ISR(&timerMux0);
}
void charge::begin(){
ads.begin();
timer0 = timerBegin(0, 80, true);
timerAttachInterrupt(timer0 , &onTimer0, true);
timerAlarmWrite(timer0, 2000000, true);
timerAlarmEnable(timer0);
}
void charge::voltage_read (void){
if (interrupt_timer0 = true) {
portENTER_CRITICAL(&timerMux0);
portEXIT_CRITICAL(&timerMux0);
for (int i = 0; i < 3; i++)
{
Serial.println("My voltage is ");
float cell_temp = analogRead(voltage_read_pin)*3.3/4095;
cell[i] = cell_temp *10;
Serial.println(cell_temp);
int16_t adc_temp = ads.readADC_SingleEnded(0);
current_cell[i] = adc_temp *100/0.075;
Serial.println("My current ");
Serial.println(current_cell[1]);
}
interrupt_timer0 = false;
}
}
void charge::check_voltage(){
Serial.println("total_cell");
total_cell_charge = (cell[0]+cell[1]+cell[2])/3 ;
Serial.println (total_cell_charge);
if (total_cell_charge > 27 ) {
digitalWrite(cut_off_pin, HIGH);
}
else if (total_cell_charge < 16 ) {
digitalWrite(cut_off_pin,HIGH);
}
else{
digitalWrite(cut_off_pin, LOW);
}
}
void charge::percentage(){
if (total_cell_charge >= 27 ){
percentage_value = 100 ;
}
else if (total_cell_charge < 27 and total_cell_charge > 26 ){
percentage_value = 95 ;
}
else if (total_cell_charge <= 26 and total_cell_charge > 25 ){
percentage_value = 90 ;
}
else if (total_cell_charge <= 25 and total_cell_charge > 24.5 ){
percentage_value = 85 ;
}
else if (total_cell_charge <= 24.5 and total_cell_charge > 23.5 ){
percentage_value = 80 ;
}
else if (total_cell_charge <= 23.5 and total_cell_charge > 23 ){
percentage_value = 75 ;
}
else if (total_cell_charge <= 23 and total_cell_charge > 22.8 ){
percentage_value = 70 ;
}
else if (total_cell_charge <= 22.8 and total_cell_charge > 22.5 ){
percentage_value = 65 ;
}
else if (total_cell_charge <= 22.5 and total_cell_charge > 22 ){
percentage_value = 60 ;
}
else if (total_cell_charge <= 22 and total_cell_charge > 21.8 ){
percentage_value = 55 ;
}
else if (total_cell_charge <= 21.8 and total_cell_charge > 21.5 ){
percentage_value = 50 ;
}
else if (total_cell_charge <= 21.5 and total_cell_charge > 21.3 ){
percentage_value = 45 ;
}
else if (total_cell_charge <= 21.3 and total_cell_charge > 21 ){
percentage_value = 35 ;
}
else if (total_cell_charge <= 21 and total_cell_charge > 20.9 ){
percentage_value = 30 ;
}
else if (total_cell_charge <= 20.8 and total_cell_charge > 20.5 ){
percentage_value = 25 ;
}
else if (total_cell_charge <= 20.5 and total_cell_charge > 20 ){
percentage_value = 20 ;
}
else if (total_cell_charge <= 20 and total_cell_charge > 19.8 ){
percentage_value = 10 ;
}
else {
percentage_value = 0 ;
}
}
void charge:: check_current(){
total_current = (current_cell[0] +current_cell[1]+current_cell[2] )/3;
if (total_current > 27 )
{
digitalWrite(cut_off_pin,HIGH);
}
else {
digitalWrite(cut_off_pin,LOW);
}
}
charge.h
#ifndef charge_h
#define charge_h
extern int percentage_value;
extern float total_cell_charge;
class charge {
public:
void begin();
void check_voltage();
void voltage_read (void);
void percentage();
void check_current();
void current_read (void);
};
#endif
main.ino
#include <Arduino.h>
#include "temperature.h"
#include "charge.h"
#include <Wire.h>
#include <WiFi.h>
#include <ESPAsyncWebServer.h>
#include <AsyncTCP.h>
#include "SPIFFS.h"
// Replace with your network credentials
const char* ssid = "ChiNghiep";
const char* password = "Chihai2411";
// Create AsyncWebServer object on port 80
AsyncWebServer server(80);
String temperature_text;
String percentage_text = "42";
temperature bms;
charge battery ;
void setup() {
Serial.begin(115200);
Serial.println("Hello!");
// Initialize SPIFFS
if(!SPIFFS.begin()){
Serial.println("An Error has occurred while mounting SPIFFS");
return;
}
// Connect to Wi-Fi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi..");
}
// Print ESP32 Local IP Address
Serial.println(WiFi.localIP());
// Route for root / web page
server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){
request->send(SPIFFS, "/index.html");
});
server.on("/temperature", HTTP_GET, [](AsyncWebServerRequest *request){
request->send_P(200, "text/plain", temperature_text.c_str());
});
server.on("/percentage", HTTP_GET, [](AsyncWebServerRequest *request){
request->send_P(200, "text/plain", percentage_text.c_str());
});
server.begin();
battery.begin();
bms.begin();
}
void loop() {
temperature_text =String(temperature_battery);
bms.calculate_temperature();
battery.voltage_read ();
battery.check_voltage();
battery.percentage();
battery.check_current();
}
SCHEMATIC HOW I CONNECT DHT11
