Raspberry Pi Emergency Light with Darkness and AC Power Line Detector

In this session we are going to make a 9WATT Emergency Lamp using Raspberry Pi and Python. This lamp will automatically detect the darkness and absence of AC power supply, and light up when there is power failure and proper light is not there.

Although there are various emergency lamps available but they are purely dedicated to serve single purpose, like one Simple Emergency Light Circuit that we have created previously, triggers only on power failure. With Raspberry Pi we can add various other functionalities to it, like here we have added LDR to detect Darkness at various levels. Here we have added two levels, when there is complete dark, the lamp will glow with full intensity and when there is semi dark, it will glow at 30% capacity. So here we are going to design this lamp to be turned ON when AC line power is OFF and when the light intensity in the room goes very low.

 

Components Required:

Here we are using Raspberry Pi 2 Model B with Raspbian Jessie OS. All the basic Hardware and Software requirements are previously discussed, you can look it up in the Raspberry Pi Introduction and Raspberry PI LED Blinking for getting started, other than that we need:

  • 1000µF capacitor
  • 1WATT LED (9 pieces)
  • +12V Sealed LEAD ACID battery
  • 6000-10000mAH power bank
  • +5V DC adapter
  • Lm324 OP-AMP chip
  • 4N25 Optocoupler
  • IRFZ44N MOSFET
  • LDR (Light Dependent Resistor)
  • LED (1 piece)
  • Resistors: 1KΩ (3 pieces), 2.2KΩ, 4.7KΩ, 100Ω (2 pieces), 10Ω (9 pieces), 10KΩ, 100KΩ
  • 10KΩ pot (3 pieces) (all resistors are 0.25 watt)

 

Description:

Before going into Circuit Connections and its working, we will learn about the components and their purpose in the circuit:

 

9 Watt LED Lamp:

The LAMP is made up of nine 1WATT LEDs. There are different kinds of LEDs present in market but 1WATT LED are easily available everywhere. These LED operate at 3.6V, so we will connect three of them in series along with protection diodes to operate at +12V. We will connect three of these strips forming a 9WATT LED lamp. We will operate this lamp with Raspberry Pi accordingly.

 

LDR (Light Dependent Resistor) to detect Darkness:

We are going to use LDR (Light Dependent Resistor) to detect the light intensity in the room. The LDR changes its resistance linearly with the light intensity. This LDR will be connected to voltage divider. With that we will have variable voltage to represent variable light intensity. If the light intensity is LOW the voltage output will be HIGH and if light intensity if HIGH voltage output will be LOW.

 

Op-amp LM324 IC for checking LDR output:

Raspberry Pi does not have an internal ADC (Analog to Digital Converter) mechanism. So this setup cannot be connected directly to Raspberry Pi. We will use OP-AMP based comparators to check the voltage outputs from LDR.

Here we have used op-amp LM324 which has four operational amplifiers inside it and we have used two op-amps out of those four. So our PI will be able to detect light intensity at two levels. Depending on these levels we will adjust the brightness of LED lamp. When there is complete dark, the lamp will glow with full intensity and when there is half dark, it will glow at 30% capacity. Check the Python code and video, at the end, to understand it properly. Here we have used PWM concept in Raspberry Pi to control the intensity of LEDs.

IC-LM324

Raspberry Pi has 26GPIO, out of which some are used for special functions. With special GPIO put aside, we have 17 GPIO. Each of the 17 GPIO pins cannot take voltage higher than +3.3V, so the Op-amp outputs cannot be higher than 3.3V. Hence we have chosen op-amp LM324, as this chip can operate at +3.3V providing logic outputs not more than +3.3V. Learn more about GPIO Pins of Raspberry Pi here. Also check our Raspberry Pi Tutorial Series along with some good IoT Projects.

 

AC to DC Adapter to check the AC Line:

We will use AC to DC adapter outlet voltage logic to detect the AC line status. Although there are various ways to detect the AC line status, this is the safest and easiest way to go. We will take +5V logic from adapter and give it to Raspberry Pi through a voltage divider circuit to covert +5V high logic to +3.3v HIGH logic. See the circuit diagram for better understanding.

 

Power Bank and 12v Lead acid Battery for Power Supply:

Keep in mind that Raspberry Pi must be operating in the absence of power, so we will drive the PI using a Power Bank (A battery pack 10000mAH) and the 9WATT LED lamp will be powered by +12V, 7AH sealed LEAD ACID battery. The LED lamp cannot be powered by power bank as they draw too much power, so they must be powered from a separate power source.

You can power the Raspberry Pi by +12V battery if you have an efficient +12V to +5v converter. By that converter you can ditch the power bank and power the entire circuit with a single battery source.

 

Circuit Explanation:

Circuit Diagram of Raspberry Pi Emergency Light is given below:

Raspberry-pi-emergency-light-with-darkness-detector-circuit

Here we have used three out of four comparator inside LM324 IC. Two of them will be used to detect light intensity levels and the third one will be used to detect the low voltage level of +12V battery.

1. OP-AMP1 or U1A: Negative terminal of this comparator is provided with 1.2V (adjust RV2 to get the voltage) and Positive terminal is connected to LDR voltage divider network. As the shade falls on the LDR, its internal resistance rises. With the rise in internal resistance of LDR, the voltage drop at the positive terminal of OP-AMP1 rises. Once this voltage goes higher than 1.2V, the OP-AMP1 provides +3.3V output. This HIGH logic output of OP-AMP will be detected by Raspberry Pi.

2. OP-AMP2 or U1B: Negative terminal of this comparator is provided with 2.2V (adjust RV3 to get the voltage) and Positive terminal is connected to LDR voltage divider network. As the shade falling on the LDR further increases, its internal resistance goes even higher. With further rise in internal resistance of LDR, the voltage drop at the positive terminal of OP-AMP2 rises. Once this voltage goes higher than 2.2V, the OP-AMP2 provides +3.3V output. This HIGH logic output of OP-AMP will be detected by Raspberry Pi.

3. OP-AMP3 or U1C:  This OP-AMP will be used to detect low voltage level of +12v battery pack. Negative terminal of this comparator is provided with 2.1V (adjust RV1to get the voltage) and positive terminal is connected to a voltage divider circuit. This divider divides the battery voltage by 1/5.7 times, thus for 12.5V battery voltage we will have 2.19V at the positive terminal of OP-AMP3. When battery voltage goes below 12.0V, voltage at positive terminal will be <2.1V. So with the 2.1v at negative terminal, OP-AMP output goes low. So when battery voltage drops below 12V (means below 2.1v at positive terminal), the OP-AMP pulls down the output, this logic will be detected by Raspberry Pi.

 

Working Explanation:

The whole function of this Raspberry Pi Emergency Lamp can de stated as:

First Raspberry Pi detects if there is AC power present or not by sensing logic at GPIO23, where +3.3V from AC adapter is taken. Once the power goes OFF, +5V from adapter goes OFF and Raspberry Pi goes to the next step only if this LOW logic is detected, if not PI will not move to next step. This LOW logic happens only when AC power goes OFF.

Next PI checks if the LEAD ACID battery level is LOW. This logic is provided by OP-AMP3 at GPIO16. If the logic is LOW, then PI does not move to next step. With battery voltage higher than +12V, PI moves to next step.

Next Raspberry Pi checks if the darkness in the room is HIGH, this logic is provided by OP-AMP2 at GPIO20. If yes, PI provides PWM (Pulse Width Modulation) output with a duty cycle of 99%. This PWM signal drives the opto-coupler which drives the MOSFET. MOSFET powers the 9WATT LED setup as shown in figure.  If there is not complete dark then PI moves to next step. Learn more about PWM in Raspberry Pi here.

Then Raspberry Pi checks if the darkness in the room is LOW, this logic is provided by OP-AMP1 at GPIO21. If yes, PI provides PWM (Pulse Width Modulation) output with a duty cycle of 30%. This PWM signal drives the opto-coupler which drives the MOSFET. MOSFET powers the 9WATT LED setup as shown in figure. If there is proper light in the room, then Raspberry Pi does not provide PWM output so the LAMP will be completely OFF.

Raspberry-pi-emergency-light-breadboard-setup

So to turn on this Emergency Lamp, both the condition must be True, means AC line must be off and there must be darkness in the room. You can get the clear understanding by checking the complete Python Code and Video below.

You can further add more interesting functionalities and darkness levels to this Emergency lamp. Also check our more Power Electronics circuits:

Code: 

import RPi.GPIO as IO   #calling for header file which helps in using GPIOs of PI
import time
IO.setwarnings(False)      #do not show any warnings
IO.setmode (IO.BCM)     #programming the GPIO by BCM pin numbers

IO.setup(19,IO.OUT)     #initialize GPIO19 as an output
IO.setup(21,IO.IN)         #initialize GPIO21,20,16,12,23 as inputs
IO.setup(20,IO.IN)
IO.setup(16,IO.IN)
IO.setup(12,IO.IN)
IO.setup(23,IO.IN)

p = IO.PWM(19,200)     #GPIO19 is set as PWM output with frequency at 200Hz
p.start(1)             #start PWM output with 1% duty cycle

while 1:
    if(IO.input(23) == False):     #If AC power is OFF
        if(IO.input(16) == True):  #If battery voltage >+12V
            if (IO.input(20) == True):      #If its complete dark
                time.sleep(0.01)
                if (IO.input(20) == True):     #If its complete dark
                    p.ChangeDutyCycle(99)   #change PWM duty cycle to 99%
            elif (IO.input(21) == True):      #else If its semi dark 
                time.sleep(0.01)
                if (IO.input(21) == True):     #else If its semi dark
                    p.ChangeDutyCycle(30)   #change PWM duty cycle to 30%
            elif (IO.input(21) == False):     #if its not even semi dark
                time.sleep(0.01)
                if (IO.input(21) == False):
                    p.ChangeDutyCycle(0)    #turn OFF the LAMP
            time.sleep(0.1)
    if(IO.input(23) == True):     #If AC power is ON
        p.ChangeDutyCycle(0)    #turn OFF the LAMP
    if(IO.input(16) == False):    #If battery voltage <+12V
        p.ChangeDutyCycle(0)    #turn OFF the LAMP 

Video: 

Leave a comment