Mitchell LundNathan FulbrightMatthew Babson
Published

Parking Proximity Sensor

Proximity Sensor that will let a driver know when they are too close to an object

IntermediateFull instructions provided10 hours1,340
Parking Proximity Sensor

Things used in this project

Hardware components

Buzzer
Buzzer
Active Buzzer
×1
Ultrasonic Sensor - HC-SR04 (Generic)
Ultrasonic Sensor - HC-SR04 (Generic)
×1
LED (generic)
LED (generic)
One Red LED, One Green LED
×2
Resistor 220 ohm
Resistor 220 ohm
×2
Resistor 100 ohm
Resistor 100 ohm
×2
Argon
Particle Argon
×3
Jumper wires (generic)
Jumper wires (generic)
×11

Software apps and online services

Particle Pi
Particle Pi

Hand tools and fabrication machines

Wire Stripper & Cutter, 18-10 AWG / 0.75-4mm² Capacity Wires
Wire Stripper & Cutter, 18-10 AWG / 0.75-4mm² Capacity Wires

Story

Read more

Schematics

Ultrasonic Sensor Circuit

Circuit Diagram for Ultrasonic Sensor

Ultrasonic Sensor Schematic

Schematic for Ultrasonic Sensor

Event Change Spreadsheet

Buzzer Circuit

Circuit Diagram for Buzzer

Buzzer Schematic

Schematic for Buzzer

LED Circuit

Circuit Diagram for LED lights

LED Schematic

Schematic for LED lights

Code

LED

C/C++
Code for both LED's. The Green light is illuminated until the driver is close to an object, then the code will turn the Red light on
int led2 = D6;
int led1 = D4; 

void setup() {

  pinMode(led1,OUTPUT);
  pinMode(led2,OUTPUT);
  
    Particle.subscribe("MEGR3171_sensorstatus_528_Clear",greenfunc);
  Particle.subscribe("MEGR3171_sensorstatus_528_Breach",redfunc);
  
  }
  
  
    int greenfunc(const char *topic, const char *data){
    digitalWrite(led2,HIGH);
    digitalWrite(led1,LOW);
  }
    int redfunc(const char *topic, const char *data){
    digitalWrite(led1,HIGH);
    digitalWrite(led2,LOW);
  }
  

Buzzer

C/C++
Code for the Buzzer. The buzzer will make a sound whenever the driver is close to an object and will work similarly to how the LED's work
void setup() 

{
    pinMode(D7,OUTPUT);
    Particle.subscribe("MEGR3171_sensorstatus_528","Clear",lowfunc);
    Particle.subscribe("MEGR3171_sensorstatus_528","Breach",highfunc);
    
}


int lowfunc(const char *topic, const char *data)
{
    tone(D7,0,0); //buzzer off
}

int highfunc(const char *topic, const char *data)
{
    tone(D7,2000,3000); //buzzer on
}

HC-SR04.ino

C/C++
// This #include statement was automatically added by the Particle IDE.
#include "hc-sr04.h"

// Sensor gateway using the Argon board. This contains the HC-SR04
// Ultrasonic code and sends data to the other nodes to notify of a trip

double cm = 0.0;
bool beam_status = false;

int trigPin = D4;
int echoPin = D5;

HC_SR04 rangefinder = HC_SR04(trigPin, echoPin);

void setup() 
{
    Spark.variable("cm", &cm, DOUBLE);
    pinMode(trigPin, OUTPUT);
    pinMode(echoPin, INPUT);
}

void loop() 
{
    cm = rangefinder.distCM();
    if (cm<50){
        if (beam_status==false){
            
            Particle.publish("MEGR3171_sensorstatus_528","Breach");
            beam_status = true;   
        }
    } else {
        if (beam_status==false){
            
        } else {
            Particle.publish("MEGR3171_sensorstatus_528","Clear");
            beam_status = false;
        }
    }
    delay(1000);
}

HC-SR04.cpp

C/C++
//
//    HC-SR04.cpp
//    Purpose: Implementation of the HC-SR04 driver
//
//    Author: Richard Nash
//    Version: 1.0.1

#include "hc-sr04.h"
#include "Particle.h"

HC_SR04::HC_SR04(int trigPin, int echoPin)
{
    this->trigPin = trigPin;
    this->echoPin = echoPin;
}

void HC_SR04::init()
{
    pinMode(trigPin, OUTPUT);
    pinMode(echoPin, INPUT);
}

float HC_SR04::distCM()
{
    // Speed of sound is approx 343 m/s
    // 343 m/s * 100 cm/m * 0.000001 s/us / 2.0 trips
    const float uSecondsToCM = ((340.0f * 100.0 * 0.000001f) /  2.0f);
    unsigned long timeUntilLow = triggerAndMeasurePulse();
    if (timeUntilLow == 0) return NO_SIGNAL;
    return (float)timeUntilLow * uSecondsToCM;
}

float HC_SR04::distInch()
{

    // Speed of sound at 70 degrees F is approximately 1128 ft/s
    // 1128 ft/s * 12 inch/ft * 0.000001 s/us / 2.0 trips
    const float uSecondsToInch = ((1128.0f * 12.0 * 0.000001f) /  2.0f);
    unsigned long timeUntilLow = triggerAndMeasurePulse();
    if (timeUntilLow == 0) return NO_SIGNAL;
    return (float)timeUntilLow * uSecondsToInch;
}

unsigned long HC_SR04::triggerAndMeasurePulse()
{
    // Response pulse usually starts in under 500 uSecs, wait up to 2ms to be sure.
    const unsigned long timeoutHigh = 2000;
    // Response pulse should be shorter than about 29ms (allow 5 meter as limit)
    const unsigned long timeoutLow = 29000;
    unsigned long start, duration;

    // Timing is crucial here, so cannot allow other threads or interrupts
    // Maximum ammount of time in this block is limitted to 10 + 2,000 + 29,000 uS
    // Or 31 milliseconds
    ATOMIC_BLOCK() {
        // Send the 10 uSec pulse
        pinSetFast(trigPin);
        delayMicroseconds(10);
        pinResetFast(trigPin);

        start = micros();
        while (pinReadFast(echoPin) != HIGH) {
            duration = micros() - start;
            if (duration >= timeoutHigh) {
                // Didn't recieve a pulse
                return 0;
            }
        }

        start = micros();
        while (pinReadFast(echoPin) != LOW) {
            duration = micros() - start;
            if (duration >= timeoutLow) {
                // Pulse lasted longer than the 4 meter range limit
                return 0;
            }
        }
        return duration;
    }
}

HC-SR04.h

C/C++
//
//    HC-SR04.h
//    Purpose: HC-SR04 driver
//
//    Author: Richard Nash
//    Version: 1.0.1

// This class provides the trigger and timing to use the range
// finder. See the usage example in this library for how to use it.

#pragma once

 class HC_SR04
 {
 public:
   // No default Constructor, need to specify the pins
   HC_SR04() = delete;

   // Constructor
   // Parameters:
   // trigPin : The pin the Trig signal is connected to
   // echoPin : The pin the Echo signal is connected to
   HC_SR04(int trigPin, int echoPin);

   // Call this function in the setup() routine to set the pin modes.
   void init();

   // NOTE: Both distCM() and distInch() block threading AND interrupts for a
   // short period of time. That time can be up to approximately 31 milliseconds

   // Returns the range distance in centimeters OR NO_SIGNAL if range finding
   // failed.
   float distCM();

   // Returns the range distance in inches OR NO_SIGNAL if range finding
   // failed.
   float distInch();

   // Sentinal value for no distance returned
   const float NO_SIGNAL = -1.0f;

 private:
   int trigPin;
   int echoPin;
   unsigned long triggerAndMeasurePulse();
 };

Credits

Mitchell Lund

Mitchell Lund

1 project • 0 followers
Nathan Fulbright

Nathan Fulbright

1 project • 0 followers
Matthew Babson

Matthew Babson

1 project • 0 followers

Comments