Skip to Content

NIGHTSTAND TOUCH LAMP

The Nightstand Touch Lamp is an innovative and user-friendly bedside lamp designed to replace traditional switches with a touch-sensitive base or body, allowing for seamless control through simple touch interactions. This modern lamp enables users to effortlessly turn the lamp on or off, as well as adjust its brightness levels, all with just a gentle touch, eliminating the hassle of fumbling for physical switches in the dark. At the heart of this project lies a capacitive touch sensor, which detects touch inputs and sends corresponding signals to an Arduino microcontroller, serving as the brain of the system. The Arduino processes these signals and controls a dimmable LED, adjusting its brightness or toggling its state based on the user's touch. By combining these components, the Nightstand Touch Lamp offers a sleek, functional, and intuitive lighting solution that enhances convenience and modernizes the traditional bedside lamp experience.

Components Required

  • Arduino Uno
  • Capacitive Touch Sensor (TTP223)
  • LED
  • 220Ω Resistor
  • MOSFET (e.g., IRF540N)
  • Power Supply
  • Enclosure
  • Breadboard
  • Jumper Wires 

Circuit Diagram

1. Set up the touch sensor: 

  • Connect the touch sensor to the Arduino. 
  • Attach the signal pin to a digital input pin (e.g., D2), VCC to 5V, and GND to  ground. 

2. Connect the MOSFET: 

  • Connect the gate pin to a PWM pin (e.g., D9) on the Arduino. 
  • Source pin to GND, and the drain pin to the LED’s cathode. 

3. Connect the LED: 

  • Connect the anode of the LED to the power supply through a 330Ω resistor. 
  • The cathode connects to the drain pin of the MOSFET. 

4. Connect power: 

  • Use an appropriate power source (5V or 12V depending on your LED and components). 

5. Upload the code: 

  • Use the Arduino IDE to upload the program for touch-sensitive control.


CODE

const int touchPin = 2; // Touch sensor pin 

const int ledPin = 9; // PWM pin for LED 

int brightness = 0; // Initial brightness 

int touchState = 0; // State of the touch sensor 

bool isOn = false; // Lamp on/off state 


void setup() { 

​ pinMode(touchPin, INPUT); 

​ pinMode(ledPin, OUTPUT); 


void loop() { 

​ touchState = digitalRead(touchPin); 

​ if (touchState == HIGH) { 

​ isOn = !isOn; // Toggle lamp state 

​ if (isOn) { 

​ for (brightness = 0; brightness <= 255; brightness += 5){ 

​ analogWrite(ledPin, brightness); 

​  ​delay(10); // Smooth brightness increase 

​ } 

​ }

​ else { 

​ for (brightness = 255; brightness >= 0; brightness -= 5) { 

​ analogWrite(ledPin, brightness); 

​  ​delay(10); // Smooth brightness decrease 

​ } 

​ } 

​ delay(300); // Debounce delay 

​ } 

}


CODE EXPLANATION

  1. Pin Definitions and Variables
const int touchPin = 2; // Touch sensor pin
const int ledPin = 9;   // PWM pin for LED
int brightness = 0;     // Initial brightness level (0-255)
int touchState = 0;     // State of the touch sensor (HIGH or LOW)
bool isOn = false;      // Tracks whether the lamp is on or off

  • touchPin: The pin connected to the capacitive touch sensor (input).
  • ledPin: The pin connected to the LED (output). Pin 9 supports PWM (Pulse Width Modulation), allowing for brightness control.
  • brightness: Stores the current brightness level of the LED (0 = off, 255 = full brightness).
  • touchState: Stores the current state of the touch sensor (HIGH when touched, LOW when not touched).
  • isOn: A boolean variable to track whether the lamp is on (true) or off (false).

    2. Setup Function

void setup() {
  pinMode(touchPin, INPUT);  // Set touch sensor pin as input
  pinMode(ledPin, OUTPUT);   // Set LED pin as output
}
  • pinMode(touchPin, INPUT): Configures the touch sensor pin as an input to read touch signals.
  • pinMode(ledPin, OUTPUT): Configures the LED pin as an output to control the LED.

   3. Loop Function

void loop() {
  touchState = digitalRead(touchPin); // Read the touch sensor state
  if (touchState == HIGH) {           // If the sensor is touched
    isOn = !isOn; // Toggle the lamp state (on/off)
    if (isOn) {   // If the lamp is now on
      // Smoothly increase brightness
      for (brightness = 0; brightness <= 255; brightness += 5) {
        analogWrite(ledPin, brightness); // Set LED brightness
        delay(10); // Delay for smooth transition
      }
    } else {      // If the lamp is now off
      // Smoothly decrease brightness
      for (brightness = 255; brightness >= 0; brightness -= 5) {
        analogWrite(ledPin, brightness); // Set LED brightness
        delay(10); // Delay for smooth transition
      }
    }
    delay(300); // Debounce delay to avoid multiple triggers
  }
}
  • Read Touch Sensor:
    • touchState = digitalRead(touchPin) reads the state of the touch sensor.
    • If the sensor is touched, touchState becomes HIGH.
  • Toggle Lamp State:
    • When the sensor is touched, isOn = !isOn toggles the lamp's state (on → off or off → on).
  • Smooth Brightness Control:
    • If the lamp is turned on (isOn == true), the brightness is increased from 0 to 255 in steps of 5 using a for loop.
    • If the lamp is turned off (isOn == false), the brightness is decreased from 255 to 0 in steps of 5.
    • analogWrite(ledPin, brightness) sets the LED brightness using PWM.
    • delay(10) creates a smooth transition effect.
  • Debounce Delay:
    • delay(300) prevents multiple triggers from a single touch by introducing a short delay after each touch.

How It All Works Together

  1. The ultrasonic sensor sends out a sound wave and measures how long it takes to bounce back.
  2. The Arduino calculates the distance to the object (your hand).
  3. If your hand is within 15 cm, the servo motor opens the lid for 3 seconds and then closes it.
  4. The process repeats continuously, making the garbage can "smart" and hands-free.

OBSERVING FUNCTIONALITY

  • Initial Setup: Connect the lamp to a power source and ensure all components are  correctly connected. 
  • Touch Test: 
    • Touch the sensor once to turn the lamp on. 
    • Touch it again to turn the lamp off. 

COMMON PROBLEMS AND SOLUTIONS

  • Touch Sensor Not Responding: 
    • Verify the sensor’s connections to the Arduino. 
    • Ensure the sensor is properly powered. 
  • LED Not Dimming Smoothly: 
    • Check the MOSFET connections. 
    • Ensure the LED is dimmable and within the power supply voltage range. 
  • Lamp Flickers: 
    • Use a stable power supply. 
    • Increase the debounce delay in the code.

Conclusion

The Nightstand Touch Lamp project is a modern, user-friendly lighting solution that replaces traditional switches with a touch-sensitive interface, allowing users to toggle the lamp on/off and adjust brightness levels with a simple touch. Using a TTP223 capacitive touch sensor, an Arduino Uno, and a dimmable LED, the system detects touch inputs, processes them via the Arduino, and controls the LED’s brightness using PWM for smooth transitions. The setup is simple, requiring minimal components like a MOSFET, resistor, and power supply, while the code includes a debounce delay to prevent accidental triggers. This project is ideal for beginners, offering a practical introduction to capacitive sensing, PWM, and Arduino programming, while also serving as a functional and stylish addition to any bedside table. Future enhancements could include multi-touch gestures, wireless control, or energy-saving features, making it a versatile foundation for smart lighting projects.


Comments

INFRARED LIGHT BARRIER ALARM