Categories
TUTORIALS

Integrating RGB LED (simple traffic light) and PIR sensor to detect the presence of people

Introduction

Our group’s project, SmartLight, aims to detect the presence of pedestrians on crosswalks and notify drivers about the presence of pedestrians through customized LEDs around both vehicle traffic lights and crosswalk traffic lights. Hence, there are two things to consider at the very initial stage: 1) How to detect the presence of people? 2) How to detect the presence of people while a traffic light is working? For this tutorial, I will introduce how to make “Passive Infrared” (PIR) sensor work with RGB LED to answer the two questions above.

RGB LED: Simulate traffic light (red, green, yellow)

PIR sensor: Detect the presence of people.

From Lab 1 and Lab 2, we have already learned how RGB LED works and how to manipulate RGB LED. It is very easy to let RGB LED show red, green and yellow, the three colors for a traffic light.

What is PIR sensor and how does PIR sensor work? (Source)

PIR (Passive Infrared) sensor is used to measure the infrared light radiating from the objects in its field of view; it detects the infrared energy released from animals and humans: when the detected energy is higher than a certain threshold level, PIR sensor will show an indication. The energy detected by PIR sensor is usually in the form of heat that is emitted by humans or animals. So PIR sensor is ideal to detect movement based on changes in infrared light in the environment, that is what our project really needs.

The following images both show the detecting area of a PIR sensor and its mechanism:

  1. Source
  2. Source

Basically, we can consider the detecting area is like a “sector” with a radius about 6-7 meters.

Other useful description of a PIR sensor (source)

  • Output: Digital pulse high (3V) when triggered (motion detected) digital low when idle (no motion detected). Pulse lengths are determined by resistors and capacitors on the PCB and differ from sensor to sensor.
  • Sensitivity range: Up to 20 feet (6 meters) 110° x 70° detection range. But the page of PIR sensor in Adafruit store also says: “Sensing range is about 7 meters (120 degree cone).”
  • Power supply: 5V-12V input voltage for most modules (they have a 3.3V regulator), but 5V is ideal in case the regulator has different specs.

Parts List

  • Arduino Uno *1
  • Breadboard *1
  • Jumper wires *6
  • 330 ohm resistor *3
  • RGB LED * 1
  • PIR sensor *1

Setup

PIR sensor has 3 pins: voltage, output and ground pins.

Source

On the back of the PIR sensor, we can find that there are some other components. What we need to pay attention to are the Delay Time Adjust and Sensitivity Adjust. The two yellow rotary switches are very important because they determine the sensitivity and delay time when we are detecting the presence of people, which directly affects the final results.

Source

In order to minimize delay time and maximize sensitivity, we may use a screwdriver to rotate the two yellow switches to min” for delay time and max” for sensitivity respectively.

Wiring

After we adjust the PIR sensor, we can set up the circuit with the PIR and the RGB LED.

The following images show how the circuit should be set up in real and its corresponding diagram.

For the PIR sensor:

  • Put the PIR sensor with its “white dome” upward with the three pins to the front. The three pins are voltage, output, ground pins from left to right.
  • Connect the voltage pin (left) to 5V on the breadboard
  • Connect the output pin (middle) to Pin 4
  • Connect the ground pin (right) to GND on the breadboard

  For the RGB LED (The following description is from Circuit 03 sketch):

  • Starting at the flattened edge of the flange on the LED, the pins are ordered RED, COMMON, GREEN, BLUE.
  • Connect RED to a 330 Ohm resistor. Connect the other end of the resistor to Arduino digital pin 13.
  • Connect COMMON pin to GND.
  • Connect GREEN to a 330 Ohm resistor. Connect the other end of the resistor to Arduino digital pin 12.
  • Connect BLUE to a 330 Ohm resistor. Connect the other end of the resistor to Arduino digital pin 11.

Code

The codes create a very simple model, and show basic manipulations of the PIR sensor and the RGB LED to simulate the interaction between PIR and a traffic light. The codes I wrote refer to 1) Circuit 03 sketch from our Arduino tutorial for RGB LED, 2) this website for the basic code for PIR sensor.

There are two tentative scenarios:

Scenario A: Let the PIR detect the presence of people when the vehicle traffic light is green.

If the vehicle traffic light is green, the pedestrians are not allowed to cross the street. So if PIR only works when the vehicle traffic light is green, pedestrian misbehaviors could be specified and drivers will finally know when they are approaching the crosswalk.

  • If PIR detects a pedestrian, the serial monitor will print “Motion has been detected!”
  • If the pedestrian has left the detecting area, the serial monitor will print “Motion has been stopped!”

Scenario B: Let PIR detect the presence of people when a traffic light is working (no matter which light it shows).

This scenario allows PIR to work all the time with the traffic light, no matter which light from the traffic light shows, so putting into the real case, it is not limited to the vehicle traffic light, but can also work with the crosswalk traffic light, because in this scenario, PIR and the traffic light work independently.

  • If PIR detects a pedestrian, the serial monitor will print “Motion has been detected!”
  • If the pedestrian has left the detecting area, the serial monitor will print “Motion has been stopped!”

For both codes, if the PIR works and detects a person who comes and leaves the detecting area, on the serial monitor, it will show:

***ATTENTION: The total time for the RGB LED to show red, green and yellow is 1 minute in my code, which is absolutely not real for a traffic light and only for the purpose of this tutorial. Also in this 1 minute, I set the time for the green light to be 40s, which is also not real. The purpose is to present how PIR works when the traffic light work.***

To summarize, the main difference between the two codes and the two scenarios is to determine when the PIR should work. Personally, I highly recommend the code of  Scenario B because it is easier to use that code to determine when the PIR should work by adjusting the values of “redlight”, “greenlight” and “yellowlight” variables in the global frame. If you want the PIR to work all the way with the RGB LED, you can simply leave all those values equal to 1 as the code shows; but if you want the PIR to work with a specific light of the RGB LED, for example, the green light, you can simply leave “greenlight =1” and change other values to 0.  

Video

Here is the video I made to show the basic circuit, and how the code and the circuit work.

  • I used my hand to represent people.
  • I showed how PIR works only when the RGB LED is green (Scenario A) as the last part of the video: There are results on serial monitor when the RGB LED is green. The results stopped coming out on the serial monitor when the RGB LED turned to be red and yellow.
  • I apologize if there is something unclear from my video, and please feel free to ask me any questions.

Thank you for your time!

References

Leave a Reply

Your email address will not be published. Required fields are marked *