Introduction
This tutorial shows how the Smart Light can work together with vehicle and crosswalk traffic light in the dark environment. We use two boards for the final prototype. One board (board 1) will include the photo resistor, crosswalk traffic light system, PIR sensor, transmitter, and SmartLight associated with the crosswalk traffic light system. On the other board (board 2), we will have the vehicle traffic light system, receiver and Smartlight associated with the vehicle traffic light system.
The tutorial includes three parts:
- How Smart Light Works with Crosswalk Traffic Light (Board 1)
PIR Sensor | Photo Resister | Crosswalk not Allowed | |
1 | There are pedestrians | Dark | Red light |
0 | No pedestrians | Bright | Green Light |
Crosswalk side:
SmartLight = PIR Sensor * Photo Resister*Crosswalk Not Allowed |
Vehicle side:
SmartLight = SmartLight on Crosswalk * Crosswalk Traffic Light |
Only when there are pedestrians on the crosswalk and the environment is dark, SmartLight will work. Once the SmartLight is triggered, it will flash at least for 5 seconds from the “start time”. After 2 seconds, it will check if there is pedestrian on the crosswalk again. If there are still pedestrians on the crosswalk, the SmartLight will reset the “start time” to be the current time and will continue to flash for 5 seconds since the new “start time”. Every time it will check if there is a pedestrian on the crosswalk at the end of 2 seconds since its start time is reset.
- How Smart Light Works with Vehicle Traffic Light (Board 2)
SmartLight on Crosswalk | Crosswalk Traffic Light | |
1 |
On |
Red |
0 | Off |
Green |
Instead of reading the value from the sensor, the components on the board 2 are simpler. We only have the vehicle traffic light system and SmartLight associated with it. The receiver will return the value of the signal of SmartLight on crosswalk side and the crosswalk traffic light. When the crosswalk traffic light is red, the vehicle traffic light is green. If the SmartLight on crosswalk side is on, that on vehicle side is on as well.
- How Photo Resister Works
The photo resister’s resistance varies with the change in light intensity. We use a function to convert light intensity to the output (1/0) to indicate whether it is in the dark environment.
The Components and Set-up
- Arduino Board (Uno)*2: here we only use one to show how to control SmartLight and crosswalk traffic light
- LED light: to simulate what will happen in real life, we use red LED for red light, use yellow LED for green light. And we use another red LED for the SmartLight. We will replace these components in the next week.
- Several wires
- Normal Resistors (330 OHM and 10k OHM)
- Photo Resistor
Wiring
The board 1 is wired as follows:
For board 1, we mainly have two components: the photo resister and the LED lights (simulate crosswalk traffic light -red and green, and the SmartLight associated with the crosswalk traffic light).
First of all, we connect 5V to (+) on the board, GND to (-) on the board.
Photo resister:
We use the A0 as the photo sensor pin, and connect it to A5. Then, we use E5 and E8 for the two sides of the photo resister. We add a 10k OHM between C1 and C5, and use two wires to connect B1 and (+), A8 to (-) respectively.
LED (three lights):
The wiring for this part is simple and similar. We use three wires to connect (-) to C10, C18, C26 respectively. Then, we connect the negative side (cathode) of the LED1 (red light), LED2 (green light) and LED3(SmartLight) to D10, D18, D26 respectively. There positive sides (anode) are placed at D9, D17,D25. Next, we add three 330 OHM resistors between E9 and G9, E17 and G17, E25 and G25. Finally, we use three wires connect J9 to pin7 (for red light), J17 to pin6 (for green light), and J25 to pin5 (for SmartLight) respectively.
Board 2:
There is no photo resister on the board 2, and the rest part is the same as the board 1.
Code
Board 1:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
//We have two boards in total, which represent crosswalk traffic light system and traffic light system, and the smart light systems associated with them respectively //This part is designed to control the crosswalk traffic light and the smart light on the crosswalk side int dark = 1; int cross_red = 5; int cross_green = 6; int smartcross = 7; const int photo = 0; unsigned long light_duration = 20000; // the timespan for red light and green light unsigned long starttime; unsigned long currenttime; int lightLevel = 0; //the light level read by the photo sensor int high = 0, low = 1023; int cross_PIR = 1; // whether there is a pedestrian on the crosswalk int smartcrosshigh = 1; // whether the SmartLight should be on or off int cross_no = 1; // whether it is allowed to cross the road int detectAgain = 2000; // the interval for the detection again int delayTime =5000; // the timespan for the SmartLight on void setup() { pinMode(photo,INPUT); pinMode(cross_red,OUTPUT); pinMode(cross_green,OUTPUT); pinMode(smartcross,OUTPUT); starttime = millis(); currenttime = millis(); Serial.begin(9600); } void loop() { digitalWrite(cross_red,HIGH); // red light is on digitalWrite(cross_green,LOW); // green light is off Serial.println("Redlight"); cross_no = digitalRead(cross_red); lightLevel = analogRead(photo); // read the light level from the sensor pin dark = LightIntensity(lightLevel); // get the dark parameter smartcrosshigh = cross_PIR*dark*cross_no; // check if is necessary to turn on the SmartLight if (smartcrosshigh == 1){ digitalWrite(smartcross,HIGH); } if((millis()-currenttime)>detectAgain){ // read the PIR sensor after 2s. If there is still pedestrian there, let the smart light flash longer Serial.println(millis()-currenttime); if (cross_PIR == 1){ currenttime = millis(); Serial.println("Pedestrians are still there!"); // cross_PIR = 0; // read the PIR sensor again. Actually we will read it from the PIR sensor again at the beginning of the loop. Just for showing. } } if((millis()-currenttime)>delayTime){ // turn off the SmartLight after 5 seconds since there are no pedestrians on the crosswalk Serial.println(millis()-currenttime); smartcrosshigh = 0; digitalWrite(smartcross,LOW); } if ((millis()-starttime) > light_duration) //turn off red light and turn on green light { digitalWrite(cross_red,LOW); digitalWrite(cross_green,HIGH); digitalWrite(smartcross,LOW); Serial.println("Greenlight"); delay(light_duration); starttime = millis(); currenttime = millis(); } } int LightIntensity(int lightLevel){ if (lightLevel < low) { low = lightLevel; //Set the new low value to be the light level } if (lightLevel > high) { high = lightLevel; //Set the new high value to be the light level } //Select the range around the light level to be squeezed to a narrow range. The low value (light level) +30 corresponds to 0, and high value (light level)-30 corresponds to 300 //The higher the initial light level, the lower the new light level. //Since as the light intensity increases, the resistance of the resistor will drop, so the voltage read by the pin will be higher, which corresponds to a lower new light level. lightLevel = map(lightLevel, low+30, high-30, 0, 300); lightLevel = constrain(lightLevel, 0, 300); //Constrain the light level range to [0,300] if (lightLevel <=300 and lightLevel >150) //When the new light level range is between (150,300], set dark to be 1, which means it is in the dark environment now. { dark = 1; } else //When the new light level range is between [0,150], set dark to be 0, which means it is not in the dark environment now. { dark = 0; } return dark; } |
Board 2:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
//We have two boards in total, which represent vehiclewalk traffic light system and traffic light system, and the smart light systems associated with them respectively //This part is designed to control the vehicle traffic light and the smart light on the vehicle side //Different from the crosswalk board, this sketch will use the value received by the receiver //The vehicle traffic light and Smart Light will change based on the traffic light and Smart Light on the crosswalk side int vehicle_red = 5; int vehicle_green = 6; int smartvehicle = 7; unsigned long light_duration = 20000; unsigned long starttime; unsigned long currenttime; int cross_red = 1;// This value will be received by the receiver, indicating that whether the traffic light on the crosswalk is red or not int smartcrosshigh = 1; // This value will be received by the receiver, indicating that whether the smart light on the crosswalk is on or off int smartvehiclehigh = 1; int detectAgain = 2000; int delayTime =5000; void setup() { pinMode(vehicle_red,OUTPUT); pinMode(vehicle_green,OUTPUT); pinMode(smartvehicle,OUTPUT); } void loop() { if(cross_red == 1){ // This value will be returned by the receiver digitalWrite(vehicle_red,LOW); digitalWrite(vehicle_green,HIGH); Serial.println("Vehicle traffic light is green now!"); if(smartcrosshigh == 1){ // This value will be returned by the receiver digitalWrite(smartvehicle,HIGH); //If the Smart Light at the crosswalk side is on, then turn on the Smart Light on the vehicle } else{ digitalWrite(smartvehicle,LOW); } } else{ digitalWrite(vehicle_red,HIGH); digitalWrite(vehicle_green,LOW); digitalWrite(smartvehicle,LOW); Serial.println("Vehicle traffic light is red now!"); } } |
Video
The video is to show how the SmartLight works together with the crosswalk traffic light system. We assume that it works at night so we remove the photo resister part.
The video is here: https://youtu.be/LZQbpcLSdNA (can’t upload)