Teensy 3.2, Teensy Audio shield and APA102 LED Strip

Status
Not open for further replies.

Azar

Member
I am trying to make APA102 LED Strip react to music from SD card (.wav) in Teensy Audio Shield.
I am trying it for 4 days. But I am unable. Someone please help me with the code.
/*
#include <FastLED.h>

#include <Audio.h>
#include <Wire.h>
#include <SPI.h>
#include <SD.h>
#include <SerialFlash.h>
#include <Bounce.h>

#define NUM_LEDS 60
#define DATA_PIN 3
#define CLOCK_PIN 13


#define SDCARD_CS_PIN 10
#define SDCARD_MOSI_PIN 7
#define SDCARD_SCK_PIN 14

AudioPlaySdWav playSdWav1;
AudioOutputI2S i2s1;
AudioConnection patchCord1(playSdWav1, 0, i2s1, 0);
AudioConnection patchCord2(playSdWav1, 1, i2s1, 1);
AudioControlSGTL5000 sgtl5000_1;

Bounce button0 = Bounce(0, 15);
Bounce button2 = Bounce(2, 15); // 15 = 15 ms debounce time

CRGB leds[NUM_LEDS];


void setup() {
Serial.begin(9600);

AudioMemory(8);
sgtl5000_1.enable();
sgtl5000_1.volume(0.5);
SPI.setMOSI(SDCARD_MOSI_PIN);
SPI.setSCK(SDCARD_SCK_PIN);
if (!(SD.begin(SDCARD_CS_PIN))) {
while (1) {
Serial.println("Unable to access the SD card");
delay(500);
}
}
FastLED.addLeds<APA102, DATA_PIN, CLOCK_PIN, BGR>(leds, NUM_LEDS);


pinMode(0, INPUT_PULLUP);
pinMode(2, INPUT_PULLUP);

}

int filenumber = 0; // while file to play

const char * filelist[4] = {
"SDTEST1.WAV", "SDTEST2.WAV", "SDTEST3.WAV", "SDTEST4.WAV"
};

void fadeall() { for(int i = 0; i < NUM_LEDS; i++) { leds.nscale8(250); } }


void loop() {

if (playSdWav1.isPlaying() == false)
{
const char *filename = filelist[filenumber];
filenumber = filenumber + 1;
if (filenumber >= 4) filenumber = 0;
Serial.print("Start playing ");
Serial.println(filename);

playSdWav1.play(filename);
delay(10); // wait for library to parse WAV info
}


// read pushbuttons
button0.update();
if (button0.fallingEdge()) {
playSdWav1.stop();
}
button2.update();
if (button2.fallingEdge()) {
playSdWav1.stop();
filenumber = filenumber - 2;
if (filenumber < 0) filenumber = filenumber + 4;
}

// read the knob position (analog input A2)
int knob = analogRead(A2);
float vol = (float)knob / 1280.0;
Serial.print("Volume");
Serial.print("LED ");
//fadeall();
FastLED.show();
sgtl5000_1.volume(vol);

}
*/
 
Thanks for posting your code straight away, but can you add a bit more context on what doesn't work for you please:
Does the fastled strip demo code work with your strip?
does the audio waveplay examples work with your audio hardware?
Have you modded a fastled example and got it to work?
As far as I can tell your code above does not update the LED values (fadeall is commented out), just stops or restarts wav playback when buttons are pushed. Is that what you get or is that not working either?

Also when writing posts there is a # symbol in the edit bar which allows you to add code tags around your program, which puts it in a scrolling box and formats it a bit nicer.
 
You need to set the colour of the Leds before show(). as GremlinWrangler says, there is nowhere in the code that sets the led colours. it would normally be something like leds[x]= your data; You will need to make this happen for each loop, so you either have a function that updates the leds that is called each loop, or you update from the loop each time.

Check this page out for options for setting led colours before showing: https://github.com/FastLED/FastLED/wiki/Basic-usage
 
Last edited:
so, before show() you might have code that did something like:
for(int pixel = 0; pixel < NUM_LEDS; pixel++) {
leds[pixel] = "get my colour from data source";}
FastLED.show();
 
Thanks for posting your code straight away, but can you add a bit more context on what doesn't work for you please:
Does the fastled strip demo code work with your strip?
does the audio waveplay examples work with your audio hardware?
Have you modded a fastled example and got it to work?
As far as I can tell your code above does not update the LED values (fadeall is commented out), just stops or restarts wav playback when buttons are pushed. Is that what you get or is that not working either?

Also when writing posts there is a # symbol in the edit bar which allows you to add code tags around your program, which puts it in a scrolling box and formats it a bit nicer.

I am very new to these concepts.

I wanted to do something like this https://www.youtube.com/watch?v=LSUK...06082319006958

Teenst audio adapter(https://www.pjrc.com/store/teensy3_audio.html) with SD card.

I wanted to light the LED strips based on the music(.wav) from the SD card.

Kindly help me.

Thank you very much
 
well, have you managed to get the led strip working on its own? also, have you managed to get the sd card to read? Its good to get each part working separately first before throwing it all together.
 
As per Mortonkopf you need to check the basics. We are happy to help, but we can't write the project for you since only you can see what your wiring looks like and understands your needs. If you do want someone else to build and code it for you say so, and expect to produce a robust design specification and pay for at least a weeks worth of someones time.

If this is all new then you need to look at Arduino tutorials starting with blink and moving up to make sure you understand setup() and loop()
Then you need to confirm that each part of your project hardware works, as per my previous post
Can you make the LED on the Teensy blink? Can you change the blink rate? - confirm teensy works and your programing setup
Can you get the fastled demo code to light your strips? if that is causeing trouble check adafruit dotstar library and it's tutorial - I suspect you have some hardware issues there but can't tell yet
Can you you get the SD cardinfo example to work - test if your SD card is formated correctly
Can you get wav playback to work - check that the audio library functions are working.

If all that is seeming a bit overwhelming welcome to the world of electronics! The key part is to break the project down into small steps that you can see a path to and actually complete that step. My desk is a living example of not following my own advice, so I know how hard that is but it's how you get to a finished project.
 
well, have you managed to get the led strip working on its own? also, have you managed to get the sd card to read? Its good to get each part working separately first before throwing it all together.

I am able to read SD card and music was heard in a speaker. But in LED strip Blink LED code is not working.
PLease help me
 
Helping you would be easier if you took the time to write more words. Please is nice, more info so we don't have to make guesses about what you are doing would be good too.

When you say LED blink code doesn't work do you mean the:
the code fails to compile
The code fails to run (no activity on pin 13 LED on the Teensy)
The LED strip never lights
Did you check the dotstartshttps://learn.adafruit.com/adafruit-dotstar-leds/overview This is a different and simpler library so may give better results for testing

Assuming the code is actually working next is to check the wiring since you haven't shown a drawing or photo of your actual hardware, so we have to guess from your code above what you are doing. One minor problem is that you are using a mixture of hardware and software capable pins which will be slowing things up but probably main problem is that you have not mentioned any level conversion hardware. Are you just feeding 3.3V signals into a 5V LED strip, or are you using a prop shield or something to step up the signals?
 
I normally have an led test in setup, to show that the leds are working, and that they show the right RGB / GRB etc order. At the moment, your code has nothing that sets your led strip. you could try putting the below into your setup area of code, just to check what happens. If you get no leds lighting up, then you probably have a wiring problem.

for(int pixel = 0; pixel < NUM_LEDS; pixel++) {
leds[pixel] = CRGB::Red;}
Serial.println("Red");//this is for debug
FastLED.show();
delay(2000);
 
I normally have an led test in setup, to show that the leds are working, and that they show the right RGB / GRB etc order. At the moment, your code has nothing that sets your led strip. you could try putting the below into your setup area of code, just to check what happens. If you get no leds lighting up, then you probably have a wiring problem.

for(int pixel = 0; pixel < NUM_LEDS; pixel++) {
leds[pixel] = CRGB::Red;}
Serial.println("Red");//this is for debug
FastLED.show();
delay(2000);

The code I used for LED strip blink
#include "FastLED.h"


#define NUM_LEDS 1


#define DATA_PIN 3
#define CLOCK_PIN 13


CRGB leds[NUM_LEDS];

void setup() {

FastLED.addLeds<APA102, DATA_PIN, CLOCK_PIN, RGB>(leds, NUM_LEDS);

}

void loop() {
// Turn the LED on, then pause
leds[0] = CRGB::Blue;
FastLED.show();
delay(5);
// Now turn the LED off, then pause
leds[0] = CRGB::Red;
FastLED.show();
delay(5);
}

Output: I have uploaded here
https://www.youtube.com/watch?v=_kia5mQRgN0
 
#define CLOCK_PIN 13

Perhaps FastLED is configuring the SPI to use pin 13, which conflicts with the audio shield's use of pin 13 to receive I2S data.

Worse yet, if you're using the SD card on the audio shield, it depends on the SPI port being configured for SCK on pin 14. Any changes to SPI pins will (probably) mess up SD card access.

Your 2 best options are to use a (likely slower) non-SPI mode to transmit to the APA102 LEDs using other pins completely unrelated to the audio or SD card, or to use Teensy 3.5 or 3.6 which have a SD card slot which uses native SDIO instead of SPI to access the SD card.
 
Perhaps FastLED is configuring the SPI to use pin 13, which conflicts with the audio shield's use of pin 13 to receive I2S data.

Worse yet, if you're using the SD card on the audio shield, it depends on the SPI port being configured for SCK on pin 14. Any changes to SPI pins will (probably) mess up SD card access.

Your 2 best options are to use a (likely slower) non-SPI mode to transmit to the APA102 LEDs using other pins completely unrelated to the audio or SD card, or to use Teensy 3.5 or 3.6 which have a SD card slot which uses native SDIO instead of SPI to access the SD card.

Thank You Paul,
So, I can't do this(https://www.youtube.com/watch?v=go_GnD4URPc)? as I am trying Paul
 
Those are WS2812 LEDs, which come with all sorts of other challenges due to the tight timing, but they don't use the SPI port. You can use a small number of those with audio, but a really long strip without OctoWS2811 will stall the audio update.

That project appears to be analyzing incoming sound. Yours seems to be attempting to play sound from a SD card, so you have the added requirement of using a SD card which that project didn't do. Even though both projects involve controlling LEDs, the other stuff you're attempting to do while controlling them is completely different.

With APA102 the default is SPI, which is conflicting with the audio lib and SD card. You can switch to a non-SPI way to transmit to the APA102s, which is probably the best approach if you don't have a large number of them. Or moving to Teensy 3.5 or 3.6 which have a dedicated SD card slot that doesn't share any signals with anything else would give you a way to read the SD card without any hardware conflicts.

Ideally, FastLED should use SPI transactions to properly share the SPI port. But as far as I know, it doesn't do that. You'd also need gated hardware like on the Prop Shield, so the SD card communication wouldn't affect your LEDs, and support within FastLED for enabling the gate (similar to normal SPI chip/slave select). Even though this way would be good, it's so difficult (given the complexity of FastLED's code) that I wouldn't recommend it. But maybe in the distant future when people may find this message by search the situation with SPI sharing on FastLED may have improved?

For now, I'd recommend either of those 2 approaches I've suggested twice now...
 
Those are WS2812 LEDs, which come with all sorts of other challenges due to the tight timing, but they don't use the SPI port. You can use a small number of those with audio, but a really long strip without OctoWS2811 will stall the audio update.

That project appears to be analyzing incoming sound. Yours seems to be attempting to play sound from a SD card, so you have the added requirement of using a SD card which that project didn't do. Even though both projects involve controlling LEDs, the other stuff you're attempting to do while controlling them is completely different.

With APA102 the default is SPI, which is conflicting with the audio lib and SD card. You can switch to a non-SPI way to transmit to the APA102s, which is probably the best approach if you don't have a large number of them. Or moving to Teensy 3.5 or 3.6 which have a dedicated SD card slot that doesn't share any signals with anything else would give you a way to read the SD card without any hardware conflicts.

Ideally, FastLED should use SPI transactions to properly share the SPI port. But as far as I know, it doesn't do that. You'd also need gated hardware like on the Prop Shield, so the SD card communication wouldn't affect your LEDs, and support within FastLED for enabling the gate (similar to normal SPI chip/slave select). Even though this way would be good, it's so difficult (given the complexity of FastLED's code) that I wouldn't recommend it. But maybe in the distant future when people may find this message by search the situation with SPI sharing on FastLED may have improved?

For now, I'd recommend either of those 2 approaches I've suggested twice now...

Thank You Paul,
Can you please teach me how to do this(You can switch to a non-SPI way to transmit to the APA102s)?
 
Can you please teach me how to do this(You can switch to a non-SPI way to transmit to the APA102s)?

I'm not an expert with the many features FastLED offers.

This question would be best at the FastLED Users Google Plus group. Ask your question there. Explain clearly that you are using Teensy with a SD card and which pins are in use by the card. People at that group can assist you with how to configure FastLED to use other pins and not conflict with SPI.
 
Status
Not open for further replies.
Back
Top