Skip to content

Instantly share code, notes, and snippets.

@phsheth
Last active December 21, 2022 22:05
Show Gist options
  • Save phsheth/111d3667f004e4edf184911a89b62808 to your computer and use it in GitHub Desktop.
Save phsheth/111d3667f004e4edf184911a89b62808 to your computer and use it in GitHub Desktop.
Arduino code for DS3231 RTC, photo resistor and 16x2 LCD
// Include LCD Library Code
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// Include the DS3231 RTC Library
#include <DS3231.h>
//Photoresistor Pin
int lightPin = 0;
//Initialise the LCD
LiquidCrystal_I2C lcd(0x27,16,2);
//initialize the DS3231 rtc
DS3231 rtc(SDA, SCL);
Time t;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
Wire.begin();
rtc.begin();
lcd.init();
lcd.clear();
//rtc.setDOW(SATURDAY); // Set Day-of-Week to SUNDAY
//rtc.setTime(22, 11, 0); // Set the time to 12:00:00 (24hr format)
//rtc.setDate(27, 1, 2018); // Set the date to 27th January 2018 (dd, mm, yyyy)
}
void loop() {
// put your main code here, to run repeatedly:
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Pushkar Sheth");
lcd.setCursor(0,1);
int lightLevel = analogRead(lightPin);
lightLevel = map(lightLevel,0,1024,2000,0);
lightLevel = constrain(lightLevel,0,2000);
t = rtc.getTime();
Serial.print(","); // this comma exist so that the annoying b' can be separated from the further variables in the string.
// this also makes the output easier to be processed by the next step.
Serial.print(lightLevel);
Serial.print(",");
Serial.print(t.year);
Serial.print(",");
Serial.print(t.mon);
Serial.print(",");
Serial.print(t.date);
Serial.print(",");
Serial.print(t.hour);
Serial.print(",");
Serial.print(t.min);
Serial.print(",");
Serial.print(t.sec);
Serial.println(",");
delay(1000);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment