PCF8574 define DHT pin

Hello,
I am new to Arduino and I am looking for support.

currently, I am working on a project to read the temperature and humidity through an extender ( PCF8574AT). if I am using the Arduino pins then I will use the sense pin in the following function DHT HT(sensePin, Type) is the pin number i.e. 2 but when it comes to the extender, I am not able to define the pin that I am attaching the DHT11 to i.e. P0

image

here is the code that I am using

#include <DHT.h>
#include <Adafruit_Sensor.h>
#define Type DHT11
#include <LiquidCrystal.h>


int rs=7;
int en=8;
int d4=9;
int d5=10;
int d6=11;
int d7=12;

LiquidCrystal lcd(rs,en,d4,d5,d6,d7);
int sensePin=2;
DHT HT(sensePin, Type);
int wait=500;

float humidity;
float temp;


void setup() {
  // put your setup code here, to run once:
 Serial.begin(9600);
 HT.begin();
 delay(wait);
 lcd.begin(16,2);
 



}

void loop() {

  lcd.setCursor(0,0);
  lcd.print("Hello World!");
  // put your main code here, to run repeatedly:
  humidity = HT.readHumidity()-10;
  temp = HT.readTemperature();
  lcd.setCursor(0,1);
  lcd.print("H:");
  lcd.print(humidity);
  lcd.print("% T:");
  lcd.print(temp);
  Serial.print("H:");
  Serial.print(humidity);
  Serial.print("% T:");
  Serial.print(temp);
  delay(2000);
  lcd.clear();


}

You would need to install the PCF8574 library and use it to control the pins on the extender board. This instructible should give you the general idea:

BTW, the library can be installed via Tools -> Manage Libraries...

However, I am not sure, but I don't think you will be able to bind the PCF8574 pin to the DHT library via the DHT HT() function so you would probably need to read the pin directly and extract the data being transmitted instead of relying on the DHT library to do it for you. The following provides some information on how to do that and a code example:

You would need to adapt it to use the PCF8574 pin calls.

BItSeeker,

thank you for the swift reply. I have tried that with an Arduino pin and it worked fine and then with PCF8574. what I have done:

  • Added the PCF8574.h lib.
  • modify all pins by adding "pcf8574" and using P1

I got the error "ERROR_TIMEOUT"

Serial monitor

00:28:43.495 -> Init pcf8574...OK
00:28:45.499 -> startTime: 2019132
00:28:45.499 -> live: 0
00:28:45.532 -> startTime: 2019132
00:28:45.532 -> live: 28
00:28:45.532 -> startTime: 2019132
00:28:45.564 -> live: 128
00:28:45.564 -> ERROR_TIMEOUT
00:28:47.535 -> startTime: 4079496
00:28:47.567 -> live: 4
00:28:47.567 -> startTime: 4079496
00:28:47.600 -> live: 36
00:28:47.600 -> startTime: 4079496
00:28:47.632 -> live: 140
00:28:47.632 -> ERROR_TIMEOUT

#include "Arduino.h"
#include "PCF8574.h"

PCF8574 pcf8574(0x38);
void setup()
{
  Serial.begin(9600);
  pcf8574.pinMode(P1, INPUT);
  Serial.print("Init pcf8574...");
	if (pcf8574.begin()){
		Serial.println("OK");
	}else{
		Serial.println("KO");
	}

}

void dec2bin(int n)
{
    int c, k;

   for (c = 15; c >= 0; c--)
    {
        k = n >> c;

        if (k & 1)
            Serial.print("1");
        else
            Serial.print("0");
        }
    } 

void dec2bin8(int n)
{
    int c, k;

   for (c = 7; c >= 0; c--)
    {
        k = n >> c;

        if (k & 1)
            Serial.print("1");
        else
            Serial.print("0");
        }
    }


void wait_for_dht11()
{
delay(2000);
}

void start_signal(uint8_t dht11_pin){
pcf8574.pinMode(dht11_pin, OUTPUT);
pcf8574.digitalWrite(dht11_pin, LOW); 
delay(18);
pcf8574.digitalWrite(dht11_pin, HIGH);
pcf8574.pinMode(dht11_pin, INPUT);
pcf8574.digitalWrite(dht11_pin, HIGH); 
}

void read_dht11(uint8_t dht11_pin)
{
  uint16_t rawHumidity = 0;
  uint16_t rawTemperature = 0;
  uint8_t checkSum = 0;
  uint16_t data = 0;

  uint8_t humi;
  uint8_t humd;
  uint8_t tempi;
  uint8_t tempd; 

  unsigned long startTime;
  
  for ( int8_t i = -3 ; i < 80; i++ ) {
    byte live;
    startTime = micros();

    do {
      live = (unsigned long)(micros() - startTime);
      if ( live > 90 ) {
        Serial.println("ERROR_TIMEOUT");
        return;
      }
    }
    while ( pcf8574.digitalRead(dht11_pin) == (i & 1) ? HIGH : LOW );

    if ( i >= 0 && (i & 1) ) {
      data <<= 1;

      // TON of bit 0 is maximum 30 usecs and of bit 1 is at least 68 usecs.
      if ( live > 30 ) {
        data |= 1; // we got a one
      }
    }

    switch ( i ) {
      case 31:
        rawHumidity = data;
        break;
      case 63:
        rawTemperature = data;
      case 79: 
        checkSum = data;
        data = 0;
        break;
    }
  }

Serial.println("Humidity: ");
dec2bin(rawHumidity);
Serial.print("\t");
humi = rawHumidity >> 8;
dec2bin8(humi);
Serial.print("\t");
rawHumidity = rawHumidity << 8;
humd = rawHumidity >> 8;
dec2bin8(humd);
Serial.print("\t");
Serial.print(humi);
Serial.print(".");
Serial.print(humd);
Serial.print("%");
Serial.println("");

Serial.println("Temperature Degree Celcius: ");
dec2bin(rawTemperature);
Serial.print("\t");
tempi = rawTemperature >> 8;
dec2bin8(tempi);
Serial.print("\t");
rawTemperature = rawTemperature << 8;
tempd = rawTemperature >> 8;
//tempd = (byte)rawTemperature;
dec2bin8(tempd);
Serial.print("\t");
Serial.print(tempi);
Serial.print(".");
Serial.print(tempd);
Serial.print("C");
Serial.println("");

Serial.println("Checksum Byte: ");
dec2bin8(checkSum);
Serial.println("");
dec2bin8(tempi + tempd + humi + humd);
Serial.println("");
if((byte)checkSum == (byte)(tempi + tempd + humi + humd)){Serial.print("CHECKSUM_OK");}
else {Serial.print("CHECKSUM_ERROR");}
Serial.println("");
Serial.println("");
Serial.println("");
}



void loop()
{
for(unsigned int x = 0; x < 5; x++)
{
wait_for_dht11();
start_signal(P1);
read_dht11(P1);
}
Serial.end(); 
}

micros() returns a type unsigned long and you have declared startTime as unsigned long. The result of micros() - startTime would therefore already be of type unsigned long and would not require casting to unsigned long. The receiving variable 'live' therefore ought also be of type unsigned long, not of type byte.

Alternatively, if you want to store the result in an 8-bit byte, then you would cast the result to a byte:

live = (byte) (micros() - startTime);

You might also try explicitly initialising live to 0, i.e.:

byte live = 0;

I am not sure it is needed, but it would rule out the possibility of the variable 'live' being initialised to some random value or the value from the previous loop and behaving cumulatively.

for some reason I burned out the DHT11. :smiley:

I will order a new one and test again

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