Counting Bubbles


I am brewing beer and I want to keep track of the bubbles that from yeast. I have a mic strapped to airlock(where bubble are). I want to see that how many bubbles were the in past minute. Also want to keep histogram.

My question is how should I do that. I want to count bubbles/minute and update it once in 30 seconds or a minute or so. So I wont DDoS the server.

I am a begginner in coding so sorry for that. My code is not working. Actually it is working but when mic digital pin is high for at least 1 second or more. I want it to gather every bubble sound as +1 for counter.

int micState = 0;
int lastMicState = 0;
char auth[] = "****";

SimpleTimer timer;

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "****";
char pass[] = "*****";

void setup()
  Blynk.begin(auth, ssid, pass);
  timer.setInterval(2000L, sendUptime);
  timer.setInterval(1000L, bubble);


  pinMode(led, OUTPUT);
  pinMode(mic, INPUT);
  digitalWrite(led, LOW);


void sendUptime()
  bla bla bla

void bubble()
  micState = digitalRead(mic);

  if (micState != lastMicState)
    if (micState == HIGH)
      digitalWrite(led, HIGH);
      digitalWrite(led, LOW);

    lastMicState = micState;

  Blynk.virtualWrite(V1, bubbleCounter);


void loop()

I tried without blynk, in the loop. It worked. How do I do with blynk. And I know it is not bubble/minute yet. It will be. Thanks.

first of all, please format your code

It formatted in IDE. But let me correct here.


well, i you do not know what i mean, you also have more homework to do…
read this:
[README] Welcome to Blynk Community!

ok, it looks human readable now :wink:

so, what do you monitoring exactely with the mic?
it generates a “high” signal for every bubble and you want to count these high signals?


then the best method for this, even for blynk or without blynk, is to use interrupt.

google “arduino interrupts” and read on
what mcu are you using?

esp8266 - nodemcu v3

i would try something like this:

#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <ESP8266mDNS.h>
#include <WiFiUdp.h>
#include <ArduinoOTA.h>

unsigned long bubbles;

char auth[] = "YourAuthToken";
char ssid[] = "YourNetworkName";
char pass[] = "YourPassword";

BlynkTimer timer;

void setup()
  Blynk.begin(auth, ssid, pass);

  timer.setInterval(60000L, bubbleTimer);

  attachInterrupt(4, bubbleCounter, RISING);  // d2 pin. here comes the mic signal + a ~10k pulldown (try lower / higher value if not working)

void loop()
  ArduinoOTA.handle();              // handles over the air code uploading;;

void bubbleTimer()                  // one minute timer function
  Blynk.virtualWrite(0, bubbles);   // sends to virtual pin 0 the counted bubbles, every minute
  bubbles = 0;                      // then resets counter value

void bubbleCounter()                // interrupt isr
  bubbles++;                        // increments value for every mic "high" signal
1 Like

Thank you very much. I will try now.

ok. please report back how it goes. the value of the pullup probably will be very important, because it should be strong enough to keep the pin low to prevent floating when no signal is coming, but in the same time has to be weak enough, to allow for the mic signal (which is probably very weak), to pull it high.

if you could have an oscilloscope, would be very helpful, to see exactly what is going on…

I am using mic module with pot attached. So It will be very easy.

by the way, the mic outputs ac, not dc. how are you converting that into dc signal for the mcu?

the other thing: how you deal with ambient noise? to hear a bubble there should be perfect silence…
i’m very interested.

I changed interval to 5000. It shows 176 and 264 one by one continuously. I dont understand why. The pot on the mic does not effect the result.

AlsoI am using a mic module. Image here. And I strap it to airlock(bubbler). The led on the mic lights up as it bubbles but I cant count it correctly. :confused:

ok, do not panic :slight_smile:

  1. what is the value of the pulldown resistor you added?
  2. if you remove the mic from d2, the numbers remain 0?
  3. the mic on the picture has analog and digital output. for interrupt you should use the digital output, but you have to set the threshold level with the trimpot first
  1. 10k
  2. It goes crazy. Random numbers.
  3. I set the threshold and I’ve connected it with digital out.

And I have an idea. Is there a way that I can measure the time between two bubbles and change it the bubble/minute than send it to blynk without ddos’ing.

For example For example if there is 400ms between two bubbles it would be.60000/400= 150 bubbles/minute.

if it goes crazy, than i would be courious exactely how you added that pull down resistor…

it should be between d2 pin and gnd.

yes, it is possible to measure time between bubbles, but it will have very low precision for bubbles per minute… first i would recommend to leave the code exactely as i wrote, because that should be correct.

first you need a stable hardware.

please double check your wireing. and first test it without mic.

only the nodemcu with the pulldown. it should display 0 all the time.

if that works, take a jumper wire, hook up to 3.3v, and touch the d2 pin. you should see the numbers incrementing in the app.

if that works too, you can connect the mic and see what happens.

@erolcanulutas, sorry, i think i made a mistake in the code. you should replace the 4 in attacheinterrupt to 2, and hook up the pulldown resistor and mic to d4 pin, instead of d2. sorry.

please try it this way, and report what happens.

I didnt disconnect mic side, I disconnected esp side. let me try other way while resistor still doing its job.

I tried and it’s always zero.

“if that works, take a jumper wire, hook up to 3.3v, and touch the d2 pin. you should see the numbers incrementing in the app.”

Yeah, I saw the incrementing numbers but one touch increments 3, 3 touch 11. like that. Than connected the mic. similar, sometimes 0 sometimes sometimes correct number sometimes 2 while it should be 6 or so.

Also there was no mistake with pins, gpio04 is d2, gpio02 is d4 and d4 is tx so it would be better if use d2 which is gpio04

And yes I tried.