This is a continuation, of my first blog for the Singing Traditional Christmas star, using the Arduino MKR ZERO. As part of this blog my mission is to add LEDs to the star inside before wrapping it up with butter paper. And I will also have to think of a way to add the speaker to one of the spokes of the star, so that the sound can be heard clearly.
For the LEDS, I know I mentioned in my previous blog post that I would use NeoPixels, but I don't have a strip which means I would have to chain the single NeoPixels which will get messy, so instead of chaining NeoPixels together one by one, I planned to use APA102 strip that I had in my kit.
Here is the updated circuit.
The APA102 LED strip connection to the Arduino MKR ZERO
-
VCC of the LED strip is connected to 5V on the Arduino MKR ZERO
-
GND connected GND
-
DI on the LED strip is connected to pin 11 SDA of the Arduino MKR ZERO
-
CI on the LED strip is connected to pin 12 SCL
PIR sensor is connected to pin 1 on the Arduino MKR Zero
Here are the connection details for the Adafruit I2S 3W Class D Amplifier Breakout MAX98357A and Arduino MKR ZERO
-
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
Refer to the previous blog post, for the Arduino IDE setup for the MKR boards, and to add the Arduino Sound Library. In addition, for the APA102 LED strip you will have to install the APA102 library
Here is the code uploaded to the Arduino MKR ZERO
#include "Arduino_APA102.h"
#include <SD.h>
#include <ArduinoSound.h>
int ledPin = LED_BUILTIN; // LED to check if some is detected
int pirPin = 1; // PIR pin
int pirStat = 0; // PIR status
// Setting up APA102
int dataPin = 11;
int clockPin = 12;
int totalLEDs = 5; //change this to the number of LEDs in the star
//Construct object, Arduino_APA102(nLEDS, data line, clock line)
Arduino_APA102 leds(totalLEDs, dataPin, clockPin);
int i,j = 0;
// filename of wave file to play
const char filename[] = "MUSICO.WAV";
// variable representing the Wave File
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, depending on your shield of breakout board
// you may need to pass a pin number in begin for SS
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");
// adjust the playback volume
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);
Serial.println("starting playback");
AudioOutI2S.play(waveFile);
}
else {
digitalWrite(ledPin, LOW); // turn LED OFF if we have no motion
Serial.println("Errr...");
j = AudioOutI2S.isPlaying();
//check if playback is still going on
if (AudioOutI2S.isPlaying())
{
Serial.println("playback going on..");
//running LEDs
leds.begin();
for(i=0; i < totalLEDs; i++)
{
leds.setPixelColor(i ,255,0,0);
leds.show();
delay(100);
}
for(i=0; i < totalLEDs; i++)
{
leds.setPixelColor(i ,0,255,0);
leds.show();
delay(100);
}
for(i=0; i < totalLEDs; i++)
{
leds.setPixelColor(i ,0,0,255);
leds.show();
delay(100);
}
}
Serial.println(j);
for(i=0; i < totalLEDs; i++)
{
leds.setPixelColor(i ,0,0,0);
leds.show();
delay(10);
}
}
delay(100);
}
Now before I get to the difficult part of the build, that is dressing up the star and hiding the components, here is a quick diffusion test of the LED strip - APA102, with one spoke of the star dressed with butter paper.
Top Comments