Error combining motion sensor and speaker

Hello all,

I’m doing an internship in Berlin this summer, which has made it a bit difficult finding resources I can understand completely online. I’m using AudioShield for Arduino, by ELV and I’m using one of those typical motion sensors, the white dome ones that show up when you Google “motion sensor Arduino”. I’m trying to make a little unit that plays music when it detects motion.

I notice that the AudioShield and motion sensor both work fine independently of each other, but when I combine the code, the motion sensor always detections motion.

#include <SD.h>
#include <SPI.h>
#include <AudioShield.h>


int bewegung = 7;
int bewegungsstatus = 0;

void setup(){
  Serial.begin(9600);  
  pinMode(bewegung, INPUT);
  if( SD.begin( SD_CS ) == false )
  {
    // Programm beenden, da keine SD-Karte vorhanden
    return;
  }
  //MP3-Decoder initialisieren
  VS1011.begin();
}

void loop(){
  unsigned char buffer[32];


  //Volume
  unsigned char leftchannel = 20;
  unsigned char rightchannel = 20;
  
  bewegungsstatus=digitalRead(bewegung);

  if (bewegungsstatus == HIGH){
         Serial.println("MOTION");  
         if( File SoundFile = SD.open( "001.mp3", FILE_READ ) ){
                 //Verstärker einschalten
                  VS1011.UnsetMute();
                  VS1011.SetVolume(leftchannel, rightchannel); 
                  //Datei bis zum Ende abspielen
                  while( SoundFile.available() ){
                  //Puffer mit Daten aus der Datei füllen
                  SoundFile.read( buffer, sizeof(buffer) );
                  //Daten aus Puffer an MP3-Decoder senden
                  VS1011.Send32( buffer );
                  }
                //Internen Datenpuffer vom MP3-Decoder mit Nullen füllen
                //damit sicher alles im Puffer abgespielt wird und Puffer leer ist
                //MP3-Decoder besitzt 2048 Byte großen Datenpuffer
                VS1011.Send2048Zeros();

                //Verstärker deaktivieren
                VS1011.SetMute();

     
                //Datei schliessen
                SoundFile.close(); 
        
              delay(1000);
        }
        Serial.println("3");  
  } 
     else {

    Serial.print("0");
    delay(2000);
    
  }
  
}

I actually notice when I comment out this part in the setup, the motion sensor works again.
VS1011 is the object that communicates with the AudioShield library.

  VS1011.begin();

I suspect, there’s too much data being processed? Not sure.

I currentlz have 9V going into Vin, the USB port connected (to look at the serial monitor), and the 5v pin connected to the motion sensor.
I think I provided enough information, but I’d be happy to answer any questions. Thanks.

Hmm, actually I may be better off asking ELV this question. I find that once I do VS1011.begin(), the sensor never works again. I need a way to shut it off at the end of each iteration. I used VS1011.Reset() and VS1011.SoftReset() but hasn't worked. I will email ELV. :)

I guess something that can work for me, is a general function I can use after a specific length of time that restarts the sketch completely. Any ideas in that area?

edit; actually I found something online. Thanks for reading through my progression of struggle.

You sure you don't have a pin conflict with your hardware? Sure sounds like you do :)

Can you please elaborate?

I found a solution around my problem, by using this after every 20 secs or so

void(* resetFunc) (void) = 0;

but is this bad practice? I wonder if there's something bad happening deep down.

I’m not familiar with the hardware or the libraries you’re using, but if for example, the audioshield and the sd lib both use a set pin that they expect to perform a certain way, then you can expect unpredictable behaviour

Edit:

Just found the FAQ for that shield, and also realised the SD card hardware is onboard. It also looks like you are fine with pin 7 (Assuming your on ARduino Uno?). Here’s the link:

faq

the white dome ones that show up when you Google “motion sensor Arduino”.

You do realise that my Google is different from your Google? Google knows where you are and it has a surprisingly good idea where you are going so it does a very good job of showing results that are relevant to you.

What is the actual sensor you have? Does it have an open-collector output? Then you need a pullup.

It's called PIR Motion sensor: http://www.vetco.net/catalog/product_info.php?products_id=12843

So I put a 10k resistor inbetween 5v pin and VCC of the motion sensor, went back to the old code where I VS1011.begin() was at the beginning, and it still only detections motion.

edit: actually, that messes up its ability to accurately detect motion. It gives patterned results now.

By pullup I think he means between the sensors output wire and your Arduinos input wire, pin 7, not the VCC line :)

Ah, ok. Well, that seemed to fix the problem of it not giving accurate motion results, but I still have the same problem when I combine motion sensor + audioshield code.

Also, unfortunately, the FAQ you found is for a shield by Adafruit. This shield is by ELV.

I feel like arduino + motion sensor + playing sound should be such an easy task. I dont understand why I am having such difficulty =(

Ok I dug a little deeper and found the libs for your shield, low and behold, within VS10xx.h (which is included from your AudioShield.h is this:

#define VS10XX_DCS      7

You have a pin conflict.

Rather than shoe horn the libs to fit, change your IR sensor pin. Looks like pin 9 might be free. Give that one a go.

Wow, it works! I'm so grateful for your help. I go home in 5 minutes and it wouldve been very unsatisfying to have spent a whole day on a problem without fixing it.

I am new to Arduino but in retrospect, it seems somewhat obvious - as you expected - that there was a pin conflict. Wouldnt have hurt to have just tried a different pin at the very beginning. Oh well, what a learning experience. Thank you ;)