Source Code
IoT 23 Sept 2025

Write a program to interface Light Dependent Resistor (LDR) and LED with Arduino board. Whenever there is sufficient light falls on LDR the LED is off and when there is dark around LDR the LED is put on.

LED is off and when there is dark around

              
  int ldrPin = A0;
  int ledPin = 9;
  int ldrValue = 0;
  int threshold = 500;
  
  void setup() {
      pinMode(ledPin, OUTPUT);
      Serial.begin(9600);
  }
  
  void loop() {
      ldrValue = analogRead(ldrPin);
      Serial.println(ldrValue);
    
    if (ldrValue < threshold) {
          digitalWrite(ledPin, HIGH);
    } else {
          digitalWrite(ledPin, LOW);
    }
    
      delay(100);
  }
              
            

▶️ Watch Now on YouTube
IoT 15 Aug 2025

Write a program to interface Light Dependent Resistor (LDR) and display the values read on the Serial monitor after delay of 2 seconds each.

interface Light Dependent Resistor

              
  // Reads LDR values and prints them to Serial Monitor

  int ldrPin = A0;   // LDR connected to analog pin A0
  int ldrValue = 0;  // Variable to store LDR value

  void setup() {
      Serial.begin(9600);  // Start Serial communication
  }

  void loop() {
      ldrValue = analogRead(ldrPin);  
      Serial.print("LDR Value: ");
      Serial.println(ldrValue);

      delay(1000); // Wait for 1 seconds
  }
              
            

▶️ Watch Now on YouTube
IoT 12 Aug 2025

Write a program to interface LEDs at pins 10,11,12,13 and button at pins 7. The press of button changes the pattern of LED glow. (considering four patterns of LED glow)

changes the pattern of LED glow

              
  // The press of button changes the pattern of LED glow

  const int ledPins[] = {10, 11, 12, 13};
  const int buttonPin = 7;

  int currentPattern = 0;
  int lastButtonState = 0; // For button state check

  void setup() {
    for (int i = 0; i < 4; i++) {
          pinMode(ledPins[i], OUTPUT);
    }
      pinMode(buttonPin, INPUT);
  }

  void loop() {
      int buttonState = digitalRead(buttonPin);

    if (buttonState == HIGH && lastButtonState == LOW) {
          currentPattern = (currentPattern + 1) % 4;
          delay(300); // debounce
    }
      lastButtonState = buttonState;

      runPattern(currentPattern);
  }

  void runPattern(int p) {
    switch (p) {
          case 0:
            // Pattern 1: Forward running light
      for (int i = 0; i < 4; i++) {
                clearLEDs();
                digitalWrite(ledPins[i], HIGH);
                delay(150);
      }
            break;

          case 1:
            // Pattern 2: Forward + Backward running light
      for (int i = 0; i < 4; i++) {
                clearLEDs();
                digitalWrite(ledPins[i], HIGH);
                delay(150);
      }
      for (int i = 2; i >= 1; i--) {
                clearLEDs();
                digitalWrite(ledPins[i], HIGH);
                delay(150);
      }
            break;

          case 2:
            // Pattern 3: Outside to Inside
            clearLEDs();
            digitalWrite(ledPins[0], HIGH);
            digitalWrite(ledPins[3], HIGH);
            delay(200);
            clearLEDs();
            digitalWrite(ledPins[1], HIGH);
            digitalWrite(ledPins[2], HIGH);
            delay(200);
            break;

          case 3:
            // Pattern 4: Quick strobe effect
      for (int j = 0; j < 10; j++) { // blink 10 times
                setAllLEDs(HIGH);
                delay(600);
                setAllLEDs(LOW);
                delay(600);
      }
            break;
    }
  }

  void clearLEDs() {
    for (int i = 0; i < 4; i++) {
          digitalWrite(ledPins[i], LOW);
    }
  }

  void setAllLEDs(int state) {
    for (int i = 0; i < 4; i++) {
          digitalWrite(ledPins[i], state);
    }
  }
              
            

▶️ Watch Now on YouTube
IoT 3 Aug 2025

Write a program to interface LEDs at pins 10,11,12,13 and buttons at pins 7,8. When first time button at pin 7(increment button) is pressed first LED at pin 10 is switched on, when second time button is pressed the next LED at 11 is switched on. Similarly, when the button at pin 8 (decrement button) is pressed the LEDs are switched off sequentially.

LEDs are switched on and off sequentially

              
  // LED Control using Increment and Decrement Buttons
  const int ledPins[] = {10, 11, 12, 13};
  const int plushBtn = 7;
  const int minusBtn = 8;

  int ledIndex = 0;
  bool plushBtnState = HIGH;
  bool minusBtnState = HIGH;

  void setup() {
    for (int i = 0; i < 4; i++) {
          pinMode(ledPins[i], OUTPUT);
          digitalWrite(ledPins[i], LOW);
    }
      pinMode(plushBtn, INPUT);
      pinMode(minusBtn, INPUT);
  }

  void loop() {
      bool currentInc = digitalRead(plushBtn);
      bool currentDec = digitalRead(minusBtn);

      // Increment button pressed
    if (plushBtnState == HIGH && currentInc == LOW) {
      if (ledIndex < 4) {
              digitalWrite(ledPins[ledIndex], HIGH);
              ledIndex++;
      }
          delay(200);
    }

      // Decrement button pressed
    if (minusBtnState == HIGH && currentDec == LOW) {
      if (ledIndex > 0) {
              ledIndex--;
              digitalWrite(ledPins[ledIndex], LOW);
      }
          delay(200);
    }

      plushBtnState = currentInc;
      minusBtnState = currentDec;
  }
              
            

▶️ Watch Now on YouTube
IoT 1 Aug 2025

Write a program to interface Button, buzzer and LED, whenever the button is pressed the buzzer gives beep for 100ms and LED status is toggled.

 interface Button, buzzer and LED

              
  // Buzzer gives beep for 100ms and LED status is toggled

  int ledState = LOW;
  int buttonState = 0;
  int lastButtonState = LOW;

  void setup() {
      pinMode(2, INPUT);
      pinMode(12, OUTPUT);
      pinMode(13, OUTPUT);
  }

  void loop() {
      buttonState = digitalRead(2);

    if (buttonState == HIGH && lastButtonState == LOW) {
          ledState = !ledState;
          digitalWrite(13, ledState);
          digitalWrite(12, HIGH);
          delay(100);
          digitalWrite(12, LOW);
    }
      lastButtonState = buttonState;
      delay(100); 
  }
              
            

▶️ Watch Now on YouTube
IoT 1 Aug 2025

Write a program to interface Button and LED, so that LED blinks/glow when button is pressed.

interface Button and LED

              
  // LED blinks/glow when button is pressed

  int buttonState = 0;

  void setup() {
      pinMode(2, INPUT);
      pinMode(13, OUTPUT);
  }

  void loop() {
      buttonState = digitalRead(2);
    if (buttonState == HIGH) {
          digitalWrite(13, HIGH);
    } else {
          digitalWrite(13, LOW);
    }
      delay(100);
  }
              
            

▶️ Watch Now on YouTube
IoT 1 Aug 2025

Write a program to interface LED and Buzzer with Arduino board, so that buzzer is put on whenever LED is on and Buzzer is put off when LED is off.

interface LED and Buzzer with Arduino board

              
  // LED and Buzzer is on and off delay of 1 sec

  void setup() {
      pinMode(13, OUTPUT);
      pinMode(12, OUTPUT);
  }

  void loop() {
      digitalWrite(13, HIGH);  
      digitalWrite(12, HIGH); 
      delay(1000);             

      digitalWrite(13, LOW);      
      digitalWrite(12, LOW);   
      delay(1000);                   
  }
              
            

▶️ Watch Now on YouTube
IoT 1 Aug 2025

Write a program to interface buzzer with Arduino board to buzz on/off with the delay of 1sec.

interface buzzer with Arduino board

              
  // Buzz on/off with the delay of 1sec
  
  void setup()
  {
      pinMode(13, OUTPUT);
  }

  void loop()
  {
      digitalWrite(13, HIGH);
      delay(1000);
      digitalWrite(13, LOW);
      delay(1000); 
  }
              
            

▶️ Watch Now on YouTube
IoT 30 Jul 2025

Write a program to run pattern(s) on LEDs connect at pins 10,11,12,13. Pattern example:

program to run pattern

              
  int leds[] = {10, 11, 12, 13};

  // Each row represents a step in the pattern
  int pattern[][4] = {
    {1, 0, 0, 0},
    {0, 1, 0, 0},
    {0, 0, 1, 0},
    {0, 0, 0, 1},
    {1, 1, 0, 0},
    {0, 1, 1, 0},
    {0, 0, 1, 1}
  };

  int steps = sizeof(pattern) / sizeof(pattern[0]);

  void setup() {
    for (int i = 0; i < 4; i++) {
          pinMode(leds[i], OUTPUT);
    }
  }

  void loop() {
    for (int i = 0; i < steps; i++) {
      for (int j = 0; j < 4; j++) {
              digitalWrite(leds[j], pattern[i][j]);
      }
          delay(3000);
    }
  }
              
            

▶️ Watch Now on YouTube
IoT 28 Jul 2025

Write a program to interface LEDs on pin no. 10,11,12,13 and blink alternatively at the delay of 1 sec.

LEDs on pin no. 10,11,12,13 and blink alternatively

              
  // Blinks 4 LEDs one-by-one

  void setup() {
    for (int i = 10; i <= 13; i++) {
          pinMode(i, OUTPUT);
    }
  }
  
  void loop() {
    for (int i = 10; i <= 13; i++) {
          // Turn OFF all LEDs
      for (int j = 10; j <= 13; j++) {
              digitalWrite(j, LOW);
      }
          // Turn ON current LED
          digitalWrite(i, HIGH);
          delay(1000);
    }
  }                
              
            

▶️ Watch Now on YouTube
IoT 26 Jul 2025

Write a program to Blink default Light Emitting Diode(LED) on Arduino board with the delay of 2 sec.

Blink default Light Emitting Diode

              
  // LED Blink with 2 Second Delay

  void setup() {
    // Pin 13 ko output mode me set karna
    pinMode(13, OUTPUT);
  }
  
  void loop() {
    // LED ON karna
    digitalWrite(13, HIGH);
    delay(2000); // 2 second delay
  
    // LED OFF karna
    digitalWrite(13, LOW);
    delay(2000); // 2 second delay
  }                
              
            

▶️ Watch Now on YouTube