Friday, June 24, 2022

Arduino Uno DS3231 RTC and Character LCD Example

In previous post, I showed an example of using a character LCD with Arduino Uno. Here I will add a Real Time Clock (RTC) module, that it's powered by the DS3231 real time clock chip. This chip contain timing data that store in its internal RAM. Optionally it contain a temperature sensor chip that tell us the current device temperature.

Arduino Uno DS3231 RTC and Character LCD Example
An RTC module that contain a DS3231 and an EEPROM

In this example, the Arduino Uno will read current timing from DS3231. That timing will display on the character LCD.

Arduino Uno DS3231 RTC and Character LCD Example
Program Example

The first line are Date/Month/Year and the device temperature in degree Celsius. The second line are the current time with AM/PM indication.

Arduino Uno DS3231 RTC and Character LCD Example
Schematic

I use PWM of the Arduino to adjust the LCD contrast Vo. The PWM pin 6 of Arduino connects to the LCD Vo pin via a 2.2 kOhm resistor. The PWM duty cycle is 50. 

The Arduino source code is only a little.

  1. //Arduino LCD and DS3231 RTC Example
  2. #include <LiquidCrystal.h>
  3.  
  4. #include <DS3231.h>
  5. #include <Wire.h>
  6.  
  7. LiquidCrystal lcd(7,8,9,10,11,12);
  8. DS3231 Clock;
  9. bool Century = false;
  10. bool h12,PM;
  11. String dut;
  12.  
  13. void setup(){
  14. //LCD Contrast
  15. pinMode(6,OUTPUT);
  16. analogWrite(6,50);
  17. //Start I2C
  18. Wire.begin();
  19. lcd.begin(16,2);
  20. lcd.print("Arduino LCD and");
  21. lcd.setCursor(0,1);
  22. lcd.print("DS3231 Example");
  23. delay(3000);
  24. lcd.clear();
  25. }
  26.  
  27. void loop(){
  28. //Check PM or AM
  29. if(PM) dut="PM";
  30. else dut="AM";
  31.  
  32. //Date
  33. lcd.setCursor(0,0);
  34. lcd.print(String(Clock.getDate(),DEC)+"/"+String(Clock.getMonth(Century),DEC)
  35. +"/"+"20"+String(Clock.getYear(),DEC)+" "+String(Clock.getTemperature(),2));
  36.  
  37. //Time
  38. lcd.setCursor(0,1);
  39. lcd.print(String(Clock.getHour(h12,PM),DEC)+":"+String(Clock.getMinute(),DEC)
  40. +":"+String(Clock.getSecond(),DEC)+" "+dut);
  41. }

Click here to download its source file.

No comments:

Post a Comment

Labels

ADC (10) Analog (14) Arduino (12) Atmega16 (19) Audio (2) AVR (20) Charger (1) Cortex-M0 (1) Counter (10) CPLD (25) Digital I/O (22) Display (34) EEPROM (2) Environment Sensor (1) esp8266 (2) Experiment Board (10) I2C (4) Interrupt (7) LCD (1) LDmicro (29) measurement and instrumentation (7) Microchip Studio (3) MikroC (1) One-Shot (3) OpAmp (1) PCB (31) PIC16 Microcontrollers (16) PIC16F877A (2) PIC16F887 MikroC (22) PLC (35) PWM (11) Regulator (1) RTC (2) Sensor (8) Shift Registers (5) SPI (5) Timer (34) UART (2) ultra-sonic sensor (1) USB (1) VHDL (21) xc8 (1) XC95108 (9) XC9536 (15) XC9572 (1) Xilinx (23) Xilinx ISE (22)