Sound-activated Counting Light

by Lan_Makerfabs in Circuits > Arduino

207 Views, 0 Favorites, 0 Comments

Sound-activated Counting Light

QQ截图20240120144850.png

This is a guide for new of esp32 and arduino, in this article, I will share how to make a sound-activated counting light.

Do you get one esp32 board but don't know how to begin? Here is a detailed tour to get you started. We will read the level of the sound sensor through GPIO, every time the sound is detected, the count will be increased by one, the range is from 1 to 120.We will control the WS2812 LEDs through GPIO, each count corresponds to a state. Connect the OLED display via I2C and print the count information on it.

Supplies

maesp-esp32-dev-kit.jpg
QQ截图20240119170415 - 副本.png
QQ截图20240120154245.png

In this demo, a sound module, a WS2812 module, and a MaESP ESP32 OLED are needed, and they all ccan find in MaESP dev kit.

  • Sound module

Sound module is the most sensitive to the environment sound intensity, commonly used to detect the intensity of the sound of the surroundings.

This sensor has three pins, two of which are power pins leveled VCC and GND and the other one is digital pin. It has an onboard power LED and a signal LED. The power LED turns on when power is applied to the board and the signal LED turns on when the circuit is triggered. This board also has a comparator Op-amp that is responsible for converting the incoming analog signal to digital signal. We also have a sensitivity adjustment potentiometer; with that, we can adjust the sensitivity of the device. Last, we have the condenser microphone that is used to detect the sound. All these together make the total Sound Sensor Module.

When module in the intensity of the sound environment can not reach set threshold, the OUT will output high level, when the intensity of the sound from the outside environment more than set threshold, the module OUT output low level;

Small digital output board OUT can be directly to the microcontroller, through single chip microcomputer to detect the high and low level, thus to detect sound environment;

  • WS2812 module

The WS2812B RGB LED Module is a small module with BIG color. The module has a special LED on board that contains three separate LEDs – red, green, and blue – as well as a smart control IC that can individually drive each LED. Each color has 256 intensity levels which allows the module to produce 24-bit color, or more than 16 million colors. 

Each module is instructed by using a special serial protocol. This protocol allows many modules to be daisy-chained together so that one microcontroller can control the whole lot – with a single data signal. 

Any number of these modules can be chained together by connecting one module’s data-out (DO) pin to another’s data-in (DI) pin.

In the attached document, you can know how it work.

MaESP ESP32 OLED is a perfect board for beginners, the back of the board is labelled with the multiplexed functions of each pin,it is easy to learn& use. In this demo, we will use two GPIO and one I2C of it. And in the attached document, you can cheak the datasheet of ESP32, and there are more other demos you can refer.

This board has detailed guide/demos both in MicroPython/Arduino, so the learners can step into ESP32 easily. There all the 12 experiments in the guide are achieved by both MicroPython/Arduino, so get one our MaESP dev kit, to start your ESP32 word in 20 hours.

  • Software support: Arduino

Hardware

IMG_20240119_151439.jpg

Connect the sound module and ws2812 module to the ESP32 OLED, VCC to VCC, GND to GND, and DI pin of ws2812 and OUT pin of sound module to GPIO pins which you difined in the code, and note, in this demo, it need to use pin4(SDA) and pin5(SCL) for OLED, so we can't use them again in other device.

Software

QQ截图20240119140329.png
  • At first, we need to prepare the environment for ESP32 development board programming in arduino, and you can cheak this guide.
  • secondly,Install the libraries and files which will be used

This is a library for our Monochrome OLEDs based on SSD1306 drivers.

#include <Adafruit_SSD1306.h>

This library allows you to communicate with I2C devices (the OLED is connected by I2C)

#include <Wire.h>

The Adafruit_GFX library provides a common syntax and set of graphics functions for all of our LCD and OLED displays and LED matrices.

#include <Adafruit_GFX.h>

Arduino library for controlling single-wire-based LED pixels and strip.

#include <Adafruit_NeoPixel.h>
  • Difine your pins, this determines which pin of the board your device should be connected to.
#define LED 3   //the DI pin of ws2812 connect to GPIO3
#define voice 13 //the OUT pin of sound module connect to GPIO13
#define NUM_LEDS 16 //the maximum number of LEDs light up
#define SCREEN_ADDRESS 0x3c // Address 0x3C for 128x32
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define OLED_RESET -1    // Reset pin # (or -1 if sharing Arduino reset pin)
#define I2C_SDA 4 //the SDA pin connect to OLED
#define I2C_SCL 5 //the SCLpin connect to OLED
  • Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
  •  Declare our NeoPixel strip object
Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUM_LEDS, LED, NEO_GRB + NEO_KHZ800);
  • Initialise your devices so that they work properly
    pinMode(voice, INPUT);  //Set the "voice" pin as an input.
    strip.begin();
    strip.setBrightness(60);
    strip.clear();
    strip.show(); //Initialising the ws2812 state
    
    Serial.begin(115200);

//Initialise I2C pins
    Wire.begin(I2C_SDA, I2C_SCL);

 // SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
    if (!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
        Serial.println(F("SSD1306 allocation failed"));
        for (;;); // Don't proceed, loop forever
    }
  • Record the number of times the sound is detected by the sound sensor,set to record up to 120 times
      if(digitalRead(voice) == 0){
       count++;
        if (count>120){
          count = 1;               
        }     
     }     
  • Adjust the number and color of ws2812 lights according to the count.
void light() {

//The ws2812 light implements a single-wire protocol, and the numbering starts at 0, not 1, so we're going to have to make some changes

color = (count-1)/12; //12 counts for a round, each round has a different colour
number = (count-1)%12; //Number of lights on in the round

    switch (color)
    {
       case 0:
        color_show( number, 255, 255, 255); //white
        break;
    case 1:
        color_show( number, 0, 0, 255); //blue
        break;
    case 2:
        color_show( number, 0, 255, 0); //green
        break;
    case 3:
        color_show( number, 0, 255, 255); //greenish blue
        break;
    case 4:
        color_show( number, 255, 0, 0); //red
        break;
    case 5:
        color_show( number, 255, 0, 255); //yellow
        break;
    case 6:
        color_show( number, 255, 255, 0); //purple
        break;
    case 7:
        color_show( number, 255, 127, 127); //light red
        break;        
    case 8:
        color_show( number, 127, 255, 127); //light green
        break;
    case 9:
        color_show( number, 127, 127, 255); //light blue
        break;

    default:
        color_show( number, 0, 0, 0); //black
        return;
    }

}


//The ws2812 light implements a single-wire protocol,We need to define the colours for each light
 // pixels.Color() takes RGB values, from 0,0,0 up to 255,255,255
// pixels.setPixelColor(i, pixels.Color(0, 150, 0));

void color_show(int number, int red, int green, int blue){
  for (int i = 0; i <NUM_LEDS - 4; i++){
    if (i <= number){
      strip.setPixelColor(i, red, green, blue);
    }
    else{
      strip.setPixelColor(i, 0, 0, 0);  
    }
  }
}

  • Lights on, and the OLED displays counting information
    display.clearDisplay();   // Clear the display buffer
    display.setCursor(10, 10); //Where to start display
    display.setTextColor(WHITE); // Draw white text
    display.setTextSize(1); // Normal 1:1 pixel scale
    display.println("Makerfabs"); //what you want display in the OLED
    display.setCursor(10, 25);
    display.setTextColor(WHITE);
    display.setTextSize(1);
    display.println("MaESP ESP32 Dev Kit");
    display.setCursor(10, 40);
    display.setTextColor(WHITE);
    display.setTextSize(1);
    display.println("count:" + String(count));
    display.display(); // actually display all of the above
    strip.show();  // light on strip
    delay(200);

The raw code you can cheak attached file.

Result

How to make a sound activated counting light by ESP32

As you can see in the vedio, when the sound sensor detects the sound, the "count" will increase, and the light ring will emit different colors of light according to the change of the number of “count”, 120 numbers for a round.

If you are intersted in it, you can try doing it yourself.