Devices & Components
Arduino Uno Rev3
Adafruit OLED 128x32
MAX30102 High-Sensitivity Pulse Oximeter and Heart-Rate Sensor for Wearable Health
Buzzer
Project description
Code
MAX_BPM_OLED_Buzzer.ino
arduino
Modified from the SparkFun MAX3010x library
1/* This code works with MAX30102 + 128x32 OLED i2c + Buzzer and Arduino UNO 2 * It's displays the Average BPM on the screen, with an animation and a buzzer sound 3 * everytime a heart pulse is detected 4 * It's a modified version of the HeartRate library example 5 * Refer to www.surtrtech.com for more details or SurtrTech YouTube channel 6 */ 7 8#include <Adafruit_GFX.h> //OLED libraries 9#include <Adafruit_SSD1306.h> 10#include <Wire.h> 11#include "MAX30105.h" //MAX3010x library 12#include "heartRate.h" //Heart rate calculating algorithm 13 14MAX30105 particleSensor; 15 16const byte RATE_SIZE = 4; //Increase this for more averaging. 4 is good. 17byte rates[RATE_SIZE]; //Array of heart rates 18byte rateSpot = 0; 19long lastBeat = 0; //Time at which the last beat occurred 20float beatsPerMinute; 21int beatAvg; 22 23#define SCREEN_WIDTH 128 // OLED display width, in pixels 24#define SCREEN_HEIGHT 32 // OLED display height, in pixels 25#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin) 26 27Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET); //Declaring the display name (display) 28 29static const unsigned char PROGMEM logo2_bmp[] = 30{ 0x03, 0xC0, 0xF0, 0x06, 0x71, 0x8C, 0x0C, 0x1B, 0x06, 0x18, 0x0E, 0x02, 0x10, 0x0C, 0x03, 0x10, //Logo2 and Logo3 are two bmp pictures that display on the OLED if called 310x04, 0x01, 0x10, 0x04, 0x01, 0x10, 0x40, 0x01, 0x10, 0x40, 0x01, 0x10, 0xC0, 0x03, 0x08, 0x88, 320x02, 0x08, 0xB8, 0x04, 0xFF, 0x37, 0x08, 0x01, 0x30, 0x18, 0x01, 0x90, 0x30, 0x00, 0xC0, 0x60, 330x00, 0x60, 0xC0, 0x00, 0x31, 0x80, 0x00, 0x1B, 0x00, 0x00, 0x0E, 0x00, 0x00, 0x04, 0x00, }; 34 35static const unsigned char PROGMEM logo3_bmp[] = 36{ 0x01, 0xF0, 0x0F, 0x80, 0x06, 0x1C, 0x38, 0x60, 0x18, 0x06, 0x60, 0x18, 0x10, 0x01, 0x80, 0x08, 370x20, 0x01, 0x80, 0x04, 0x40, 0x00, 0x00, 0x02, 0x40, 0x00, 0x00, 0x02, 0xC0, 0x00, 0x08, 0x03, 380x80, 0x00, 0x08, 0x01, 0x80, 0x00, 0x18, 0x01, 0x80, 0x00, 0x1C, 0x01, 0x80, 0x00, 0x14, 0x00, 390x80, 0x00, 0x14, 0x00, 0x80, 0x00, 0x14, 0x00, 0x40, 0x10, 0x12, 0x00, 0x40, 0x10, 0x12, 0x00, 400x7E, 0x1F, 0x23, 0xFE, 0x03, 0x31, 0xA0, 0x04, 0x01, 0xA0, 0xA0, 0x0C, 0x00, 0xA0, 0xA0, 0x08, 410x00, 0x60, 0xE0, 0x10, 0x00, 0x20, 0x60, 0x20, 0x06, 0x00, 0x40, 0x60, 0x03, 0x00, 0x40, 0xC0, 420x01, 0x80, 0x01, 0x80, 0x00, 0xC0, 0x03, 0x00, 0x00, 0x60, 0x06, 0x00, 0x00, 0x30, 0x0C, 0x00, 430x00, 0x08, 0x10, 0x00, 0x00, 0x06, 0x60, 0x00, 0x00, 0x03, 0xC0, 0x00, 0x00, 0x01, 0x80, 0x00 }; 44 45 46void setup() { 47 display.begin(SSD1306_SWITCHCAPVCC, 0x3C); //Start the OLED display 48 display.display(); 49 delay(3000); 50 // Initialize sensor 51 particleSensor.begin(Wire, I2C_SPEED_FAST); //Use default I2C port, 400kHz speed 52 particleSensor.setup(); //Configure sensor with default settings 53 particleSensor.setPulseAmplitudeRed(0x0A); //Turn Red LED to low to indicate sensor is running 54 55} 56 57void loop() { 58 long irValue = particleSensor.getIR(); //Reading the IR value it will permit us to know if there's a finger on the sensor or not 59 //Also detecting a heartbeat 60if(irValue > 7000){ //If a finger is detected 61 display.clearDisplay(); //Clear the display 62 display.drawBitmap(5, 5, logo2_bmp, 24, 21, WHITE); //Draw the first bmp picture (little heart) 63 display.setTextSize(2); //Near it display the average BPM you can display the BPM if you want 64 display.setTextColor(WHITE); 65 display.setCursor(50,0); 66 display.println("BPM"); 67 display.setCursor(50,18); 68 display.println(beatAvg); 69 display.display(); 70 71 if (checkForBeat(irValue) == true) //If a heart beat is detected 72 { 73 display.clearDisplay(); //Clear the display 74 display.drawBitmap(0, 0, logo3_bmp, 32, 32, WHITE); //Draw the second picture (bigger heart) 75 display.setTextSize(2); //And still displays the average BPM 76 display.setTextColor(WHITE); 77 display.setCursor(50,0); 78 display.println("BPM"); 79 display.setCursor(50,18); 80 display.println(beatAvg); 81 display.display(); 82 tone(3,1000); //And tone the buzzer for a 100ms you can reduce it it will be better 83 delay(100); 84 noTone(3); //Deactivate the buzzer to have the effect of a "bip" 85 //We sensed a beat! 86 long delta = millis() - lastBeat; //Measure duration between two beats 87 lastBeat = millis(); 88 89 beatsPerMinute = 60 / (delta / 1000.0); //Calculating the BPM 90 91 if (beatsPerMinute < 255 && beatsPerMinute > 20) //To calculate the average we strore some values (4) then do some math to calculate the average 92 { 93 rates[rateSpot++] = (byte)beatsPerMinute; //Store this reading in the array 94 rateSpot %= RATE_SIZE; //Wrap variable 95 96 //Take average of readings 97 beatAvg = 0; 98 for (byte x = 0 ; x < RATE_SIZE ; x++) 99 beatAvg += rates[x]; 100 beatAvg /= RATE_SIZE; 101 } 102 } 103 104} 105 if (irValue < 7000){ //If no finger is detected it inform the user and put the average BPM to 0 or it will be stored for the next measure 106 beatAvg=0; 107 display.clearDisplay(); 108 display.setTextSize(1); 109 display.setTextColor(WHITE); 110 display.setCursor(30,5); 111 display.println("Please Place "); 112 display.setCursor(30,15); 113 display.println("your finger "); 114 display.display(); 115 noTone(3); 116 } 117 118} 119
SparkFun MAX3010x library
Adafruit SSD1306
Adafruit GFX library
SparkFun MAX3010x library
Adafruit SSD1306
Adafruit GFX library
Downloadable files
Wiring_MAX30102_OLED_Buzzer
Both modules have i²c interface, if you're using a 2 pin buzzer (-) with GND and (+) with a resistor then D3
Wiring_MAX30102_OLED_Buzzer

Wiring_MAX30102_OLED_Buzzer
Both modules have i²c interface, if you're using a 2 pin buzzer (-) with GND and (+) with a resistor then D3
Wiring_MAX30102_OLED_Buzzer

Comments
Only logged in users can leave comments