What we want to achieve
Using an Arduino - we are using a Nano V3.0 compatible board - we want to demonstrate on how an alert system could be build, where an alert is triggered if an object interrupts the path of light.
Think of a customer entering a shop, an intruder entering an area.
You could reverse the example and trigger an alarm when the light path is restored - for example to detect if an object has been removed.
Note
This tip uses a laser as a directed and focus light source. While we are only using a 650nm 5mW laser, please be always careful not to point the laser at persons, animals.
Our code can run in two modes
1) It will continuously sound alarm once it has been triggered. Only way to stop alert is reset the Arduino
2) It will sound alert when light path is lost and will stop sounding alert once light path is restored.
Requirements
Arduino (compatible) development board - we used the Arduino Nano compatible board (see here)
Laser Transmitter Module - KY-008 (see here)
Passive Buzzer Module - KY-006 (see here)
Photo-resistor Module - KY-018 (see here)
Arduino IDE (download here)
Restrictions
This tip is focused on using the Arduino IDE
How to
Wiring
KY-006
Arduino
Pin S
D4
Pin (middle)
NOT USED
Pin -
GND
KY-018
Arduino
Pin S
A0
Pin +
VCC
Pin -
GND
Code
//// Photo Resistor Module KY-018 and Passive Buzzer Module KY-006 for Arduino// 20 January 2020 // Author: dante@serveronthemove.com.au//// Hardware:// Arduino// Photo Resistor Module KY-018// Passive Buzzer Module KY-006//// Wiring:// KY-018 -> Arduino// S -> A0// + (middle) -> VCC 3.3V or 5V// - (GND) -> GND//// KY-006 -> Arduino// S -> D4// (middle) -> NOT USED// - (GND) -> GND//// Purpose:// Sound alert when photo resistor is no longer getting light.// The code supports to modes controlled by "TriggerModeOnce"// triggerModeOnce = true; // will sound alert once light source is lost and will continue to sound even if light source is restorted (Intruder alert). To reset, push reset button// triggerModeOnce = false; // will sound alert only when light source is lost. When lught source is restored, sound stops (visitor entered shop)//// Hardware configint photoModule = A0; // S Pin of Photo Resistor Module KY-021 on Arduino A0int buzzerModule = 4; // S Pin of Passive Buzzer Module KY-006 on Arduino D4// Software configboolean triggerModeOnce = true; // will sound alert once light source is lost and will continue to sound even if light source is restorted (Intruder alert). To reset, push reset buttonboolean photoTriggered = false; // indicator if photoresistor lost light sourceint triggerThreshold = 150; // threshold for alert trigger. Anything beyond this value coming from Photoresistor will trigger alert// setup PINsvoid setup() { pinMode(photoModule, INPUT); pinMode(buzzerModule, OUTPUT); }void loop() { // sound alert if // 1) threshold is exceeded - or - // 2) triggerModeOnce is alert and has been triggered before if ((analogRead(photoModule) > triggerThreshold) || (photoTriggered && triggerModeOnce)) { photoTriggered = true; // mark trigger // reset buzzer digitalWrite(buzzerModule, HIGH); digitalWrite(buzzerModule, LOW); // sound alert with PWM for (int i = 0; i < 80; i++) { // make a sound digitalWrite(buzzerModule, HIGH); // send high signal to buzzer delay(1); // delay 1ms digitalWrite(buzzerModule, LOW); // send low signal to buzzer delay(1); } delay(50); for (int j = 0; j < 100; j++) { //make another sound digitalWrite(buzzerModule, HIGH); delay(2); // delay 2ms digitalWrite(buzzerModule, LOW); delay(2); } delay(100); } else { // make sure is down digitalWrite(buzzerModule, LOW); }}