Gabriel InojosaDaniel Law
Published © GPL3+

Cabinet Stock Sensor

Set up a device in your cabinet to notify you when to wash your dishes. This device requires the Particle Argon or Photon.

BeginnerWork in progress2 hours526
Cabinet Stock Sensor

Things used in this project

Hardware components

Argon
Particle Argon
×1
Ultrasonic Sensor - HC-SR04 (Generic)
Ultrasonic Sensor - HC-SR04 (Generic)
×1
Resistor 220 ohm
Resistor 220 ohm
×1
Resistor 330 ohm
Resistor 330 ohm
*Optional
×3
RGB Diffused Common Cathode
RGB Diffused Common Cathode
*Optional
×1
Arduino KY-009 SMD RGB Module
*Optional *This comes in 10 pcs for $2.29 to replace the Common Cathode RGB LED
×1

Software apps and online services

Particle Build Web IDE
Particle Build Web IDE
Maker service
IFTTT Maker service
Arduino IDE
Arduino IDE
*Not to flash code on the Argon but to listen to the device. Can be replaced with TeraTerm or any other application.

Story

Read more

Schematics

Fritzing Schematics

Replace the Photon with the Particle Argon. Digital pins remain the same. VBat should be VUSB.

Code

Argon Code

C/C++
Upload and flash this through the Particle IDE at build.particle.io
/*************************************************************************
 * Copyright (C) 2020-Present Gabriel Inojosa 
 * 
 *  This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <https://www.gnu.org/licenses/>.
 * 
***************************************************************************/
#include <math.h>

const int trigPin = D7;
const int echoPin = D2;

const int ledGPin = D5; //These three
const int ledRPin = D4; //lines are
const int ledBPin = D3; //optional

const float displaceCap = 30.0;

float duration, distance;

bool debOperator = false;

int ledRVal, ledGVal, ledBVal; //This is optional if you want to include RGB. 


void setup() {
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  
  pinMode(ledRPin, OUTPUT);
  pinMode(ledGPin, OUTPUT);
  pinMode(ledBPin, OUTPUT);
  
  Serial.begin(9600);
}

float getDistance() {
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);

  duration = pulseIn(echoPin, HIGH);
  distance = (duration*.0343)/2;
  return (distance);
}

void luminateRGB() { //This method is optional
    int newDist = round(distance);
    ledRVal = map(newDist, 0 , 100, 0, 4095);
    if (!debOperator){
        digitalWrite(ledBPin, LOW);
        digitalWrite(ledRPin, HIGH);
        delay(500);
        digitalWrite(ledRPin, LOW);
        delay(500);
    }
    else{
        analogWrite(ledRPin, newDist);
        digitalWrite(ledBPin, HIGH);
    }
}

void loop(){
  distance = getDistance();
  Serial.print("Distance: ");
  Serial.println(distance);
  
  luminateRGB(); //This line is also optional
  
  bool verifier = (distance >= displaceCap);
  if (verifier){
      if (debOperator){
        Serial.println("Vacant space detected!");
      Particle.publish("verifier", "true", PRIVATE);  
      debOperator = false;
      }
  }
  else {
      debOperator = true;
  }
  delay(100);
}

Credits

Gabriel Inojosa
3 projects • 3 followers
Contact
Daniel Law
46 projects • 9 followers
Teacher. Maker. Citizen of the planet.
Contact

Comments

Please log in or sign up to comment.