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

HC-SR04 and DS18B20 help?

Post by KristjanV »

Hi!

Im having some trouble setting up HC-SR04 sensor to measure pellet bunker level. The sensor is on top of the bunker and needs to be working in reverse. If the bunker level gets lower, the value should go down. Is it possible? I have no idea what lines to add to sample code https://z-uno.z-wave.me/examples/hc-sr0 ... ce-sensor/ and how to write value in %, not cm. Any help?

Later i will add 6 DS18B20 temp sensors. But again i have no idea about the code for both of those things to work.
Im using Vera Edge controller at the moment.

The whole idea is to put z-uno with some central in one box, so i could measure 6 different temperatures on my heater and monitor pellet bunker level over the internet.

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

Re: HC-SR04 and DS18B20 help?

Post by KristjanV »

So i got the distance reading correct with different transformer. 10cm is displayed as 0.10 in the interface.

Could this work to display percentage? Or at least level 100(full) - 0(empty)

Code: Select all

void loop(){
      int duration,distance,percentage,heightTank,deviation;
      //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=151;
      deviation=29;
  
      digitalWrite(trigPin,HIGH);
      delayMicroseconds(1000);
      digitalWrite(trigPin,LOW);
      duration=pulseIn(echoPin,HIGH);
      distance=(duration/2)/29.1;
      percentage=100-(((distance-deviation)*100)/heightTank);
      Serial.println(distance);
      Serial.println(percentage);
     
      delay(1000);
User avatar
PoltoS
Posts: 7565
Joined: 26 Jan 2011 19:36

Re: HC-SR04 and DS18B20 help?

Post by PoltoS »

Sure. You need to change

Code: Select all

    ZUNO_SENSOR_MULTILEVEL(
        ZUNO_SENSOR_MULTILEVEL_TYPE_DISTANCE,
        0, // scale is meters
        SENSOR_MULTILEVEL_SIZE_TWO_BYTES,
        2, // two decimals after dot
        getter
    )
to something like

Code: Select all

ZUNO_SENSOR_MULTILEVEL_GENERAL_PURPOSE(getter)
And change the getter to return one byte only in percentage (instead of word).

The value should be calculated according to your tank:
percentage_value = (byte) ((word)(L - distance) * 100 / L)
KristjanV
Posts: 17
Joined: 19 May 2018 10:54

Re: HC-SR04 and DS18B20 help?

Post by KristjanV »

I can build things but coding is not my strongest side. Still learning-reading what some of those words even mean, sorry! :)
KristjanV
Posts: 17
Joined: 19 May 2018 10:54

Re: HC-SR04 and DS18B20 help?

Post by KristjanV »

Okay so i modified the code to this but i get an error when compiling: Error compiling for board Z-Wave>ME Z-Uno. Aaand i have no idea what im missing here.

Code: Select all

// HC-SR04 Ultrasonic Distance Sensor

ZUNO_SETUP_CHANNELS(
   ZUNO_SENSOR_MULTILEVEL_GENERAL_PURPOSE(getter)
);


int readPin = 9;
int triggerPin = 10;
byte controlState = 0;
word percentage;

void setup() {
  Serial.begin();
  pinMode(readPin, INPUT);
  pinMode(triggerPin, OUTPUT);
  digitalWrite(triggerPin, LOW);
}

void loop(){
      int duration, distance, percentage, heightTank, deviation;
      
      //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=151;
      deviation=29;
  
       digitalWrite(triggerPin, LOW);
       delayMicroseconds(10);
       digitalWrite(triggerPin, HIGH);
       delayMicroseconds(10);
       digitalWrite(triggerPin, LOW);
     
      duration = pulseIn(readPin,HIGH, 100000);
      
      distance = (duration/2)/29.1;
      percentage = 100-(((distance-deviation)*100)/heightTank);
      Serial.println(percentage);
     
      delay(1000);
}

word getter() {
  return percentage;
}
And change the getter to return one byte only in percentage (instead of word).
Didn't quite understand what im supposed to change?
User avatar
PoltoS
Posts: 7565
Joined: 26 Jan 2011 19:36

Re: HC-SR04 and DS18B20 help?

Post by PoltoS »

This (corrected) code compiles. But I've not tried it with the hardware.

Code: Select all

// HC-SR04 Ultrasonic Distance Sensor

ZUNO_SETUP_CHANNELS(
   ZUNO_SENSOR_MULTILEVEL_GENERAL_PURPOSE(getter)
);


int readPin = 9;
int triggerPin = 10;
byte controlState = 0;
byte percentage;

void setup() {
  Serial.begin();
  pinMode(readPin, INPUT);
  pinMode(triggerPin, OUTPUT);
  digitalWrite(triggerPin, LOW);
}

void loop(){
      byte distance, heightTank, deviation;
      int duration;
      
      //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=151;
      deviation=29;
  
      digitalWrite(triggerPin, LOW);
      delayMicroseconds(10);
      digitalWrite(triggerPin, HIGH);
      delayMicroseconds(10);
      digitalWrite(triggerPin, LOW);
     
      duration = pulseIn(readPin, HIGH, 100000);
      
      distance = (duration*10/2)/291;
      percentage = (byte)(100-(((distance-deviation)*100)/heightTank));
      Serial.println(percentage);
     
      delay(1000);
}

byte getter() {
  return percentage;
}
User avatar
PoltoS
Posts: 7565
Joined: 26 Jan 2011 19:36

Re: HC-SR04 and DS18B20 help?

Post by PoltoS »

But your code compiles too, so make sure you have selected the Tools->Board->Z-Uno
KristjanV
Posts: 17
Joined: 19 May 2018 10:54

Re: HC-SR04 and DS18B20 help?

Post by KristjanV »

Yep it compiled now. There was some error with my file so i created a new one and all good but with both codes, Vera doesn't show any result - level. It shows the device but no value. I tried moving object from 10cm to 100cm but still nothing.
KristjanV
Posts: 17
Joined: 19 May 2018 10:54

Re: HC-SR04 and DS18B20 help?

Post by KristjanV »

Still no results, i tried to modify the code back and forth but no use, cant get any readings from Vera. Maybe its the Vera controller? Even though it worked with example code. :? :?
Image
KristjanV
Posts: 17
Joined: 19 May 2018 10:54

Re: HC-SR04 and DS18B20 help?

Post by KristjanV »

Any ideas? I still haven't made any progress with the distance reading in percent.
Post Reply