Have you ever almost burnt your house down? I have multiple times. I'm a very forgetful person so leaving on hot tools, such as my hair straightener is an issue I constantly deal with. Leaving a hot hair straightener on can serve as a major fire hazard, so I created a hair straightener that turns off if I don't use it after 5 minutes.
BackgroundThis project was created as part of the Lane Tech HS Physical Computing Lab (Software App Development 2: Portfolio) course.
CreationStep One: Tilt Sensor
My first problem was to figure out how to know if my hair straightener was not being used. In order to track the movement of my straightener I used a tilt sensor. A tilt sensor works as follows: there is a tiny metal ball inside a small tube, at one end of the tube there is a metal contact, as the ball rolls it can either touch the metal contact completing a circuit, or roll away from the contact breaking the circuit.
The tilt sensor gave me a digital signal of either 1 or 0. Circuit completed or circuit open. In order to track the movement of my hair straightener I wrote this basic algorithm:
Here is how the code works. I established two variables: currentValue and lastValue and set them both equal to zero. My algorithm reads the digital signal from the tilt sensor (1 or 0) and sets currentValue to that. I then compare currentValue to lastValue and if the values are not equal that would mean movement was detected and my LED would flash. After the comparison I reset lastValue to currentValue, and start the loop over again reading a new value from the tilt sensor. Here's an example to make it make more sense: say the last reading from the tilt sensor was 1, if I read another value from the tilt sensor and get 0, my code says they are not equal, which means there was movement, and flash the LED. Then the lastValue will be set to currentValue (0) and my code will take a new reading the from the tilt sensor and assign it to currentValue. This cycle runs continuously as my code polls the tilt sensor.
Here's my code in action:
The LED is on because the tilt sensor and my code is detecting movement.
Step Two: Relay
In order to control power to my straightener I decided to use a relay. More specifically use a relay to modify an extension cord and plug my straightener into that extension cord.
Here's the relay I used:
I spliced the positive side of the extension cord leaving 2 sides of the copper wire exposed. I then fed each side into part of my relay, one side into the COM and the other into NO.
Since the wires are in the NO and COM inserts they don't touch unless the relay is activated.
As you can see in this model, the circuit is normally open, when the relay is activated the circuit is completed. The relay is an OUTPUT sensor. So I send a digital signal to the relay. I send the relay a HIGH digital in order for it to flip the switch and complete the circuit.
For coding the relay I started with sending the relay a digital LOW signal, turning the relay on (completing the circuit). Then after the 5 minutes of no movement has passed I send a digital HIGH signal (breaking the circuit and cutting power to my straightener).
Step 3: The timer
For this project I used a software timer:
I set the timer to add 1 to my variable count every 1000 milliseconds (1 second). Therefore, count acts as a variable that stores the current time passed since the last movement.
I used the timer in a while loop that stated if count (time since last movement) is below 5 minutes the relay will stay on and the tilt sensor will still work. Furthermore, within that while loop, if movement is detected the count is reset to 0, restarting the loop and the countdown to 5 minutes. If there's no movement after 5 minutes the while loop will break and default to the else statement that sends a digital HIGH signal to my relay, cutting the power to my straightener.
Here's my code:
Step 4: Putting it all together
First I started by attaching all my components to the breadboard:
Then I taped the tilt sensor to my hair straightener with painters tape:
Lastly I plugged my straightener into the relay controlled extension cord:
And now its all put together!











Comments