Traffic System

../_images/TrafficSystem.jpg

Description

Not only the traffic in tunnels can cause people to reach school or work late, but also it can expose the passengers (on motorcycles, in cars or buses) to health risks and stress especially in Beirut.

This project has a double goal: First, to help people save time by avoiding the traffic in road tunnels. Second, to improve the ventilation process of the road tunnels in order to protect the health of the passengers during their presence inside the tunnel.

This project will inform drivers or commuters about the traffic in tunnels before reaching the tunnel and give them the choice to change their road to save time and fuel. And in case they were trapped in the tunnel traffic, it will help them have a better air quality inside the tunnel to reduce the respiratory problems and pollution inside the tunnel by an improved ventilation process.

Learning Objectives

The Traffic System is a project that helps students learn and apply the following:

  • Velocity and speed
  • Maths with sensors
  • Internet of things
  • Implementation of the engineering design process
  • Application of theoretical concepts to real life situations
  • Coding, electronics, and physical science through a hands-on application that can be directly related to real life situations

Materials Needed

  • Raspberry pi with micro SD card and Raspbian
  • Jumper wires
  • Breadboard
  • Glue gun and glue sticks
  • Cutter blades or scissors
  • Ultrasonic sensors (2)
  • DC motors (3)
  • L298N Module
  • 1.5V battery (12)
  • Breadboard
  • Wires
  • Fans (3)
  • Plastic Trucks (6)

Setup and Functionality

The tunnel will have two ultrasonic sensors, one at the beginning of the tunnel (counting the cars entering the tunnel) and one at the end of the tunnel (counting the cars leaving the tunnel). When the number of the car inside the tunnel (entering - leaving) reaches the maximum capacity of the tunnel, a command will be sent to an account on twitter and make the following tweet “#Traffic detected in stay focused tunnel” so that drivers can check the traffic and change their road before reaching the tunnel. Moreover, at the same time, another command will be sent to the DC motors (air turbines) inside the tunnel to turn them on and improve ventilation in case of traffic. This way, energy will be saved since not all the turbines will be turned on during all the day. However, in case the number of cars inside the tunnel does not exceed the full capacity, no tweet is sent and only one turbine will keep working out of three.

../_images/TrafficSystem2.jpg

Circuitry and Electronics:

The diagrams below show all the wiring and connections of all the components used in the Traffic System:

../_images/TrafficSystem.png

Programming

The Traffic System was programmed using Python.

Warning

In order for the code to work you must first install and setup your twitter account tokens. You can learn how to do that by following this link: http://www.compjour.org/tutorials/getting-started-with-tweepy/

import RPi.GPIO as GPIO
import time
import tweepy

#obtain these tokens from twitter
consumer_token = '###CONFIDENTIAL###'
consumer_secret = '####CONFIDENTIAL###'
access_token = '###CONFIDENTIAL###'
access_token_secret = '####CONFIDENTIAL###'

auth = tweepy.OAuthHandler(consumer_token, consumer_secret)
auth.set_access_token(access_token, access_token_secret)

api = tweepy.API(auth)


trigger1=39
echo1=38
trigger2=37
echo2=36

GPIO.setmode(GPIO.BOARD)

distance = 22  ####### CHANGE DISTANCE HERE
maxCars = 5    ####### CHANGE MAX CAR NUMBER FOR TRAFFIC

####################################
####################################
######  CHANGE PIN NUMBERS ########
motor1A=3
motor1B=5

motor2A=11
motor2B=13

GPIO.setup(trigger1,GPIO.OUT)
GPIO.setup(echo1,GPIO.IN)

GPIO.setup(trigger2,GPIO.OUT)
GPIO.setup(echo2,GPIO.IN)

GPIO.setup(motor1A,GPIO.OUT)
GPIO.setup(motor1B,GPIO.OUT)

GPIO.setup(motor2A,GPIO.OUT)
GPIO.setup(motor2B,GPIO.OUT)


def ultra(trigger,echo):
    time.sleep(0.5)
    GPIO.output(trigger,True)
    time.sleep(0.0001)
    GPIO.output(trigger,False)
    while GPIO.input(echo)==0:
        pulseStart=time.time()
    while GPIO.input(echo)==1:
        pulseEnd=time.time()
    pulseDuration = pulseEnd-pulseStart
    distance=pulseDuration*17150
    print'distance',distance
    return distance
    time.sleep(0.5)

def fanOn(A,B):
    GPIO.output(A,True)
    GPIO.output(B,False)

def fanOff(A,B):
    GPIO.output(A,False)
    GPIO.output(B,False)

    
counter =0

while True:    
    if ultra(trigger1,echo1)<distance:
        counter = counter +1
    if ultra(trigger2,echo2)<distance:
        counter = counter -1
    if counter > maxCars:
        fanOn(motor1A,motor1B)
        fanOn(motor2A,motor2B)
   
        # send twitter
        api.update_status(status="#Traffic detected inside stayfocused tunnel!")
                
    else:
        fanOff(motor1A,motor1B)
        fanOff(motor2A,motor2B)

Science Concepts and Skills

Road safety is a big issue all around the world. This project aims at using simple and cheap methods in order to enhance safety and reduce injuries.

Using two ultrasonic sensors on the side of the road, a moving car will pass in front of the first sensor and then in front of the second sensor. When any car passes in front of the first senor, time measurement is triggered and the time needed to reach the second sensor in recorded. Since the two sensors are located at a known distance apart from each other, the velocity can be calculated based on the following formula:

Speed = distance/time

Speed in measured in centimeters/second Distance in centimeters Time in second

If the calculated speed is greater than a set figure (50 cm/s in this case) the camera is triggered to take a picture to document the plate number for the driver to get a ticket.

However, since the main concern in this project is safety, in the case of a speeding car on a certain lane, the red lights will be delayed on the other lanes until the car passes the cross road. The time needed to pass the crossroad is based on the calculated speed of the car.

Tutorial Video.

Flex your brain! How can this this project be improved to help ambulance and fire engine get faster to their destinations without risking the lives of others?