ESP32-Based Smart Traffic Monitoring and Environmental Sensing System with Wi-Fi Connectivity
View and Edit Project In Cirkit Designer
Circuit Documentation
Summary
This circuit is designed to monitor various environmental parameters and control devices based on sensor inputs. It uses an ESP32 microcontroller to interface with multiple sensors and actuators, including a DHT11 temperature and humidity sensor, an HC-SR04 ultrasonic sensor, a sound sensor, an MQ135 air quality sensor, a PIR sensor, a 16x2 I2C LCD, two micro servos, a traffic light module, a buzzer, and two photocells (LDRs). The data collected is sent to a Google Sheet for logging and analysis.
Component List
ESP32 (30 pin)
- Description: Microcontroller with Wi-Fi and Bluetooth capabilities.
- Pins: EN, VP, VN, D34, D35, D32, D33, D25, D26, D27, D14, D12, D13, GND, Vin, D23, D22, TX0, RX0, D21, D19, D18, D5, TX2, RX2, D4, D2, D15, 3V3
HC-SR04 Ultrasonic Sensor
- Description: Measures distance using ultrasonic waves.
- Pins: VCC, TRIG, ECHO, GND
16x2 I2C LCD
- Description: Displays information on a 16x2 character screen.
- Pins: GND, VCC, SDA, SCL
Sound Sensor
- Description: Detects sound levels.
- Pins: Analog, GND, VCC, Digital
DHT11
- Description: Measures temperature and humidity.
- Pins: DATA, GND, VCC
Micro servo 9G
- Description: Small servo motor for precise control.
- Pins: GND, +5V, PWM
PIR sensor
- Description: Detects motion.
- Pins: VDD, SIG, GND
Photocell (LDR)
- Description: Light-dependent resistor for measuring light intensity.
- Pins: pin 0, pin 1
Traffic Light
- Description: Simulates a traffic light with red, yellow, and green LEDs.
- Pins: Green, Yellow, Red, GND
MQ135
- Description: Measures air quality.
- Pins: VCC, GND, A0, D0
Resistor
- Description: Provides resistance in the circuit.
- Pins: pin1, pin2
- Properties: Resistance: 2000 Ohms
Buzzer
- Description: Emits sound when activated.
- Pins: PIN, GND
Wiring Details
ESP32 (30 pin)
- D4: Connected to DHT11 DATA
- D34: Connected to Sound Sensor Analog
- D35: Connected to MQ135 A0
- RX2: Connected to PIR sensor SIG
- Vin: Connected to VCC of multiple components (PIR sensor, DHT11, Sound Sensor, MQ135, Micro servo 9G, 16x2 I2C LCD, HC-SR04 Ultrasonic Sensor, Photocell (LDR))
- GND: Connected to GND of multiple components (PIR sensor, DHT11, Sound Sensor, MQ135, Micro servo 9G, Traffic Light, 16x2 I2C LCD, Buzzer, HC-SR04 Ultrasonic Sensor, Resistor)
- D13: Connected to Micro servo 9G PWM
- D12: Connected to Traffic Light Green
- D14: Connected to Traffic Light Red
- D27: Connected to Traffic Light Yellow
- D15: Connected to Buzzer PIN
- D21: Connected to 16x2 I2C LCD SDA
- D22: Connected to 16x2 I2C LCD SCL
- D18: Connected to HC-SR04 Ultrasonic Sensor ECHO
- D5: Connected to HC-SR04 Ultrasonic Sensor TRIG
- D19: Connected to Micro servo 9G PWM
- D33: Connected to Resistor pin1 and Photocell (LDR) pin 1
- D32: Connected to Resistor pin1 and Photocell (LDR) pin 1
HC-SR04 Ultrasonic Sensor
- VCC: Connected to ESP32 Vin
- TRIG: Connected to ESP32 D5
- ECHO: Connected to ESP32 D18
- GND: Connected to ESP32 GND
16x2 I2C LCD
- GND: Connected to ESP32 GND
- VCC: Connected to ESP32 Vin
- SDA: Connected to ESP32 D21
- SCL: Connected to ESP32 D22
Sound Sensor
- Analog: Connected to ESP32 D34
- GND: Connected to ESP32 GND
- VCC: Connected to ESP32 Vin
DHT11
- DATA: Connected to ESP32 D4
- GND: Connected to ESP32 GND
- VCC: Connected to ESP32 Vin
Micro servo 9G
- GND: Connected to ESP32 GND
- +5V: Connected to ESP32 Vin
- PWM: Connected to ESP32 D13
PIR sensor
- VDD: Connected to ESP32 Vin
- SIG: Connected to ESP32 RX2
- GND: Connected to ESP32 GND
Photocell (LDR)
- pin 0: Connected to ESP32 Vin
- pin 1: Connected to Resistor pin1 and ESP32 D33
Traffic Light
- Green: Connected to ESP32 D12
- Yellow: Connected to ESP32 D27
- Red: Connected to ESP32 D14
- GND: Connected to ESP32 GND
MQ135
- VCC: Connected to ESP32 Vin
- GND: Connected to ESP32 GND
- A0: Connected to ESP32 D35
Resistor
- pin1: Connected to Photocell (LDR) pin 1 and ESP32 D33
- pin2: Connected to ESP32 GND
Buzzer
- PIN: Connected to ESP32 D15
- GND: Connected to ESP32 GND
Code Documentation
#include <Arduino.h>
#include "time.h"
#include <ESP_Google_Sheet_Client.h>
#include <ESP32Servo.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <DHT.h>
#include <WiFi.h>
// Define pin mappings
#define BUZZER2_PIN 15 // Buzzer for MQ (air quality) and DHT (temperature/humidity)
#define TRIG_PIN 5 // Ultrasonic trigger
#define ECHO_PIN 18 // Ultrasonic echo
#define PIR_PIN 16 // PIR sensor for recycle bin
#define DHT_PIN 4 // DHT11 temperature and humidity sensor
#define MQ135_PIN 35 // MQ135 air quality sensor
#define SOUND_PIN 34 // Sound sensor
#define SERVO1_PIN 13 // Servo for recycle bin
#define SERVO2_PIN 19 // Servo for solar tracker
#define RED_LED_PIN 14 // Red LED for traffic light
#define GREEN_LED_PIN 12 // Green LED for traffic light
#define YELLOW_LED_PIN 27 // Yellow LED for traffic light
#define SDA_PIN 21 // LCD SDA pin
#define SCL_PIN 22 // LCD SCL pin
#define BIG_LDR_PIN 32 // Solar tracker LDR (big)
#define SMALL_LDR_PIN 33 // Solar tracker LDR (small)
// WIFI & Firebase Credentials
#define WIFI_SSID "TonyJR"
#define WIFI_PASSWORD "003456789"
// Google Project ID
#define PROJECT_ID "traffic-monitoring-system-data"
// Service Account's client email
#define CLIENT_EMAIL "traffic-storage-data@traffic-monitoring-system-data.iam.gserviceaccount.com"
// Service Account's private key
const char PRIVATE_KEY[] PROGMEM =
"-----BEGIN PRIVATE KEY-----\n"
"MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQCRsOXK9keH5MwN\n"
"4NMUVvyCvxxBMxNNNbbYBUFkqF0Ag/1xtqtVZHgrpD86giSpQ+J1GmtwRYqQp2LU\n"
"3kQb/ZDCpgDLHmdrnsvrKZ+OVLd3HLvBDursibPxzgWGbDBdC9Sf7xO