Learning Objectives
- Emphasis: Equal
- Hardware: Analog and digital components
- Software: Intermix analog and digital methods
Materials
- Arduino Uno
- Breadboard and wires
- Red LED
- 8 ohm speaker
- 10 kohm photoresistor
- 100 ohm resistor
- 330 ohm resistor
- 10 kohm resistor
Reference Files
Breadboard Layout
- Pins to breadboard: For this project, we'll use the amount of light coming in to the photoresistor to set off a blinking LED and a buzzer. Go ahead and set up a ground rail and a power rail on your breadboard.
- LED: Connect the 330 ohm resistor from pin 9 to the long leg of the LED. Connect the short leg of the LED to ground.
- Speaker: Connect the 100 ohm resistor from pin 10 to the speaker and then to ground. Speakers allow bidirectional current, so it doesn't matter which direction it's plugged in.
- Photoresistor: Connect the photoresistor from the power rail to the 10k resistor to ground. Connect a wire from in between the two resistors to pin A0 on the Arduino.
- You're done! Plug your Arduino into the USB port on your computer.
Reference Schematic
Software
- Getting started: Initialize variables for the speaker, LED, and photoresistor. Also include the pitches.h file by saving it in the same folder as your sketch and then typing #include 'pitches.h' at the top of your sketch. In your setup method, declare the LED and speaker as output and the photoresistor as input.
- Define motion threshhold: The photoresistor will give us higher readings when light is blocked. If something moves over it, the shadow will temporarily decrease the light, and the reading will go up. We're going to choose a threshhold value to tell whether something has passed over the photoresistor. A higher value will make your motion detector less sensitive; a lower one will increase sensitivity. Let's choose 800 as a starting point. Up where you initalized your other variables, write int threshold = 800.
- Read photoresistor input: In your loop method, read in and store the analog photoresistor input. We won't need to map it to anything this time, as we're using it for purely digital purposes.
- Turn on alarm: We're going to use if statements to check if the stored value is above our threshold. If(value > threshold), first turn on the led with digitalWrite(ledPin, HIGH). Also play a tone to your speaker. We used NOTE_C5 since it's high enough to be annoying, but you can pick any note you want.
- Turn off alarm: In the else block, turn off your LED with digitalWrite(ledPin, LOW). Also turn off the speaker with noTone(speakerPin). It'll get annoying if you leave it on too long!
- Running the program: Upload the program and try passing your hand back and forth across the photoresistor to see if the alarm turns on. If it's too sensitive or not sensitive enough, change your threshold value and try again. You can also output the values to the Serial monitor to find a threshold that works.
- Experiment: Can you make the LED blink instead of just staying on? What about adding more than one photoresistor to increase your accuracy? You can also think about putting the entire setup into a box or something similar so it's more inconspicuous. It depends on how sneaky you want to be!
Reference Code
<-- Previous: Electronic Harmonica
Next: Keyboard -->