Automated light control module
The purpose of this project is to control a light switch based on the IR and light readings using the mbed.
Parts Needed
- 1 x Solid State Relay SSR-25DA
- 1 x Photoresistor
- 1 x PIR HC-SR501
- 1 x Amplifier 2N3904
- 1 x 10k Ohm resistor
- 1 x 20k Ohm resistor
Design Layout
Please Read!
The relay was connected to a detached light socket for the ability to demo the project anywhere. This is not needed, as this design would traditionally be installed inline with a light switch.


Code
main.cpp
#include "mbed.h"
#include "rtos.h"
DigitalOut relayPin(p29);
DigitalOut relayPin2(LED1); // for debugging purposes
DigitalIn pirPin(p30);
AnalogIn photoPin(A0);
// initialize relay to off
float photoThreshold = 0.87;
// initialize light to off
int lightStatus = 0;
// thread to turn off and on light
void lightStatus_thread(void const *args)
{
while (true)
{
// photoPin.read() > photoThreshold implies light is off
if((photoPin.read() > photoThreshold) && (pirPin.read() == 1))
{
lightStatus = 1;
}
else
{
lightStatus = 0;
}
Thread::wait(2000);
}
}
int main() {
relayPin = 0; // initialize light to off
Thread thread1(lightStatus_thread);
while(1)
{
relayPin = lightStatus;
relayPin2 = lightStatus;
}
}
Pinout
| mbed pins | Relay pins | PIR pins | BJT pins |
|---|---|---|---|
| p29 | -- | -- | Base |
| p30 | -- | OUT | -- |
| VOUT | p3 | -- | -- |
| VU | -- | VCC | -- |
| GND | -- | GND | Emitter |
Circuit

Video Demonstration
Please log in to post comments.
