HC-SR04 and DS18B20 help?

Discussion about Z-Uno product. Visit http://z-uno.z-wave.me for more details.
KristjanV
Posts: 17
Joined: 19 May 2018 10:54

Re: HC-SR04 and DS18B20 help?

Post by KristjanV » 11 Jul 2018 10:00

Thanks man! But... now for some reason ultrasonic reading is a bit off. In code i have deviation 10cm and tank height 110cm so every 10cm in real life shoulb be 10% in the reading. But it isn't. If i move an object 10cm the reading drops only 4%. I have input voltage for Z-Uno on 5.02V, so that should not be an issue.

KristjanV
Posts: 17
Joined: 19 May 2018 10:54

Re: HC-SR04 and DS18B20 help?

Post by KristjanV » 12 Jul 2018 08:38

Other than that, the project turned out quite well.
I have put Z-Uno and Vera Edge into one box. The whole idea was to make a device that can measure different temperatures and also pellet bunker fulfillment level and you can access that information through mobile phone plus get notifications when temperatures are too high/low and also when bunker level gets too low. To justify the use of z-wave devices, it leaves open the opportunity to always add more devices like smoke detector or flood sensor etc.

Heres some pictures for you guys.
Image
Image
Image
Image
Image

User avatar
PoltoS
Posts: 4348
Joined: 26 Jan 2011 19:36

Re: HC-SR04 and DS18B20 help?

Post by PoltoS » 15 Jul 2018 01:00

Can we share your project with a reference to you on our site?

KristjanV
Posts: 17
Joined: 19 May 2018 10:54

Re: HC-SR04 and DS18B20 help?

Post by KristjanV » 16 Jul 2018 08:03

PoltoS wrote:
15 Jul 2018 01:00
Can we share your project with a reference to you on our site?
Yes ofcourse, no problem!


KristjanV
Posts: 17
Joined: 19 May 2018 10:54

Re: HC-SR04 and DS18B20 help?

Post by KristjanV » 20 Jul 2018 11:12

Here's the code i used. Doesn't look too good but it works (for me). Feel free to adjust and fix =)

And BIG THANKS to you all who helped me get this thing working!

-PoltoS
-mdietinger@gmail.com
-p0lyg0n1

Code: Select all

// 4 temperature sensors DS18B20, 1 ultrasonic distance sensor HC-SR04

#include "ZUNO_DS18B20.h"

#define DS18B20_BUS_PIN 11                  // Pin to which DS18B20 bus is connected
#define N_SENSOR 4                          // Number of DS18B20 sensors
// You need to adopt ZUNO_SETUP_CHANNELS and add more getters if tou need more sensors

OneWire ow(DS18B20_BUS_PIN);
DS18B20Sensor ds18b20(&ow);

#define ADDR_SIZE 8                         // Size of address of devices on 1-wire bus
byte addresses[ADDR_SIZE * N_SENSOR];        // Here we store all the scanned addresses
#define ADDR(i) (&addresses[i * ADDR_SIZE]) // Macro to simplify our life
byte number_of_sensors;                     // Number of sensors found
word temperature[N_SENSOR];                 // Here we store temperatures

//ultrasonic sensor
int readPin = 9;
int triggerPin = 10;
byte controlState = 0;
byte percentage;
//ultrasonic sensor

// Define Z-Wave channels. Add more if you need more sensors
ZUNO_SETUP_CHANNELS(
        ZUNO_SENSOR_MULTILEVEL(ZUNO_SENSOR_MULTILEVEL_TYPE_TEMPERATURE, SENSOR_MULTILEVEL_SCALE_CELSIUS, SENSOR_MULTILEVEL_SIZE_TWO_BYTES, SENSOR_MULTILEVEL_PRECISION_TWO_DECIMALS, getterTemp1),
        ZUNO_SENSOR_MULTILEVEL(ZUNO_SENSOR_MULTILEVEL_TYPE_TEMPERATURE, SENSOR_MULTILEVEL_SCALE_CELSIUS, SENSOR_MULTILEVEL_SIZE_TWO_BYTES, SENSOR_MULTILEVEL_PRECISION_TWO_DECIMALS, getterTemp2),
        ZUNO_SENSOR_MULTILEVEL(ZUNO_SENSOR_MULTILEVEL_TYPE_TEMPERATURE, SENSOR_MULTILEVEL_SCALE_CELSIUS, SENSOR_MULTILEVEL_SIZE_TWO_BYTES, SENSOR_MULTILEVEL_PRECISION_TWO_DECIMALS, getterTemp3),
        ZUNO_SENSOR_MULTILEVEL(ZUNO_SENSOR_MULTILEVEL_TYPE_TEMPERATURE, SENSOR_MULTILEVEL_SCALE_CELSIUS, SENSOR_MULTILEVEL_SIZE_TWO_BYTES, SENSOR_MULTILEVEL_PRECISION_TWO_DECIMALS, getterTemp4),
        

//ultrasonic sensor        
        ZUNO_SENSOR_MULTILEVEL_GENERAL_PURPOSE(getter)  
);

void setup() {
  number_of_sensors = ds18b20.findAllSensors(addresses);

//ultrasonic sensor
  Serial.begin();
  pinMode(readPin, INPUT);
  pinMode(triggerPin, OUTPUT);
  digitalWrite(triggerPin, LOW);
//ultrasonic sensor
}

void loop() {

  for (byte i = 0; i < number_of_sensors; i++) {
    // Read temperature 
   // It already encoded for format VVV.vv and doesn't use float
    temperature[i] = ds18b20.getTempC100(ADDR(i));
    }

//ultrasonic sensor
      byte distance, heightTank, deviation;
      int duration;
      // WE HAVE TO DO IT JUST ONCE, NOT INSIDE  DS18B20 LOOP^
      //You'll probably want to change the next 2 lines.
      // The first one is the max. level of the water.
      // The next one is how high the sensor is above that max. level.
      heightTank=110;
      deviation=10;
   
      digitalWrite(triggerPin, LOW);
      delayMicroseconds(10);
      digitalWrite(triggerPin, HIGH);
      delayMicroseconds(10);
      digitalWrite(triggerPin, LOW);
     // this code loads the uC hard, so lets report all values after it ends
      duration = pulseIn(readPin, HIGH, 100000);
      // This uses float thats not good for this uS, it eats lots of code
      // distance = (duration/2)/29.1;
      // percentage = (byte)(100-(((distance-deviation)*100)/heightTank));
     // alternative will be: (RISC style)
      duration >>= 1; // /2
      duration *= 100;
      duration /= 291;
      duration -= deviation;
      // To ensure there is no overflow
      duration = max(duration, heightTank);
      duration *= 100;
      duration /= heightTank;
      percentage = 100 - duration;
      Serial.println(percentage);
     // Report all the sensors at the end of the loop
     for(byte i=0;i<5;i++){
       zunoSendReport(i+1);
     }
     // delay while the OS could do something with  reports & radio
     delay(3000); 
  }

// Getters. Add more if you need more sensors
word getterTemp1() {
  return temperature[0];
}

word getterTemp2() {
  return temperature[1];
}

word getterTemp3() {
  return temperature[2];
}

word getterTemp4() {
  return temperature[3];
}

//ultrasonic sensor
byte getter() {
  return percentage;
//ultrasonic sensor
}

Post Reply