Things used in this project

Hardware components:
Photon new
Particle Photon
Relay (generic)
Songle SRD-05VDC-SL-C
13285 01
PIR Motion Sensor (generic)
10167 01
DHT22 Temperature Sensor
Adafruit industries ada161 image 75px
Photo resistor
Omron b3f 1000 image 75px
SparkFun Pushbutton switch 12mm
11026 02
Jumper wires (generic)
12002 04
Breadboard (generic)
Mfr 25frf52 10k sml
Resistor 10k ohm
protect photo-resistor
Mfr 25fbf52 221r sml
Resistor 221 ohm
protect DHT22-Sensor
Mfr 25frf52 1k sml
Resistor 1k ohm
LED control PIR-Sensor activities
Software apps and online services:
Blynk logo avatars


Myled relay sketch steckplatine pye3doci0a


Untitled fileC/C++
// This #include statement was automatically added by the Particle IDE.
#include <Adafruit_DHT.h>

// This #include statement was automatically added by the Particle IDE.
#include <blynk.h>

// Sources:
// --> Relay Belegung und Schaltung

int inputPin = D0;              // choose the input pin (for PIR sensor)
int ledPin = D1;                // LED Pin
int pirState = LOW;             // we start, assuming no motion detected
int val = 0;                    // variable for reading the pin status
int boardLed = D7;              // photon onBoard LED
int boardRelay = D2;            // Relay
int calibrateTime = 10000;      // wait for the thingy to calibrate
float light_threshold = 20;     // brightness to decide its time for more light in %
int analogvalue;                // Here we are declaring the integer variable analogvalue, which we will use later to store the value of the photoresistor.
int photoresistor = A0;         // This is where your photoresistor is plugged in. The other side goes to the "power" pin (below).
int photopower = A5;            // This is the other end of your photoresistor. The other side is plugged into the "photoresistor" pin (above).
                                // The reason we have plugged one side into an analog pin instead of to "power" is because we want a very steady voltage to be sent to the photoresistor.
                                // That way, when we read the value from the other side of the photoresistor, we can accurately calculate a voltage drop.
const int pushButton = D4;      // Button auf D4 (buttonPin)
int x = 1;
// variables will change:
int pushButtonState = 0;        // Statusabfrage vom Button (buttonState)
int oldButtonState = LOW;

// DHT parameters
#define DHTPIN 5 // what pin we´re connected to
#define DHTTYPE DHT22 // DHT 22 (AM2302)
    // Variables
float temperature;
int humidity;
    // DHT sensor
unsigned long lastmillis = 0;

char auth[] = "<< your Blync code here >>"; 

void setup()
    // Start Blynk
    // Start DHT22 Sensor

  pinMode(photoresistor,INPUT);  // Our photoresistor pin is input (reading the photoresistor)
  pinMode(photopower,OUTPUT);         // The pin powering the photoresistor is output (sending out consistent power)
                                 // Next, write one pin of the photoresistor to be the maximum possible, so that we can use this for power.

    // We are going to declare a Particle.variable() here so that we can access the value of the photoresistor from the cloud.
    Particle.variable("PIR-Motion", &analogvalue, INT);
    //Particle.publish("PIR-Motion", String(analogvalue), PRIVATE);
    // This is saying that when we ask the cloud for "analogvalue", this will reference the variable analogvalue in this app, which is an integer variable.

  pinMode(boardLed,OUTPUT); // on-board LED 
  pinMode( ledPin, OUTPUT );
  pinMode(inputPin, INPUT);     // declare sensor as input
  pinMode(boardRelay,OUTPUT);   // declare Relay
  pinMode(pushButton, INPUT_PULLUP);   // declare Button as input
   // Now flash the D7 LED on and off 
  digitalWrite(boardLed,HIGH); // Start der Kallibrierung mit D7 on
  Particle.publish("PIR-Motion","now online",100,PRIVATE);
  digitalWrite(boardLed,LOW); // Start der Kallibrierung mit D7 off

void loop()
  // Start Blynk;
  // Start Button-check

      // check to see what the value of the photoresistor is and store it in the int variable analogvalue
    analogvalue = analogRead(photoresistor);
  // if the sensor is calibrated
  if ( calibrated() )
  // get the data from the sensor

    // report it out, if the state has changed
            if ((millis() - lastmillis) > 20000) {
                lastmillis = millis();

void TempHumi() {
    // Humidity measurement
    temperature = dht.getTempCelcius();
    // Humidity measurement
    humidity = dht.getHumidity();
    Particle.publish("PIR Motion - temp", String(temperature), PRIVATE);
    Particle.publish("PIR Motion - humidity", String(humidity), PRIVATE);
    Blynk.virtualWrite(V3, String(temperature)); // 
    Blynk.virtualWrite(V4, String(humidity)); //
    Blynk.virtualWrite(V5, String(temperature)); // 
    Blynk.virtualWrite(V6, String(humidity)); //

void readButton_einaus() {
    // Get the current state of the button
  int newButtonState = digitalRead(pushButton);

  // Has the button gone high since we last read it?
  if (newButtonState == HIGH && oldButtonState == LOW) {

    if (x == 0) {
      // Toggle on
      digitalWrite(boardRelay, HIGH);
      x = 1;

    } else {
      // Toggle off
      digitalWrite(boardRelay, LOW);
      x = 0;

  // Store the button's state so we can tell if it's changed next time round
  oldButtonState = newButtonState;

void readTheSensor() {
  val = digitalRead(inputPin);

bool calibrated() {
  return millis() - calibrateTime > 0;

void reportTheData() {

  // if the sensor reads high
  // or there is now motion
  if (val == HIGH) {

    // the current state is no motion
    // i.e. it's just changed
    // announce this change by publishing an eent
    if (pirState == LOW) {
      // we have just turned on
      Particle.publish("PIR Motion", "Motion detected", PRIVATE);
      // Update the current state
      pirState = HIGH;
      setLED( pirState );
        if (analogvalue < light_threshold) {
              //Particle.publish("PIR Motion", String(light), PRIVATE);
              Particle.publish("PIR Motion", String(analogvalue), PRIVATE);
              Blynk.virtualWrite(V1, String(analogvalue));
              //Blynk.virtualWrite(V1, String(light)); 
              Particle.publish("PIR Motion", String(light_threshold), PRIVATE);
              digitalWrite(boardRelay, HIGH); // Relay anschalten
              Particle.publish("PIR Motion", "Relay on", PRIVATE);
              delay(15000); // 15 sec bleibt das Relay geschalten
              digitalWrite(boardRelay, LOW); // Relay ausschalten
        } else {
            Particle.publish("PIR Motion", String(analogvalue), PRIVATE);
            //Particle.publish("PIR Motion", String(light), PRIVATE);
            Blynk.virtualWrite(V2, String(analogvalue)); 
            //Blynk.virtualWrite(V2, String(light)); 
  } else {
    if (pirState == HIGH) {
      // we have just turned of
      // Update the current state
      pirState = LOW;
      setLED( pirState );

void setLED( int state )
  digitalWrite( ledPin, state );


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