Pulse Sensor Speaker Tutorial

Speaker from World Famous Electronics on Vimeo.

 

 

 Here's a list of materials for this tutorial.

  • Pulse Sensor Amped
  • Arduino UNO
  • 8 ohm Speaker
  • Red LED
  • 1K Resistor
  • 470 ohm Resistor
  • 10uF Capacitor
  • A breadboard and some jumper wires

Our Pulse Sensor Arduino code is designed to find the moment of heart beat in the pulse waveform for accurate BPM calculation (go here for an in-depth discussion of our code). In order to do this, we are using one of the hardware timers on the ATmega328 (Arduino UNO's 'brain') to sample sensor data at exactly 500Hz (we sample every 2mS). The default Timer we use is Timer2, but Timer2 is also used  by the tone() command in Arduino. So we have to make a small change to the code to use Timer1 instead of Timer2. This code is located in the Interrupt.ino tab in the Arduino sketch.

 

 Original Interrupt Setup Modified Interrupt Setup

void InterruptSetup(){

// Initializes Timer2 to throw and interrupt every 2mS

TCCR2A = 0x02;

TCCR2B = 0X06;

OCR2A = 0x7C;

TIMSK2 = 0x02;

sei();

}

void InterruptSetup(){

// Initializes Timer1 to throw and interrupt every 2mS

TCCR1A = 0x00;

TCCR1B = 0x11;

TCCR1C = 0x00;

ICR1 = 16000;

sei();

}

ISR(TIMER2_COMPA_vect)

ISR(TIMER1_OVF_vect)

 

Now that we've switched the Timer that we're using, we can use the Arduino tone() command and make a nice little beep when the heart beats! Let's lash up the circuit!

Here's a schematic of what we're going to do

The LED is connected to Arduino pin 5. This pin will make the LED turn on when your heart beats, then it will fade softly for a nice effect. The code that does this is already a part of the Pulse Sensor Amped Arduino code. It is important to use at the very least a 470 ohm resistor. You can use up to 1K, but any less than 470 may result in weird behavior. 

The 8ohm speaker is a standard device that you can pick up almost anywhere, or you can hack them out of old equipment if you like :). The 1K resistor in between pin 2 and the speaker controls the volume of the beeps. Try different values for this resistor to increase or decrease the volume. The 10uF capacitor is there to protect the speaker from DC current which could damage it. Make sure to use the 10uF capacitor!

Here's a picture of the breadboarded circuit.

 

Find the code we are using for this tutorial on our github here. Here's how we modified the original code to make this work:

 First, we create an alias for pin 2, where we connect the speaker, above the void setup() routine.

int Speaker = 2;  // plug the speaker into Arduino pin 2

Then in the loop(), we turn on the speaker when Arduino finds the heartbeat. Here's the loop:

 

void loop(){
    serialOutput() ;

    if (QS == true){    // A Heartbeat Was Found
                               // BPM and IBI have been Determined
                               // Quantified Self "QS" true when arduino finds a heartbeat
        fadeRate = 255; // Makes the LED Fade Effect Happen
                                  // Set 'fadeRate' Variable to 255 to fade LED with pulse
        serialOutputWhenBeatHappens();  // A Beat Happened, Output that to serial.
       QS = false;        // reset the Quantified Self flag for next time
       tone(speaker,1047);  // tone pin, frequency, duration in mS
    }

    ledFadeToBeat();  // Makes the LED Fade Effect Happen
    delay(20);  // take a break
}

 

The QS variable is set to true when Arduino finds the heart beat. So in our conditional if statement is where we turn on the speaker. I just arbitrarily chose 1047 as the frequency of the tone. You can change this around to fine a tone you like. Now that the tone is on, we will need to turn it off soon! I'm using the ledFadeToBeat() function to do this. Notice that when QS is true, the variable fadeRate is set to 255. That is the maximum value for our PWM signal, which is what we're using to fade the LED.

 

void ledFadeToBeat(){
fadeRate -= 15; // set LED fade value
fadeRate = constrain(fadeRate,0,255); // keep LED fade value from going into negative 
if(fadeRate == 0){        // when the fade is done
noTone(speaker);  // turn off the tone
}
analogWrite(fadePin,fadeRate); // fade LED
}
The function above gets called once per loop, and the loop has a delay of 20 milliseconds. The fadeRate variable starts at 255, and gets 15 subtracted from it once per loop. That means the LED fades and the beep lasts for (255/15)x20 = 340 milliseconds.