Ultrasonic range finder simple code


#1

Robotshop ultrasonic range finder

Could not find some really simple range finding code, so I wrote this. I know that you can calculate the distances but I always like to see what I actually read.

D7 lights when the object is far away, The other LED lights when the object is closer to the sensor.

// EXAMPLE
//using Ultrasonic Range Finder from Robotshop
//http://www.robotshop.com/ca/en/hc-sr04-ultrasonic-range-finder.html


unsigned long duration;

void setup()
{

    pinMode(D7, OUTPUT);   // LED on D7
    pinMode(D1, OUTPUT);   // Other LED connected from D1 through resistor to ground
    
                           // ultrasonic range finder Robotshop RB-lte-54
                           
                           // GND pin goes to ground
    pinMode(D6, INPUT);    // echo
    pinMode(D2, OUTPUT);   // Trig
                           // VCC pin goes to VIN on the photon
                           
           
}

void loop(){
    
        delay(10);                      // even cicuits need a break
        digitalWrite(D2, HIGH);         // activate trigger
        delayMicroseconds(10);
        digitalWrite(D2, LOW);          // de-activate trigger

        duration = pulseIn(D6, HIGH);   // how long until a reply?
                                        // a blocking call so may wait a few seconds
                                        
        if (duration > 2000    ){        // raw data from 200 to 16000                                         
                                        // where  2000 raw = ~35cm,  4000 raw = ~80cm
                                        
            digitalWrite(D7, HIGH);     // D7 Blue LED on if far
            digitalWrite(D1, LOW);      // Other LED off
        } else {
            digitalWrite(D7, LOW);      // D7 Blue LED off
            digitalWrite(D1, HIGH);     // Other LED on if Close

        } 
   
}


Ultrasonic Distance Detector (HC-SR04)
Teaching High School Robotics with the Spark Photon
#2

That might be interesting to try with an interrupt. Not to steal your thunder, just sharing thoughts.

unsigned long duration;
unsigned long startMicros;
long last;
bool checking;

void setup()
{
    pinMode(D7, OUTPUT);   // LED on D7
    pinMode(D1, OUTPUT);   // Other LED connected from D1 through resistor to ground

                       // ultrasonic range finder Robotshop RB-lte-54
                       
                       // GND pin goes to ground
    pinMode(D6, INPUT);    // echo
    pinMode(D2, OUTPUT);   // Trig
                       // VCC pin goes to VIN on the photon

    attachInterrupt(D6, rangeISR, FALLING); // interrupt on falling edge
}

void loop(){

    if ( checking == false && millis() - last > 1000 ){  // once per second if not doing anything
        checking = true; // set a flag so it doesn't get confused
        duration = 0;
        last = millis();  // get time now for timer
        digitalWrite(D2, HIGH);         // activate trigger
        delayMicroseconds(10);
        digitalWrite(D2, LOW);          // de-activate trigger
        startMicros = micros();  // get time now for start
    }
                                    
    if ( duration ) // it's been set by ISR
    {
        if ( duration > 2000 ){        // raw data from 200 to 16000                                         
                                    // where  2000 raw = ~35cm,  4000 raw = ~80cm
                                    
            digitalWrite(D7, HIGH);     // D7 Blue LED on if far
            digitalWrite(D1, LOW);      // Other LED off
        } else {
            digitalWrite(D7, LOW);      // D7 Blue LED off
            digitalWrite(D1, HIGH);     // Other LED on if Close
        }
        checking = false;            // reset the flag to signal a start
        duration = 0;                   // reset this as well
        delay(10);                      // even circuits need a break
    }
}

void rangeISR()
{
    duration = micros() - startMicros;  // sets duration to current time - start time
}

#3

@Hypnopompia had a project with a sensor like this
Distance sensor for an adjustable height desk?


#4

Yeah, so here’s the code for my ping sensor project.

I’ve even got some sensor reading smoothing in there since this sensor is a bit temperamental. The functions you should check out are avgReading(), readPingSensor(), ping(), microsecondsToInches() and microsecondsToCentimeteres()

This code started out from @BDub’s work from this thread: Simple Photon Ping Sensor (HC-SR04)