Posted on Leave a comment

Smart City Mini Project Using Arduino UNO

(Street Light Automation, Smart Gate & Air Quality Alert)

Smart cities are built by combining automation, sensing, and decision-making.
In this project, a Smart City model was developed using an Arduino UNO and commonly available sensors to demonstrate how embedded systems can be used for urban automation.

This project focuses on real-world logic, not just blinking LEDs.


📌 Project Demonstration Video

🎥 Watch the complete working demonstration here:

👉 The video shows the automatic street lights, smart gate operation, and air quality alert system working in real time.


Project Overview

The model demonstrates four smart city subsystems working together:

  1. Automatic Street Light System (LDR based)
  2. Smart Entry Gate / Traffic Control (Ultrasonic + Servo)
  3. Air Quality Monitoring System (MQ135 sensor)
  4. Alert System (Buzzer for pollution warning)

Each subsystem works independently but is controlled by a single Arduino UNO, simulating how real smart-city nodes operate.


Components Used

  • Arduino UNO
  • LDR (Light Dependent Resistor – discrete, not module)
  • Ultrasonic Sensor (HC-SR04)
  • Servo Motor (gate control)
  • MQ135 Air Quality Sensor
  • Buzzer
  • LEDs (street lights)
  • 330 Ω resistors (current limiting)
  • Breadboard, jumper wires, external LED wiring

🔌 Circuit Diagram

🧩 Complete circuit diagram for this project:

Open circuit Open circuit

The circuit includes:

  • LDR voltage divider connected to an analog input
  • Ultrasonic sensor connected to digital pins
  • Servo motor connected to a PWM pin
  • MQ135 sensor connected to an analog input
  • Buzzer connected to a digital output
  • LED street lights driven through resistors

Street Light Implementation (Important Design Detail)

To simulate street lights, multiple LEDs were used.

Instead of driving LEDs directly from the Arduino pin, the following safe current-limiting method was implemented:

  • Two GPIO pins were used for street lighting
  • Each GPIO pin was connected through a 330 Ω resistor
  • The other end of each resistor was connected to 4–5 LEDs in parallel

Why this method was used

  • The resistor limits the current drawn from the GPIO pin
  • Using two GPIOs distributes the load instead of stressing a single pin
  • Parallel LEDs represent multiple street lights connected to one junction

This approach keeps the circuit safe for the Arduino while still allowing multiple LEDs to turn ON together during night conditions.


Automatic Street Light Logic (LDR)

  • The LDR continuously measures ambient light
  • During daytime:
    • LDR value is high
    • Street LEDs remain OFF
  • During night:
    • LDR value drops below a threshold
    • Street LEDs turn ON automatically

This demonstrates energy conservation, a core principle of smart cities.


Smart Gate / Traffic Control System

An ultrasonic sensor is placed near the entry point of the model.

  • When a vehicle or object comes within a fixed distance:
    • The ultrasonic sensor detects it
    • The servo motor rotates
    • The gate opens automatically
  • When the object moves away:
    • The gate closes again

This simulates automatic toll gates, parking entry systems, and traffic barriers.


Air Quality Monitoring (MQ135 Sensor)

To introduce environmental monitoring, an MQ135 air quality sensor was added.

  • The sensor measures pollution levels in the surrounding air
  • When the pollution value crosses a preset threshold:
    • The buzzer turns ON
    • This acts as an air pollution warning

This models how pollution monitoring stations work in smart cities.


Alert System (Buzzer)

The buzzer is used as a city alert mechanism.

  • OFF during normal air conditions
  • ON when pollution exceeds safe limits

This introduces the concept of public warning systems and real-time environmental alerts.


💻 Firmware Upload (Arduino Code)

Flash the firmware directly to Arduino UNO:

👉 This allows users to upload the tested firmware without opening the Arduino IDE.

#include <Servo.h>

#define TRIG_PIN 9
#define ECHO_PIN 8
#define LED_PIN  3
#define LED_PIN1 5
#define LDR_PIN  A0
#define MQ135_PIN A1
#define BUZZER_PIN 7
#define SERVO_PIN 6

Servo gateServo;

long duration;
int distance;
int ldrValue;
int airValue;

int LDR_THRESHOLD = 500;     // adjust after testing
int DIST_THRESHOLD = 15;     // cm
int AIR_THRESHOLD  = 400;    // MQ135 threshold (adjust)

void setup() {
  pinMode(TRIG_PIN, OUTPUT);
  pinMode(ECHO_PIN, INPUT);
  pinMode(LED_PIN, OUTPUT);
  pinMode(LED_PIN1, OUTPUT);
  pinMode(BUZZER_PIN, OUTPUT);

  gateServo.attach(SERVO_PIN);
  gateServo.write(90);   // gate closed

  Serial.begin(9600);
  Serial.println("Smart City System Starting...");
}

void loop() {

  /* ---------- LDR STREET LIGHT ---------- */
  ldrValue = analogRead(LDR_PIN);

  if (ldrValue < LDR_THRESHOLD) {
    analogWrite(LED_PIN, 255);     // street light ON
    digitalWrite(LED_PIN1, HIGH);
  } else {
    digitalWrite(LED_PIN, LOW);    // street light OFF
    digitalWrite(LED_PIN1, LOW);
  }

  /* ---------- ULTRASONIC SENSOR ---------- */
  digitalWrite(TRIG_PIN, LOW);
  delayMicroseconds(2);
  digitalWrite(TRIG_PIN, HIGH);
  delayMicroseconds(10);
  digitalWrite(TRIG_PIN, LOW);

  duration = pulseIn(ECHO_PIN, HIGH, 30000);
  distance = duration * 0.034 / 2;

  /* ---------- SERVO GATE CONTROL ---------- */
  if (distance > 0 && distance < DIST_THRESHOLD) {
    gateServo.write(0);    // open gate
  } else {
    gateServo.write(90);   // close gate
  }

  /* ---------- MQ135 AIR QUALITY ---------- */
  airValue = analogRead(MQ135_PIN);

  if (airValue > AIR_THRESHOLD) {
    digitalWrite(BUZZER_PIN, HIGH);   // pollution alert
  } else {
    digitalWrite(BUZZER_PIN, LOW);
  }

  /* ---------- SERIAL MONITOR ---------- */
  Serial.print("LDR: ");
  Serial.print(ldrValue);
  Serial.print(" | Distance: ");
  Serial.print(distance);
  Serial.print(" cm | Air: ");
  Serial.println(airValue);

  delay(1000);
}

Software Logic Summary

The Arduino program continuously performs:

  • Ambient light measurement (LDR)
  • Distance measurement (Ultrasonic)
  • Air quality measurement (MQ135)
  • Decision-making based on thresholds
  • Actuator control (LEDs, servo, buzzer)

All logic runs in a loop, similar to how embedded systems operate in real infrastructure.


Educational Value

This project helps learners understand:

  • GPIO usage and current limiting
  • Sensor interfacing and calibration
  • Real-world automation logic
  • Embedded decision-making
  • Power and safety considerations
  • Smart city concepts beyond theory

Unlike ready-made kits, this setup uses discrete components, encouraging deeper understanding and troubleshooting skills.


Conclusion

This Smart City project demonstrates how simple electronics and embedded programming can be combined to address urban automation challenges such as energy efficiency, traffic control, and pollution monitoring.

It serves as a reference implementation for students and educators exploring smart-city concepts using Arduino.

Posted on Leave a comment

How to make an Obstacle Avoiding Vehicle using Arduino UNO

Arduino Sketch

#define m1p 7
#define m1n 6

#define m2p 5
#define m2n 4

#define echopin 9
#define trigpin 10

void motor_forward();
void motor_stop();
void motor_left();
void motor_right();
void motor_back();

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  pinMode(m1p, OUTPUT);
  pinMode(m1n, OUTPUT);
  pinMode(m2p, OUTPUT);
  pinMode(m2n, OUTPUT);

  pinMode(echopin, INPUT);
  pinMode(trigpin, OUTPUT);
}

long duration, distance;

void loop() {
  // put your main code here, to run repeatedly:

  digitalWrite(trigpin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigpin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigpin, LOW);
  duration = pulseIn(echopin, HIGH);
  distance = (duration *0.0343 / 2);
  Serial.println(distance);
 // Serial.println(" cm");
  //delay(1000);
  //Serial.println(distance);               //print data serially
  if (distance > 25) {                   // if distance is more than 25 move bot forward
    motor_forward();
    delay(20);
    motor_forward(); 
    
  }
  else {
   // motor_stop();
    //delay(500);
   // motor_back();
   // delay(3000);
    motor_stop();                   // if distance is less than 25 move bot right
    delay(50);
    motor_back();
    delay(100);
     motor_right();
    delay(100);
  }
 
}
void motor_back(){
  digitalWrite(m1p, LOW);
  digitalWrite(m1n, HIGH);
  digitalWrite(m2p, HIGH);
  digitalWrite(m2n, LOW);
}
void motor_forward(){
  digitalWrite(m1p, HIGH);
  digitalWrite(m1n, LOW);
  digitalWrite(m2p, LOW);
  digitalWrite(m2n, HIGH);
}
void motor_stop(){ 
  digitalWrite(m1p, LOW);
  digitalWrite(m1n, LOW);
  digitalWrite(m2p, LOW);
  digitalWrite(m2n, LOW);
}
void motor_left(){
  digitalWrite(m1p, HIGH);
  digitalWrite(m1n, LOW);
  digitalWrite(m2p, LOW);
  digitalWrite(m2n, LOW);
}
void motor_right(){
  digitalWrite(m1p, LOW);
  digitalWrite(m1n, LOW);
  digitalWrite(m2p, HIGH);
  digitalWrite(m2n, LOW);
}

Code Explanation

The code is a simple obstacle avoidance program for a robot using an ultrasonic sensor and two motors. Let’s break down the code and explain each section:

#define m1p 7
#define m1n 6
#define m2p 5
#define m2n 4
#define echopin 9
#define trigpin 10

In this section, the code defines constants for motor pins (m1p, m1n, m2p, m2n) and pins for the ultrasonic sensor (echopin for echo and trigpin for trigger).

void motor_forward();
void motor_stop();
void motor_left();
void motor_right();
void motor_back();

Here, the code declares five functions (motor_forward, motor_stop, motor_left, motor_right, motor_back) that will be used to control the movement of the robot.

void setup() {
  Serial.begin(9600);
  pinMode(m1p, OUTPUT);
  pinMode(m1n, OUTPUT);
  pinMode(m2p, OUTPUT);
  pinMode(m2n, OUTPUT);
  pinMode(echopin, INPUT);
  pinMode(trigpin, OUTPUT);
}

In the setup function, the serial communication is initialized, and the pin modes for motor control and the ultrasonic sensor are set.

long duration, distance;

Here, two variables duration and distance are declared to store the duration of the ultrasonic pulse and the calculated distance, respectively.

void loop() {
  digitalWrite(trigpin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigpin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigpin, LOW);
  duration = pulseIn(echopin, HIGH);
  distance = (duration * 0.0343 / 2);
  Serial.println(distance);

  if (distance > 25) {
    motor_forward();
    delay(20);
    motor_forward(); 
  } else {
    motor_stop();
    delay(50);
    motor_back();
    delay(100);
    motor_right();
    delay(100);
  }
}

In the loop function, the ultrasonic sensor is triggered to measure the distance. If the measured distance is greater than 25, the robot moves forward. Otherwise, it stops, moves back a bit, and then turns right.

void motor_back() {
  digitalWrite(m1p, LOW);
  digitalWrite(m1n, HIGH);
  digitalWrite(m2p, HIGH);
  digitalWrite(m2n, LOW);
}

void motor_forward() {
  digitalWrite(m1p, HIGH);
  digitalWrite(m1n, LOW);
  digitalWrite(m2p, LOW);
  digitalWrite(m2n, HIGH);
}

void motor_stop() {
  digitalWrite(m1p, LOW);
  digitalWrite(m1n, LOW);
  digitalWrite(m2p, LOW);
  digitalWrite(m2n, LOW);
}

void motor_left() {
  digitalWrite(m1p, HIGH);
  digitalWrite(m1n, LOW);
  digitalWrite(m2p, LOW);
  digitalWrite(m2n, LOW);
}

void motor_right() {
  digitalWrite(m1p, LOW);
  digitalWrite(m1n, LOW);
  digitalWrite(m2p, HIGH);
  digitalWrite(m2n, LOW);
}

These functions define the motor control logic for moving the robot in different directions. For example, motor_forward makes the robot move forward by setting the appropriate motor pins.

In summary, this code implements a basic obstacle avoidance mechanism for a robot using an ultrasonic sensor. If the robot detects an obstacle within 25 cm, it stops, moves back a bit, and then turns right. Otherwise, it continues moving forward. The motor control is achieved through the defined functions that set the appropriate pin states for the motor driver.

Posted on Leave a comment

Interfacing a 5V Ultrasonic Sensor with 3.3V GPIO of Raspberry Pi Pico: A Voltage Divider Solution

I have an old HC-Sr04 ultrasonic sensor. I don’t know if it’s GPIO voltage compatible with the 3.3V microcontroller.
On the internet, I found that the old sensors work with 5V.

So, I used a voltage divider made of 1K ohm and 1.5K ohm Surface mount resistors. To bring down the 5V to a suitable 3V.

I want to reduce the voltage level on the ECHO pin of the ultrasonic sensor to a safe level for the Raspberry Pi Pico’s GPIO pins. Let’s assume we have chosen resistors R1 and R2 with values of 1K and 1.5K, respectively.

You can also use the voltage divider calculator for this
Voltage Divider Calculator

Using the voltage divider formula, we can calculate the output voltage at the midpoint (E_3) of the voltage divider circuit:

V_out = V_in * (R2 / (R1 + R2))

Since the Vsys pin of the Raspberry Pi Pico provides a voltage of 5V, we can calculate the output voltage:

V_out = 5V * (1.5K / (1K + 1.5K))
= 5V * (1.5K / 2.5K)
= 5V * 0.6
= 3V

With the given resistor values, the output voltage at the midpoint of the voltage divider circuit will be 3V. This 3V output is within the safe voltage range for the GPIO pins of the Raspberry Pi Pico.

Code Implementation:

To implement the interface between the ultrasonic sensor and the Raspberry Pi Pico, we will utilize MicroPython, a lightweight Python implementation for microcontrollers. The following code snippet demonstrates the necessary steps:

from machine import Pin
import utime
trigger = Pin(2, Pin.OUT)
echo = Pin(3, Pin.IN)
def ultra():
   trigger.low()
   utime.sleep_us(2)
   trigger.high()
   utime.sleep_us(10)
   trigger.low()
   while echo.value() == 0:
       signaloff = utime.ticks_us()
   while echo.value() == 1:
       signalon = utime.ticks_us()
   timepassed = signalon - signaloff
   distance = (timepassed * 0.0343) / 2
   print("The distance from object is ",distance,"cm")
   
while True:
   ultra()
   utime.sleep(1)