I took the idea from Instructables. It's also a good idea for people who are afraid of sleeping in the dark.
Link of the idea www.instructables.com/Scary-Dark-Dark-the-Breathing-Lamp-in-Dark/
Software:
I used Thinkercad to simulate my electronic circuit
I used Arduino IDE to write and upload my code to my Arduino.
The Material
is a Plastic box I used as shade for a lamp
breadboard
helps organize connections without soldering.
Connect components to the Arduino Uno and breadboard.
allowing signals to transfer.
Arduino Uno is a small programmable board that reads inputs and controls outputs based on uploaded code.
White LED: is an output component and Emits bright white light when current flows through it.
ultrasonic sensor
Measures distance by sending and receiving ultrasonic waves.
Sends an electrical signal to the Arduino based on the detected distance.
potentiometer
is a three-terminal resistor with a sliding or rotating contact that forms an adjustable voltage divider. and I used it to adjust the time of the LED
is an output component. It’s used to display text, numbers in Arduino projects.
9 Volts Adapter:
Supplies a stable 9V power to circuits and Arduino
Open Tinkercad: Tinkercad Circuits and create a new circuit.
Add Components: Drag and drop the following:
Arduino
Ultrasonic Sensor
LCD Display
LED
Resistor (330Ω )
Potentiometer (for Adjusting Time)
connect 5V and GND in the breadboard in a positive and negative
and then connect the following:
Ultrasonic Sensor:
VCC to 5V on breadboard
GND to GND on breadboard
TRIG to Pin 7 on Arduino
ECHO to Pin 8 on Arduino
LCD Display:
VCC to 5V on breadboard
GND to GND on breadboard
SDA to A4 on Arduino Leonardo (SDA)
SCL to A5 on Arduino Leonardo (SCL)
LED:
Anode (+, longer leg) to Pin 6 on Arduino
Cathode (-, shorter leg) to GND on breadboard via a 330Ω resistor
Potentiometer
Left pin to 5V on a breadboard
Middle pin (output) to A0 on Arduino
Right pin to GND on breadboard
start writing the code
I took the code from: www.instructables.com/Scary-Dark-Dark-the-Breathing-Lamp-in-Dark/, but I made some adjustments to fit the circuit.
I installed the "LiquidCrystal I2C by Frank de Brabander." library to use it in my code
code objective:
Reads object distance using an ultrasonic sensor.
Uses a potentiometer to set the LED on time.
Turns on LED if an object is close.
Gradually fades LED off.
Prevents continuous activation with _flag.
Resets when no object is detected for 3 seconds.
Wire.h: Handles I2C communication between the microcontroller and LCD display.
Pin Definitions
TRIG_PIN (7): Sends the ultrasonic pulse.
ECHO_PIN (8): Receives the reflected pulse.
LED_PIN (6): Controls the LED.
POT_PIN (A0): Reads the potentiometer value.
Variables
brightness: Stores the LED brightness level (used for fading effect).
Time: Stores the delay time (controlled by the potentiometer).
flag: Controls whether the LED can turn on (prevents continuous activation).
0x27: I2C address of the LCD.
16, 2: LCD has 16 columns and 2 rows.
Serial. begin(9600): Initializes serial communication for debugging.
lcd_I2C_27.init(): Initializes the LCD display.
lcd_I2C_27.backlight(): Turns on the LCD backlight.
pinMode(): Sets pins as INPUT or OUTPUT.
Reads distance from the ultrasonic sensor and prints it via serial monitor.
Reads potentiometer value and maps it from 0–1023 to 1–300 seconds.
constrain(value, 1, 300): Ensures _time stays between 1 and 300 seconds.
Clears the LCD.
If an object is within 30 cm and _flag == 1, display _time on LCD.
If an object is within 30 cm and _flag == 1:
Turn on the LED.
Delay for _time seconds.
Slowly dim the LED (fade-out effect).
Set _flag = 0 to prevent reactivation.
Turn off the LCD backlight.
If distance > 50 cm and _flag == 0:
Wait 3 seconds.
If the object is still farther than 50 cm, reset _flag = 1.
Turn the LCD backlight back on.
Connect the 5V and the GND by 2 wires to the positive and negative in breadboard
Connect the Ultrasonic Sensor:
VCC to 5V on breadboard
GND to GND on Arduino
TRIG to Pin 7 on Arduino
ECHO to Pin 8 on Arduino
Connect the LCD Display:
VCC to 5V on breadboard
GND to GND on Arduino
SDA to A4 on Arduino Leonardo (SDA)
SCL to A5 on Arduino Leonardo (SCL)
connect LED:
Anode (+, longer leg) to Pin 6 on Arduino
Cathode (-, shorter leg) to GND on breadboard via a 330Ω resistor
connect the Potentiometer
Left pin to 5V on a breadboard
Middle pin (output) to A0 on Arduino
Right pin to GND on breadboard
connect the Arduino to the laptop by USB to upload code
the video
Assemble the components in the cardboard.
Connect the components (Arduino, ultrasonic sensor, LCD Display - LED, resistor (330Ω), potentiometer, and breadboard)in cardboard and I set the wires.
the video in dark
I had trouble installing the LED and potentiometer on the cardboard and plugging it into a wire to work.
The solution I install the wire with the duct tape in the LED and potentiometer terminals so that it is fixed and not taken off.
The composition of the components was also difficult in cardboard and the wires were taking off so I solved this problem by partitioning the cardboard into two halves installing the breadboard in cardboard and starting to install each component alone.