Day 15- LCD and RTC with NodeMCU

 Day 15- LCD and RTC with NodeMCU


we will learn how to interface an LCD (Liquid Crystal Display) to the NodeMCU board.


These 16x2 LCDs are very popular and broadly used in electronics projects as they are good for displaying information like sensor data from your project, and also they are very cheap.


Hardware Components

NodeMCU

16x2 LCD

I2C Board

Breadboard

Jumper Wires

Micro USB Cable


Software Components

Arduino IDE


Connecting LCD to I2C and then interfacing it to NodeMCU is very simple.

The LCD’s registers from D0 to D7 and Vcc, GND, RS, R/W pins will be connected to I2C.

GND pin of I2C is connected Ground pin (GND) of the NodeMCU.

VCC pin of I2C is connected Vin pin of the NodeMCU. (Because we need to supply 5v to LCD)

SDA pin of I2C is connected D4 of the NodeMCU.

SCL pin of I2C is connected D3 pin of the NodeMCU.

Before you get started with coding you need Arduino IDE.To download Arduino IDE and for NodeMCU setup, you can check my previous instructacle.


Circuit Diagram




Code

----------------------------------------------------

// i2c Scanner

#include <Wire.h>


void setup()
{
  Wire.begin();

  Serial.begin(9600);
  Serial.println("\nI2C Scanner");
}


void loop()
{
  byte error, address;
  int nDevices;

  Serial.println("Scanning...");

  nDevices = 0;
  for(address = 1; address < 127; address++ )
  {
    // The i2c_scanner uses the return value of
    // the Write.endTransmisstion to see if
    // a device did acknowledge to the address.
    Wire.beginTransmission(address);
    error = Wire.endTransmission();

    if (error == 0)
    {
      Serial.print("I2C device found at address 0x");
      if (address<16)
        Serial.print("0");
      Serial.print(address,HEX);
      Serial.println("  !");

      nDevices++;
    }
    else if (error==4)
    {
      Serial.print("Unknow error at address 0x");
      if (address<16)
        Serial.print("0");
      Serial.println(address,HEX);
    }    
  }
  if (nDevices == 0)
    Serial.println("No I2C devices found\n");
  else
    Serial.println("done\n");

  delay(5000);           // wait 5 seconds for next scan
}

--------------------------------------------------


//LCD print with nodemcu

-------------------------------------

#include<LiquidCrystal_I2C_Hangul.h>
#include<Wire.h>

LiquidCrystal_I2C_Hangul lcd(0x27 ,16,2); //LCD 클래스 초기화

void setup() {
  Serial.begin(9600);
  lcd.init();
  lcd.backlight();
  lcd.print("Hello World!");
}

void loop() {
}

------------------------------------

Nodemcu RTC connection







// display time from RTC on serial Monitor

/*
  DS1307 RTC (Real-Time-Clock) Example

  Uno       A4 (SDA), A5 (SCL)
  Mega      20 (SDA), 21 (SCL)
  Leonardo   2 (SDA),  3 (SCL)
 */

#include <Wire.h>
#include <DS1307.h>


DS1307 rtc;


void setup()
{
  //init Serial port
  Serial.begin(9600);
  while(!Serial); //wait for serial port to connect - needed for Leonardo only

  //init RTC
  Serial.println("Init RTC...");
  rtc.begin();

  //only set the date+time one time
  // rtc.set(0, 0, 8, 24, 12, 2014); //08:00:00 24.12.2014 //sec, min, hour, day, month, year

  //stop/pause RTC
  // rtc.stop();

  //start RTC
  rtc.start();
}


void loop()
{
  uint8_t sec, min, hour, day, month;
  uint16_t year;

  //get time from RTC
  rtc.get(&sec, &min, &hour, &day, &month, &year);

  //serial output
  Serial.print("\nTime: ");
  Serial.print(hour, DEC);
  Serial.print(":");
  Serial.print(min, DEC);
  Serial.print(":");
  Serial.print(sec, DEC);

  Serial.print("\nDate: ");
  Serial.print(day, DEC);
  Serial.print(".");
  Serial.print(month, DEC);
  Serial.print(".");
  Serial.print(year, DEC);

  //wait a second
  delay(1000);
}

---------------------------------


// Display RTC time on LCD

#include <Wire.h>
#include <DS1307.h>
#include<LiquidCrystal_I2C_Hangul.h>


LiquidCrystal_I2C_Hangul lcd(0x27 ,16,2); //LCD 클래스 초기화


DS1307 rtc;


void setup()
{
  //init Serial port
  Serial.begin(9600);
  lcd.init();
  lcd.backlight();
  lcd.print("Hello World!");
  while(!Serial); //wait for serial port to connect - needed for Leonardo only
  //init RTC
  Serial.println("Init RTC...");
  rtc.begin();

  //only set the date+time one time
 // rtc.set(0, 56, 20, 25, 12, 2023); //08:00:00 24.12.2014 //sec, min, hour, day, month, year
 // stop/pause RTC
 //  rtc.stop();

  //start RTC
  rtc.start();
}


void loop()
{
  uint8_t sec, min, hour, day, month;
  uint16_t year;

  //get time from RTC
  rtc.get(&sec, &min, &hour, &day, &month, &year);

  //serial output
  Serial.print("\nTime: ");
  Serial.print(hour, DEC);
  Serial.print(":");
  Serial.print(min, DEC);
  Serial.print(":");
  Serial.print(sec, DEC);

  Serial.print("\nDate: ");
  Serial.print(day, DEC);
  Serial.print(".");
  Serial.print(month, DEC);
  Serial.print(".");
  Serial.print(year, DEC);
lcd.clear();

lcd.setCursor(0,0);
lcd.print("Time");
lcd.print("-");
lcd.print(hour);
lcd.print(":");
lcd.print(min);
lcd.print(":");
lcd.print(sec);

lcd.setCursor(0,1);
lcd.print("Date");
lcd.print("-");
lcd.print(day);
lcd.print(":");
lcd.print(month);
lcd.print(":");
lcd.print(year);
  //wait a second
  delay(1000);
}

----------------------------------

// Display and SET RTC time on LCD


#include <Wire.h>
#include <DS1307.h>
#include<LiquidCrystal_I2C_Hangul.h>


LiquidCrystal_I2C_Hangul lcd(0x27 ,16,2); //LCD 클래스 초기화

DS1307 rtc;

int sethr=14; //D5
int setmin=12; //D6

void setup()
{

pinMode(sethr,INPUT);
digitalWrite(sethr,HIGH);

pinMode(setmin,INPUT);
digitalWrite(setmin,HIGH);

  //init Serial port
  Serial.begin(9600);
  lcd.init();
  lcd.backlight();
  lcd.print("Hello World!");
  while(!Serial); //wait for serial port to connect - needed for Leonardo only
  //init RTC
  Serial.println("Init RTC...");
  rtc.begin();

  //only set the date+time one time
 // rtc.set(0, 56, 20, 25, 12, 2023); //08:00:00 24.12.2014 //sec, min, hour, day, month, year
 //  rtc.stop();

  rtc.start();
}


void loop()
{

  uint8_t sec, min, hour, day, month;
  uint16_t year;

  //get time from RTC
  rtc.get(&sec, &min, &hour, &day, &month, &year);

  //serial output
  Serial.print("\nTime: ");
  Serial.print(hour, DEC);
  Serial.print(":");
  Serial.print(min, DEC);
  Serial.print(":");
  Serial.print(sec, DEC);

  Serial.print("\nDate: ");
  Serial.print(day, DEC);
  Serial.print(".");
  Serial.print(month, DEC);
  Serial.print(".");
  Serial.print(year, DEC);
/////////////////////////////////////////////////

byte sethrsw=digitalRead(sethr);
byte setminsw=digitalRead(setmin);

if (sethrsw==0){hour=hour+1;  if (hour>23){hour=0;}
  rtc.set(sec, min, hour, day, month, year); //08:00:00 24.12.2014 //sec, min, hour, day, month, year
//delay(500);
}

if (setminsw==0){min=min+1;  if (min>59){min=0;}
  rtc.set(sec, min, hour, day, month, year); //08:00:00 24.12.2014 //sec, min, hour, day, month, year
//delay(500);
}

////////////////////////////////////////////////


lcd.clear();

lcd.setCursor(0,0);
lcd.print("Time");
lcd.print("-");
lcd.print(hour);
lcd.print(":");
lcd.print(min);
lcd.print(":");
lcd.print(sec);

lcd.setCursor(0,1);
lcd.print("Date");
lcd.print("-");
lcd.print(day);
lcd.print(":");
lcd.print(month);
lcd.print(":");
lcd.print(year);
  //wait a second
  delay(1000);
}

-------------------------------------------------

No comments:

Post a Comment

Arduion Uno pin out