IoT - Exp 1

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

Experiment No.

: 01

i) To interface LED/Buzzer with Arduino/Raspberry Pi and write a program to “turn


ON” LED for 1 sec after every 2 seconds.
Program:
#define LED_PIN 4
void setup()
{
// put your setup code here, to run once:
pinMode(LED_PIN,OUTPUT);
}
void loop()
{
digitalWrite(LED_PIN,HIGH);
delay(1000);
digitalWrite(LED_PIN,LOW);
delay(2000);
}

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.

Pin Connections: connect pin 9 of Arduino board to LED (RED/BLUE/GREEN)

Result: LED is ON for 1 second and OFF for 2 seconds.


The overall effect of the code is that the LED connected to pin 4 will turn on for 1 second,
then turn off for 2 seconds, creating a simple blinking pattern. This pattern will continue
indefinitely as the loop() function runs continuously.
Experiment No.: 01

ii) To interface Push button/Digital sensor (IR/LDR) with Arduino/Raspberry Pi and


write a program to “turn ON” LED when push button is pressed or at sensor
detection.
Program:
const int ledPin = 2;
const int ldrPin = A0;
void setup()
{
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
pinMode(ldrPin, INPUT);
}
void loop()
{
int ldrStatus = analogRead(ldrPin);
if (ldrStatus <= 700)
{
digitalWrite(ledPin, HIGH);
Serial.print("Its DARK,The LED is Turned ON: ");
Serial.println(ldrStatus);
}
else
{
digitalWrite(ledPin, LOW);
Serial.print("Its BRIGHT,The LED is Turned OFF : ");
Serial.println(ldrStatus);
}
delay(500);
}

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.

You might also like