- Traffic Signals play an important role in our day-to-day life. They not only help movement and but also conduct an orderly flow of vehicles.
- We all have got stuck in situations where non-working Traffic Lights have caused a mayhem of traffic leading to more likelihood of mishaps.
- As they say prevention is better than cure, this prototype is of an automated traffic signal system which can be easily programmed according to the varying situations.
Setting up our LED's -
1) Connect the 3 LED's to the breadboard, preferably in the order of
RED - YELLOW - GREEN
2) Connect 330 Ohm Resistor to the breadboard, such that it is connected positive end(longer leg) of Each Led
3) Now, connect one end end of Male to Male Jumper Wire to breadboard, such that it is connected with other end of the Resistor for each LED.
4) The remaining end of Jumper Wires are to be connected to the digital pins of Bolt WiFi Module. In this case, I have taken digital pins 0, 1, 2 for RED, YELLOW, GREEN respectively.
5) Connect a Jumper wire from GND pin of Bolt WiFi Module to the breadboard.
6) Using Jumper Wires, connect negative end(shorter leg) of each LED to the GND pin Wire on the Breadboard.
7) Thus, in this effective setup we have ensured, each LED is connected to ground as well as Digital Pins and with Resistors in Place.
8) After connection, power on the Bolt device and connect it to the internet-enabled wifi network and log in to your Ubuntu and create a Python file.
SOFTWARE SETUPThe Python coding for this project has been done in Ubuntu (Linux).
Python3 is used in this case.
Make sure, Bolt Python Libraries are installed to control our WiFi Module.
LOGIC AND STRUCTURE BEHIND THE CODE:- Before writing our program, we need to make a configuration file which will have the specific keys for each user/device. The advantage of this is that each user will only have to change the contents the configuration file to use the product.
from boltiot import Bolt
import time
from time import sleep
api_key = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
device_id = "BOLTXXXXX"
mybolt = Bolt(api_key, device_id)
- Now let's look into the commands needed to control our LED.
1) mybolt.digitalWrite('0', 'HIGH')
2) mybolt.digitalWrite('0', 'LOW')
These commands are used to switch on/off a LED light. 0 stands for the pin number, to which LED is connected (can be 0/1/2/3/4 depending upon circuit) and LOW/HIGH are the control states.
- Notice that we have called time and sleep modules respectively.
import time
from time import sleep
We need them to add delay(timer) to control our Traffic Lights in a proper and timely sequence.
- In real life, most of the time we observe that ONE Traffic Light is switched on at a time, and others are off.
- Therefore, we will use one function for each traffic light. It is required that, when one light is switched on, others are to be switched off. For example: When RED light is switched on, other 2 lights - yellow and green are switched off
- Thus, for each Traffic Light - Red, Yellow and Green, we are going to create a function which switches on the RESPECTIVE LIGHT and switches OFF all the others.
- Thus, in the main code, we are just going to call our functions according to the sequence we want.
- Using time.sleep() a delay is added, before next function is to be executed.
- A stop()(optional) is added, which switches off all the lights.
- For simplicity sake, in this prototype, we run the Traffic Light Sequence of,
- RED (5 secs) - YELLOW (3 secs) - GREEN (5 secs)
- Using a While loop, the sequence is executed again and again automatically for 'n' number of times. (Here, 3 times the loop is executed).
- The code and its explanation is given in the Code file.
- By using functions respectively, we ensure flexibility and code-reusability
- Advantage of using functions is that the sequence and the time for which each Traffic Light is to be used can be varied easily depending on the requirement
- Moreover, according to the traffic schedule, the number of times the sequence is to be executed can be varied also.
- For busy traffic areas, by adding an infinite Loop, the Traffic Lights can be used continuously i.e (24x7)
Comments