Things used in this project

Hardware components:
Photon new
Particle Photon
×1
51gzz5eu9pl. sx425
Ultrasonic Sensor - HC-SR04 (Generic)
×1
11026 02
Jumper wires (generic)
4 pieces
×1
12002 04
Breadboard (generic)
×1
Particle Power Shield with headers v2.1
×1
Gus 1719
C.H.I.P. Approved 3.7 V LiPo Battery
PKCell, LP7850, 2500 mAh, 3.7V
×1
Software apps and online services:
Blynk logo avatars
Blynk

Code

TrashcanC/C++
v1.0
// Ingo Lohs, 13.11.2017, works with Particle Photon v0.7.0-rc.3
// trashcan v1.0
// Checks the remaining amount of waste in the paper dustbin

// BOM:
// ** Breadboard
// ** Photon
// ** Ultrasonic HC-SR04
// ** 4 Wires
// ** Particle Power Shield and
// ** LiPo - in my case 2500mAh 3.7V

// Schematic:
// ** only HC-SR04 with trig > A3, echo > A4, VCC > VIN, GND > GND
 
#include <blynk.h>

int baudrate = 9600;               // Serial Monitor 
#define internalLED D7             // internal Device LED from Spark Core or Particle Photon
const int tank_max = 90;           // if distance in cm from Ultrasonic Sensor >=90 could be that the lid of the paper bin can no longer be closed = max. filling in cm
long distance, duration;           // UltraSonic values
#define trigPin	A3                 // UltraSonic HC-SR04 Pin Definitions
#define echoPin	A4                 // UltraSonic HC-SR04 Pin Definitions

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "your Blynk code here";

unsigned long lastmillis = 0;      // time for iteration the loop

// *********  

void setup() 
{
  Serial.begin(baudrate);
  while(!Serial);                   // Waiting for Serial connection
  pinMode(internalLED, OUTPUT);     // Set pin D2 as an OUTPUT
  digitalWrite(internalLED, LOW);   // internal LED off
  pinMode(trigPin, OUTPUT);         // Set pin A3
  pinMode(echoPin, INPUT);          // Set pin A4
  Blynk.begin(auth);                // Blynk magic starts here
}
 
// *********     
    
void loop() 
{
    Blynk.run();    
    
//    !!! http://docs.blynk.cc/#troubleshooting-flood-error  
//    no delays in loop - DO NEVER in LOOP!

            if ((millis() - lastmillis) > 2000) { // run every 2 seconds
                lastmillis = millis();
                digitalWrite(internalLED, HIGH);  // internal LED on
                  readData();
                  checkData();
                digitalWrite(internalLED, LOW);   // internal LED off
            }

}

// *********  

void readData()
{
      // read the Distance with the Ultrasonic HC-SR04
      digitalWrite(trigPin, LOW);  
      delayMicroseconds(2); 
      digitalWrite(trigPin, HIGH);
      delayMicroseconds(10); 
      digitalWrite(trigPin, LOW);
      duration = pulseIn(echoPin, HIGH);
      distance = (duration/2) / 29.1;
        
      Serial.print("Mülleimer: ");
      Serial.print(distance);
      Serial.println(" cm");
      
      // put a Graph-Widget in your Blynk App and map it to V0 (400 Energy Points)
      Blynk.virtualWrite(V0, distance);
}

// *********  

void checkData()
{
    // step 1 - Validate the Distance  - check the right position from UltraSonic Sensor - larger then 0 cm?
      if (distance == 0)
      {
        Serial.println("Distance Value = 0!");
        Blynk.virtualWrite(V1, "Error: Distance Value = 0!");
      }
    // step 2 - Is the distance in range?
      else if (distance >= tank_max || distance <= 0)
      {
        Serial.println("Out of range! Check position from UltraSonic Sensor!");
        Blynk.virtualWrite(V1, "Error: Echo distance problem!");
      }
      else
      {
        Blynk.virtualWrite(V1, "...Value read...");
      }
}

Credits

Img 6554 3ywwumwxbh
Ingo Lohs
17 projects • 39 followers
I am well over 40 years and come from the middle of Germany. You can contact me also in German. Donation for projects: paypal.me/ingolohs
Contact

Replications

Did you replicate this project? Share it!

I made one

Love this project? Think it could be improved? Tell us what you think!

Give feedback

Comments

Add projectSign up / Login