Electron DHT11 issues

Hi all!

I need Your help getting temperature and humidity reading from DHT11 sensor. I have tried a lot of different things and none of them work :confused:

I have Electron upgraded to device OS 1.4.4
I’ve connected LED to D6, Photosensor to D5 and D0 (as per manuals). I’ve also added a sound sensor to A1. All of these seem to work. I then added DHT11 connected to D2 with 10kOm pull-up to 3v3.

On the Particle desktop IDE:
I’ve tried Adafruit_DHT 0.0.4 and PietteTech_DHT 0.0.12 and flashing their corresponding simple examples (after selecting DHT11 device and D2 pin). I then connect to serial but see nothing at all! I’ve checked all COM ports and don’t see anything with putty either.

On the web IDE:
I want to get the values into Particle variables. So once I read the values I then convert them to string and send them to the variables. With Adafruit I always got 255 for both variables, with Piette I always get -7!

What am I missing here?

Sidenote - why can’t I see variables and functions on the console if I flash the same code from desktop IDE?

Thanks for any help!

// This #include statement was automatically added by the Particle IDE.
#include <PietteTech_DHT.h>

#define DHTTYPE  DHT11       // Sensor type DHT11/21/22/AM2301/AM2302
#define DHTPIN   D2          // Digital pin for communications

// We're going to start by declaring which pins everything is plugged into.

int led = D6; // This is where your LED is plugged in. The other side goes to a resistor connected to GND.

int photoresistor = A0; // This is where your photoresistor is plugged in. The other side goes to the "power" pin (below).
int microphone = A1;

int power = A5; // This is the other end of your photoresistor. The other side is plugged into the "photoresistor" pin (above).
// The reason we have plugged one side into an analog pin instead of to "power" is because we want a very steady voltage to be sent to the photoresistor.
// That way, when we read the value from the other side of the photoresistor, we can accurately calculate a voltage drop.

int lightlevel; // Here we are declaring the integer variable analogvalue, which we will use later to store the value of the photoresistor.
int soundvolume;
float h;
float t;

char h_char[10];
char t_char[10];


// Next we go into the setup function.

void setup() {

    // First, declare all of our pins. This lets our device know which ones will be used for outputting voltage, and which ones will read incoming voltage.
    pinMode(led,OUTPUT); // Our LED pin is output (lighting up the LED)
    pinMode(photoresistor,INPUT);  // Our photoresistor pin is input (reading the photoresistor)
    pinMode(power,OUTPUT); // The pin powering the photoresistor is output (sending out consistent power)

    // Next, write the power of the photoresistor to be the maximum possible, so that we can use this for power.

    // We are going to declare a Particle.variable() here so that we can access the value of the photoresistor from the cloud.
    Particle.variable("light_level", lightlevel);
    Particle.variable("sound_volume", soundvolume);
    Particle.variable("temperature", t_char);
    Particle.variable("humidity", h_char);
    // This is saying that when we ask the cloud for "analogvalue", this will reference the variable analogvalue in this app, which is an integer variable.

    // We are also going to declare a Particle.function so that we can turn the LED on and off from the cloud.
    // This is saying that when we ask the cloud for the function "led", it will employ the function ledToggle() from this app.



// Next is the loop function...

void loop() {

    // check to see what the value of the photoresistor is and store it in the int variable analogvalue
    lightlevel = analogRead(photoresistor);
    soundvolume = analogRead(microphone);
    h = DHT.getHumidity();
    // Read temperature as Celsius
	t = DHT.getCelsius();
	sprintf(t_char, "%.2f", t);
	sprintf(h_char, "%.2f", h);


// Finally, we will write out our ledToggle function, which is referenced by the Particle.function() called "led"

int ledToggle(String command) {

    if (command=="on") {
        return 1;
    else if (command=="off") {
        return 0;
    else {
        return -1;


Can you share how you have wired everything?

Sure Thing.

I’ve figured out the empty serial issue. I’ve had two files in the project, one .ino and one .cpp. I’ve made all the changes to the .cpp one, but compiler probably only compiled .ino. Now I transferred all the code to .ino and I can finally see serial output.

Regarding the DHT11, I still can’t figure out why I get -7 all the time…

The full wiring is:

DHT pin1 - Electron pin 3v3 (I've tried connecting directly to battery ~4V, but the result is the same)
DHT pin2 - 10kOm to pin1 
DHT pin2 - Electron D2
DHT pin3 - not connected
DHT pin4 - Electron pin Gnd

LED catode - Electron D6
LED anode - 220Ohm - Electron Gnd

Photoresistor pin1 - Electron A5
Photoresistor pin2 - Electron A0
Electron A0 - 220Ohm - Electron Gnd

Sound sensor pin1 (Analog Out) - Electron A1
Sound sensor pin2 - Electron Gnd
Sound sensor pin3 - Electron 3v3
Sound sensor pin4 - not connected

In the PietteTech_DHT library you’ll find this line

const int  DHTLIB_ERROR_NOTSTARTED       = -7;

So the -7 you keep getting indicates that you haven’t actually started any measurement acquisition.
In the samples you’ll find a line

  int result = DHT.acquireAndWait(1000); // wait up to 1 sec (default indefinitely)

which is missing in your code.

Yes! Now everything works just fine. Thank You so much!

Am I correct in assuming that DHT.acquireAndWait(1000) is the actual moment the measurements are taken, and DHT.getHumidity(); only extracts humidity data from the measurement? What is the purpose of 1000ms delay?

That is correct

That's not a delay but a timeout - the acquisition is allowed to take up to 1000ms.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.