HC-SR04 + DHT11 in same project

Discussion about Z-Uno product. Visit http://z-uno.z-wave.me for more details.
Post Reply
iData
Posts: 2
Joined: 29 Mar 2018 22:24

HC-SR04 + DHT11 in same project

Post by iData »

Hi,

I would like to know if it's possible to use the HC-SR04 with the DHT11 into same INO project.

HC-SR04 works if I comment the two next lines :

Code: Select all

  humidity = dht.readHumidity();
  temperature = dht.readTemperature();
Same if I comment HC-SR04 and uncomment DHT11.

I can't have the two at the sametime.

Here my full code :

Code: Select all

// add library
#include "ZUNO_DHT.h"
// pin connection
#define DHTPIN 9

ZUNO_SETUP_CHANNELS(
    ZUNO_SENSOR_MULTILEVEL(
        ZUNO_SENSOR_MULTILEVEL_TYPE_DISTANCE,
        0, // scale is meters
        SENSOR_MULTILEVEL_SIZE_TWO_BYTES,
        2, // two decimals after dot
        getter
    ),
    ZUNO_SENSOR_MULTILEVEL_TEMPERATURE(getterTemperature),
    ZUNO_SENSOR_MULTILEVEL_HUMIDITY(getterHumidity)
);

//ZUNO_SETUP_ASSOCIATIONS(ZUNO_ASSOCIATION_GROUP_SET_VALUE); // to control other devices

int turn_on_distance_cm = 50; // Turn on light in CTRL_GROUP_1 if distance is < 50 cm

int readPin = 11;
int triggerPin = 10;
byte controlState = 0;
word lastValue;

int humidity;      // here we will store the humidity
int temperature;   // here we will store the temperature

DHT dht(DHTPIN, DHT11);

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);

  
  pinMode(readPin, INPUT);
  pinMode(triggerPin, OUTPUT);
  digitalWrite(triggerPin, LOW);

  dht.begin();
}

void loop() {
  
  // obtaining readings from the sensor DHT
  //humidity = dht.readHumidity();
  //temperature = dht.readTemperature();
  Serial.print("Humidity = "); 
  Serial.print(humidity);
  Serial.print(" %  ");
  Serial.print("Temperature = "); 
  Serial.print(temperature);
  Serial.println(" *C");
  // send data to channels
  zunoSendReport(2);
  zunoSendReport(3);
  

  
  int tmp;

  // trigger measurement
  digitalWrite(triggerPin, LOW);
  delayMicroseconds(10);
  digitalWrite(triggerPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(triggerPin, LOW);

 // read pulse width
  tmp = pulseIn(readPin, HIGH, 100000);
  if (tmp != 0) {
    lastValue = tmp / 58; // convert to cm, see datasheet
    Serial.print("CM : ");
    Serial.println(lastValue);
    
    // send report to controller (Life Line group)
    zunoSendReport(1);
  }
  
  Serial.println("humtemp");

  delay(1000);
}




word getter() {
  return lastValue;
}

byte getterTemperature() {
  return temperature;
}

byte getterHumidity() {
  return humidity;
}
Based on this two examples :
https://z-uno.z-wave.me/examples/dht11/
https://z-uno.z-wave.me/examples/hc-sr0 ... ce-sensor/


If I comment the two DHT11 lines, changes are taken (Message output) :

Code: Select all

Humidity = 0 %  Temperature = 0 *C
CM : 108
humtemp
If I uncomment the two DHT11 lines and changing the Serial.println("humtemp"); to Serial.println("humtemp comment out");
Change are not taken :

Code: Select all

Humidity = 0 %  Temperature = 0 *C
CM : 222
humtemp
I don't know why :/


Is someone have any idea plz ?

Thanks a lot.

---
iData
p0lyg0n1
Posts: 242
Joined: 04 Aug 2016 07:14

Re: HC-SR04 + DHT11 in same project

Post by p0lyg0n1 »

Please use the constants here. I have found that global "s_pin" variables don't work right at this moment.
That sketch have to work.

Code: Select all

// add library
#include "ZUNO_DHT.h"
// pin connection
#define DHTPIN 9

ZUNO_SETUP_CHANNELS(
    ZUNO_SENSOR_MULTILEVEL(
        ZUNO_SENSOR_MULTILEVEL_TYPE_DISTANCE,
        0, // scale is meters
        SENSOR_MULTILEVEL_SIZE_TWO_BYTES,
        2, // two decimals after dot
        getter
    ),
    ZUNO_SENSOR_MULTILEVEL_TEMPERATURE(getterTemperature),
    ZUNO_SENSOR_MULTILEVEL_HUMIDITY(getterHumidity)
);

//ZUNO_SETUP_ASSOCIATIONS(ZUNO_ASSOCIATION_GROUP_SET_VALUE); // to control other devices

int turn_on_distance_cm = 50; // Turn on light in CTRL_GROUP_1 if distance is < 50 cm
#define readPin 11
// s_pin readPin = 11;
#define triggerPin 10
//s_pin triggerPin = 10;
byte controlState = 0;
word lastValue;

int humidity;      // here we will store the humidity
int temperature;   // here we will store the temperature

DHT dht(DHTPIN, DHT11);

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);

  
  dht.begin();
  
  pinMode(readPin, INPUT);
  pinMode(triggerPin, OUTPUT);
  digitalWrite(triggerPin, LOW);

  
}

void loop() {
  
  // obtaining readings from the sensor DHT
  humidity = dht.readHumidity();
  temperature = dht.readTemperature();
  Serial.print("Humidity = "); 
  Serial.print(humidity);
  Serial.print(" %  ");
  Serial.print("Temperature = "); 
  Serial.print(temperature);
  Serial.println(" *C");
  // send data to channels
  zunoSendReport(2);
  zunoSendReport(3);
  
  
  int tmp;
  digitalWrite(triggerPin, LOW);
  delayMicroseconds(10);
  digitalWrite(triggerPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(triggerPin, LOW);

 // read pulse width
  tmp = pulseIn(readPin, HIGH, 100000);
  // Serial.println("RAW:");
  // Serial.println(tmp);

  if (tmp != 0) {
    lastValue = tmp / 58; // convert to cm, see datasheet
    Serial.print("CM : ");
    Serial.println(lastValue);
    
    // send report to controller (Life Line group)
    zunoSendReport(1);
  }
  
  Serial.println("humtemp");

  delay(1000);
}




word getter() {
  return lastValue;
}

byte getterTemperature() {
  return temperature;
}

byte getterHumidity() {
  return humidity;
}
I have found that "pulseIn" was calibrated for external power supply, connected to Z-Uno via USB (see this for details https://forum.z-wave.me/viewtopic.php?f ... 5&start=10). We will fix it in next version. But If you want a precise values of distance now, you can modify constants in HLCore.cpp this way:

Code: Select all

// ...
#define F_CONTANT_NANOS 5123 // 3069
#define C_CONTANT_NANOS 4900 // 3200
// ...
instead of:

Code: Select all

#define F_CONTANT_NANOS 3069
#define C_CONTANT_NANOS 3200
Thank you, we will fix the "s_pin" issue in the next release too.
iData
Posts: 2
Joined: 29 Mar 2018 22:24

Re: HC-SR04 + DHT11 in same project

Post by iData »

Thank you a lot for your response.

If I understand, with the USB alimentation there is a low frequency accuracy.
I need to try with an external supply for a better accuracy.
I'll try as soon as possible.


Have you tried with the 2 sensors ?
I'll tried with the 2 values but nothing changed, I can't have the two sensors at the same time :/

Thank a lot
Post Reply