Introduction: Alarm Clock With Motion Sensor
Ever woke up thinking: "How is it this late already?!" Well you probably used the snooze button one too many times. With this alarm clock thats a problem of the past. The alarm clock only stops when the motion detector sends a signal. You have to get out of bed to activate the sensor and by the time you're out of bed you'll be too grumpy to go back to sleep. At least that's the idea ;)
Step 1: Overview
The idea is that when the alarm starts it sends its output to an audio jack. The motion sensor waits until there is a movement which will stop the alarm. Once a person is out of bed they're not so likely to go back in bed anymore.
- A microcontroller (We'll be using a raspberry 2 model B in this example)
- A PIR motion sensor (BIS0001)
- A RTC (DS3231)
- SD card to hold the OS
- Paper or wood to make a box that focuses the motion sensor
- Speaker (I used an old laptop speaker, but any AUX speaker will work)
- Laser Cutter (Creating a case for the raspberry pi, keeping it safe)
Step 2: Installing Your Rasperry Pi
Raspberry Pi comes without an operating system, so we'll need to install one :)
For completeness sake I'll give a short guide:
- Download NOOBS
- Format the SD card
- Extract NOOBS files to the SD card
- Boot Raspberry Pi with SD card and follow the initial set-up
That's it, we installed an operating system for the Raspberry Pi :)
Step 3: Installing the RTC
If your Raspberry is connected to the internet, you dont really need this. The Raspberry will pull the time and date from NTP servers. Currently the Raspberry pi 3 is the only on with built in wifi, so if your not using a Pi 3, i highly recommend using a RTC to keep the time :)
In the picture you'll see that the pins of the RTC are labeled.
We wont be using the 32K and SQW pin.
For the other pins the lay-out is as follows:
- GND to PIN9
- VCC to PIN1
- SDA to PIN3
- SCL to PIN5
When correctly installed a small red light should turn on. In the next step we'll start using the RTC to keep our time.
P.S. I advice on keeping your RTC inside the box along the Raspberry Pi
Step 4: Making the Raspberry Pi Use Our RTC
On the Raspberry Pi click on the terminal (Blackscreen icon with a right facing arrow followed by an underscore)
type: sudo nano /etc/modules
Add the following lines:
When done press CTRL+X and press "Y" and the Enter
Do the same with: sudo nano /etc/modprobe.d/raspi-blacklist.conf
but this time enter a "#" before "#i2c-bcm2708"
Note: It is possible that this line is not present in your configuration. Dont worry it'll still work :)
Reboot your Pi
This can also be done in the terminal: simply type: reboot
After the reboot go back to the terminal and enter: apt-get install i2c-tools
After this is done use the command: i2cdetect -y 1
This command can change per Raspberry Pi. Using a model 2? Keep the 1 in the command, if you have a model 1 change the 1 to a 0.
If everything went correct you'll see the same output as in the picture. But instead of UU you'll see 68.
Now we'll install the RTC, type: echo ds1307 0x68 > /sys/class/i2c-adapter/i2c-0/new_device
!Again the 0 in i2c-0 represents the model!
Step 5: Time to Set the Time
when you've finally installed the RTC and the I2C tools its time to load the time onto the RTC.
Type: sudo bash
Reads the time from the RTC
Writes the current system time to the RTC
We'll set the date with for example: date -s "24 JAN 2017 17:30:00"
write the time to the RTC and the last step is to say to the Raspberry Pi to use this RTC on startup.
use: sudo nano /etc/rc.local
type between fi and exit0, the following:
- echo ds1307 0x68 > /sys/class/i2c-adapter/i2c-0/new_device
- hwclock -s
Keep in mind that i2c-0 depens on what model you are.
Leave the editor and you're done with the RTC ;)
Step 6: Installing the PIR
Luckily the PIR is a breeze to install compared to the RTC
The PIR has 3 pins, the pinout is as follows:
- Middle pin goes to PIN 7
- The right pin (closest to yellow header) goes to PIN 9
- The left pin connects to PIN 2
You've probably noticed that the PIR has 2 small yellow screws.
I've turned them all the way to the left. Your situation can differ, so you can play around a little with the senitivity.
Step 7: Programming
Just before we start programming go the console once more and type the following:
- sudo pip install vlc
- sudo apt-get install vlc
Click on the Raspberry Pi logo > Programming > Python 3 IDLE
Click file > new
I'll give a brief explanation of the code:
Import = you're importing that the language does not have as a standard. VLC is used for the .mp3 playback. GPIO is used for using the pins on the board.
GPIO.setmode = this sets the GPIO mode, on Pinout.xyz you can see that there is also some kind of BCM numbering. We'll stick with board numbering.
GPIO.setup = tell the Pi we're gonna use this port for input data.
P = this could be any kind of name, just make sure you change it in the rest of the document as well.
You see a link to a .mp3 file. This differs per Pi and you'll have to supply your own Alarm sounds.
Step 8: Running the Program and Configuring a Time
In the Python environment press F5 to run the module. You can use this to test the sensitivity of the PIR.
But how do we configure to run this program at a set time?
we could write something in the code to make this happen, but linux has a much better system. Crontab
Crontab makes a program run on a specific time,year or week.
Open the console and type: crontab -e
After the pound signs/hashtags:
There are 5 things that need to be set, seperated by a space.
A * means that it will happen everytime.
- First is the minute
- Second is the hour
- third is the day of month ( makes it possible to say every third day of the month)
- fourth is month
- fifth is the day of the week( so every sunday for example)
Because this is an alarm clock you'll probably want it everyday, here is a handy examples:
30 7 * * * Means everyday at 7:30AM
After the time, you want to specify which file to run.
I use this command:
40 7 * * * /home/Pi/Documents/MotionClock.py
If you have a different file, use the file path to your file.
Step 9: Focusing the PIR and Case for the Raspberry Pi
While this is not truly necesarry I just wanted to give some advice on how to focus the PIR to a specific range.
The PIR scans 360 degrees around it, so any motion will trigger it. No problem if you're using this device in the room next to your bedroom. But not really usefull if it's in your bedroom.
If you wanna focus the PIR beam, just make it so that there only light coming from that direction, this is easily to do with for example an empty toilet roll.
The intro image shows how I've done it in my situation. I placed my PIR inside an old CPU box. This focused the PIR enough to not be triggered by other things, but left enough space to be detected when I stepped of out my bed.
I made a case for a Raspberry Pi Model B v2. This protects my Pi against damage and makes it look a lot nicer.
The files are available here.
The original files are from here , I edited them to make them more user friendly in our project. But credit where credit is due :)
We have a be nice policy.
Please be positive and constructive.