简体   繁体   中英

Power DHT11 with pin out Wemos D1 R1

I am trying to run a DHT11 sensor from my Wemos D1 R1. I have the ground connected to ground and the sensor connected to D2 pin with a resister between sensor and power line. I have tried to connect the power to the D3 pin and setting the pinMode(D3,OUTPUT);and digitalWrite(D3,HIGH);, but the sensor isn't recognized. If I connect the power to the 3.3v output pin on the Wemos it works fine. I am plugging the Wemos into my computer's USB. Can someone tell my why the Wemos isn't being powered up by the D3 pin. Do I need to connect 9v to the Wemos instead of the computer power? Not a big deal but would be nice to understand why and to hook it up to another power pin. This is my code that activates the pins and turns on the power to the D3 pin. 在此处输入图片说明 在此处输入图片说明

#include <DHTesp.h>

DHTesp dht;

pinMode(D3,OUTPUT);//make pin D3 a power outlet for 3.3v

void setup() {


    Serial.begin(115200);
    dht.setup(D2, DHTesp::DHT11);

digitalWrite(D3,HIGH);//make pin D3 hot
}


void loop() {


  delay(8000);

    float t = dht.getTemperature();
    float f = (t*1.8) + 32;

    if (isnan(t))
  {
    Serial.println("Failed to read from DHT2 sensor!"); **//when the sensor is powered by pin D3 this shows up but when powered by 3.3v it does get the sensor amount**
    return;
  }
    Serial.print(", \"maintemp\": ");
    Serial.print(f);
    Serial.print("}\n");

delay(2000);
}

DHT11 pinouts as follow:

1 VCC -> outer power is better, operates from 3.5 to 5.5V

2 DATA -> WEMOS D2...D7

3 NC

4 GND -> GND

if connect to D2 then write your code as follow:

dht.setup(D2, DHTesp::DHT11);

在此处输入图片说明

Are you sure you can do dht.setup(...) before you have power on the DHT11? I would guess that it need power first and then setup. You might even need to have a pause before.

Also; make sure you compile for the right board or else D3 might not relate to D3 on the board.

The following code tries to first initialize the DHT sensor but only powers it up afterwards. That won't work as the initialization code already tries to communicate with the sensor, which has no power:

dht.setup(D2, DHTesp::DHT11);
digitalWrite(D3,HIGH);//make pin D3 hot

So instead write:

digitalWrite(D3,HIGH); // power up the sensor
delay(500);  // allow some time to become ready
dht.setup(D2, DHTesp::DHT11); // initialiye the sensor

BTW: Is the pinMode command outside the setup function? Does it compile like this?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM