Christmas is nearly here! Santa is coming! When I was a kid I always wanted to see Santa for myself.
I still do.
Using the new RGB color sensor available at DFRobot, I rigged up a simple proximity alarm. I can use this to warn me if someone is in a close range of my Christmas tree putting presents under there. “Why not use a sonar sensor?” I hear you cry. Well, you easily could, but you want an alarm to be discrete and a sonar sensor is quite big and recognisable, whereas this sensor has a small form factor and is less noticeable. It also gives you some scope to utilize the other features this sensor has to offer in future – such a colour recognition or gesture recognition - something I challenge you to do if you have the time and inclination. However, this is just a jumping off point for all you budding tinkerers – take my design and improve it, take lots of pictures and let me know on the DF Forum!!
Function
This simple proximity alarm has 3 trigger ranges: “near”, “nearer” and “too close”. When each range is triggered, a different alarm sounds and a different light effect occurs. When the near range is triggered, a whaling alarm sounds. When the nearer range is triggered, a different whaling alarm sounds, plus a red LED blinks with it. When the too close range is triggered, a more urgent whaling alarm sounds, plus the LED blinks in a different, more urgent pattern.
Hardware
The hardware of this project is pretty simple and suitable for beginners. All of these and many other interesting things are available on the DFRobot store! Order now and get it by Christmas!
I have included a Lipo battery in the diagram, you can also power it through a USB cable.
Code
I cobbled together the following code to get this working. It’s a mish-mash of code using the sensor’s proximity example as a basis, with some simple features added. Hardly elegant, but it functions well enough.
This code is quite accessible for a beginner, in fact it’s very similar to one of the codes in our Arduino beginner kits. If you’re interested in this project and a better coder than me, why not try improving it? Don’t forget to let us know on the forum if you do!
(Note: I am using the included library for the RGB Color sensor and the Arduino Wire library. You will need to download these in order for the code to compile/upload).
#include <Wire.h>
#include <APDS9960.h>
int buzzer = 8;
int redled = 6;
float sinVal;
int toneVal;
// Initialize Serial port
Serial.begin(9600);
Serial.println();
Serial.println(F("------------------------------------"));
Serial.println(F("APDS-9960 - ProximitySensor"));
Serial.println(F("------------------------------------"));
// Initialize APDS-9960 (configure I2C and initial values)
if ( apds.init() ) {
Serial.println(F("APDS-9960 initialization complete"));
} else {
Serial.println(F("Something went wrong during APDS-9960 init!"));
}
// Adjust the Proximity sensor gain
if ( !apds.setProximityGain(PGAIN_2X) ) {
Serial.println(F("Something went wrong trying to set PGAIN"));
}
// Start running the APDS-9960 proximity sensor (no interrupts)
if ( apds.enableProximitySensor(false) ) {
Serial.println(F("Proximity sensor is now running"));
} else {
Serial.println(F("Something went wrong during sensor init!"));
}
digitalWrite(redled, LOW);
}
void loop() {
// Read the proximity value
if ( !apds.readProximity(proximity_data) ) {
Serial.println("Error reading proximity value");
} else {
Serial.print("Proximity: ");
Serial.println(proximity_data);
}
The “If” function
This enables the sensor to trigger the alarm at a certain proximity.
The sensor measures distances as a value ranged between 0 – 255. 0 is very far away, 255 is very close.
The “if” function reads the data coming from the sensor and IF it hits a defined ranged, e.g. 150, the alarm/LED will trigger. IF NOT, the alarm will not trigger. I used 3 arguments in the code, one for each trigger proximity.
The “Sin” function
The “Sin” function generates a random number that we use to create an alarm tone.
A sin value is generated and we then take this value and map it on to a frequency that the buzzer can generate.
The sin function sounds more intimidating than it actually is. I’m more of a hands-on person than a theory-person, and I was just as intimidated as you probably were after reading that, but it’s not so hard – give it a try – there is plenty of documentation online).
Project Limitations/Evaluation
As I mentioned, this sensor can also sense colour but it needs a lot of ambient light to do it. Originally I wanted to use this function but it needs to work in low lighting (Santa visits at night) and this sensor needs ambient light for the colour detection to give reliable data.
It would have been a cool feature to have it trigger only if the sensor detected a close range AND the colour red - i.e. when Santa is nearby.
Activating some lights (maybe Christmas lights?) would have also been interesting and do-able if I had some more time. Once again, give it a go yourself and see if you can do it!
The Arduino is not a multi-threaded device and prefers to work on one process at a time – as a result making it generate sin values whilst reading proximity data caused its little processor to choke a little, and this is down the way my software is coded. I invite any coders out there to improve upon it – if you do please let me know – learning never ends!
Other Potential Applications:
Someone told me that Santa isn’t real. I’ll have to double check this, but if it is true then there are some other applications for this project:
If you’re a parent, you can use it to make sure your kids aren’t sneaking in to your wardrobe and peeking at hidden presents before Christmas day.
You could also use it for other proximity functions – they don’t have to be Christmas related:
Perhaps someone is stealing pennies from your penny jar. Mount it nearby to be alerted when people are near.
Use it as a house alarm for a tiny dolls house. It’s just about the right scale for a miniature house alarm. Use it to keep burglars away!
This is a very basic application for a rather complex sensor, so if you can think of other features or you can improve it tell us on the forum. We love pictures of projects, so if you try this at home make sure you include lots of them!
Starting Nov 21st (EST), grab a Mystery Box for just $1 and stand a chance to get Raspberry Pi 5B / LattePanda 3 Delta Single Board Computers, with guaranteed prizes worth over $15!
Explore the performance of Small Language Models (SLMs) on the Raspberry Pi 5 SBC, including models like mathstral, phi 3, llama 3.1, gemma2 2b, and Qwen.
Explore our top picks for interactive sensors, DIY lighting, essential maker tools, and exciting project ideas to make this Halloween truly unforgettable!