Introduction: Smart Traffic Light

Picture of Smart Traffic Light

In today’s world, traffic lights are essential for a safe road. However, many times, traffic lights can be annoying in situations where someone is approaching the light just as it is turning red. This wastes time, especially if the light is preventing a single vehicle from getting through the intersection when there is nobody else on the road. My innovation is a smart traffic light that uses live object detection from a camera to count the number of cars on each road. The hardware I will use for this project is a Raspberry Pi 3, a camera module, and various electronic hardware for the light itself. Using OpenCV on the Raspberry Pi, the information gathered will be run through code that controls the LEDs via the GPIO. Depending on these numbers, the traffic light will change, letting cars through in the most optimal order. In this case, the lane with the most cars would be let through so that the lane with fewer cars would be idling, reducing air pollution. This would eliminate situations when many cars are stopped while there are no cars on the intersecting road. Not only does this save time for everybody, but it also saves the environment. The amount of time people are stopped at a stop sign with their engine idling increases the amount of air pollution, so by creating a smart traffic light, I am able to optimize the light patterns so that cars spend the least time possible with their vehicle stopped. Ultimately, this traffic light system could be implemented in cities, suburbs, or even rural areas to be more efficient for people would reduce air pollution.

Step 1: Parts List

Materials:

Raspberry Pi 3 Model B v1.2

Raspberry Pi Camera v2.1

5V/1A micro USB power supply

HDMI monitor, keyboard, mouse SD card with Raspbian Jessie

Raspberry Pi GPIO breakout cable

Red, yellow, green LEDs (2 of each color)

Female connectors for Raspberry Pi (7 unique colors)

Assorted 24 gauge wire (different colors) + heat shrink tubing

2’x2’ wood panel or platform

Wood screws

Black surface (cardboard, foam board, poster board, etc.)

White (or any color other than black) tape for road markings

Black spray paint (for PVC)

½” PVC pipe with 90 degree elbow joints(2), T socket (1), female adapter (2)

Tools:

Soldering Iron

3D Printer

Drill with various drill bits

Breadboard

Heat gun

Step 2: Setting Up the Raspberry Pi

Load the SD card into the Raspberry Pi and boot up.

Follow this guide to install required OpenCV libraries. Make sure you have time to do this step, as installing the OpenCV library can take a couple hours. Be sure to also install and set up your camera here.

You should also pip install:

picamera

gpiozero

RPi.GPIO


Here is the finalized code:

from picamera.array import PiRGBArray

from picamera import PiCamera

import picamera.array

import numpy as np

import time

import cv2

import RPi.GPIO as GPIO

import time

GPIO.setmode(GPIO.BCM)

for i in (23, 25, 16, 21):

GPIO.setup(i, GPIO.OUT)

cam = PiCamera()

cam.resolution=(480,480)

cam.framerate=30

raw=PiRGBArray(cam, size=(480,480))

time.sleep(0.1)

colorLower = np.array([0,100,100])

colorUpper = np.array([179,255,255])

initvert = 0

inithoriz = 0

counter = 0

for frame in cam.capture_continuous(raw, format="bgr", use_video_port=True):

frame = frame.array

hsv = cv2.cvtColor(frame,cv2.COLOR_BGR2HSV)

mask = cv2.inRange(hsv,colorLower,colorUpper)

mask = cv2.blur(mask,(3,3))

mask= cv2.dilate(mask,None,iterations=5)

mask= cv2.erode(mask,None,iterations=1)

mask= cv2.dilate(mask,None,iterations=3)

me,thresh = cv2.threshold(mask,127,255,cv2.THRESH_BINARY)

cnts = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)[-2]

center = None

vert = 0

horiz = 0

if len(cnts) > 0:

for c in cnts:

(x,y),radius = cv2.minEnclosingCircle(c)

center = (int(x),int(y))

radius = int(radius)

cv2.circle(frame,center,radius,(0,255,0),2)

x = int(x)

y = int(y)

if 180 < x < 300:

if y > 300:

vert = vert +1

elif y < 180:

vert = vert +1

else:

vert = vert

if 180 < y < 300:

if x > 300:

horiz = horiz +1

elif x < 180:

horiz = horiz +1

else:

horiz = horiz

if vert != initvert:

print"Cars in vertical lane: " + str(vert)

initvert = vert

print"Cars in horizontal lane: " + str(horiz)

inithoriz = horiz

print '----------------------------'

if horiz != inithoriz:

print"Cars in vertical lane: " + str(vert)

initvert = vert

print"Cars in horizontal lane: " + str(horiz)

inithoriz = horiz

print '----------------------------'

if vert < horiz:

GPIO.output(23,GPIO.HIGH)

GPIO.output(21,GPIO.HIGH)

GPIO.output(16,GPIO.LOW)

GPIO.output(25,GPIO.LOW)

if horiz < vert:

GPIO.output(16,GPIO.HIGH)

GPIO.output(25,GPIO.HIGH)

GPIO.output(23,GPIO.LOW)

GPIO.output(21,GPIO.LOW)

cv2.imshow("Frame",frame)

cv2.imshow("HSV",hsv)

cv2.imshow("Thresh",thresh)

raw.truncate(0)

if cv2.waitKey(1) & 0xFF == ord('q'):

break

cv2.destroyAllWindows()

GPIO.cleanup()

Step 3: Raspberry Pi and Camera Mount

Picture of Raspberry Pi and Camera Mount

3D print the case and camera mount and assemble.

Step 4: Traffic Light Assembly

Picture of Traffic Light Assembly

Test out the traffic light with a breadboard. Each opposing set of LEDs share an anode, and all of them share a common cathode (ground). There should be a total of 7 input wires: 1 for each pair of LEDS (6) + 1 ground wire. Solder and assemble the traffic lights.

Step 5: Wiring (Part 1)

Picture of Wiring (Part 1)

Solder the female header pins to about 5 feet of wire. These are the sides that These wires will snake through the PVC pipes later on. Be sure to be able to distinguish the different sets of lights (2 x 3 colors and 1 ground). In this case, I marked the ends of another set of red, yellow, and blue wires with sharpie so I know which is which.

Step 6: Building the Environment

Picture of Building the Environment

Building the environment Make a 2 feet square wood pallet like this. Scrap wood is fine as it will be covered up. Drill a hole that just fits your adapter. Drill screws through the sides of the pallet to secure the PVC pipe into place. Cut the black foam board to match the wood pallet underneath. Drill a hole that fits around the PVC pipe. Repeat on the opposite corner. Mark the roads with some white tape.

Step 7: Finalizing the PVC Frame

Picture of Finalizing the PVC Frame

On the top pipe, drill a hole that can fit a bundle of wires. A rough hole is fine as long as you can access the insides of the pipes. Snake the wires through the PVC pipes and elbow joints for a test fit. Once everything is finalized, paint the PVC with some black spray paint to clean up the look of the main frame. Cut a small gap in one of the PVC pipes to fit a T-joint. Add a PVC pipe to this t-joint for the traffic light to hang down from. The diameter could be the same as the main frame (1/2"), though if you use a thinner pipe, make sure the 7 wires can snake through. Drill a hole through this pipe for the traffic light to hang from.

Step 8: Wiring (Part 2)

Picture of Wiring (Part 2)

Re-wire everything as tested previously. Double check the traffic light and wiring with a breadboard to confirm all the connections have been made. Solder the traffic light to the wires coming through the T-joint arm. Wrap the exposed wires with electrical tape to prevent any shorts and for a cleaner look.

Step 9: Finished!

Picture of Finished!

To run the code, be sure to set your source as ~/.profile and cd to your project location.

Step 10: Extras (Photos)

Picture of Extras (Photos)

Comments

rrm (author)2017-12-03

Nice idea! Can you network traffic signals at several intersections?

LegoSurvivor (author)2017-11-29

Send this to the president when you have the perfect prototype. you might get some money.

About This Instructable

803views

11favorites

License:

Bio: The BCAMRL is a Mechatronics Research Lab, found in 2014 on the campus of Bergen County Academies a magnet high school within the Bergen County ... More »
More by bcamrl:Infrared Red Therapy Device (IRTD)RQ Bot IN-STOP
Add instructable to: