Making a Voice-activated Desk Lamp with Arduino Nano

This article is a tutorial to make a voice-activated desk lamp with Arduino Nano, including a voice-activated switch, brightness adjustment, color adjustment, scenario mode, and timer function.

Materials needed

  • Arduino Nano control board
  • Software: Arduino IDE
  • One LED table lamp or LED strip light
  • A sound sensor module
  • One potentiometer
  • One RGB LED bead
  • DuPont wire (female-female, female-male, male-male)
  • One breadboard

Wiring Steps

  1. Plug the Arduino Nano into the breadboard, then plug the sound sensor module into the breadboard and connect them through the duplex cable. Connect VCC of the sound sensor to 5V, GND to GND, and AO (analog output) to A0.
  2. Plug the potentiometer into the breadboard and connect one of the terminals to 5V, the other terminal to GND, and the middle terminal to A1.
  3. Plug the RGB LED bead into the breadboard and connect its red pin to D9, green pin to D10, blue pin to D11, connect the GND pin to GND, and connect the positive terminal to 5V.
  4. Connect the LED desk lamp or LED strip to D5, connect the GND pin to GND, and connect the positive terminal to 5V.

Programming Steps

  • Open the Arduino IDE software and create a new file. First, you need to introduce three library files.
#include <Wire.h>
#include <Adafruit_NeoPixel.h>
#include <SoftwareSerial.h>
  • Set the pins of RGB beads
#define PIN 6
#define NUMPIXELS 1
Adafruit_NeoPixel pixels(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
  • Setting the pins of the sound sensor and potentiometer
const int soundSensorPin = A0; 
const int potPin = A1; 
  • Setting timer variables and scenario variables
unsigned long previousMillis = 0;
const long interval = 5000;
int mode = 0;
  • Set the brightness value and color value
int brightness = 100;
int red = 255;
int green = 0;
int blue = 0;
  • Define a function to control the light switch
void toggleLight() {
  if (brightness > 0) {
    pixels.setBrightness(0);
    brightness = 0;
  } else {
    pixels.setBrightness(255);
    brightness = 255;
  }
  pixels.show();
}
  • Define a function to adjust the brightness of the light
void adjustBrightness() {
  int value =analogRead(potPin);
brightness = map(value, 0, 1023, 0, 255);
pixels.setBrightness(brightness);
pixels.show();
}
  • Define a function to adjust the color of the light
void adjustColor() {
  pixels.setPixelColor(0, pixels.Color(red, green, blue));
  pixels.show();
}
  • Define a function to switch between scenario modes
void toggleMode() {
  mode++;
  if (mode > 2) {
    mode = 0;
  }
  switch (mode) {
    case 0:
      red = 255;
      green = 0;
      blue = 0;
      adjustColor();
      break;
    case 1:
      red = 0;
      green = 255;
      blue = 0;
      adjustColor();
      break;
    case 2:
      red = 0;
      green = 0;
      blue = 255;
      adjustColor();
      break;
  }
}
  • Set the setup function to initialize the program
void setup() {
  pixels.begin();
  pixels.setBrightness(255);
  toggleLight();
  Serial.begin(9600);
}

Finally, we need to define a function that will control the profile mode. This function will set different colors and brightness depending on the profile mode. The following is a code example.

void setMode(int mode) {
  switch (mode) {
    case 0:
      red = 255;
      green = 0;
      blue = 0;
      brightness = 100;
      break;
    case 1:
      red = 0;
      green = 255;
      blue = 0;
      brightness = 100;
      break;
    case 2:
      red = 0;
      green = 0;
      blue = 255;
      brightness = 100;
      break;
    case 3:
      red = 255;
      green = 255;
      blue = 0;
      brightness = 150;
      break;
    case 4:
      red = 255;
      green = 0;
      blue = 255;
      brightness = 150;
      break;
    default:
      break;
  }
  pixels.setPixelColor(0, pixels.Color(red * brightness / 255, green * brightness / 255, blue * brightness / 255));
  pixels.setBrightness(brightness);
  pixels.show();
}

This code will set different RGB values and brightness values respectively according to the passed-in mode parameters. In the main loop, we can call this function to set the scenario mode.

Leave a Comment

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

en_USEnglish
Scroll to Top