Professional Documents
Culture Documents
IoT - Exp 1
IoT - Exp 1
IoT - Exp 1
: 01
Explanation:
#define LED_PIN 4: This line defines a constant named LED_PIN with a value of 4.
This constant is used later in the code to represent the pin number to which the LED is
connected.
void setup(): The setup() function is called once when the Arduino starts. It is used to
initialize settings, configurations, or pin modes. In this case, it sets the LED_PIN (pin
4) as an OUTPUT.
void loop(): The loop() function is continuously executed after the setup() function. It
contains the main code that runs repeatedly.
digitalWrite(LED_PIN, HIGH): This line turns the LED on by setting the voltage
on the specified pin (LED_PIN) to HIGH (5V).
delay(1000): This line introduces a delay of 1000 milliseconds (1 second), keeping
the LED in the ON state for 1 second.
digitalWrite(LED_PIN, LOW): This line turns the LED off by setting the voltage
on the specified pin (LED_PIN) to LOW (0V).
delay(2000): This line introduces a longer delay of 2000 milliseconds (2 seconds),
keeping the LED in the OFF state for 2 seconds.
Explanation:
Constants Definition:
const int ledPin = 2;: This line defines a constant variable ledPin and sets its value to
2, representing the pin number to which the LED is connected.
const int ldrPin = A0;: This line defines a constant variable ldrPin and sets its value to
A0, representing the analog pin to which the LDR is connected.
Setup() Function:
Serial.begin(9600): Initializes serial communication with a baud rate of 9600 for
debugging purposes.
pinMode(ledPin, OUTPUT);: Sets the LED pin as an OUTPUT.
pinMode(ldrPin, INPUT);: Sets the LDR pin as an INPUT.
loop() Function:
int ldrStatus = analogRead(ldrPin);: Reads the analog value from the LDR and stores
it in the variable ldrStatus.
if (ldrStatus <= 700) { ... }: Checks if the LDR value is less than or equal to 700,
indicating a dark environment.
if true, it turns on the LED, prints a message to the Serial monitor, and displays the
LDR value.
if false, it turns off the LED, prints a different message, and displays the LDR value.
delay(500): Introduces a delay of 500 milliseconds between each iteration of the
loop() to avoid rapid changes in the LED state.
Pin Connections: pin D12 to LED, D2 to SW2, GND (optional)
Result: Watch results on serial monitor.
The program essentially creates a simple light-sensitive circuit where the LED is turned on
when it's dark (LDR value below or equal to 700) and turned off when it's bright. The Serial
monitor is used for debugging by printing the LDR values along with corresponding
messages.