Introduction: Prototype Motion Sensor From Photocell
This is a quick project I threw together after washing dishes this weekend. I brought my computer into the kitchen to play music while I worked. Unfortunately, there was a flaw with my plan; I hadn't thought about what I would do when I wanted to switch to another song. WIth my hands wet and soapy, I couldn't use the keyboard or trackpad. I realized I could whip up a quick solution with my Arduino.
Step 1: The Original Plan: PIR Sensor
I needed some way to interact with my computer without physically touching it. My first thought was to use a Passive Infrared (PIR) motion sensor. After all they're designed to detect motion. I could just wave my hand and cause the Arduino to do my bidding. Unfortunately, my PIR sensor wasn't in a cooperative mood. Nothing I did, software or hardware, could make the sensor consistently respond to my hand waves. I also ran into the reset limitation of the sensor. The PIR sensor takes an infrared snapshot of the environment and compares it to later snapshots. When there's a change in the environment, the sensor picks it up. After the motion, the sensor has a short reset period where the sensor takes another comparison snapshot. During that reset time, the sensor is unresponsive. It's not a huge issue, but it was another problem with the sensor.
Step 2: Plan B: Makeshift Motion Sensor
So, as is often the case with my projects, I had to come up with an alternative to my original idea. As I rummaged through my box of parts, I remembered a project I made when I was learning basic Arduino concepts. In the book "Getting Started with Arduino", there was a project to make a photocell controlled LED lamp. When I copied that project, I learned that casting a shadow over the light sensor caused the ambient light to drop low enough to activate the sensor.
I realized I could use this to make a makeshift motion sensor out of a photocell. Casting a shadow over the sensor could serve as my touch-free button, and my wet hands would go nowhere near my computer.
Step 3: Materials
- Any OS that can run Arduino should work, but I used a Mac, and my instructions will be written for Macs
- I used a Duemilanove
• Photocell Light Sensor
• 10K Resistor
• Jumper Wire, Breadboard
Step 4: Assemble the Circuit
Assemble the circuit as shown in the pictures. It's basically a single photocell circuit. Attach the 5V and ground pins to the power rails of the breadboard, then run power to one pin of the photocell. Ground the photocell through a 10K resistor, and attach the photocell to analog pin 0.
I had a rather complicated R & D period for this project; I had many issues throughout the process. Originally, I worried about using the circuit in varying light environments. My solution was to have a setup period built into the code, allowing the user to calibrate the sensor to the environment before they actually use it. This calibration process was built into the code, and ended up being far too complicated for my meager coding skills to handle. So I realized I had to scale back my plans to a far simpler model.
Step 5: Software - Arduino Code
So...my code takes a little explaining. In order to make this work properly, the user will have to interact with the code. To deal with the varying light conditions, the user enters the code and defines their light conditions based on measurements by the photocell.
Step by Step Process:
1. Upload the code to the Arduino. Open the serial monitor.
2. The photocell should be printing values to the serial monitor. If you hover a shadow over the photocell, you'll notice a decrease in these printed values.
3. Replace the values for the shadow and noshadow variables. Use a number 5-10 units less than the observed high light condition values for the noshadow variable (i.e. if you observed values of 160, use 150 or so). Use a number 5-10 units greater than the observed low light condition for the shadow variable (i.e. if you observed values of 40, use 50 or so). This should ensure that you avoid false positives and that every shadow is registered as such.
5. Comment out the Serial.println(amblightlvl) line, as it is only there to help the user set up their code.
4. Reupload the code.
Step 6: Software - Computer Code
This step will be the most variable and the most complicated. I've written it for Mac computers because thats what I used, but it can likely be adapted for Windows as well.
There are several options to give the Arduino the ability to interact with computer applications. I've chosen the most basic, although more advanced methods are covered on other Instructables. In my method, the Arduino communicates with a C-based program running on the terminal, which in turn runs an Applescript that will perform the desired action in the application. It's a roundabout method, but it utilizes preexisting software architectures in the operating system and thus requires the least coding effort on my part.
The first step to making this work is to download the C code to give the Arduino access to the computer's terminal program. It was written by Tod Kurt, and is available off this website. However, after much experimentation, I realized that my coding skills weren't up to par with modifying the code to fit my needs (I needed the serial communications to cause the applescript to run). Luckily, another instructable user has done work in this area. User aymans wrote code in his foot switch instructable that modifies Tod Kurt's code to allow serial communications from the arduino to execute applescripts. This turned out to be exactly what I needed, so I modified my current arduino code and files to fit his code. To download his code, visit his instructable
You will also need to download or write your own Applescript to switch the track in iTunes. I've included one attached to this step, but you can always write your own and use it for whatever application and action you'd like. If you write your own, make sure to name it "1.app" and save it in the same folder as the rest of your files to make it work with the other code.
Now that you have all the files, we can try it out. Make sure iTunes is on and that your set-up arduino code is uploaded to your device. Then execute the arduino-serial program (after its been compiled of course) with the command "./arduino-serial -b 9600 -p [your arduino serial port] -R". Replace the brackets with your arduino's serial port, which can be found in the Arduino IDE under the Tools menu. Once this is done, everything should work. Test it out, feel the power of controlling your computer without even touching it. Sure it's a lot of work for a glorified button, but over engineering is always fun.