Hackster is hosting Hackster Holidays, Finale: Livestream & Giveaway Drawing. Watch previous episodes or stream live on Tuesday!Stream Hackster Holidays, Finale on Tuesday!
Iain Nash
Published © MIT

Personal Hearing Safety Canary

It can be hard to measure environments that can affect your hearing over time. This wearable monitors environmental sound levels.

IntermediateFull instructions provided5 hours711
Personal Hearing Safety Canary

Things used in this project

Hardware components

MAX32630FTHR
Maxim Integrated MAX32630FTHR
×1
Pmod MIC3
Digilent Pmod MIC3
×1
Vibration Motor
Used as an alert for the user when the ambient sound is really loud
×1
General Purpose Transistor PNP
General Purpose Transistor PNP
Used to switch the motor from GPIO pins.
×1
3.7 V LiPo Battery
Can be a generic lipo with a standard mini JST connector.
×1

Story

Read more

Schematics

Connections to Dev Board

Code

main.cpp

C/C++
This is the main code from the Arm MBED Ide, using the mbed-os and the MAX77650 library.
#include "mbed.h"
#include "MAX77650.h"

PwmOut redLed(LED_RED);
PwmOut grnLed(LED_GREEN);
PwmOut bluLed(LED_BLUE);


#define HAS_VIBRATION
#define DEBUG

#define CALIB_MAX 3000


#ifdef HAS_VIBRATION
PwmOut vibrationMotor(P1_5);
#endif

#ifdef DEBUG
Serial pc(USBTX, USBRX); // tx, rx
#endif

// I2C master 2
I2C i2c2(I2C2_SDA, I2C2_SCL);

// MAX77650 PMIC and Charger
MAX77650 max77650(i2c2);

// mosi/miso/sclk/ssel
SPI spi(P1_1, P1_2, P1_0);
DigitalOut cs(P1_3);

int stateOfCharge;
float longAvg = 0.0;
float medAvg = 0.0;
float shortAvg = 0.0;
long runsOk = 0;
long cycles = 0;
Timer lastVibration;

void updateLeds() {
    if (longAvg > CALIB_MAX) {
        if (lastVibration.read() > 60*5) { // 5 mins
          vibrationMotor.write(0.4f);
          wait(0.5);
          vibrationMotor.write(0.9f);
          wait(0.3);
          vibrationMotor.write(0.0f);
          lastVibration.reset();
        }
        redLed = 1;
    } else {
        redLed = 0;
    }
}

// PMic Led Data
static char ledData[] = {
    MAX77650::CNFG_LED0_A,
    0x04, 0x04, 0x44,
    0x0F, 0x0F, 0x0F,
    0x01,
};

// main() runs in its own thread in the OS
int main() {
    //pc.baud(19200);
    // Turn off Low-Dropout Linear Regulator (LDO); unused on MAX32620FTHR platform
    max77650.disableLDO();
    
    lastVibration.start();
    vibrationMotor.write(0.0f);
    
    // Set SIMO Buck-Boost Regulator 2 target voltage; provides VDDIOH
    max77650.setSBB2Voltage(3.3f);
    
    // Chip must be deselected
    cs = 1;
 
    // Setup the spi for 8 bit data, high steady state clock,
    // second edge capture, with a 1MHz clock rate
    spi.format(8,3);
    spi.frequency(2000000);
 
    // Select the device by seting chip select low
    cs = 0;
    
    max77650.writeReg(ledData, sizeof(ledData));
    
    
    while (true) {
        cs = 0;
        /* Combine bytes into the single package */
        uint16_t package = ((spi.write(0) << 8) | spi.write(0)); 
        package &= 0x0fff;
        
        shortAvg = shortAvg * 0.5 + package * 0.5;
        
        if (cycles % 5) {
            medAvg = medAvg * 0.7 + shortAvg * 0.3;
        }
        if (cycles % 10) {
            longAvg = longAvg * 0.9 + medAvg * 0.1;
            bluLed.write(medAvg / 4000);
        }
        
        if (runsOk < 1000000) {
            runsOk += 1;
            if (cycles % 10000) {
                redLed = !redLed;
            }
        } else {
            if (cycles % 10) {
                updateLeds();
            }
        }
        
        if (cycles % 100000) {
            max17055.reportSOC(&stateOfCharge);
            if (stateOfCharge < 51.0) {
                ledData[0] = 0x44;
            } else {
                ledData[0] = 0x04;
            }
            max77650.writeReg(ledData, sizeof(ledData));
        }
        
        if (cycles % 1000) {
            // actual data out from mic
#ifdef DEBUG
            pc.printf("d:%d,am:%f,al:%f\n", package, medAvg, longAvg);
#endif

            grnLed.write(medAvg / 4000);
        }
        cycles++;
      
        /* close the SPI conversation correctly */
        cs = 1;
    }
}

Credits

Iain Nash
3 projects • 0 followers

Comments