Itzel Nunez-arizaBrittany ArmstrongJonah Martin
Published

Motion Sensor Traffic Light

No more waiting for the light to turn green at an empty intersection with motion sensor triggered traffic lights.

IntermediateFull instructions provided828
Motion Sensor Traffic Light

Things used in this project

Hardware components

Argon
Particle Argon
×3
Jumper wires (generic)
Jumper wires (generic)
×1
Breadboard (generic)
Breadboard (generic)
×1
Photo resistor
Photo resistor
×1
LED (generic)
LED (generic)
×1
Adafruit PIR Human Sensor
×1
SparkFun Push button
×1
Resistor 220 ohm
Resistor 220 ohm
×2
Traffic Light
×1

Software apps and online services

Particle Build Web IDE
Particle Build Web IDE

Story

Read more

Schematics

Schematic of Traffic Light and Motion Sensor

This is the wiring and setup for Particle Argons 1 and 3 that control the motion sensor and traffic lights.

Schematic of Push Button and Photoresistor

This is the wiring and setup of the photoresistor and pushbutton that are connected to Particle Argon 2.

Code

Motion Sensor

C/C++
This code is flashed onto particle argon 1. It publishes the data from the motion sensor and also graphs the data.
// This #include statement was automatically added by the Particle IDE.
#include <ArduinoJson.h>

#include <HttpClient.h>

#include "Particle.h"
#include "HttpClient.h"

// Declare an instance of the HttpClient class
HttpClient http;

// Replace these values with your own Beebotte API key and secret
const char* apiKey = -----
const char* secretKey = -----


// Channel and Resource Information for Beebotte
const char* channel = "Vehicle_Detection";
const char* resource = "detection";
const char* triggerCountResource = "trafficdensity";

// pins for connecting the motion sensor to the argon
int motionPin = A3; // ----------------------------------------------------------------------------------------------
int motionState = 0;
unsigned long lastTriggerTime = 0;
const unsigned long triggerDelay = 20000;

int triggerCount = 0;

void setup() {
    pinMode(motionPin, INPUT);
}

void loop() {
    motionState = digitalRead(motionPin);
    if (motionState == HIGH && millis() - lastTriggerTime > triggerDelay) {
        Particle.publish("vehicle_detected", "1", PRIVATE);
        lastTriggerTime = millis();

        // Write data to the Vehicle_Detection channel and detection resource
        writeData(channel, resource, 1);
        writeData(channel, resource, 0);

        // Add additional code here to write more data to Beebotte
        // For example:
        // writeData(channel, "trafficdensity", "");

        // Write the value of triggerCount to Beebotte if triggerCount > 0
        if (triggerCount > 0) {
            writeData(channel, triggerCountResource, triggerCount);
        }

        // Reset the trigger count
        triggerCount = 0;
    } else if (motionState == HIGH) {
        // Increment the trigger count
        triggerCount++;
    }
}

void writeData(const char* channel, const char* resource, int value) {
    http_header_t headers[] = {
        { "Content-Type", "application/json" },
        { "X-Auth-Token", secretKey },
        { NULL, NULL } // NOTE: Always terminate headers with NULL
    };

    http_request_t request;
    request.hostname = "api.beebotte.com";
    request.port = 80;

    String path = "/v1/data/write/";
    path += channel;
    path += "/";
    path += resource;
    request.path = path;

    StaticJsonDocument<200> jsonBuffer;
    jsonBuffer["data"] = value;
    char requestBody[200];
    serializeJson(jsonBuffer, requestBody, sizeof(requestBody));
    request.body = requestBody;

    http_response_t response;
    http.post(request, response, headers);
}

Crosswalk

C/C++
This code is flashed onto particle argon 2 and publishes when the crosswalk button is pushed.
int buttonPin = D10;

void setup()
{
  pinMode( buttonPin , INPUT_PULLUP); 
}

void loop()
{
delay(100);
   int buttonState = digitalRead( buttonPin );
  if( buttonState == LOW )
  {
    Particle.publish("button-pushed", "Pedestrian at crosswalk", PRIVATE);
  }
}

Street Light

C/C++
This code is flashes onto Particle Argon 2 which turns on the LED light when there is no light.
int led1 = A5; 
int led2 = D7; 
int photoresistor = A0;
int analogValue;

void setup() {

  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
pinMode(photoresistor, INPUT);
}

void loop() {

analogValue = analogRead(photoresistor);
if (analogValue > 1) {
    digitalWrite(led1, LOW);
    digitalWrite(led2, LOW);
}else{
    digitalWrite(led1, HIGH);
    digitalWrite(led2, HIGH);
}
}

Traffic Light

C/C++
This code is flashes onto particle argon 3. It is responsible for taking the data from the motion sensor and the push button and responding accordingly by using the subscribe function.
// This #include statement was automatically added by the Particle IDE.
#include <ArduinoJson.h>

// This #include statement was automatically added by the Particle IDE.
#include <HttpClient.h>
#include "Particle.h"

// Declare an instance of the HttpClient class
HttpClient http;

// Replace these values with your own Beebotte API key and secret
const char* apiKey = ---------;
const char* secretKey = ------;


// Channel and Resource Information for Beebotte
const char* channel = "Vehicle_Detection";
const char* redresource = "redLED";
const char* greenresource = "greenLED";
const char* yellowresource = "yellowLED";

int redLed = D6;
int yellowLed = D5;
int greenLed = D4;

int red_on = 0;
int green_on = 0;
int yellow_on = 0;

void setup() {
  pinMode(redLed, OUTPUT);
  pinMode(yellowLed, OUTPUT);
  pinMode(greenLed, OUTPUT);

  digitalWrite(redLed, HIGH); // set red LED on by default
  red_on = 1;
  writeData(channel, redresource, red_on);

  Particle.subscribe("vehicle_detected", myHandler);
  Particle.subscribe("button-pushed", myHandler2);
}

void myHandler(const char *event, const char *data) {
  delay(2000);
  digitalWrite(redLed, LOW);
  red_on = 0;
  writeData(channel, redresource, red_on);
  digitalWrite(greenLed, HIGH);
  green_on = 1;
  writeData(channel, greenresource, green_on);
  delay(15000);
  digitalWrite(greenLed, LOW);
  green_on = 0;
  writeData(channel, greenresource, green_on);
  digitalWrite(yellowLed, HIGH);
  yellow_on = 1;
  writeData(channel, yellowresource, yellow_on);
  delay(3000);
  digitalWrite(yellowLed, LOW);
  yellow_on = 0;
  writeData(channel, yellowresource, yellow_on);
  digitalWrite(redLed, HIGH);
  red_on = 1;
  writeData(channel, redresource, red_on);
}

void myHandler2(const char *event, const char *data) {
    delay(2000);
    digitalWrite(redLed, LOW);
    red_on = 0;
    writeData(channel, redresource, red_on);
    digitalWrite(greenLed, LOW);
    green_on = 0;
    writeData(channel, greenresource, green_on);
    digitalWrite(yellowLed, HIGH);
    yellow_on = 1;
    writeData(channel, yellowresource, yellow_on);
    delay(2000);
    digitalWrite(redLed, HIGH);
    red_on = 1;
    writeData(channel, redresource, red_on);
    green_on = 0;
    writeData(channel, greenresource, green_on);
    yellow_on = 0;
    writeData(channel, yellowresource, yellow_on);
    digitalWrite(greenLed, LOW);
    digitalWrite(yellowLed, LOW);
    delay(10000);
    }

void loop() {
  // Nothing to do here
}

void writeData(const char* channel, const char* resource, int value) {
    http_header_t headers[] = {
        { "Content-Type", "application/json" },
        { "X-Auth-Token", secretKey },
        { NULL, NULL } // NOTE: Always terminate headers with NULL
    };

    http_request_t request;
    request.hostname = "api.beebotte.com";
    request.port = 80;

    String path = "/v1/data/write/";
    path += channel;
    path += "/";
    path += resource;
    request.path = path;

    StaticJsonDocument<200> jsonBuffer;
    jsonBuffer["data"] = value;
    char requestBody[200];
    serializeJson(jsonBuffer, requestBody, sizeof(requestBody));
    request.body = requestBody;

    http_response_t response;
    http.post(request, response, headers);
}

Credits

Itzel Nunez-ariza

Itzel Nunez-ariza

1 project • 0 followers
Brittany Armstrong

Brittany Armstrong

1 project • 0 followers
Jonah Martin

Jonah Martin

1 project • 1 follower

Comments