Hello all, hope you're enjoying your life!
In this tutorial, we will be using Python, Raspberry Pi and a transistor to light up lanterns exactly on sunset! this is useful for Muslims during the holy month of Ramadan since sunset remarks the time of Iftar (aka time to feast!!)
- Raspberry Pi (any model should work just fine) with Python3 and PIP installed
- 3V low power LED Lanterns (I got mine from IKEA, uses 0.35W @ 3V)
- 10k Resistor and PN2222 NPN Transistor
- Basic wires and breadboard (or you could basically solder the whole thing like I did)
Step 1: Make Sure Your Lanterns Work the Way They're Supposed To
To make sure your lanterns/LEDs are working the way they are supposed to:
1- Strip the wires of the LED and disconnect any batteries connected to them
2- Construct the circuit shown in the picture, use a 100Ω resistor,
*Please note that we're using the 3V port and LEDs have polarity, meaning that they won't work if you wired them wrong.
Step 2: Connect the Circuit
Once everything is confirmed to be working well, you can wire up the circuit, it is to be noted that we will be using the transistor as a low power switch instead of a relay, this is the case because relays consume much more power than transistors and because I didn't have any relays laying around at the moment ;-)
The second picture is only to illustrate the functionality of a transistor as a switch. credit:
- NP2222 NPN Transistor
- 10kΩ resistor
- Raspberry Pi (All of them should work fine)
LEDs should not light up right now but if they did, that's okay! you can just unplug the ground wire to disconnect them.
Step 3: Let's Code!
Here's the python code that I used:
Feel free to copy and edit as you wish.
PLEASE NOTE THAT:
- You MUST install suntime library, get it from:
- Before using the code, you need to change your exact location information (i.e. latitude and longitude)
- Before using the code, you need to change the time zone by either adding your subtracting hours from UTC timing. (Refer to code comments)
After making the required modifications, save the code in your /home/pi directory.
To make the code run on boot up, access the terminal and use:
sudo nano /etc/rc.local
then add the following line just before 'exit 0' statement:
sudo python3 /home/pi/YOUR_FILE_NAME.py &
*Change YOUR_FILE_NAME.py to what you named your file.
That's it! now exit using CTRL+X, a little message will appear asking if you wanted to save the file, just press 'y' then press 'Enter' twice.
credit goes to:
You can further modify the code to have better accuracy, for me, +-2 seconds is good enough.
Step 4: Time to Celebrate Your Creation!!
Congratulations!! you're ALMOST done.
now it is the time to disconnect your Raspberry Pi and wander around the house to find a good spot to install your masterpiece, I installed it just right next to our Iftar table, maybe you should do the same too! :D :)