Open nonsintetic opened 5 years ago
I guess the ESP analog library then has a problem. Because on AVR works, so the calculation must be correct. I think I cannot do anything about this issue. You'd have to debug the analogread output.
I guess the ESP analog library then has a problem. Because on AVR works, so the calculation must be correct. I think I cannot do anything about this issue. You'd have to debug the analogread output.<
I also get this issue with my ESP8266 , but this only happens when I use the 8 bit resolution. When I use the 10 bit resolution, the input gets maxed out when it's louder rather than going to 0.
I figured it out why the audio sets back to zero when it's louder in 8 bit. The reason is because one cannot just define a variable that is usually in the range 0 - 1023 to 8 bit. If you want to get the input as 8 bit, you have to map from 10 bit to 8 bit and this will prevent the data from going back to zero in loud frequencies.
I figured out the same thing eventually. Due to time restraints I just modified the needed functions from the library to tolerate 10 bit values and it worked fine. I don't have a pull request because I just fixed the small part I used.
@nonsintetic what part of the code you changed?
Sadly I just took the parts I needed and put them into my code, I didn't have time to change the library or I would have done a pull request. Here are the relevant bits in what I'm using and work just fine on the ESP8266:
#define MSGEQ7_SMOOTH 200 //0-1024
#define ReadsPerSecond(f) (1000000UL / (f))
#define MSGEQ7_FPS ReadsPerSecond(50)
int bands[7];
void loop() {
currentMicros = micros();
if( (currentMicros - lastSample) > MSGEQ7_FPS) {
readMSGEQ7();
lastSample = currentMicros; //store time of last sample
}
}
void initMSGEQ7() {
pinMode(RESET_PIN, OUTPUT);
pinMode(STROBE_PIN, OUTPUT);
digitalWrite(RESET_PIN,LOW);
digitalWrite(STROBE_PIN,HIGH);
}
void readMSGEQ7() {
for(int band = 0; band < 7; band++) {
digitalWrite(STROBE_PIN,HIGH);
digitalWrite(STROBE_PIN,LOW);
delayMicroseconds(40); // allow MSGEQ7 time to settle
int val = analogRead(A0); //read ADC
//smooth
bands[band] = int( bands[band] * MSGEQ7_SMOOTH + val * (1024 - MSGEQ7_SMOOTH) ) / 1024;
delayMicroseconds(400); //allow ADC to settle
}
}
Sadly I just took the parts I needed and put them into my code, I didn't have time to change the library or I would have done a pull request. Here are the relevant bits in what I'm using and work just fine on the ESP8266:
#define MSGEQ7_SMOOTH 200 //0-1024 #define ReadsPerSecond(f) (1000000UL / (f)) #define MSGEQ7_FPS ReadsPerSecond(50) int bands[7]; void loop() { currentMicros = micros(); if( (currentMicros - lastSample) > MSGEQ7_FPS) { readMSGEQ7(); lastSample = currentMicros; //store time of last sample } } void initMSGEQ7() { pinMode(RESET_PIN, OUTPUT); pinMode(STROBE_PIN, OUTPUT); digitalWrite(RESET_PIN,LOW); digitalWrite(STROBE_PIN,HIGH); } void readMSGEQ7() { for(int band = 0; band < 7; band++) { digitalWrite(STROBE_PIN,HIGH); digitalWrite(STROBE_PIN,LOW); delayMicroseconds(40); // allow MSGEQ7 time to settle int val = analogRead(A0); //read ADC //smooth bands[band] = int( bands[band] * MSGEQ7_SMOOTH + val * (1024 - MSGEQ7_SMOOTH) ) / 1024; delayMicroseconds(400); //allow ADC to settle } }
I will make a pull request soon and thank you!
The issue was because smoothing is on and smoothing is not supported with 10 bit.
Maybe you could prepare a PR with a fix?
Maybe you could prepare a PR with a fix?
Created a PR!
Ouuups. The PR was not merged yet.
I currently do not have an MSGEQ7 to test with, but last thing I remember is that analogRead(int adc)
was returning values from 0-1024 and not 0-1023 which the algorithm in the system caused it to return 0 when the sound is loud.
Using this library with an ESP8266 (just a ESP12 module and passives). When the output from the MSGEQ7 is at max (3.3v) the library reads 0 for that band until the level is lower. I checked the output from the chip on the scope and it doesn't go low or anything, it looks as I would expect.
Sketch:
Output on tapping the mic:
- yellow is strobe pin, red is ADC pin