Introduction: Raspberry Pi Smart NightLight
I recently got up early, many times the days outside are still dark, but I don't want to turn on the lights to affect my wife's rest, so I have been thinking about buying a night light. I searched for a lot of nightlight shops, but I don't think it is I want it, but I saw a night light called DockerPi in a shop in Amazon, which is controlled by the Raspberry Pi. It is very good and can provide DIY features. I read the wiki description of this product, it is Can be directly controlled by the command line, as a programmer using Linux system, I think this is very cool, so I bought it back and started this project.
Step 1: Step 1: Prepare All the Stuff
After unboxing and then I remove the protect cover on the acrylic plate ,and then mount this DockerPi's module to my Raspberry Pi with screews and copper stick. it fixed stable.
Step 2: Step2 : Flash the Latest Rasbpian OS Image to SD Card
I download the latest Raspbian image from :https://www.raspberrypi.org/downloads/raspbian/
and then flash the image via a software called : etcher
you can download here: https://www.balena.io/etcher/
unzip the image file from the gzip package and you will get a *.img file, select the image and select drive that your PC recognized the TF card , just press " Flash" and wait for several minutes, it will be done.
and then remove the TF card and insert it to your Raspberry Pi and power it up.
Step 3: Step 3: Turn on I2C Function From Raspi-config
When Raspberry pi started up, I opened an terminal and typing this command: sudo raspi-config
and navigated to "Interfacing Options" and select "I2C" and enabled it. why should i using this command ?
Because the DockerPi's nightlight module is using I2C protocol communicate with Raspberry Pi.
Step 4: Step 4: PLug the Acrylic Panel to the Slot
It fit for the slot very well, and you can see the picture that the acrylic panel can stay still in the slot.
next step is running test code to check if it works properly.
I've download the example code from github via typing this command in terminal at raspberry Pi.
cd ~
git clone https://github.com/geeekpi/dockerpi.git
cd dockerpi/Nightlight/
sudo ./Nightligh.sh
and then my nightlight turn on and shinning.
I read the instruction on it's wiki and found all of the LED light's register map chart.
next step will be the funnest part, I want to add a human body infrared pyroelectric sensor, let it detect that I am lit up to light this little night light~
Step 5: Step 5: Setup the Infrared Pyroelectric Sensor
Here, we are using a PIR motion sensor. PIR stands for passive infrared. This motion sensor consists of a fresnel lens, an infrared detector, and supporting detection circuitry. The lens on the sensor focuses any infrared radiation present around it toward the infrared detector. Our bodies generate infrared heat, and as a result, this heat is picked up by the motion sensor. The sensor outputs a 5V signal for a period of one minute as soon as it detects the presence of a person. It offers a tentative range of detection of about 6–7 meters and is highly sensitive. When the PIR motion sensor detects a person, it outputs a 5V signal to the Raspberry Pi through its GPIO and we define what the Raspberry Pi should do as it detects an intruder through the Python coding. Here we are just printing "Intruder detected".
After you have set up your Raspberry Pi, we can now start messing around with its GPIO pins. Here, we will try to blink an LED using a Python script. Copy and paste the following code into your Raspberry Pi. You can do this by opening the text editor "leafpad" on your Raspberry Pi and copying this code into it, and save this as a Python file: nightlight.py :
#import libararies.
import RPi.GPIO as GPIO
import time
import smbus
DEVICE_BUS = 1
DEVICE_ADDR = 0x15
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)
GPIO.setup(11, GPIO.IN) #Read output from PIR motion sensor
bus = smbus.SMBus(DEVICE_BUS) # instance of smbus for i2c device, means the nightlight.
while True:
try:
i=GPIO.input(11)
if i==0: #When output from motion sensor is LOW
print ("No intruders" ,i)
for i in range(1,25):
bus.write_byte_data(DEVICE_ADDR, i, 0x00) #Turn OFF LED
time.sleep(0.2)
time.sleep(0.1)
elif i==1: #When output from motion sensor is HIGH
print("Intruder detected",i)
for i in range(1,25):
bus.write_byte_data(DEVICE_ADDR, i, 0xFF) #Turn OFF LED
time.sleep(0.2)
time.sleep(0.1)
except KeyboardInterrupt as e:
print("Quit the loop")
and then save it and make it running during the raspberry pi boot up.
sudo vim.tiny /etc/rc.local
and add this line before exit 0:
sudo python /home/pi/nightlight.py &
and then save it and reboot your Pi, it will work properly ...
Thanks for watching ~