PIR sensor to trigger PWM sound - HELP

Hi guys,

Totally new, and need a point in the right direction to figure out how to program my project.

Project: Create motion activated sound from Arduino, without using an SD card shield.

I was able to figure out how to get the arduino to play my sound using a combination of sources

and ended up using Audacity to get the C code conversion after down sampling my source .wav file.

I then realized how awfully quiet the sound was, and wired up an amplifier.


I have a PIR sensor, and was hoping someone could point me to some example sketches that would work with the code I am using to play the sound?

#include <PCM.h>

const unsigned char sample[] PROGMEM = {

/*My PWM C code goes here...."*/


void setup()
  startPlayback(sample, sizeof(sample));

void loop()

Any help will be very appreciated!

**Oh, and I should let you guys know, I’m OK with hardware, but know nothing about software/programming. I am trying to get this completed quickly, as I am going out of town and wanted to have this done within the next week. If I didn’t have that rush, I would try to research/learn it on my own. Looking for a point in the right direction to figure it out, not necessarily asking someone to code it for me, just help me speed up the learning process. Thanks.

I have a PIR sensor,

Do you know how to read the sensor ?

Kind of.

Since originally posting, I found this:

And have the sensor wired in, and it will activate and play a buzz sound. I’m not sure about my PIR though, because no matter how I adjust the trim pots, it doesn’t seem to trigger consistently (I can troubleshoot this later though I guess).

I was wondering if on line 26 I could delete the values “300” and “160” and insert my values?

I’ve got about 14,415 values in total.

Also, at the end of the code it looks like there is something there that handles duration and period, etc. Can I just delete that because I don’t want a repeat of the same sound, just a one-time play though of the 14,415 values.

// Uses a PIR sensor to detect movement, buzzes a buzzer
// more info here: http://blog.makezine.com/projects/pir-sensor-arduino-alarm/
// based upon:
// PIR sensor tester by Limor Fried of Adafruit
// tone code by michael@thegrebs.com

int ledPin = 13;                // choose the pin for the LED
int inputPin = 2;               // choose the input pin (for PIR sensor)
int pirState = LOW;             // we start, assuming no motion detected
int val = 0;                    // variable for reading the pin status
int pinSpeaker = 10;           //Set up a speaker on a PWM pin (digital 9, 10, or 11)

void setup() {
  pinMode(ledPin, OUTPUT);      // declare LED as output
  pinMode(inputPin, INPUT);     // declare sensor as input
  pinMode(pinSpeaker, OUTPUT);

void loop(){
  val = digitalRead(inputPin);  // read input value
  if (val == HIGH) {            // check if the input is HIGH
    digitalWrite(ledPin, HIGH);  // turn LED ON
    playTone(300, 160);

    if (pirState == LOW) {
      // we have just turned on
      Serial.println("Motion detected!");
      // We only want to print on the output change, not state
      pirState = HIGH;
  } else {
      digitalWrite(ledPin, LOW); // turn LED OFF
      playTone(0, 0);
      if (pirState == HIGH){
      // we have just turned off
      Serial.println("Motion ended!");
      // We only want to print on the output change, not state
      pirState = LOW;
// duration in mSecs, frequency in hertz
void playTone(long duration, int freq) {
    duration *= 1000;
    int period = (1.0 / freq) * 1000000;
    long elapsed_time = 0;
    while (elapsed_time < duration) {
        delayMicroseconds(period / 2);
        digitalWrite(pinSpeaker, LOW);
        delayMicroseconds(period / 2);
        elapsed_time += (period);

Well, I just tried copying and pasting my values in…it yelled at me - didn’t like that.

Error: expected “)” before “;” token

I noticed that my values in my original code are housed within {} brackets and this new code seems to want the values housed in ()

I warned you, I’m a complete novice with programming.

I’m a complete novice with programming.

and I am not very good at providing help on code that I have not seen