This Holiday season, and for this years Project 14 Holiday theme , I planned to build a new traditional Christmas star at my Dad’s home with a twist. Basically the idea is - when someone visits the house OR when the children come Christmas caroling, the star which is placed in the path of the front door way will detect movement and play a small welcome message followed by a Holiday theme song/carol. To detect the movement I am using a PIR sensor connected to the Arduino MKR Zero, which in turn is connect to an I2S amplifier break out to play music downloaded to an SD card. Here is a picture of the Star from last year 2020..
And before I forget, Happy Holiday !! to you and your family ..
Circuit
Here are the connection details for the Adafruit I2S 3W Class D Amplifier Breakout – MAX98357A and Arduino MKR ZERO https://www.newark.com/arduino/abx00012/dev-board-music-digital-audio/dp/47AC4166
- VIN on the I2S amplifier connected 5V of the Arduino MKR Zero
- GND connected GND
- LRC connected to pin 3
- BCLK connected to pin 2
- DIN connected to pin A6
PIR sensor is connected to pin 1 on the Arduino MKR Zero
The Arduino sketch below only accepts wav files, which means if you have mp3 files, they will have to be converted to wav using a tool like Audacity Once your done with the conversion process, copy the .wav file to the SD card.
Now, if this is the first time you are using the MKR family of Arduino boards, go to the board manager and install the latest version for the Arduino SAMD Boards
In addition, before you upload the sketch below you will also have to install the Arduino sound library using the Library Manager.
Once done, upload the code below to the Arduino MKR ZERO
#include <SD.h>
#include <ArduinoSound.h>
int ledPin = LED_BUILTIN; // using the bulit in LED as a visual indicator if motion is detected by PIR
int pirPin = 1; //PIR signal pin
int pirStat = 0;
// filename of wave file to play
const char filename[] = "MUSICO.WAV"; //change this to the name you have given in the SD Card
SDWaveFile waveFile;
void setup() {
pinMode(ledPin, OUTPUT);
pinMode(pirPin, INPUT);
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
// setup the SD card
Serial.print("Initializing SD card...");
if (!SD.begin()) {
Serial.println("initialization failed!");
return;
}
Serial.println("initialization done.");
// create a SDWaveFile
waveFile = SDWaveFile(filename);
// check if the WaveFile is valid
if (!waveFile) {
Serial.println("wave file is invalid!");
while (1); // do nothing
}
// print out some info. about the wave file
Serial.print("Bits per sample = ");
Serial.println(waveFile.bitsPerSample());
long channels = waveFile.channels();
Serial.print("Channels = ");
Serial.println(channels);
long sampleRate = waveFile.sampleRate();
Serial.print("Sample rate = ");
Serial.print(sampleRate);
Serial.println(" Hz");
long duration = waveFile.duration();
Serial.print("Duration = ");
Serial.print(duration);
Serial.println(" seconds");
// playback volume, change this to a lower value if it is loud.
AudioOutI2S.volume(95);
// check if the I2S output can play the wave file
if (!AudioOutI2S.canPlay(waveFile)) {
Serial.println("unable to play wave file using I2S!");
while (1); // do nothing
}
}
void loop() {
pirStat = digitalRead(pirPin);
if (pirStat == HIGH) { // if motion detected
digitalWrite(ledPin, HIGH);
//check if playback is still going on
if (!AudioOutI2S.isPlaying())
{
// playback has stopped
Serial.println("playback stopped");
while (1); // do nothing
}
Serial.println("starting playback");
AudioOutI2S.play(waveFile); // playing the wav file
}
else {
digitalWrite(ledPin, LOW); // turn LED OFF if we have no motion
Serial.println("Waiting...");
}
delay(10);
}
Here are some picture of the Traditional Star being built for Bamboo sticks.
Stay tuned for the next blog which will include completing the star, and applying the red and white butter paper to the frame. And, I also plan on adding Neopixels in the center hexagon for a diffusion effect !!