How to make a Smart Desk Lamp with Audio Function?

This tutorial uses STM32 Nucleo Board to make a smart desk lamp with audio function, the lamp with WS2812B RGB LED strip, the audio module choose DFPlayer Mini MP3 module, use Arduino IDE to write the program, TSL2561 digital light intensity sensor, DS18B20 digital temperature sensor, to achieve according to The necessary sample code is added to automatically adjust the light and sound according to the environmental changes.

General steps to make a smart table lamp with audio function:

  • Design and make a light strip: You can make a light strip using an LED light strip and the corresponding circuit design. You can choose monochrome or colorful LED strips, depending on your personal preference.
  • Choose a sound module: Choose a suitable sound module, such as the DFPlayer Mini MP3 module. This module can play audio files stored on the SD card and has a simple serial control interface for you to control the music playback.
  • Choose a controller: Choose a suitable controller to control the whole smart desk lamp audio system. You can choose to use a controller such as Arduino, Raspberry Pi, or STM32 Nucleo Board.
  • Add sensors: You can add sensors, such as light sensors and temperature sensors so that you can automatically adjust the lights and sound according to environmental changes.
  • Write software: Write software code to control the lights and sound and interact with the sensors. You can use software such as Arduino IDE, Raspberry Pi’s Python programming language, or STMCubeIDE.
  • Complete testing: After completing the configuration and connection of all hardware and software, test to ensure that the entire system works properly.

Required materials

  • STM32 Nucleo Board
  • WS2812B RGB LED strip
  • DFPlayer Mini MP3 module
  • TSL2561 Digital Light Intensity Sensor
  • DS18B20 digital temperature sensor
  • Several DuPont wires
  • Breadboard

Step 1: Design and make the light strip

  1. Select LED strip: Choose the WS2812B RGB LED strip, which is digital and each LED can control its color independently. In addition, its lamp beads are more dense, which can provide more uniform light.
  2. Connect the LED strip: Connect the LED strip to the STM32 Nucleo Board. Connect the DI (digital input) pins of the light strip to the D8 pins of the STM32 Nucleo Board, and connect the power and ground pins to the power and ground wires.
  3. Write a program: Use the Arduino IDE to write a program to control the brightness, color, mode, etc. of the light strip. You can use the FastLED library to control the strip, which provides a large number of lighting effects and control methods.
  4. Test the program: After you finish writing the program, upload the program to STM32 Nucleo Board and connect the light strip and power supply for testing. Test whether the program can correctly control the properties such as color and brightness of the LED strip.

The following is a simple example code that sets the light strip to a rainbow effect.

#include <FastLED.h>
#define LED_PIN     8
#define LED_COUNT  60
CRGB leds[LED_COUNT];
void setup() {
    FastLED.addLeds<WS2812B, LED_PIN, GRB>(leds, LED_COUNT);
}
void loop() {
    // Rainbow cycle
    for(int i=0; i<LED_COUNT; i++) {
        leds[i] = CHSV(i * 256 / LED_COUNT, 255, 255);
    }
    FastLED.show();
    delay(20);
}

Step 2: Add the sound module

  1. Select an audio module: Select the DFPlayer Mini MP3 module, which can play audio files stored on an SD card and has a simple serial port control interface for you to control the music playback.
  2. Connect the audio module: Connect the DFPlayer Mini MP3 module to the STM32 Nucleo Board. Connect the TX pin of the module to the D2 pin of the STM32 Nucleo Board, connect the RX pin to the D3 pin, and connect the power and ground pins to the power and ground wires.
  3. Write a program: Use Arduino IDE to write a program to control the operations such as play and pause of the audio module. You can use the DFPlayer Mini library to make the audio module, which provides a convenient interface and sample code.
  4. Test program: After completing the program writing, upload the program to STM32 Nucleo Board and connect the audio module and power supply for testing. Test whether the program can correctly control the operations such as playing and pausing music.

The following is a simple example code for playing an audio file numbered 1 on an SD card.

#include <SoftwareSerial.h>
#include <DFPlayerMini_Fast.h>
SoftwareSerial mySerial(D2, D3); // RX, TX
DFPlayerMini_Fast myDFPlayer;
void setup() {
    mySerial.begin(9600);
    myDFPlayer.begin(mySerial);
    myDFPlayer.setTimeOut(500);
    myDFPlayer.volume(15); // Set the volume to 15
    myDFPlayer.play(1); // Play the audio file numbered 1 on the SD card
}
void loop() {
    // Do not do anything after playback
}

Step 3: Add light intensity sensor

  1. Select the light intensity sensor: Select the TSL2561 digital light intensity sensor, which can measure the light intensity and output the data as a digital signal.
  2. Connect the light intensity sensor: Connect the TSL2561 digital light intensity sensor to the STM32 Nucleo Board. It needs to be connected to the I2C bus. Connect the SCL pin of the sensor to the D15 pin of the STM32 Nucleo Board, connect the SDA pin to the D14 pin, and connect the power and ground pins to the power and ground wires.
    • The connections are as follows:
    • TSL2561 VCC – 3.3V
    • TSL2561 GND – GND
    • TSL2561 SDA – D14
    • TSL2561 SCL – D15
  3. Install the library file: In the Arduino IDE, we need to install the Adafruit_TSL2561 library, which is a library that facilitates the operation of the TSL2561 sensor. In the menu bar, select “Tools” -> “Library Manager”, then search for “Adafruit TSL2561” and install the library.
  4. Write a program: Use Arduino IDE to write a program to read the data from the light intensity sensor and automatically adjust the brightness of the light according to the light intensity. You can read the sensor data using the Adafruit_TSL2561 library, which provides a convenient interface and sample code.
  5. Test the program: After completing the program, upload the program to the STM32 Nucleo Board and connect the light intensity sensor and power supply for testing. Test whether the program can be read correctly

The following is a simple example code to read the light intensity sensor data and set the light brightness to be proportional to the light intensity.

#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_TSL2561_U.h>
Adafruit_TSL2561_Unified tsl = Adafruit_TSL2561_Unified(TSL2561_ADDR_FLOAT, 12345);
#define LED_PIN     8
#define LED_COUNT  60
CRGB leds[LED_COUNT];
void setup() {
    tsl.begin();
    tsl.enableAutoRange(true);
    tsl.setIntegrationTime(TSL2561_INTEGRATIONTIME_13MS);
    pinMode(LED_PIN, OUTPUT);
}
void loop() {
    sensors_event_t event;
    tsl.getEvent(&event);
    if (event.light) {
        int brightness = event.light / 10;
        FastLED.setBrightness(brightness);
    }
    delay(100);
}

This will adjust the brightness of the light according to the value of the light intensity. The light brightness will decrease when the ambient light intensity is low and will increase when the ambient light intensity is high.

After completing the above steps, upload the code to the Nucleo board and check the output of the serial monitor. You should be able to see the value of the light intensity and the lights will adjust as the light intensity changes.

Step 4: Write the program

We can use Arduino IDE to write the program, the following are the main functions of the program.

  1. Read the data from TSL2561 digital light intensity sensor and DS18B20 digital temperature sensor.
  2. Automatically adjust the light and sound according to the light intensity and temperature value of the environment. Turn on the lights when the light intensity is below a certain value and play music at the same time; turn off the lights and music when the temperature is above a certain value.
  3. Control the color and brightness of the WS2812B RGB LED strip. We can use the Adafruit NeoPixel library to control the light strip.
  4. Control the playback of the DFPlayer Mini MP3 module. We can use the DFPlayer Mini library to control the MP3 module.

The following is the main code of the program.

#include <OneWire.h>
#include <DallasTemperature.h>
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_TSL2561_U.h>
#include <DFMiniMp3.h>
#include <Adafruit_NeoPixel.h>
#define ONE_WIRE_BUS A0
#define NUM_PIXELS 60
#define PIXEL_PIN 10
#define LIGHT_THRESHOLD 150
#define TEMP_THRESHOLD 28
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUM_PIXELS, PIXEL_PIN, NEO_GRB + NEO_KHZ800);
DFMiniMp3 mp3;
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
DeviceAddress insideThermometer;
Adafruit_TSL2561_Unified tsl = Adafruit_TSL2561_Unified(TSL2561_ADDR_FLOAT);
void setup()
{
  sensors.begin();
  pixels.begin();
  tsl.begin();
  if (!tsl.begin())
  {
    Serial.println("TSL2561 not found");
    while (1);
  }
  if (!mp3.begin())
  {
    Serial.println("DFPlayer Mini not found");
    while (true);
  }
  mp3.volume(20);
}
void loop()
{
  sensors.requestTemperatures();
  float temperature = sensors.getTempC(insideThermometer);
  sensors.getAddress(insideThermometer, 0);
  uint16_t lux = getLux();
  if (lux < LIGHT_THRESHOLD && temperature < TEMP_THRESHOLD)
  {
    setLights(255, 255, 255, 200);
    playMusic();
  }
  else
  {
    setLights(0, 0, 0, 0);
    stopMusic();
  }
  delay(1000);
}
uint16_t getLux()
{
  sensors_event_t event;
  tsl.getEvent(&event);
  if (event.light)
  {
    uint16_t lux = tsl.calculateLux(event.light);
    return lux;
  }
  else
  {
    return 0;
  }
}
void setLights(uint8_t red, uint8_t green, uint8_t blue, uint8_t brightness)
{
  for (int i = 0; i < NUM_PIXELS; i++)
  {
    pixels.setPixelColor(i, pixels.Color(red, green, blue));
    pixels.setBrightness(brightness);
    pixels.show();
  }
}
void playMusic()
{
  mp3.play(1);
}
void stopMusic()
{
  mp3.stop();
}

In the above program, we use several libraries: the OneWire and DallasTemperature libraries are used to read the data from the DS18B20 digital temperature sensor. Also, the Wire and Adafruit_TSL2561 libraries are used to read the TSL2561 sensor.

Step 5: Testing

After all hardware and software configurations and connections have been made, tests can be performed to ensure the system is working properly.
Check the serial output through the serial monitor to ensure that the system is starting up properly and that all sensors are reading data correctly.

  1. Cover the TSL2561 sensor with your hand to ensure that the light strip is properly brightened and then remove your hand to ensure that the light strip is properly reduced in brightness.
  2. Place the DS18B20 sensor in ice water to ensure that the color and sound of the light strip and audio change accordingly.
  3. Try connecting the Bluetooth module of the DFPlayer Mini MP3 module using a cell phone or other device and play different music files. Make sure the sound module can play music properly and the sound can be controlled by adjusting the volume buttons.
  4. Try to use the buttons on the STM32 Nucleo Board to switch between different music files and observe the color and sound changes of the light strip and the sound.

Leave a Comment

Your email address will not be published. Required fields are marked *

en_USEnglish
Scroll to Top