Jump to content

Experiment 1: Introduction to Arduino and Digital I/O Control

From cognifront
Revision as of 04:15, 9 October 2025 by Siraj (talk | contribs)

Experiment 1: Introduction to Arduino and Digital I/O Control

Objective

To understand digital input/output operations by interfacing LEDs and push buttons with Arduino.

Task

  1. Connect LEDs and push buttons to Arduino.
  2. Write a program to turn LEDs ON/OFF based on button press.
  3. Observe the response on the Serial Monitor.

Circuit Diagram

Circuit Diagram

Board Connections

Board Connections

⚠️ Important Note:
1. Connect only the Arduino Uno (without any component connections) to the computer via USB.
2. Upload the code to the Arduino.
3. Disconnect your Arduino from the computer while making component connections.
4. Make all component connections using patch cords as per the circuit diagram.
5. Then connect the 9V adapter to provide external power supply to the Arduino.


Circuit Connections

Push Button Arduino Pin
Push Button 1 5
Push Button 2 4
Push Button 3 2
Push Button 4 3
Push Button GND GND
Push Button VCC 5V
LED Bar Arduino Pin
LED 1 7
LED 2 8
LED 3 9
LED 4 10
LED GND GND
  • Serial Communication: 9600 baud rate

Arduino Code

// © 2025, Cognifront. Member of The IoT Kits Family.

// Experiment 1: Introduction to Arduino and Digital I/O Control

// Define push button input pins
int push1Pin = 5;
int push2Pin = 4;
int push3Pin = 2;
int push4Pin = 3;

// Variables to store button states
int push1Value;
int push2Value;
int push3Value;
int push4Value;

// Define LED output pins
int ledPin1 = 7;
int ledPin2 = 8;
int ledPin3 = 9;
int ledPin4 = 10;

void setup() {
  // Start serial communication at 9600 baud for debugging
  Serial.begin(9600);

  // Set push button pins as inputs
  pinMode(push1Pin, INPUT);
  pinMode(push2Pin, INPUT);
  pinMode(push3Pin, INPUT);
  pinMode(push4Pin, INPUT);

  // Set LED pins as outputs
  pinMode(ledPin1, OUTPUT);
  pinMode(ledPin2, OUTPUT);
  pinMode(ledPin3, OUTPUT);
  pinMode(ledPin4, OUTPUT);

  Serial.println("System Initialized: Monitoring Push Buttons and LEDs...");
}

void loop() {
  // Read the state of each push button (HIGH = pressed, LOW = not pressed)
  push1Value = digitalRead(push1Pin);
  push2Value = digitalRead(push2Pin);
  push3Value = digitalRead(push3Pin);
  push4Value = digitalRead(push4Pin);

  // Determine LED states and print to Serial Monitor
  if(push1Value == HIGH) {
    digitalWrite(ledPin1, HIGH);
    digitalWrite(ledPin2, LOW);
    digitalWrite(ledPin3, LOW);
    digitalWrite(ledPin4, LOW);
    Serial.println("Button1 PRESSED → LED1 ON");
  }
  else if(push2Value == HIGH) {
    digitalWrite(ledPin1, LOW);
    digitalWrite(ledPin2, HIGH);
    digitalWrite(ledPin3, LOW);
    digitalWrite(ledPin4, LOW);
    Serial.println("Button2 PRESSED → LED2 ON");
  }
  else if(push3Value == HIGH) {
    digitalWrite(ledPin1, LOW);
    digitalWrite(ledPin2, LOW);
    digitalWrite(ledPin3, HIGH);
    digitalWrite(ledPin4, LOW);
    Serial.println("Button3 PRESSED → LED3 ON");
  }
  else if(push4Value == HIGH) {
    digitalWrite(ledPin1, LOW);
    digitalWrite(ledPin2, LOW);
    digitalWrite(ledPin3, LOW);
    digitalWrite(ledPin4, HIGH);
    Serial.println("Button4 PRESSED → LED4 ON");
  }
  else {
    // If no button is pressed, turn OFF all LEDs
    digitalWrite(ledPin1, LOW);
    digitalWrite(ledPin2, LOW);
    digitalWrite(ledPin3, LOW);
    digitalWrite(ledPin4, LOW);
    Serial.println("No button pressed → All LEDs OFF");
  }

  // Print raw pushbutton states for debugging
  Serial.print("Buttons: ");
  Serial.print(push1Value);
  Serial.print(" ");
  Serial.print(push2Value);
  Serial.print(" ");
  Serial.print(push3Value);
  Serial.print(" ");
  Serial.println(push4Value);

  delay(500); // Small delay for better readability in Serial Monitor
}

Observation

  • Pressing any button will turn ON the corresponding LED.
  • Only one LED lights up at a time because of the else-if structure.
  • Serial Monitor can be used to confirm button press readings.

Conclusion

This experiment demonstrates basic digital input and output control using Arduino.