Lois
Published © CERN-OHL2

Arduino LED and LCD Display Circuit

This circuit uses an Arduino UNO to control an LED and display its status and a switch status on an HD44780 LCD.

BeginnerProtip2 hours98

Things used in this project

Story

Read more

Schematics

Arduino LED and LCD Display Circuit

This circuit uses an Arduino UNO to control an LED and display its status and a switch status on an HD44780 LCD.

Code

Arduino LED and LCD Display Circuit

Arduino
This circuit uses an Arduino UNO to control an LED and display its status and a switch status on an HD44780 LCD.
#include <LiquidCrystal.h>

// Initialize the LCD display with the pins connected to Arduino
LiquidCrystal lcd(7, 6, 5, 4, 3, 2);

// Define the pins for the LED and the switch
const int ledPin = 13;
const int switchPin = 8;

bool ledState = false; // Initial state of the LED is off
bool lastSwitchState = HIGH; // Assume the switch is initially open (high level)

// Debouncing function
bool readSwitch() {
  bool currentSwitchState = digitalRead(switchPin);
  if (currentSwitchState != lastSwitchState) {
    // Wait for a while to let any bouncing stabilize
    delay(50);
    currentSwitchState = digitalRead(switchPin);
  }
  return currentSwitchState;
}

void setup() {
  // Set the LED pin as output mode
  pinMode(ledPin, OUTPUT);
  // Set the switch pin as input mode
  pinMode(switchPin, INPUT_PULLUP);
  
  // Initialize the LCD display
  lcd.begin(16, 2);
  // Display initial information
  displayStatus();
}

void loop() {
  // Read the switch state with debouncing
  bool currentSwitchState = readSwitch();
  
  // Check if the switch state has changed
  if (currentSwitchState != lastSwitchState) {
    ledState = !ledState; // Toggle the LED state
    digitalWrite(ledPin, ledState); // Update the LED state
    lastSwitchState = currentSwitchState; // Update the switch state
    displayStatus(); // Display the new state
  }
}

void displayStatus() {
  // Clear the display and set the cursor position
  lcd.clear();
  lcd.setCursor(0, 0);
  // Display the switch status
  lcd.print("Switch: ");
  lcd.print(lastSwitchState == HIGH ? "OFF" : "ON");
  // Display the LED status
  lcd.setCursor(0, 1);
  lcd.print("LED: ");
  lcd.print(ledState ? "ON" : "OFF");
}

Credits

Lois
13 projects • 2 followers
Contact

Comments

Please log in or sign up to comment.