Maker Pro
Arduino

How to Make an Arduino OLED Temperature Display With Real-Time Clock

August 07, 2017 by Muhammad Aqib
Share
banner

Make an Arduino weather clock with time, date, and temperature.

In this article, we are going to make Arduino weather clock which will tell us the time, date, and temperature. The LM35 sensor will give us the temperature, the DS3231 will tell us the date and the time, and we will use the OLED to display the temperature, date, and time.

Circuit Diagram

First of all, connect the OLED with the Arduino. The OLED works with the Arduino through the SPI as well as I2C communication but we have connected it using SPI communication. The SPI pins on the Arduino are pin 8, 9, 10, 11, and 13. The connections of the OLED with the Arduino are as follows:

  • Connect CS of the OLED to pin 10 of the Arduino.
  • Connect DC of the OLED to pin 9 of the Arduino.
  • Connect RES of the OLED to pin 8 of the Arduino.
  • Connect D1 of the OLED to pin 11 of the Arduino.
  • Connect D0 of the OLED to pin 13 of the Arduino.
  • Connect VCC of the OLED to 5V pin of the Arduino.
  • Connect GND of the OLED to GND pin of the Arduino.

After that, connect the DS3231 module with the Arduino. The DS3231 module works with the Arduino through I2C communication. The pins for I2C communication on the Arduino are SDA and SCL. Connect the DS3231 module to the Arduino as follows:

  • Connect GND of the DS3231 to the GND pin of the Arduino.
  • Connect VCC of the DS3231 to 5V pin of the Arduino.
  • Connect SDA of the  OLED to A4 on the Arduino.
  • Connect SCL on the OLED to the A5 pin on the Arduino.
In the end, connect the LM35 sensor with the Arduino. Connect the left pin of LM35 to 5V on the Arduino, the middle pin to A0 on the Arduino and the left pin to ground on the Arduino.

Code Explanation

First of all, we added the libraries for the DS3231 module and the OLED. The Adafruit library for the OLED has more functions as compared to the other libraries used for OLEDs.
#include 
#include 
Next, we defined the pins where we have connected the DS3231 module and the OLED. The DS3231 module works with the Arduino through the I2C communication so we have used the SDA and SCL pins of the Arduino.

The OLED works with the Arduino through SPI communication. Therefore we initialized the pins 8, 9, 10, 11, 13 which are for the SPI communications. After that, we initialized the LM35 sensor pin and other variables.


DS3231 rtc(SDA, SCL);
#define OLED_MOSI 11
#define OLED_CLK 13
#define OLED_DC 9
#define OLED_CS 10
#define OLED_RESET 8 Adafruit_SSD1306 display(OLED_MOSI, OLED_CLK, OLED_DC, OLED_RESET, OLED_CS);
const int sensor_pin = A0;
float temp;
float output;

In the setup function, we declared the LM35 sensor pin as input because the Arduino will take the input from the sensor. Then we gave the command to DS3231 and OLED to start communicating with the Arduino.

pinMode(sensor_pin,INPUT);
rtc.begin();
display.begin(SSD1306_SWITCHCAPVCC);
The below lines are commented in the code but if you are using the DS3231 module for the first time and you want to set the day, date, and time, then uncomment these lines and set it.

  //rtc.setDOW(WEDNESDAY);     // Set Day-of-Week to SUNDAY
//rtc.setTime(19, 02, 0);     // Set the time to 12:00:00 (24hr format)
//rtc.setDate(7, 12, 2017);   // Set the date to June 6th, 2017
Then we printed the “weather clock” on the OLED for 5 seconds.

  display.clearDisplay();
display.setTextColor(WHITE);
display.setTextSize(2);
display.setCursor(0,0);
display.print("  Weather ");
display.setCursor(0,17);
display.print("  Clock ");
display.display();
delay(5000);
In the loop function, we read from the LM35 and calculated the temperature. Then we get the time and date and printed on the OLED.
  
 output = analogRead(sensor_pin);
temp =(output*500)/1023;
display.clearDisplay();
display.setTextSize(2);
display.setCursor(20,0);
display.print(rtc.getTimeStr());
display.setTextSize(1);
display.setCursor(0,15);
display.print(rtc.getDateStr());
display.setTextSize(1);
display.setCursor(70,15);
display.print(rtc.getDOWStr());

Full Arduino Code

#include 
#include 
DS3231 rtc(SDA, SCL);
#define OLED_MOSI 11
#define OLED_CLK 13
#define OLED_DC 9
#define OLED_CS 10
#define OLED_RESET 8
Adafruit_SSD1306 display(OLED_MOSI, OLED_CLK, OLED_DC, OLED_RESET, OLED_CS);
const int sensor_pin = A0;
float temp;  
float output;

void setup() 
{
pinMode(sensor_pin,INPUT);
rtc.begin();
display.begin(SSD1306_SWITCHCAPVCC);
//rtc.setDOW(WEDNESDAY);     // Set Day-of-Week to SUNDAY
//rtc.setTime(19, 02, 0);     // Set the time to 12:00:00 (24hr format)
//rtc.setDate(7, 12, 2017);   // Set the date to June 6th, 2017
display.clearDisplay();
display.setTextColor(WHITE);
display.setTextSize(2);
display.setCursor(0,0);
display.print("  Weather ");
display.setCursor(0,17);
display.print("  Clock ");
display.display();
delay(5000);
}

void loop()
{
output = analogRead(sensor_pin);
temp =(output*500)/1023;
display.clearDisplay();
display.setTextSize(2);
display.setCursor(20,0);
display.print(rtc.getTimeStr());
display.setTextSize(1);
display.setCursor(0,15);
display.print(rtc.getDateStr());
display.setTextSize(1);
display.setCursor(70,15);
display.print(rtc.getDOWStr());
display.setTextSize(1);
display.setCursor(20,25);
display.print("Temp: ");
display.print(temp);
display.print("C");
display.display(); 
delay(1000);
}

Related Content

Comments


You May Also Like