|Please put some money in the tip jar by clicking on the donate button to support me so I can continue creating contend like this. P.S. please donate more than $1 as PayPal takes minimum $0.30 per transaction|
With Halloween coming around the corner it is time to see how we can automate some scary projects. We are going to explore two of my favourite ways to do this. First we are going to look how to integrate a pressure plate switch (a big push button you step on which we are going to build), then we will look at how to connect a motion sensor (Passive Infrared Sensor or PIR) to your scaretastick project for the maximum scare factor. I recommend you also watch the video below to get the most out of this tutorial.
Integrating your Pressure Plate switch
One of the most used activators of any Halloween projects is a pressure plate switch, which you can hide under a mat. Your unsuspecting victim steps on your door mat activating your amazing Halloween project, and scares the socks off them.
But how do we connect this to your Arduino and write the code for it. First let’s look how we can build a simple pressure plate switch:
Materials and tools needed for your pressure switch:
- 3 identical pieces of corrugated cardboard about half a centimeter thick
- Aluminum foil
- Masking tape
- Glue or double sided tape
- Hookup wire long enough for your purpose
- Utility Knife
- Wire stripper
Before you begin make sure the width of your cardboard is the same or smaller than your aluminum foil you are going to use.
With your utility knife cut the center out of one of your pieces of cardboard leaving a border of about 3 cm to 4 cm.
Use glue or double sided tape to affix your aluminum foil to the 2 remaining pieces of cardboard. Make sure that your aluminum foil is well adhered to the cardboard so it doesn't sag.
Leave a small border (4cm / 1 inch) without aluminum foil around the edges (In my picture it is not visible because I forgot to do it). If you don’t do this you have the chance that you create a short, resulting in your project not functioning correctly (which happened to me ).
Strip one end of your hookup wires exposing 5 cm/2 inches of the copper wire inside. Lay the 2 pieces of cardboard next to each other with the aluminum foil facing up. Use masking tape to tape the exposed copper wire to the aluminum foil.
Affix the 3rd piece of cardboard (The one with the center cut out) to one of the aluminum foil covered sides. Now place the other Aluminum foil covered piece of cardboard on top (aluminum foil facing down) creating a sandwich. If you have a multi meter test the switch(before taping this all together) to make sure you don't have a shorts.
Tape the 3 layers of cardboard together so they are fixed in place and can’t slide or move, and your switch is completed
Connecting the Pressure Plate Switch to your Arduino
Now you have created a pressure plate switch (or a very large push button) it is time to connect it to your Arduino. A pressure plate switch is a really big temporary switch or push button. To connect one of these things to your Arduino you going to need:
- An Arduino or compatible
- 10 K resistor
- Jumper wire
- Your completed pressure plate
Connecting your pressure plate is much like connecting any temporary switch/button to your Arduino. The following diagram shows what it would look like if you connect it to an Arduino Uno.
As you see connecting it to your Arduino is a breeze. Take your 10K resistor and connect it to 5v as seen on the diagram. Connect one lead of your Pressure Plate to ground and the other one to your 10K resistor as displayed in the diagram. Next connect a jumper wire from the 10K resistor to digital pin 2 on your Arduino.
The most complex part of this setup is that 10K resistor. This is what we call a pull up resistor. Its purpose is to eliminate false readings from you digital input pin on your Arduino. I am not going to go deeper into the workings of this pull up resistor as it falls out of the scope of this tutorial.
The Code for your Pressure Plate
Here is where the rubber meets the road. Although connecting your components correctly is paramount for a successful project, writing the code actually make things happen. One of the most basic things we handle is a push button. But lots of people are still do this wrong.
If you don’t create a debounce system in your code you get many erroneous inputs from this button. Without debouncing a button, one press of the button (or one step on the pressure plate) might be interpreted by the microcontroller as many button presses. This is because of the mechanical properties of these push buttons and the speed the microcontroller reads the inputs.
The way we are going to debounce the button is by counting milliseconds. We set how many milliseconds we want to wait until we allow the next action from a button. To do this we use the millis() function.
The millis() function basically returns in milliseconds how long your sketch has been running. After about 50 days the millis() number has become so big that it returns to 0 and the fun start all over again.
How does this work?
We create two variables:
unsigned long debounceDelay = 1000;
The debounceDelay variable tells the sketch how long you want to wait before the next action (in milliseconds). The lastDebounceTime variable gets a new value every time the button gets pressed, and the wait time set by the debounceDelay variable has expired. It uses the millis() function to get its new value.
I have created two example sketches. One that will activate your project when somebody steps on the plate (step_on.ino) and one that will activate your project when stepping off the plate (step_off.ino). You can download them by clicking on the links below
In the sketch you only need to edit the debounceDelay value to your preferred delay time (in milliseconds). You copy your code in the action() function found at the bottom of the sketches provided. Don't forget to also copy your includes and variables you have created to the top of the sketch, and modify the setup() function to reflect your needs.
//your code goes here
Using a motion sensor (Passive Infrared (PIR) Sensor)
A motion sensor operates much like a button. If somebody is in the line of sight of the infrared beam it sends a signal to the digital pin on your Arduino (pulls the pin high). Lets take a closer look at how to integrate your PIR sensor into your project.
- An Arduino or compatible
- Jumper wire
- A PIRmotion sensor
As you see in the diagram it is a very simple process to connect your motion sensor. You connect the power in lead to 5V and the ground lead to gnd on the Arduino Uno. The signal lead goes to digital pin 2 on your Arduino, and your sensor is ready for some code.
How does the code works
The sketch for the motion sensor works much the same as the sketches for the pressure plate switch. We use the same variables and also include the action() function to copy your code in. You can download the sketch named pir_sensor.ino here.
In this example we use the debounceDelay variable to delay the activation of your project not because of electrical noise, but to stop the project from constantly working because people stand in the beam
Now you just need to copy your code in the appropriate places (the action() function) and alter the debounceDelay variable to your liking. If you bought a PIR sensor with adjustments you can tune down the sensitivity of your sensor. The sensor I am using doesn't have that capability. To reduce the sensitivity of my sensor I created a protection hood that only allows a small amount of the infrared beam to return to the sensor.
If you like this tutorial and want to see more of this please If you are interested subscribe to my periodical Newsletter by filling out the form below, or follow me on Facebook by clicking on this link, and click the follow button.