Quantcast
Channel: Sensors - Arduino Forum
Viewing all articles
Browse latest Browse all 1074

Humidity / Temperature sensor failing startup

$
0
0


The image is of the wiring of my project
The code i am using is below

#include <LiquidCrystal.h>

const int rs = 8, en = 7, d4 = 3, d5 = 4, d6 = 5, d7 = 6;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

int DHpin = 2; // input/output pin
byte dat[5];   

byte read_data()
{
  byte i = 0;
  byte result = 0;
  for (i = 0; i < 8; i++) {
      while (digitalRead(DHpin) == LOW); // wait 50us
      delayMicroseconds(30); //The duration of the high level is judged to determine whether the data is '0' or '1'
      if (digitalRead(DHpin) == HIGH)
        result |= (1 << (8 - i)); //High in the former, low in the post
    while (digitalRead(DHpin) == HIGH); //Data '1', waiting for the next bit of reception
    }
  return result;
}

void start_test()
{
  digitalWrite(DHpin, LOW); //Pull down the bus to send the start signal
  delay(30); //The delay is greater than 18 ms so that DHT 11 can detect the start signal
  digitalWrite(DHpin, HIGH);
  delayMicroseconds(40); //Wait for DHT11 to respond
  pinMode(DHpin, INPUT);
  while(digitalRead(DHpin) == HIGH);
  delayMicroseconds(80); //The DHT11 responds by pulling the bus low for 80us;
  
  if(digitalRead(DHpin) == LOW)
    delayMicroseconds(80); //DHT11 pulled up after the bus 80us to start sending data;
  for(int i = 0; i < 5; i++) //Receiving temperature and humidity data, check bits are not considered;
    dat[i] = read_data();
  pinMode(DHpin, OUTPUT);
  digitalWrite(DHpin, HIGH); //After the completion of a release of data bus, waiting for the host to start the next signal
}

void setup()
{
  Serial.begin(9600);
  lcd.begin(16, 2);
  lcd.print("Starting...");
  pinMode(DHpin, OUTPUT);
}

void loop() {
  start_test(); // Perform DHT11 sensor data acquisition

  lcd.setCursor(15,1);
  lcd.print(" ");

  byte expectedChecksum = dat[0] + dat[1] + dat[2] + dat[3]; // Calculate the expected checksum

  //Serial.print("Difference in Checksum: "); //ACTIVATE IF ! SHOWS ON LCD-DISPLAY
  //Serial.println(dat[4] - expectedChecksum);
  

  if (dat[4] != expectedChecksum) { // Compare expected checksum with actual checksum
    if (dat[4] != expectedChecksum + 2) {
      lcd.setCursor(15,1);
      lcd.print('!');
    }
  }

  lcd.setCursor(0,0);
  lcd.print("Humidity = "); // Print received data values
  lcd.print(dat[0], DEC); // Displays the integer bits of humidity
  lcd.print('.');
  lcd.print(dat[1], DEC); // Displays the decimal places of the humidity
  lcd.println('%');

  lcd.setCursor(0,1);
  lcd.print("Temp = ");
  lcd.print(dat[2], DEC); // Displays the integer bits of temperature
  lcd.print('.');
  lcd.print(dat[3], DEC); // Displays the decimal places of the temperature
  lcd.print(char(223));
  lcd.print('C');

  delay(1000); // Delay before taking next reading
}

When i plug the ardiono into power it prints "Starting..." on the LCD but then nothing else happens, if i press the reset button on the arduino, the "Starting..." text flashes and it successfully prints the temperature and humidity
Why does it not succeed the first time and how can i fix it?

4 posts - 4 participants

Read full topic


Viewing all articles
Browse latest Browse all 1074

Trending Articles