# Basic where to start debugging

**URL:** <https://community.particle.io/t/basic-where-to-start-debugging/44922>\
**Category:** Troubleshooting\
**Created:** [November 1, 2018, 5:14pm UTC](https://community.particle.io/t/basic-where-to-start-debugging/44922 "2018-11-01T17:14:39Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![Condoman](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/condoman/32/18562_2.png) [@Condoman](https://community.particle.io/u/Condoman)\
**Post date:** [November 1, 2018, 5:14pm UTC](https://community.particle.io/t/basic-where-to-start-debugging/44922/1 "2018-11-01T17:14:39Z")

</div>

I decided to build [this project](https://community.particle.io/t/measure-oil-level-or-any-liquid-and-alert-hardware-firmware-code/38393/30) and getting it going was easy with the great directions.

My problem as a first time Photon user is why does it not seem to work. I was able to successfully compile and flash. I see with the app the data fields but they make no reliable sense. The Photon connects just fine and the app displays the variables.

Thought the SR04 was defective so I bought 2 more, same garbage readings. I get: oil-gallons=0.0, oil-inches=46.9, distance=-1.0 and 4.73 VDC on the GND and VCC of the SR04.

To cut down on any issues I commented out all the alert code.

Any suggestions where to start?

---

<div class="post-metadata">

**Author:** ![ScruffR](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/scruffr/32/6952_2.png) [@ScruffR](https://community.particle.io/u/ScruffR)\
**Post date:** [November 2, 2018, 11:08am UTC](https://community.particle.io/t/basic-where-to-start-debugging/44922/2 "2018-11-02T11:08:30Z")

</div>

Can you share _your_ code (if using Web IDE the easiest is to use the SHARE THIS REVISION button and post the link) and also state how _you_ connected the sensor?  
That’d make debugging _your_ issue easier 😉

---

<div class="post-metadata">

**Author:** ![Condoman](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/condoman/32/18562_2.png) [@Condoman](https://community.particle.io/u/Condoman)\
**Post date:** [November 2, 2018, 1:59pm UTC](https://community.particle.io/t/basic-where-to-start-debugging/44922/3 "2018-11-02T13:59:13Z")

</div>

Thanks for the reply.

The code is posted in the “this project” link which links to the [blog](https://blog.vpetkov.net/2017/11/12/diy-monitor-heating-oil-tank-gallons-with-pushbullet-sms-and-email-alerting/)

The connections are just as shown in the posting of the project, except I have commented out the alert section.

I can repost the code if needed.

---

<div class="post-metadata">

**Author:** ![peekay123](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/peekay123/32/810_2.png) [@peekay123](https://community.particle.io/u/peekay123)\
**Post date:** [November 2, 2018, 2:30pm UTC](https://community.particle.io/t/basic-where-to-start-debugging/44922/4 "2018-11-02T14:30:04Z")

</div>

@Condoman, posting a picture of your wiring might be helpful. You can find other posts from other members regarding the SR04 sensor which may be helpful.

> [@JSN-SR04 ultrasonic sensor](https://community.particle.io/t/jsn-sr04-ultrasonic-sensor/34161):
>
> I have a JSN-SR04 sensor I just purchased that I suspect is bad. Can anyone tell me if it is the same as the HC-SR04 except they are waterproof? I am able to get firmware to work with the HC-SR04 but not the JSN-SR04.

> [@Keep on getting -1 as a reading when trying to send info from an SR04 sensor to thingspeak](https://community.particle.io/t/keep-on-getting-1-as-a-reading-when-trying-to-send-info-from-an-sr04-sensor-to-thingspeak/26354):
>
> so i am having trouble with the ultra sonic sensor SR04 because when i try to send thingspeak data it only gets -1. im not sure why so here is my code: // This #include statement was automatically added by the Particle IDE. #include "HC\_SR04/HC\_SR04.h" int distance = 0; int cm; int trigPin = D2; int echoPin = D6; HC\_SR04 rangefinder = HC\_SR04(trigPin, echoPin); void setup() { } void loop() { distance = rangefinder.getDistanceCM(); cm = distance; Particle.publish("ultra\_senso…

---

<div class="post-metadata">

**Author:** ![VideoLiam](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/videoliam/32/10331_2.png) [@VideoLiam](https://community.particle.io/u/VideoLiam)\
**Post date:** [November 2, 2018, 4:01pm UTC](https://community.particle.io/t/basic-where-to-start-debugging/44922/5 "2018-11-02T16:01:25Z")

</div>

Hi,

I’ve played around with the HC-SR04 myself and discovered a bit of an issue with the basic way mine functioned.

Using the simple examples I found on the net I would keep gettting randon strange way off the scale results from time to time. It turns out after scopeing the echo pin, if the sensor failed to get an echo back within some time out value it would send the echo pin high before resetting itself. There are a lot of reasons it might not get an echo, be it unrefleactive surfaces, not pointing in the right direction etc… but this reset throws your code if your not aware of it. To that end I re-worked the basic example to allow for an out of range reading and then to ignor the next echo high pulse. I found it alot more reliable.

Liam

---

<div class="post-metadata">

**Author:** ![VideoLiam](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/videoliam/32/10331_2.png) [@VideoLiam](https://community.particle.io/u/VideoLiam)\
**Post date:** [November 2, 2018, 4:09pm UTC](https://community.particle.io/t/basic-where-to-start-debugging/44922/6 "2018-11-02T16:09:52Z")

</div>

Hi,

Update!

I’ve just realised the version I shared, was faulty, so I’ve deleted the link… I’ve doing this during my lunch break, so I’ll reshare once I’ve double, doubled checked the version…

Liam

---

<div class="post-metadata">

**Author:** ![Condoman](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/condoman/32/18562_2.png) [@Condoman](https://community.particle.io/u/Condoman)\
**Post date:** [November 2, 2018, 6:06pm UTC](https://community.particle.io/t/basic-where-to-start-debugging/44922/7 "2018-11-02T18:06:51Z")

</div>

peekay123: I looked at many examples of issues with the sensor both in this forum and on the web. Nothing I saw seemed to speak about my issues.

 ![image](https://us1.discourse-cdn.com/flex026/uploads/particle/original/3X/7/2/72a6d4b5a1a7136848db06ab9e41998f3c6ff601.jpg)

Above is how I wired this in accordance with the project I am copying.

VideoLiam: I don’t know if the code ventz wrote accommodated the echo timing.

Without a scope it is very hard to determine what is not working. I have checked the continuity of the jumpers and the pin voltage on the sensor. I simply do not know what else to do. I suppose I could have gotten 3 bad sensors but not likely.

Because the Particle app displays some number in the device data I have to assume the Photon is working. Is there something else I can do to verify that?

---

<div class="post-metadata">

**Author:** ![peekay123](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/peekay123/32/810_2.png) [@peekay123](https://community.particle.io/u/peekay123)\
**Post date:** [November 2, 2018, 6:38pm UTC](https://community.particle.io/t/basic-where-to-start-debugging/44922/8 "2018-11-02T18:38:58Z")

</div>

@Condoman, how long are the wires between the SR04 and the Photon? The original project used pins D0 and D1:

```auto
Connect an HC-SR04 Range finder as follows:
Spark HC-SR04
GND GND
5V VCC
D0 Trig
D1 Echo

```

You use D1 and D2. Did you change the code accordingly? Why the change of pins?

---

<div class="post-metadata">

**Author:** ![Condoman](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/condoman/32/18562_2.png) [@Condoman](https://community.particle.io/u/Condoman)\
**Post date:** [November 2, 2018, 7:23pm UTC](https://community.particle.io/t/basic-where-to-start-debugging/44922/9 "2018-11-02T19:23:52Z")

</div>

Nice catch. I had followed the instructable which shows D1 & D2. I see the code comment shows D0 & D1. I will change and report back. The wires are 8".

---

<div class="post-metadata">

**Author:** ![Condoman](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/condoman/32/18562_2.png) [@Condoman](https://community.particle.io/u/Condoman)\
**Post date:** [November 2, 2018, 7:28pm UTC](https://community.particle.io/t/basic-where-to-start-debugging/44922/10 "2018-11-02T19:28:49Z")

</div>

I am seeing some valid data now. I will reassemble the container and put it back in the tank and report back.

---

<div class="post-metadata">

**Author:** ![VideoLiam](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/videoliam/32/10331_2.png) [@VideoLiam](https://community.particle.io/u/VideoLiam)\
**Post date:** [November 2, 2018, 8:42pm UTC](https://community.particle.io/t/basic-where-to-start-debugging/44922/11 "2018-11-02T20:42:58Z")

</div>

Hi,

OK, This is the code I wote to return a much more reliable reading from the HC-SR04, that I got off the net.

This actually rejects dud readings and returns 0cm… You can tell its a dud reading if there is a pause in the reading whilst monitoring it on the serial terminal.

Hope this is of help to someone.

Liam

```auto
/* HC-SR04 Ping / Range finder wiring:
 * -----------------------------------
 * Particle - HC-SR04
 * GND - GND
 * VIN - VCC
 * D0 - TRIG
 * D1 - ECHO
 */
 
#include "application.h"
#define trig_pin D0
#define echo_pin D1

uint32_t duration, inches, cm, Answer;

bool InRange = false;

void setup() {
    Serial.begin(9600);
    pinMode(trig_pin, OUTPUT);
    pinResetFast(trig_pin);
  
    pinMode(echo_pin, INPUT);
    pinResetFast(echo_pin);
    
    Particle.variable("Distance", String(cm));
  
}

void loop() {
   
    Answer = Reading();
    Serial.printlnf(" %6d cm ",Answer);
    //Particle.publish("Distance", String(cm));
   delay(200);
    
}

uint32_t Reading(){
     pinSetFast(trig_pin);
    delayMicroseconds(12);
    pinResetFast(trig_pin);
 
    duration = pulseIn(echo_pin, HIGH); 
 
    if(duration>50001){
        InRange = false;
    }
    
    if(duration<50000&&InRange==true){
    cm = duration / 29 / 2;
    return cm;
    //Serial.printlnf(" %6d cm / %6d us", cm, duration);
   
        }
        
    if(duration<50000&&InRange==false){
        InRange=true;
        
    }
    
    if(InRange==false){
        cm=0;
        return cm;
       // Serial.println("Out of Range!");
    }
    
    
}

```

---

<div class="post-metadata">

**Author:** ![ScruffR](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/scruffr/32/6952_2.png) [@ScruffR](https://community.particle.io/u/ScruffR)\
**Post date:** [November 2, 2018, 9:57pm UTC](https://community.particle.io/t/basic-where-to-start-debugging/44922/12 "2018-11-02T21:57:20Z")

</div>

> [@Condoman](#):
>
> I had followed the instructable which shows D1 & D2. I see the code comment shows D0 & D1.

That's why we said we need to see _ **your** _ setup and code 😉
