Announcement

Collapse
No announcement yet.

Arduino - HC-SR04 Ultrasonic Sensor Distance Module

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • aldo
    replied
    Thank you Greig.

    Leave a comment:


  • enigmatheatre
    replied
    Originally posted by aldo View Post
    Not sure why but the values given by the sensor are sporadic, anything that we would need to adjust in the code?

    I'm also trying to implement the water level switch as part of this implementation, it has been already tested in the normal Arduino code, I would like to implement it into the API, is this the code that I would need to add to the API?

    #define inputPin 5
    --
    pinMode(inputPin, INPUT);
    --
    SendToHS(2,inputPin);

    Then create in the plugin an input pin.

    Thanks,
    Aldo
    Aldo Sorry I missed this post.
    Yes this is the code to add to the API but also add a change detection "IF" because this will send the value to HS every time the code loops and this is very fast so will likely crash the plugin.

    Greig.

    Leave a comment:


  • aldo
    replied
    Not sure why but the values given by the sensor are sporadic, anything that we would need to adjust in the code?

    I'm also trying to implement the water level switch as part of this implementation, it has been already tested in the normal Arduino code, I would like to implement it into the API, is this the code that I would need to add to the API?

    #define inputPin 5
    --
    pinMode(inputPin, INPUT);
    --
    SendToHS(2,inputPin);

    Then create in the plugin an input pin.

    Thanks,
    Aldo

    Leave a comment:


  • aldo
    replied
    Edit: I need to play with this some more, in the mean time I think I have found the issue with Unknown, I was setting the status to value, I changed it to string and it seem to be working ok. Still not sure if the refresh is right or needs to be adjusted.

    Thanks Greig, almost there. Sometime it reads the values like Dim 73% then mostly shows the value Unknown, I think it can not keep up with the value changes.
    these are the messages I get in the log
    May-15 6:36:26 PM Arduino Plugin Sent to IP:192.168.0.47 Port No:8900 = 1 h ,Ack Command
    May-15 6:36:26 PM Arduino Plugin Sent to IP:192.168.0.47 Port No:8900 = 1 h ,Ack Command
    May-15 6:36:26 PM Arduino Plugin Sent to IP:192.168.0.47 Port No:8900 = 1 h ,Ack Command
    May-15 6:36:26 PM Arduino Plugin Sent to IP:192.168.0.47 Port No:8900 = 1 h ,Ack Command
    May-15 6:36:26 PM Arduino Plugin Sent to IP:192.168.0.47 Port No:8900 = 1 h ,Ack Command

    Trust me, I'm reading as much as I can and getting my brain to overload, I saw it in your manual at one point but I recollected it after you told me.

    If I would like to integrate the switch into the API. Let me know if I'm doing it incorrectly:

    #define inputPin 5
    --
    pinMode(inputPin, INPUT);
    --
    SendToHS(2,inputPin);

    Then create in the plugin an input pin.
    Last edited by aldo; May 15th, 2017, 06:18 PM.

    Leave a comment:


  • enigmatheatre
    replied
    Originally posted by aldo View Post
    Quick question:
    I have attached the picture, what the input and output should be set at?

    After making changes or adding pins, do I need to download the sketch and make your changes into the new uploaded sketch?

    Thanks, Aldo
    There is no output set up in the code so you only need the input for the moment.

    If you are using Ethernet then you can not use some of the pins as they are used for the shield. Pins 12 and 13 are two of them so change the pins in the code to pins 8 and 9 and connect the sensor to them and upload the code again.

    You do not need to update the code if you make changes to the plugin config only if you change the code for the API.

    For the normal Sketch you do not need to update the board once it is set unless you change the IP config or change it from IP to a com port.

    Greig.

    Leave a comment:


  • aldo
    replied
    Quick question:
    I have attached the picture, what the input and output should be set at?

    After making changes or adding pins, do I need to download the sketch and make your changes into the new uploaded sketch?

    Than ks, Aldo

    Originally posted by enigmatheatre View Post
    Again this is untested code but I think this is what you want. I have removed the LED code and the Serial print and added the command to send it to API device 1.
    There might be timing problems but best to test it first as changing this may need more code.

    Code:
    #define trigPin 13
    #define echoPin 12
    int LastDistance = 0;
    
    void setup() {
      pinMode(trigPin, OUTPUT);
      pinMode(echoPin, INPUT);
    }
    
    void loop() {
      long duration, distance;
      digitalWrite(trigPin, LOW);  // Added this line
      delayMicroseconds(2); // Added this line
      digitalWrite(trigPin, HIGH);
      delayMicroseconds(10); // Added this line
      digitalWrite(trigPin, LOW);
      duration = pulseIn(echoPin, HIGH);
      distance = (duration/2) / 29.1;
      if (distance != LastDistance){
      SendToHS(1,distance);
      LastDistance = distance;
      }
    }
    Greig.
    Attached Files

    Leave a comment:


  • enigmatheatre
    replied
    You do not need the Void Setup or Void Loop as this is what the HSSteup and HSLoop are doing.

    Try this.

    Code:
    /************************************************************ *
    *Arduino to Homeseer 3 Plugin API written by Enigma Theatre.*
    * V1.0.0.140 *
    * *
    ************************************************************ */
    int FromHS[50]; 
    boolean IsConnected = false; 
    //************************************************************
    //**************Declare your variables here******************* 
    
    #define trigPin 13
    #define echoPin 12
    int LastDistance = 0;
    
    //************************************************************ ****
    void HSSetup() {
    
    //************************
    //Add YOUR SETUP HERE;
    //************************
    pinMode(trigPin, OUTPUT);
    pinMode(echoPin, INPUT);
    }
    
    void HSloop() {
    if (IsConnected == true) {
    /*Execute ONLY when HomeSeer is connected*/
    
    long duration, distance;
    digitalWrite(trigPin, LOW); // Added this line
    delayMicroseconds(2); // Added this line
    digitalWrite(trigPin, HIGH);
    delayMicroseconds(10); // Added this line
    digitalWrite(trigPin, LOW);
    duration = pulseIn(echoPin, HIGH);
    distance = (duration/2) / 29.1;
    if (distance != LastDistance){
    SendToHS(1,distance);
    LastDistance = distance;
    }
    }
    }
    Greig.

    Leave a comment:


  • aldo
    replied
    Thank you both for the code and for the write up of your configuration. I will try both ways, as of curiosity did you try using the hc_sr04 sensor instead and why did you choose the others instead? I have both set of sensors, I think the hc-sro4 are cool but not sure how effective they are compared to your sensors.

    Sent from my SM-G935V using Tapatalk

    Leave a comment:


  • bdraper
    replied
    I wrote about my sump pump setup and using Jon00 Database Charting Utility here, see https://board.homeseer.com/showthrea...171875&page=14

    An update of the setup...

    I have a low place in my yard near my HVAC unit. Of course the yard is always wet near the HVAC unit, so I ran a 4 inch drain out to the street that also has some of the house downspouts on it. Unfortunately relying on gravity does not allow the water to flow fast enough during a heavy rain, so I had to come up with a plan. I took a plastic 55 gallon drum, put a trash pump in it, and buried it. Then I ran the downspouts and the line from my HVAC unit to a catch basin that leads to the 55 gallon drum. The trash pump takes the rain water and pushes it to the street. I have 4 sensors in the drum that indicate level, (1/4 full, half full, 3/4 full and full) so that I can monitor the level in the tank. Recording the readings from the sensors will allow me to chart the levels in the tank over time and see the performance of the pump. We had a very bad storm on 09-10-2016, the readings gathered from that time period, indicate that the water never made it to the half full sensor. The pump is adjusted to cut on when the tank is between 1/4 and half full. I included a few pictures of the 55 gallon drum with the extender on it as well as the finished product.

    We have had a significant amount of rain in 2016, more than I have ever seen in White House since we moved here just over 18 years ago. During one of the recent storms we received 6 to 8 inches of rain in just 4 hours. The power did not go out so the pump did its job very well. No standing water in any of the low places. However some of our neighbors did not fare so well and received water under their house and garage. Interesting that the city of White House is higher than Nashville but when that much rain falls in a short amount of time, well it has to go somewhere. I should have mentioned that the pump is obviously reliant on electricity and during storms there is a higher possibility of a power outage. The 55 gallon drum has a 4 inch pipe that is connected at the top of the extender and runs to the street. This pipe is used as an overflow. This way if the electricity is out, the water still has a gravity fed path out to the street. The water will not get out as quick, because the fall rate is not ideal (obviously the reason for a pump), but it is better than not having any path at all.

    This is picture of the plastic 55 gallon drum with the man hole extender on it
    Click image for larger version

Name:	sp1.jpg
Views:	2
Size:	48.8 KB
ID:	1190760

    The tank has 4 level sensors they monitor water level and report their status back to HomeSeer. There is also a temperature sensor that reports its value back to an Arduino that is monitored by HomeSeer.

    The level sensors are Water Level Monitor Sensor Right Angle Float Switches ZPC1 White. The temperature sensors are Vktech 5pcs 3M Waterproof Digital Temperature Temp Sensor Probe DS18B20
    Click image for larger version

Name:	level sensor.jpg
Views:	1
Size:	41.6 KB
ID:	1190763Click image for larger version

Name:	temp sensor.jpg
Views:	1
Size:	36.9 KB
ID:	1190764

    Finished product, notice the grate next to the HVAC unit, the runoff goes into the grate from the concrete and the unit itself, a 4 inch pipe from the grate leads to the 55 gallon drum. There are also 4 downspouts from the house that are routed to the 55 gallon drum.
    Click image for larger version

Name:	sp3 finished.jpg
Views:	2
Size:	157.3 KB
ID:	1190761Click image for larger version

Name:	sp4.jpg
Views:	1
Size:	163.5 KB
ID:	1190762

    I have setup Jon00 Database Charting Utility to monitor the levels in the tank, so far the water level in the tank has not gotten above the first sensor and we have had some very heavy rains. I have setup Jon00 Database Charting Utility to monitor the levels in the tank, so far the water level in the tank has not gotten above the first sensor and we have had some very heavy rains. The graph below shows 2 hours of runtime during a storm we had on September 17, 2016.

    Click image for larger version

Name:	graph.png
Views:	2
Size:	50.7 KB
ID:	1190765

    Leave a comment:


  • enigmatheatre
    replied
    Again this is untested code but I think this is what you want. I have removed the LED code and the Serial print and added the command to send it to API device 1.
    There might be timing problems but best to test it first as changing this may need more code.

    Code:
    #define trigPin 13
    #define echoPin 12
    int LastDistance = 0;
    
    void setup() {
      pinMode(trigPin, OUTPUT);
      pinMode(echoPin, INPUT);
    }
    
    void loop() {
      long duration, distance;
      digitalWrite(trigPin, LOW);  // Added this line
      delayMicroseconds(2); // Added this line
      digitalWrite(trigPin, HIGH);
      delayMicroseconds(10); // Added this line
      digitalWrite(trigPin, LOW);
      duration = pulseIn(echoPin, HIGH);
      distance = (duration/2) / 29.1;
      if (distance != LastDistance){
      SendToHS(1,distance);
      LastDistance = distance;
      }
    }
    Greig.

    Leave a comment:


  • aldo
    replied
    This is what I call a "forum" and proud of being part of it, you guys rock.
    On a side note, it sounds corny but the best part of my day is reading posts on this board and listening to all the great people like you guys that make a difference. Looking forward to get my Arduino soon.

    Aldo

    Originally posted by enigmatheatre View Post
    It should be easy as you only need to remove all the serial print lines and add in a check if the value of distance has changed and if so print to Homeseer with SendToHS(1,distance);
    Then add this to the API sketch.
    I would send you the code but I am just on my phone so can not type it out.
    Give me a shout if you get stuck.

    Greig.

    Sent from my SM-G925F using Tapatalk

    Leave a comment:


  • enigmatheatre
    replied
    It should be easy as you only need to remove all the serial print lines and add in a check if the value of distance has changed and if so print to Homeseer with SendToHS(1,distance);
    Then add this to the API sketch.
    I would send you the code but I am just on my phone so can not type it out.
    Give me a shout if you get stuck.

    Greig.

    Sent from my SM-G925F using Tapatalk

    Leave a comment:


  • aldo
    replied
    I'm getting ahead of myself here, just thinking aloud, the project seems not to be that difficult, I'm trying to figure out how the plugin would read the values from it, like it is 42 cm from water. Thanks again for all your input.

    Originally posted by logbuilder View Post
    Here is a link to the steps to implement an ultrasonic sensor. It covers how to wire it and the sketch (code) to interact with the sensor. These types of examples are widely available for most anything you want to do with an Arduino.

    http://www.instructables.com/id/Simp...-SR04-Example/

    Leave a comment:


  • logbuilder
    replied
    Here is a link to the steps to implement an ultrasonic sensor. It covers how to wire it and the sketch (code) to interact with the sensor. These types of examples are widely available for most anything you want to do with an Arduino.

    http://www.instructables.com/id/Simp...-SR04-Example/

    Leave a comment:


  • aldo
    replied
    I can read code but not actually write them. I got the Arduino to learn so what you gave me is a great start. Thank you all.

    Aldo

    Originally posted by logbuilder View Post
    Have you ever coded before? If you by chance have some C background, you will have no problems.

    The way I learned Arduino (I do have many years in C development at corporate level) was to get an Arduino, I chose the Uno, a breadboard, some jumpers, and a sensor kit. My kit has 36 different sensor types. Some sensor kits contain a ultrasonic sensor and/or water sensor. You need to download the Arduino IDE and try to connect a sensor to your board. Lots of examples and videos available. Just go to youtube and search for 'arduino 101' and you will get lots of getting started type videos. After you are comfortable with the Arduino, IDE, and a few sensors, then you might be ready for the plugin.

    Arduino IDE
    https://www.arduino.cc/en/Main/Software

    Starter Kit
    https://www.amazon.com/SunFounder-Pr.../dp/B00CXMMDZI

    Leave a comment:

Working...
X