The best tools to make your project dreams come true

Login or Signup

Use a LaunchPad to Build a Clock with a Temperature and Humidity Meter

By All About Circuits

Courtesy of All About Circuits -

Want to learn how to make a clock controlled by a LaunchPad? I'll do you one better: not only is this a digital clock, it will also periodically measure and display the temperature and the relative humidity of your room. The circuit is built around TI’s LaunchPad and the software is developed in Energia IDE.

Part List

The Circuit

Scheme-it

As shown in the schematic, there are four main components of the circuit: the LaunchPad, the DS1307 Real Time Clock, the DHT22 Sensor, and the 4-Digit Seven Segment Display.

The DS1307 is a Real Time Clock IC that provides both time and date information serially over an I2C bus. The transferred data includes hours, minutes, and seconds; as well as year, month, and day information. The DS1307 requires an external clock with a frequency at 32.768 kHz which is provided by a crystal oscillator. When the IC’s main power is cut, the IC switches to the backup mode and will continue to operate in low current mode.

Use a LaunchPad to Build a Clock

The time information is read directly from the DS1307 using the LaunchPad’s internal I2C module. The RTCLib library is used for this. The LaunchPad and the RTC are connected over SCL labeled (P1.6) and SDA (P1.7) as shown in the schematic. These lines are pulled-up to the VCC through the 2.2 kohm resistors labled R2 and R3 for proper I2C operation.

The DS1307 requires a supply voltage of 5V. I used an external 5V AC-DC adapter since the LaunchPad doesn’t provide a5V output. Instead I used an external 5V AC-DC adapter. A CR2025 3V Cell is connected to the VBAT pin on the DS1307 RTC as the backup power source. The backup power source can last a few years.

The application doesn’t have external controls to set the DS1307’s date and time yet. We’ll use the command below to give the DS1307 current date and time information,;

Copy Code
RTC.adjust(DateTime(__DATE__, __TIME__));

This command will get the current date and time from the programmer PC as the input and will set the DS1307 after the sketch is uploaded. This command should be commented out after the first upload and should only used only when a new setting is required.

The temperature and the humidity sensor of the circuit is a DHT22, also known as the AM2302. The DHT22 communicates over a serial single data bus. When the microcontroller sends the start bit, the DHT22 transfers a 40-bit data package that includes the information for relative humidity and the temperature. We used the DHT22 library adapted to Energia to gather information from the DHT22 in this application.

Use a LaunchPad to Build a Clock

The 4-digit, seven segment display is another main component of the circuit. It’s display shows the time, as well as the temperature and the relative humidity information. Because the segment lines are common for all four display digits, you’ll need to use the multiplexing method. In this method, we turn on only one digit at a time by using a PNP transistor 2N3906 as a switch. The digits are turned on and off sequentially in 3ms periods. This action is too fast for the human eye can to recognize the sequencing. 100 ohm resistors are connected in series to limit the LED segment currents.

Use a LaunchPad to Build a Clock

The Software

The software for this application was built in Energia IDE and is provided below. The program runs in a loop which includes these actions;

  • Initialize the board
  • Read the time from DS1307
  • Display the time for a while
  • Read the relative humidity and the temperature data
  • Display the temperature for a while
  • Display the relative humidity for a while.

 

 

The Energia sketch includes comments to clarify each step. Please feel free to ask your questions in the comment section.

Copy Code
                  #include "Wire.h"
#include "RTClib.h"
#include "DHT22_430.h"
#define DHTPIN P2_6
RTC_DS1307 RTC;
DHT22 mySensor(DHTPIN);

unsigned long timestamp;
unsigned int clockduration = 0;
unsigned int tempduration = 0;
unsigned int humduration = 0;

// Seven Segment Display driver function
void displaydigit (int value, int digit, boolean dot) {

// Create each digit by turning on and off LED segments
P1OUT |= 0b00011111;
P2OUT |= 0b00111000;
switch (value) {
case 0:
P1OUT &= 0b11100100; P2OUT &= 0b11001111; break;
case 1:
P1OUT &= 0b11111111; P2OUT &= 0b11001111; break;
case 2:
P1OUT &= 0b11100101; P2OUT &= 0b11010111; break;
case 3:
P1OUT &= 0b11110101; P2OUT &= 0b11000111; break;
case 4:
P1OUT &= 0b11111110; P2OUT &= 0b11000111; break;
case 5:
P1OUT &= 0b11110100; P2OUT &= 0b11100111; break;
case 6:
P1OUT &= 0b11100100; P2OUT &= 0b11100111; break;
case 7:
P1OUT &= 0b11111101; P2OUT &= 0b11001111; break;
case 8:
P1OUT &= 0b11100100; P2OUT &= 0b11000111; break;
case 9:
P1OUT &= 0b11110100; P2OUT &= 0b11000111; break;
case 10:
P1OUT &= 0b11111111; P2OUT &= 0b11111111; break;
case 11:
P1OUT &= 0b11111100; P2OUT &= 0b11010111; break;
case 12:
P1OUT &= 0b11101100; P2OUT &= 0b11010111; break;
}

// Turn on/off the dot LED
if (dot) P1OUT &= 0b11111011;
else P1OUT |= 0b00000100;


// Turn on the selected digit
digitalWrite(P1_5, HIGH);
digitalWrite(P2_0, HIGH);
digitalWrite(P2_1, HIGH);
digitalWrite(P2_2, HIGH);

switch (digit) {
case 1:
digitalWrite(P1_5, LOW); break;
case 2:
digitalWrite(P2_0, LOW); break;
case 3:
digitalWrite(P2_1, LOW); break;
case 4:
digitalWrite(P2_2, LOW); break;
}
}

void setup()
{
// Set the port directions
P1DIR |= 0b00111111;
P2DIR |= 0b00111111;


mySensor.begin();
Wire.begin();
RTC.begin();

// Write the current date and time to DS1397
RTC.adjust(DateTime(__DATE__, __TIME__));
}

void loop()
{

// Read and display the time
while (clockduration <= 500) {

timestamp = micros();
DateTime now = RTC.now();
while (timestamp 3000 >= micros());

if (now.hour()/10)
displaydigit((now.hour()/10),1,0);
else
displaydigit(10,1,0);
delay(3);
if (now.second()%2)
displaydigit((now.hour()),2,1);
else
displaydigit((now.hour()),2,0);
delay(3);
displaydigit((now.minute()/10),3,0);
delay(3);
displaydigit((now.minute()),4,0);

clockduration ;
}

displaydigit(10,1,0);
displaydigit(10,2,0);
displaydigit(10,3,0);
displaydigit(10,4,0);

// Read and display the temperature and the relative humidity
mySensor.get();
int32_t h = mySensor.humidityX10();
int32_t t = mySensor.temperatureX10();


while (tempduration <= 300) {
delay(3);
displaydigit(t/100,1,0);
delay(3);
displaydigit((t/10),2,1);
delay(3);
displaydigit(t,3,0);
delay(3);
displaydigit(11,4,0);
tempduration ;
}

while (humduration <= 300) {
delay(3);
displaydigit(h/100,1,0);
delay(3);
displaydigit((h/10),2,1);
delay(3);
displaydigit(h,3,0);
delay(3);
displaydigit(12,4,0);
humduration ;
}

clockduration=0;
tempduration=0;
humduration=0;

}

Key Parts and Components

Add all Digi-Key Parts to Cart
  • 1471-1464-ND
  • 370-1191-ND
  • N188-ND
  • A105998CT-ND
  • CF14JT2K20CT-ND