Jump to content

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

From cognifront

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 Connections

Component Arduino Pin
Push Button 1 5
Push Button 2 4
Push Button 3 2
Push Button 4 3
LED 1 7
LED 2 8
LED 3 9
LED 4 10
  • **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);
}

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);

  // Check which button is pressed and turn ON the corresponding LED
  // Only one LED will turn ON at a time because of the else-if structure
  if(push1Value == HIGH) {
    digitalWrite(ledPin1, HIGH); // Turn ON LED1 if button1 is pressed
  }
  else if(push2Value == HIGH) {
    digitalWrite(ledPin2, HIGH); // Turn ON LED2 if button2 is pressed
  }
  else if(push3Value == HIGH) {
    digitalWrite(ledPin3, HIGH); // Turn ON LED3 if button3 is pressed
  }
  else if(push4Value == HIGH) {
    digitalWrite(ledPin4, HIGH); // Turn ON LED4 if button4 is pressed
  }
  else {
    // If no button is pressed, turn OFF all LEDs
    digitalWrite(ledPin1, LOW);
    digitalWrite(ledPin2, LOW);
    digitalWrite(ledPin3, LOW);
    digitalWrite(ledPin4, LOW);
  } 
}

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.