Db reading not the same

Hi all
I am working on me bee hive monitor and am happy so far with what I have achieved.
But a but stumped on this one.
When I load my microphone sketch by its self I get good reading from the mic (if a little high) but when I include the code in my temp and humidity sketch, I get substantially higher readings for the mic.

Attached is the code and screenshots of the readings
Code for Microphone sketch

const int sampleWindow = 50; // Sample window width in mS (50 mS = 20Hz)
unsigned int sample;

void setup()
{
   Serial.begin(9600);
}


void loop()
{
   unsigned long startMillis= millis();  // Start of sample window
   unsigned int peakToPeak = 0;   // peak-to-peak level

   unsigned int signalMax = 0;
   unsigned int signalMin = 1024;

   // collect data for 50 mS
   while (millis() - startMillis < sampleWindow)
   {
      sample = analogRead(A0);
      if (sample < 1024)  // toss out spurious readings
      {
         if (sample > signalMax)
         {
            signalMax = sample;  // save just the max levels
         }
         else if (sample < signalMin)
         {
            signalMin = sample;  // save just the min levels
         }
      }
   }
   peakToPeak = signalMax - signalMin;  // max - min = peak-peak amplitude
   double volts = ((peakToPeak * 3.3) / 1024) * 0.707;  // convert to volts
   double first = log10(volts/0.00631)*20; // The microphone sensitivity is -44 ±2 so V RMS / PA is 0.00631
   double second = first + 94 - 44; // The microphone sensitivity is -44 ±2
   Serial.println("Db "+String(second));
}

Code for temp and humidity sketch

[code]
/*
 * Rui Santos 
 * Complete Project Details https://randomnerdtutorials.com
 */

// Include the libraries we need
#include <OneWire.h>
#include <DallasTemperature.h>
#include "DHT.h"

#define DHTPIN 4     // Digital pin connected to the DHT sensor


#define DHTTYPE DHT22   // DHT 22  (AM2302), AM2321

#define ONE_WIRE_BUS 15 // Data wire is connected to GPIO15


// Setup a oneWire instance to communicate with a OneWire device
OneWire oneWire(ONE_WIRE_BUS);
// Pass our oneWire reference to Dallas Temperature sensor 
DallasTemperature sensors(&oneWire);

DeviceAddress sensor1 = { 0x28, 0x6A, 0x48, 0x7C, 0x0C, 0x00, 0x00, 0xC4 };
DeviceAddress sensor2 = { 0x28, 0xFA, 0x14, 0x7B, 0x0C, 0x00, 0x00, 0x47 };
//DeviceAddress sensor3= { 0x28, 0xFF, 0xA0, 0x11, 0x33, 0x17, 0x3, 0x96 };

DHT dht(DHTPIN, DHTTYPE);

const int sampleWindow = 50; // Sample window width in mS (50 mS = 20Hz)
unsigned int sample;

void setup(void)
{
  Serial.begin(9600);
  sensors.begin();
  dht.begin();
}

void loop(void){ 
  //Serial.print("Requesting temperatures...");
  sensors.requestTemperatures(); // Send the command to get temperatures
 // Serial.println("DONE");
  
  Serial.print("Brood Box: ");
  Serial.print(sensors.getTempC(sensor1)); 
  Serial.print("°C:    ");
  
 
  Serial.print("Flow Super: ");
  Serial.print(sensors.getTempC(sensor2)); 
  Serial.print("°C:    ");

   // Reading temperature or humidity takes about 250 milliseconds!
  // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
  float h = dht.readHumidity();
  // Read temperature as Celsius (the default)
  float t = dht.readTemperature();
  

  // Check if any reads failed and exit early (to try again).
  if (isnan(h) || isnan(t)) {
    Serial.println(F("Failed to read from DHT sensor!"));
    return;
  }

  Serial.print(F("Roof Humidity: "));
  Serial.print(h);
  Serial.print(F("%   Roof Temperature: "));
  Serial.print(t);
  Serial.print(F("°C   "));

{
   unsigned long startMillis= millis();  // Start of sample window
   unsigned int peakToPeak = 0;   // peak-to-peak level

   unsigned int signalMax = 0;
   unsigned int signalMin = 1024;

   // collect data for 50 mS
   while (millis() - startMillis < sampleWindow)
   {
      sample = analogRead(A0);
      if (sample < 1024)  // toss out spurious readings
      {
         if (sample > signalMax)
         {
            signalMax = sample;  // save just the max levels
         }
         else if (sample < signalMin)
         {
            signalMin = sample;  // save just the min levels
         }
      }
   }
   peakToPeak = signalMax - signalMin;  // max - min = peak-peak amplitude
   double volts = ((peakToPeak * 3.3) / 1024) * 0.707;  // convert to volts
   double first = log10(volts/0.00631)*20; // The microphone sensitivity is -44 ±2 so V RMS / PA is 0.00631
   double second = first + 94 - 44; // The microphone sensitivity is -44 ±2
   Serial.println(" Db "+String(second));
}

  Serial.print('\n'); 
  delay(2000);
}

[/code]

I'm not sure what those libraries are doing but you might try a DC voltage (probably with a voltage divider) and just print-out the raw ADC readings to see what's happening and to see if that additional code is throwing-off your readings.

What are you using for a microphone? Are you getting "reasonable" raw ADC readings with (near) silence loud sounds? Are you using a microphone board or a raw mic element? (0.00631V is way too low to be useful without amplification.)

Do you have an SPL meter to check your calibration? Real SPL meters are calibrated, not "calculated". Real SPL meters are also usually time-averaged and A-weighted so it's hard to make one that's calibrated the same as the real one for all kinds of sounds.... But you should be able to calibrate it with a tone and the sound spectrum from a beehive should be consistent enough to get usable readings.

Or, maybe it's not that important as long as you are getting loudness readings and know what a normal reading is from your particular setup...

The .707 factor is for peak-to-RMS conversion (of a sine wave). You've got peak-to-peak voltage so your factor should be .354. Or, you don't even need to make that conversion know that if you know what the peak-to-peak is at your reference dB level.

And, there's no need to make a voltage conversion/calculation. You just need to calculate or measure to find a reference ADC reading and then calculate dB from your peak-to-peak ADC readings.

The microphone sensitivity is -44 ±2

I'm not sure what that means... Typically a microphone spec is something like 10mV @ 94dBSPL, and then there's a preamp that has to be included in the calculation.

There's probably digital crosstalk getting into the microphone signal from the OneWire and DHT
signals?

I used the same wiring setup for both sketches. But when I removed the DHT data wire from the NodeMCU is got this. No DHT but also no Mic input

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.