Milla Zimonjic El-Zarif
Published

Temperature and Motion sensor

My project incorporates early detection with an temperature sensor and an automatic door opening system which minimises the spread of Covid

BeginnerWork in progress6 hours3,152
Temperature and Motion sensor

Things used in this project

Hardware components

PIR Motion Sensor (generic)
PIR Motion Sensor (generic)
×1
Temperature Sensor
Temperature Sensor
×1
Arduino Mega 2560
Arduino Mega 2560
×1
LED (generic)
LED (generic)
×2
Resistor 221 ohm
Resistor 221 ohm
×2
DC motor (generic)
×1
Jumper wires (generic)
Jumper wires (generic)
×9

Software apps and online services

Arduino IDE
Arduino IDE

Hand tools and fabrication machines

3D Printer (generic)
3D Printer (generic)

Story

Read more

Schematics

Schematic diagram

A schematic diagram is a diagram showing all the components of my circuitry.

Code

Temperature and Motion sensor code

Arduino
Comments are included in the code
// Include the libraries we need
#include <OneWire.h>
#include <DallasTemperature.h>
#include <Servo.h>


Servo myservo;  // create servo object to control a servo
// twelve servo objects can be created on most boards


int pos = 0;    // variable to store the servo position

// Data wire is plugged into port 5 on the Arduino
#define ONE_WIRE_BUS 5
#define Rled 4
#define Gled 3
#define pir 12 //motion sensor 
long whenthedooropened = 0;
bool doorclosed = true;
// Define trigger temp for green led not a covid risk
float goodtempstart = 35.5;
// define trigger temp for red led potentially covid risk
float goodtempend = 37.4;
// define temp
float temp = 0; 
float adjustment = 9;

// 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);


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

  // Start up the library
  sensors.begin();
  pinMode(Rled, OUTPUT); 
  pinMode(Gled, OUTPUT);
  pinMode(pir, INPUT);
  digitalWrite(Rled, LOW);   // turn the LED on (HIGH is the voltage level)
  digitalWrite(Gled, LOW);   // turn the LED on (HIGH is the voltage level)

  // setup servo, used to unlock door
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object
  myservo.write(0);
}

/*
 * Main function, get and show the temperature
 */
void loop(void)
{ 
   
  // 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.
  //Serial.print("Temperature for the device 1 (index 0) is: ")
  Serial.print(sensors.getTempCByIndex(0));  
  temp = sensors.getTempCByIndex(0) + adjustment;
  Serial.print(",   ");
  Serial.println(temp); // adjusted temp

  if(temp < goodtempstart){
   digitalWrite(Rled, LOW);   // turn the LED on (HIGH is the voltage level)
  digitalWrite(Gled, LOW);   // turn the LED on (HIGH is the voltage level)   
  }
  if(temp >= goodtempstart){
    digitalWrite(Rled, LOW);   // turn the LED on (HIGH is the voltage level)
  digitalWrite(Gled, HIGH);   // turn the LED on (HIGH is the voltage level)   
  }
  if(temp >= goodtempend){
    digitalWrite(Rled, HIGH);   // turn the LED on (HIGH is the voltage level)
  digitalWrite(Gled, LOW);   // turn the LED on (HIGH is the voltage level)   
  }

  if(millis() > 10000) { // wait 10 seconds after power on before activating the servo.
    // If the Green LED is on, open the door
    if(digitalRead(Gled) == HIGH) { 

      if(doorclosed == true){
        // move servo, open door
        for (pos = 0; pos <= 90; pos += 1) { // goes from 0 degrees to 180 degrees
          // in steps of 1 degree
          myservo.write(pos);              // tell servo to go to position in variable 'pos'
          delay(15);                       // waits 15ms for the servo to reach the position
        }
        doorclosed = false; 
        whenthedooropened = millis(); // record the time that the door opened
        //for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
         // myservo.write(pos);              // tell servo to go to position in variable 'pos'
         // delay(15);                       // waits 15ms for the servo to reach the position
       // } 
      }
    }


    if(doorclosed == false){
      if(millis() - whenthedooropened > 5000){
        
        // close the door
        for (pos = 90; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
          myservo.write(pos);              // tell servo to go to position in variable 'pos'
          delay(15);                       // waits 15ms for the servo to reach the position
        } 
        doorclosed = true;
      }
    }

  }


  Serial.println(digitalRead (pir)); // show pir
    Serial.println(doorclosed); // show if door is closed
  
  
}

Credits

Milla Zimonjic El-Zarif

Milla Zimonjic El-Zarif

1 project • 1 follower

Comments