Ronan LodatoSam KnowltonTrent Zuber
Published © GPL3+

Automatic Light and Temperature Sensor

The goal of the project is to construct a system that can detect the temperature of a room and turn on a light when people enter the room.

IntermediateFull instructions provided2 days13
Automatic Light and Temperature Sensor

Things used in this project

Hardware components

Argon
Particle Argon
×3
PIR Motion Sensor (generic)
PIR Motion Sensor (generic)
×1
Adafruit Waterproof DS18B20 Digital temperature sensor
Adafruit Waterproof DS18B20 Digital temperature sensor
×1
SG90 Micro-servo motor
SG90 Micro-servo motor
×1
USB-A to Micro-USB Cable
USB-A to Micro-USB Cable
×3
Jumper wires (generic)
Jumper wires (generic)
×1
Adafruit Medium Vibration Sensor Switch
×1
Breadboard (generic)
Breadboard (generic)
×3

Software apps and online services

Particle Build Web IDE
Particle Build Web IDE

Story

Read more

Schematics

Temperature/Vibration Sensor Diagram

PID Motion Sensor Diagram

Servo Motor Diagram

Temperature and Vibration Sensors

Motion Sensor

Servo Motor

Code

Motion Sensor Code

C/C++
Uses a PIR motion sensor to sense motion and send a signal to the argon with the servo motor attached.
// This #include statement was automatically added by the Particle IDE.
#include <LiquidCrystal.h>

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


int inputPin = D5;              // choose the input pin (for PIR sensor)
int ledPin = D7;                // LED Pin
int pirState = LOW;             // we start, assuming no motion detected
int val = 0;                    // variable for reading the pin status

int calibrateTime = 100;      

void setup()
{
  pinMode( ledPin, OUTPUT );
  pinMode(inputPin, INPUT);     
}

void loop()
{

  if ( calibrated() )
  {
  
    readTheSensor();

    // report it out, if the state has changed
    reportTheData();
  }
}

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("motion detected");
      delay(1000);
      // Update the current state
      pirState = HIGH;
      setLED( pirState );
    }
  } else {
    if (pirState == HIGH) {
      // we have just turned of
      // Update the current state
      pirState = LOW;
      setLED( pirState );
    }
  }
}

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

Motor Code

C/C++
Moves the servomotor after motion is detected by the PIR motion sensor
Servo myservo;
int pos1=0; //servo position for light switch off
int pos2=180; //servo position for light switch on




void setup() {

myservo.attach(D6); //attatch servo pin to D5
Particle.subscribe("motion detected", motor, "e00fce68465fa2d7187401d1"); 
delay(1000);
}


void motor(const char *event, const char *data){
   {
    myservo.write(pos1); 
    Particle.publish("light", "on"); 
    delay(6000);   
    myservo.write(pos2);
    Particle.publish("light", "off"); 
    delay(1000);
   }
}

Temperature Sensor and Vibration Sensor Code

C/C++
Send temperature readings and data to a graph, also uses vibrations to sense if movement is happening near the device, which then sends a signal to argon with the servo motor attached.
// Include the libraries we need
#include <OneWire.h>

// This #include statement was automatically added by the Particle IDE.
#include <spark-dallas-temperature.h>

// Data wire is plugged into port 2 on the Arduino
#define ONE_WIRE_BUS 2

// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);

// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);

int inputPin = A3;
int motionDetected = LOW;
int sensorVal = 0;

/*
 * The setup function. We only start the sensors here
 */
void setup()
{
  // start serial port
  Serial.begin(9600);
  Serial.println("Dallas Temperature IC Control Library Demo");

  // Start up the library
  sensors.begin();
  pinMode(inputPin, INPUT); 
  
  sensorVal = analogRead(inputPin);
  
}

/*
 * Main function, get and show the temperature
 */
void loop()
{
  // call sensors.requestTemperatures() to issue a global temperature
  // request to all devices on the bus
  Serial.print("Requesting temperatures...");
  sensors.requestTemperatures(); // Send the command to get temperatures
  Serial.println("DONE");
  // After we got the temperatures, we can print them here.
  // We use the function ByIndex, and as an example get the temperature from the first sensor only.
  float tempC = sensors.getTempCByIndex(0);

  // Check if reading was successful
  if (tempC != DEVICE_DISCONNECTED_C)
  {
    Serial.print("Temperature for the device 1 (index 0) is: ");
    Serial.println(tempC);
  }
  else
  {
    Serial.println("Error: Could not read temperature data");
  } 
     if (sensorVal > 50);
   delay(100);
   Particle.publish("motion detected", PUBLIC);
  }
  

Credits

Ronan Lodato

Ronan Lodato

1 project • 2 followers
Sam Knowlton

Sam Knowlton

1 project • 2 followers
Trent Zuber

Trent Zuber

1 project • 2 followers

Comments

Add projectSign up / Login