Steven NguyenKenan Hennessee
Published

MEGR 3171 IOT 6 Parking Sensor

Parking Sensor using an Ultrasonic Sensor and Particle Argons.

BeginnerFull instructions provided5 hours8
MEGR 3171 IOT 6 Parking Sensor

Things used in this project

Hardware components

Argon
Particle Argon
×2
Breadboard (generic)
Breadboard (generic)
×4
Ultrasonic Sensor - HC-SR04 (Generic)
Ultrasonic Sensor - HC-SR04 (Generic)
×1
Elegoo RGB led
×2
Jumper wires (generic)
Jumper wires (generic)
×9
Resistor 220 ohm
Resistor 220 ohm
×1
ELEGOO 37-in-1 Sensor Module Kit V1.0
ELEGOO 37-in-1 Sensor Module Kit V1.0
×1
Elegoo Button
×1
Elegoo RGB Light
×2

Software apps and online services

Particle Build Web IDE
Particle Build Web IDE
ThingSpeak API
ThingSpeak API

Hand tools and fabrication machines

Wire Stripper & Cutter, 32-20 AWG / 0.05-0.5mm² Solid & Stranded Wires
Wire Stripper & Cutter, 32-20 AWG / 0.05-0.5mm² Solid & Stranded Wires

Story

Read more

Schematics

Light Receiver

Ultrasonic Sensor

Code

Ultrasonic Sensor Code with Particle Argon

C/C++
Activating the code calculates distance based on the sound waves produced by the ultrasonic sensor. If anything is less than 3 feet from the sensor the particle argon will create an event called Red. If anything is between 3 feet and 8 feet an event called Blue will be created. If there is nothing within 8 feet an even called Green will be created. These events are sent to the second particle argon which will send events back to confirm the 2-way communication. If the other particle argon receives the events "Red", "Blue", or "Green" it will create an event confirming the communication based on the color. The first particle argon will receive the event and a RGB led will emit a light based on the event. This code also includes live data graphing using Think Speak to graph the distance over time.
// This #include statement was automatically added by the Particle IDE.
#include <ThingSpeak.h>

int TRIG = D2;
int ECHO = D6;
int small_green = D5;
int small_blue = D4;
int small_red = D3;
int count = 0;

TCPClient client;

unsigned long myChannelNumber = 1580880;
const char * myWriteAPIKey = "2NJZBPROV8QXQICG";

void setup() {
  Particle.subscribe("IOT_LED_LIGHT",lightFeedback, MY_DEVICES);
  ThingSpeak.begin(client);
  
  Serial.begin(9600);

  pinMode(TRIG, OUTPUT);
  pinMode(ECHO, INPUT);
  pinMode(small_green, OUTPUT);
  pinMode(small_blue, OUTPUT);
  pinMode(small_red, OUTPUT);
  digitalWrite(small_green, LOW);
  digitalWrite(small_blue, LOW);
  digitalWrite(small_red, LOW);
  
}

void loop() {
 
  long timeTaken, distance;
  digitalWrite(TRIG, LOW);
  delayMicroseconds(2);
  digitalWrite(TRIG, HIGH);
  delayMicroseconds(10);
  digitalWrite(TRIG, LOW);
  timeTaken = pulseIn(ECHO, HIGH);//determines the distance of the wave
  distance = (timeTaken/2)/29.1;//calculates the distance
  

if (count > 1000) {  
    if(distance<=90){
        Particle.publish("IOT6_Ultrasonic_Sensor","Red",PRIVATE);
       //if less than or equal to 90 cm (3 feet) the red led will turn on
    }
    else if(distance <=240 && distance >=90){
        Particle.publish("IOT6_Ultrasonic_Sensor","Blue",PRIVATE);
        //if less than or equal to 240 cm (8 feet) and greater than or equal to 90 cm (3 feet) the red and green led will turn on to make yellow
    }
    else{
        Particle.publish("IOT6_Ultrasonic_Sensor","Green",PRIVATE);
      //if greater than 240 cm (8 feet) the green led will turn on
    }
    count = 0;
    ThingSpeak.setField(1, distance);
    Serial.print(distance);
    Serial.println("distance");
    ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);
}
    count = count + 100;
    delay(100);
    

    

    

}



void turnOnRed() {
    digitalWrite(small_green, LOW);
    digitalWrite(small_blue, LOW);
    digitalWrite(small_red, HIGH); 
    //delay(1000);
}

void turnOnBlue() {
    digitalWrite(small_green, LOW);
    digitalWrite(small_blue, HIGH);
    digitalWrite(small_red, LOW);
    //delay(1000);
}

void turnOnGreen() {
    digitalWrite(small_green, HIGH);
    digitalWrite(small_blue, LOW);
    digitalWrite(small_red, LOW);
    //delay(1000);
}

void lightFeedback (const char *event, const char *data)
{
    //digitalWrite(small_green, HIGH);
    //digitalWrite(small_blue, HIGH);
    //digitalWrite(small_red, HIGH);
    
    if (strcmp(data,"Confirm_Red")==0){
        turnOnRed();

    }
    else if (strcmp(data,"Confirm_Blue")==0){
        turnOnBlue();

    }
    else{
        turnOnGreen();
    }
    delay(1000);
}
        
    

Button Switch and RGB Led with Particle Argon

C/C++
This code receives events from the first particle argon and will emit certain lights based on the events being received. Event "Red" will turn on the red led light, event "Blue" will turn on the blue led light, and event "Green" will turn on the green led light. Based on the event this code will create a new event that will be received by the first particle argon and it will activate the RGB led connected to the particle argon. The button switch on this setup turns the ultrasonic sensor on and off so it does not run continuously. To determine when the ultrasonic sensor is on the D7 light will light up and if the sensor is off the D7 light will be turned off.
int blue = D6;
int green = D4;
int red = D2;
int button = D1;
int currentButtonState;
int lastButtonState;
int ledState = LOW;
int ledLight = D7;


//int state = 0;



void setup() {
    
Particle.subscribe("IOT6_Ultrasonic_Sensor", Scuba_Handler);

// subscribe to the HC-SR04 sensor. Name is MEGR3171_IOTGROUP18_ParkingSensor 
// The handler name is used below for the void loop. Make sure name is identical. 

Serial.begin(9600);

pinMode(blue, OUTPUT);
pinMode(green, OUTPUT);
pinMode(red, OUTPUT);
pinMode(button, INPUT);
pinMode(ledLight, OUTPUT);
digitalWrite(blue, LOW);
digitalWrite(green, LOW);
digitalWrite(red, LOW);
digitalWrite(ledLight, LOW);
currentButtonState = digitalRead(button);

}


void loop () { 
    lastButtonState = currentButtonState;
    currentButtonState = digitalRead(button);
    
    if (lastButtonState == HIGH && currentButtonState == LOW){
        ledState = !ledState;
        
    }
    digitalWrite(ledLight, ledState);

}

void Scuba_Handler(const char *event, const char *data)
{
   digitalWrite(blue, LOW);
   digitalWrite(green, LOW);
   digitalWrite(red, LOW);
  
    if (strcmp(data,"Red")==0){
     if (ledState == HIGH){
       Particle.publish("IOT_LED_LIGHT","Confirm_Red",PUBLIC);
       //if less than or equal to 90 cm (3 feet) the red led will turn on
       digitalWrite(blue, LOW);
       digitalWrite(red, HIGH);
       digitalWrite(green, LOW);
   }
   }
   
    else if (strcmp(data,"Blue")==0) {
       if (ledState == HIGH){
        Particle.publish("IOT_LED_LIGHT","Confirm_Blue",PUBLIC);
        //if less than or equal to 240 cm (8 feet) and greater than or equal to 90 cm (3 feet) the red and green led will turn on to make yellow
        digitalWrite(blue, HIGH);
        digitalWrite(red, LOW);
        digitalWrite(green, LOW);
       
   }
   }
  
    else {
         if (ledState == HIGH){
      Particle.publish("IOT_LED_LIGHT","Confirm_Green",PUBLIC);
      digitalWrite(green, HIGH);
      digitalWrite(blue, LOW);
      digitalWrite(red, LOW);
      //if greater than 240 cm (8 feet) the green led will turn on

    }
   }
}

Credits

Steven Nguyen

Steven Nguyen

0 projects • 0 followers
Kenan Hennessee

Kenan Hennessee

0 projects • 0 followers

Comments

Add projectSign up / Login