Hackster is hosting Hackster Holidays, Ep. 7: Livestream & Giveaway Drawing. Watch previous episodes or stream live on Friday!Stream Hackster Holidays, Ep. 7 on Friday!
Ali Mostafa
Published © GPL3+

Automatic Temperature Sensing Window Opener and Closer

A home automation project using a servo and temperature sensor to determine when to open and close a window automatically.

BeginnerFull instructions provided6 hours57
Automatic Temperature Sensing Window Opener and Closer

Things used in this project

Hardware components

Photon 2
Particle Photon 2
This is the microcontroller I used; you could probably use an Arduino to make things easier.
×1
USB-A to Micro-USB Cable
USB-A to Micro-USB Cable
This is to connect to and power the Photon 2, but you won't need this if you already have one.
×1
Breadboard (generic)
Breadboard (generic)
You'll need a breadboard to host your small electronics.
×1
Jumper wires (generic)
Jumper wires (generic)
You're probably going to need at least six of these.
×1
uxcell NTC Thermistor
I used a thermistor to measure temperature. They work by measuring changes in resistance to estimate temperature.
×1
Resistor 10k ohm
Resistor 10k ohm
I used a 10k Ohm resistor which is what I recommend if you try this yourself. When I experimented with lower ohm resistors I got more inaccurate results.
×1
ASMC-04B High Power Servo
This is the high power servo that is powered by the power supply.
×1
120W Power Supply MH-120N-R3
I am using this power supply to power the servo. It's a generic power supply but does 24V, which I used for the servo. You don't need to use this one in particular.
×1
Power Bank
You may need a power bank to power the Photon 2 without being connected to your computer. I'm not going to link one since there are so many, but get a cheap one that doesn't have an idle timeout, so it doesn't keep turning off your Photon. You could also try setting up a battery pack, but I used a generic power bank in my project.
×1

Software apps and online services

Particle Build Web IDE
Particle Build Web IDE
This is where you edit your Photon 2 code after setting it up.
Online Serial Monitor
This is the online serial monitor I use.

Hand tools and fabrication machines

Multitool, Screwdriver
Multitool, Screwdriver
You need a screwdriver when connecting power cables to the power supply and servo.
Mastech MS8217 Autorange Digital Multimeter
Digilent Mastech MS8217 Autorange Digital Multimeter
Any multimeter will do. It's not required for the project but will help when configuring your thermistor.
HJ Digital Servo Tester
Allows you to test servos. It was helpful for testing the mechanical parts of my project without risking damaging anything.
Thermometer
Any thermometer will work. It's not required for the project but will help when configuring your thermistor.

Story

Read more

Schematics

Circuit Diagram

I made this in TinkerCad. This is a circuit diagram similar to what my setup looks like. I don't know how to use the Photon 2 here, so I substituted it for an Arduino while using the same pins. Additionally, the temperature sensor and servo are different, but the wires should be going into the same places.

Code

temperature.ino

C/C++
This is the code that operates on the Photon 2. Remember to install the proton-thermistor library first for this to work!
#include <photon-thermistor.h>
#include "Particle.h"

SYSTEM_MODE(AUTOMATIC);
SerialLogHandler logHandler(LOG_LEVEL_INFO);

// Declare variables for thermistor and servo.
Thermistor *thermistor;
Servo windowServo;

// SETTINGS!
int temperatureThreshold = 80; // Temperature threshold before turning servo
int servoTurnTime = 5000; // How many milliseconds to spend turning the servo.
int servoOpenAngle = 70; // Angle of the servo that will open the window.
int servoCloseAngle = 100; // Angle of the servo that will close the window.

bool windowOpen = false; // Define state variable for window (window should start closed).

// Function to slowly turn servos over t milliseconds
void slowTurnServo(int degrees, int t) {
    int currentDegrees = windowServo.read();
    
    // Sometimes the servo read returns bad data the first time, we can just fix it here.
    // 300 degrees is the max for my servo, feel free to change so that it matches yours.
    if (currentDegrees < 0 || currentDegrees > 300) {
        currentDegrees = 150;
    }
    
    int direction = 1;

    if (currentDegrees > degrees) {
        direction = -1;
    }
    
    int distance = abs(currentDegrees - degrees);
    int desiredDelay = t/distance;

    // Slowly iterate over every degree and wait for the desired timeout.
    for (int newDegree = currentDegrees; newDegree != degrees; newDegree += direction) {
        windowServo.write(newDegree);
        delay(desiredDelay);
    }
}

void toggleWindow(bool toggle) {
    // This function will "toggle" the window by instructing the servo to turn to a certain point.
    // You can edit the angles the servo turns to depending on your setup.
    if (toggle) {
        Serial.println("open");
        slowTurnServo(servoOpenAngle, servoTurnTime);
    } else {
        Serial.println("close");
        slowTurnServo(servoCloseAngle, servoTurnTime);
    }
    windowOpen = toggle;

    // This is the delay before we start checking the temperature again, I wouldn't recommend setting this to less than 3 seconds.
    delay(6000);
}

void setup() {
    Serial.begin(9600);
    // Here the thermistor is defined with the required values.
    // Check the proton-thermistor library docs for references on how to change this.
    thermistor = new Thermistor(A0, 10000, 10830, 22, 3435, 10, 40);

    windowServo.attach(A2); // Attach Servo
    toggleWindow(false); // We want to start in the closed position.
}

void loop() {
    // Read the current temperature from the thermistor.
    double currentTempF = thermistor -> readTempF();

    // If the window is open, and needs to be closed, close it.
    // else If the window is closed, and needs to be opened, open it.
    if (windowOpen) {
        if (currentTempF < temperatureThreshold) {
            toggleWindow(false);
        }
    } else {
        if (currentTempF > temperatureThreshold) {
            toggleWindow(true);
        }
    }
    
    // This is the delay between checking the temperature, you can change this value but its probably not necessary.
    delay(100);
}

Credits

Ali Mostafa
1 project • 0 followers

Comments