Basic where to start debugging

I decided to build this project 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?

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 :wink:

1 Like

Thanks for the reply.

The code is posted in the “this project” link which links to the blog

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.

@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.

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

2 Likes

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

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.

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?

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

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?

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".

1 Like

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

1 Like

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

/* 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!");
    }
    
    
}

That’s why we said we need to see your setup and code :wink:

1 Like