Edwin Cruz-DiazJoshua Kilpatrick
Published

IoT Distance Sensor for Car!

Always be a safe distance from a wall in your parking spot. Introducing our IoT project with a HC-SR04 distance sensor.

IntermediateProtip10 hours411
IoT Distance Sensor for Car!

Things used in this project

Hardware components

Ultrasonic Sensor - HC-SR04 (Generic)
Ultrasonic Sensor - HC-SR04 (Generic)
×1
Photon
Particle Photon
×2
Solderless Breadboard Half Size
Solderless Breadboard Half Size
×2
5 mm LED: Yellow
5 mm LED: Yellow
×2
Resistor 220 ohm
Resistor 220 ohm
×2
Male/Male Jumper Wires
×10

Software apps and online services

Fritzing
Particle Build Web IDE
Particle Build Web IDE

Hand tools and fabrication machines

Particle Starter Kit

Story

Read more

Schematics

Top View of Photon with Sensor (Outside of Car)

Top View of Photon with Sensor (Outside of Car). This view has a "wall" infront of it and shows how the led comes on.
Img 3376 ver6bwsu9c

Side View of Photon with Sensor(Outside of Car)

Side View of Photon with Sensor (Outside of Car) This view has a "wall" infront of it and shows how the led comes on. Also shows HC-SR04 sensor with more detail.
Img 7998 cwsycv1vwk

Top View of Photon with Sensor (Outside of Car)

Top View of Photon with Sensor(Outside of Car) This view has the light off therefore no wall at close distance.
Img 9954 8u9hntzh3o

Top View of Photon that receives signal (Inside of Car)

Top View of Photon that receives signal (Inside of Car) Light is on therefore the main photon with sensor has a wall infront of it.
Img 4889 fdjt7khlpd

Side View of Photon that receives signal (Inside of Car)

Side View of Photon that receives signal (Inside of Car) Light is on therefore the main photon with sensor has a wall infront of it.
Img 0174 o20vsufudc

Top View of Photon that receives signal (Inside of Car)

Top View of Photon that receives signal (Inside of Car) Light is off therefore main photon with sensor has no wall infront.
Img 5441 lee9cdn5ji

Schematic of Main Photon with HC-SR04 Sensor

Detailed wire schematic for main photon.
Main photon 9nzammbqjg

Schematic of Photon that Receives Signal.

Detailed schematic of photon that receives signal from main photon.
2nd photon zjgbuqensg

Code

Code for Main Photon that has sensor Connected to it.

Arduino
This code works by selecting specific LEDs on the main photon to register the signals being sent through them. The loop has an "if statement" saying if the HR-SRO4 is within a certain distance, for example 35cm, the circuit will be triggered and light up the LED light on the main breadboard. The way the actual HR-SRO4 device works is it sends out signals and measures how long it takes for the signals to return to the device. This is usually recorded in microseconds. When the device is close enough to trigger a response, it records an event and publishes it to the console. This works by choosing a Particle.publish function and choosing what to label events that will appear in the console. A delay is added in order to give it time to reset and also for the other photon to have time to read the data that is already published.
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
            Particle.publish("Wall828", "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
}

Photon Receiver Code

Arduino
This code was used for the second photon in the circuit. This photon was responsible for signaling when the HR-SRO4 sensor was too close an object. This was achieved by subscribing this particle to the main photon and the console that contains all the events that were being recorded. The same LED instructions from the main code were also used to set off the light that was attached to the secondary photon. A delay was added to this device as well however, unlike the the first code, this delay only lasts for two seconds. After the delay, which is about two seconds, the power to the light is switched off. That allows the LED light to come on, get recognized and then reset incase it is needed again.
unsigned long duration;
unsigned long startMicros;
long last;
bool checking;
void setup()
{   
    Particle.subscribe("Wall828", myHandler, "300025000d51353532343635");
    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 myHandler(const char *event, const char *data){ 
  digitalWrite(D1, HIGH);
    
 
  //  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(2000);  
       digitalWrite(D1, LOW);// even circuits need a break
}
void rangeISR()
{
   // duration = micros() - startMicros;  // sets duration to current time - start time
}

Credits

Edwin Cruz-Diaz

Edwin Cruz-Diaz

1 project • 0 followers
Joshua Kilpatrick

Joshua Kilpatrick

1 project • 1 follower
Thanks to Marekj and Community Particle.

Comments

Add projectSign up / Login