Ultrasonic Sensor circuit crashes



  • Attached are pics and code of my circuit using a HC-SR04 ultrasonic sensor. I believe they are actually + modules as that is what they say on the boards. The board is a wemos d1 mini pro.

    Code...Arduino dev environment
    #define echoPin D4 // Echo Pin
    #define trigPin D5 // Trigger Pin

    long duration, distance; // Duration used to calculate distance

    void setup() {

    Serial.begin(74880);
    //Define inputs and outputs
    pinMode(trigPin, OUTPUT);
    pinMode(echoPin, INPUT);
    }

    void loop()
    {

    digitalWrite(trigPin, LOW);
    delayMicroseconds(2);
    digitalWrite(trigPin, HIGH);
    delayMicroseconds(10);
    digitalWrite(trigPin, LOW);
    duration = pulseIn(echoPin, HIGH);
    //Calculate the distance (in cm) based on the speed of sound.
    distance = duration/58.2;
    Serial.println(distance);
    //Delay 50ms before next reading.
    delay(5000);

    }
    1_1490666401782_IMG_20170323_171530__1490308971_99_198_160_173.jpg 0_1490666401782_IMG_20170323_171504__1490308934_99_198_160_173.jpg

    As long as the ground isn't connected the code runs without issue.
    As soon as the ground is connected the board begins outputting to the serial monitor...

    Fatal exception (0):
    epc1=0x40100003, epc2=0x00000000, epc3=0x00000000, excvaddr=0x00000000, depc=0x00000000

    I have double checked the connection 10x and tried other sensors.
    Does anyone have any ideas.
    Thanks
    Gary



  • How are you powering the whole thing? Make sure it has enough power for the sensor -- those sensors can draw a lot in spikes when doing the pings. A large capacitor across the sensor's gnd and vcc may help with spikes.



  • 1_1490742573075_2.jpg 0_1490742573075_1.jpg



  • I moved D4 to D8 and the problem with the wemos crashing vanished. D8 has a pull up 10k but could not find anything that Echo needs pull up. Any thoughts on this issue?


Log in to reply
 

Looks like your connection to WEMOS Forum was lost, please wait while we try to reconnect.