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.
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.
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.
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.
//Arduino LCD and DS3231 RTC Example #include <LiquidCrystal.h> #include <DS3231.h> #include <Wire.h> LiquidCrystal lcd(7,8,9,10,11,12); DS3231 Clock; bool Century = false; bool h12,PM; String dut; void setup(){ //LCD Contrast pinMode(6,OUTPUT); analogWrite(6,50); //Start I2C Wire.begin(); lcd.begin(16,2); lcd.print("Arduino LCD and"); lcd.setCursor(0,1); lcd.print("DS3231 Example"); delay(3000); lcd.clear(); } void loop(){ //Check PM or AM if(PM) dut="PM"; else dut="AM"; //Date lcd.setCursor(0,0); lcd.print(String(Clock.getDate(),DEC)+"/"+String(Clock.getMonth(Century),DEC) +"/"+"20"+String(Clock.getYear(),DEC)+" "+String(Clock.getTemperature(),2)); //Time lcd.setCursor(0,1); lcd.print(String(Clock.getHour(h12,PM),DEC)+":"+String(Clock.getMinute(),DEC) +":"+String(Clock.getSecond(),DEC)+" "+dut); }
Click here to download its source file.
No comments:
Post a Comment