Project tutorial

Arduino OLED Temperature Display with Real Time Clock © GPL3+

In this article, we are going to make an Arduino weather clock which will tell us the time, date and temperature.

  • 2,991 views
  • 0 comments
  • 25 respects

Components and supplies

About this project

We are going to make an 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.

Required Components

The components required for this project are as follows

  • DS3231 (Real time clock module)
  • OLED display (An SSD1306 was used here)
  • Connecting wires
  • Breadboard

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.

You can find a full-size schematic here

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 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 print it 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);
}

Schematics

arduino-weather-clock_bb_r3RznyasiL.jpg
Arduino weather clock bb (1) r3rznyasil

Code

Code snippet #4Plain text
  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);
Code snippet #5Plain text
 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());
Code snippet #6Plain text
#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);
}

Comments

Similar projects you might like

Carfox: A Device to Find Them All

Project tutorial by Luis Roda Sánchez

  • 161 views
  • 0 comments
  • 5 respects

LoRa Gateway for DeviceHive

Project tutorial by DeviceHive IoT team

  • 465 views
  • 0 comments
  • 12 respects

Pac-Man LED Pixel Panel Costume

Project tutorial by Ben Muller

  • 3,391 views
  • 3 comments
  • 57 respects

Arduino-Based Automatic Guitar Tuner

Project tutorial by Ben Overzat

  • 351 views
  • 0 comments
  • 6 respects

IoT Bird Feeder with Sigfox and Tweeter

Project showcase by Gaël Porté

  • 190 views
  • 0 comments
  • 3 respects

Raspberry Pi and Arduino Laptop

Project tutorial by Dante Roumega

  • 16,799 views
  • 6 comments
  • 42 respects
Add projectSign up / Login