The transition towards smart cities begins with small, intelligent systems that optimize resources and increase efficiency. An automatic street light control system is a perfect example of this principle in action. By using an Arduino as the brain, we can create a device that not only conserves energy but also demonstrates the core concepts of the Internet of Things (IoT). This project is a practical introduction to automation, teaching you how to gather data from the environment and trigger a physical response.
The system's logic is beautifully simple. A light sensor, known as an LDR, acts as the system's "eye, " constantly measuring the ambient light. When the sensor detects darkness falling below a specific threshold, the Arduino commands an LED to turn on. At dawn, when the light level rises again, the system automatically switches the light off. This eliminates human intervention and wasted electricity.
Detailed Component BreakdownArduino Uno R3: The project's microcontroller brain. It reads inputs, processes data based on your code, and controls the output. Its 6 analog input pins are crucial for reading the light sensor's precise value.
- Arduino Uno R3: The project's microcontroller brain. It reads inputs, processes data based on your code, and controls the output. Its 6 analog input pins are crucial for reading the light sensor's precise value.
LDR (Light Dependent Resistor): This is the key sensor. Its electrical resistance varies with light intensity; high in darkness, low in brightness. This property allows it to detect day and night cycles.
- LDR (Light Dependent Resistor): This is the key sensor. Its electrical resistance varies with light intensity; high in darkness, low in brightness. This property allows it to detect day and night cycles.
Resistors:
10kΩ Resistor: Used to create a "voltage divider" circuit with the LDR. This converts the LDR's variable resistance into a variable voltage that the Arduino's analog pin can read.
- 10kΩ Resistor: Used to create a "voltage divider" circuit with the LDR. This converts the LDR's variable resistance into a variable voltage that the Arduino's analog pin can read.
220Ω Resistor: A current-limiting resistor. It protects the LED from burning out by restricting the amount of current flowing from the Arduino pin.
- 220Ω Resistor: A current-limiting resistor. It protects the LED from burning out by restricting the amount of current flowing from the Arduino pin.
- Resistors:10kΩ Resistor: Used to create a "voltage divider" circuit with the LDR. This converts the LDR's variable resistance into a variable voltage that the Arduino's analog pin can read.220Ω Resistor: A current-limiting resistor. It protects the LED from burning out by restricting the amount of current flowing from the Arduino pin.
LED (5mm): Acts as our miniature street light. It is the output device that is controlled by the Arduino's command.
- LED (5mm): Acts as our miniature street light. It is the output device that is controlled by the Arduino's command.
Breadboard & Jumper Wires: These provide the platform and connections for building the circuit without soldering, making prototyping and modifications easy.
- Breadboard & Jumper Wires: These provide the platform and connections for building the circuit without soldering, making prototyping and modifications easy.
The code, or "sketch, " is what transforms a simple circuit into an intelligent system. It is written in the Arduino Integrated Development Environment (IDE).
How the Code Works:
In the setup()
function, we define the LED pin as an OUTPUT
and start serial communication for monitoring.
- In the
setup()
function, we define the LED pin as anOUTPUT
and start serial communication for monitoring.
The loop()
function runs continuously. It first reads the analog value from the LDR sensor (pin A0). This value ranges from 0 (dark) to 1023 (bright).
- The
loop()
function runs continuously. It first reads the analog value from the LDR sensor (pin A0). This value ranges from 0 (dark) to 1023 (bright).
The code then uses an if
else
statement to make a decision.
If the sensor reading is below a set threshold (e.g., 500, which you can adjust), it means it's dark. The Arduino sets the LED pin to HIGH
, turning the light ON.
- If the sensor reading is below a set threshold (e.g., 500, which you can adjust), it means it's dark. The Arduino sets the LED pin to
HIGH
, turning the light ON.
Else (meaning the reading is above the threshold), it sets the LED pin to LOW
, turning the light OFF.
- Else (meaning the reading is above the threshold), it sets the LED pin to
LOW
, turning the light OFF. - The code then uses an
if
else
statement to make a decision.If the sensor reading is below a set threshold (e.g., 500, which you can adjust), it means it's dark. The Arduino sets the LED pin toHIGH
, turning the light ON.Else (meaning the reading is above the threshold), it sets the LED pin toLOW
, turning the light OFF.
A small delay ensures stability between readings.
- A small delay ensures stability between readings.
cpp
// Automatic Street Light Control System
// Code for Arduino Uno
// Define the pin numbers
const int ldrPin = A0; // LDR connected to Analog pin A0
const int ledPin = 13; // LED connected to Digital pin 13
// Variables for sensor value and threshold
int sensorValue = 0;
int threshold = 500; // Adjust this value based on testing
void setup() {
pinMode(ledPin, OUTPUT); // Set the LED pin as output
Serial.begin(9600); // Start serial communication for debugging
}
void loop() {
sensorValue = analogRead(ldrPin); // Read the LDR value (0-1023)
// Print the sensor value to Serial Monitor
Serial.print("LDR Value: ");
Serial.println(sensorValue);
// Decision Making: Turn LED ON if dark, OFF if bright
if (sensorValue < threshold) {
digitalWrite(ledPin, HIGH); // Turn LED ON
Serial.println("It's DARK - LED ON");
} else {
digitalWrite(ledPin, LOW); // Turn LED OFF
Serial.println("It's BRIGHT - LED OFF");
}
delay(500); // Wait for half a second before next reading
}
By building this circuit and uploading this code, you create a miniature smart city component. You can
Comments