0% found this document useful (0 votes)
13 views9 pages

Optional - Temperature & Timer

Uploaded by

Johnson Li
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views9 pages

Optional - Temperature & Timer

Uploaded by

Johnson Li
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

146 Chapter 16A LCD1602 www.freenove.

com █

Project 16.2 LCD1602 Clock

In the previous chapter, we have used LCD1602 to display some strings, and now let us use LCD1206 to make
a clock.

Component List

Control board x1 Breadboard x1

USB cable x1 Jumper M/M x18

LCD1602 x1 Rotary potentiometer x1 Thermistor Resistor 10kΩ


x1 x1

Need help? Contact [email protected]


█ www.freenove.com Chapter 16A LCD1602 147

Code Knowledge

Timer
A timer can be set to produce an interrupt after a period of time. When a timer interrupt occurs, the processor
will jump to the interrupt function to process the interrupt event. And after completion the processing,
execution will return to the interrupted location to go on. If you don't close the timer, interrupt will occur at
the intervals you set.

Program

Configuration timer

Process interrupt event


Set time

First time interrupt

Set time

Second time interrupt

Close timer, then interrupt


will not occur again

█ Need help? Contact [email protected]


148 Chapter 16A LCD1602 www.freenove.com █

Circuit

The connection between control board and LCD1602 is shown below, and a rotary potentiometer is used to
adjust the contrast of LCD1602. Pin A4 port is used to detect the voltage of thermistor.
Schematic diagram

Hardware connection

Need help? Contact [email protected]


█ www.freenove.com Chapter 16A LCD1602 149

Sketch

Sketch 16.2.1
Before writing code, we need to import the library needed.
Click “Add .ZIP Library...” and then find FlexiTimer2.zip in libraries folder (this folder is in the folder unzipped
form the ZIP file we provided). This library can help manipulate the timer.
Now write code to make LCD1602 display the time and temperature, and the time can be modified through
the serial port.
1 #include <LiquidCrystal.h> // Contains LiquidCrystal Library
2 #include <FlexiTimer2.h> // Contains FlexiTimer2 Library
3
4 // initialize the library with the numbers of the interface pins
5 LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
6
7 int tempPin = 4; // define the pin of temperature sensor
8 float tempVal; // define a variable to store temperature value
9 int hour, minute, second; // define variables stored record time
10
11 void setup() {
12 lcd.begin(16, 2); // set up the LCD's number of columns and rows
13 startingAnimation(); // display a dynamic start screen
14 FlexiTimer2::set(1000, timerInt); // configure the timer and interrupt function
15 FlexiTimer2::start(); // start timer
16 Serial.begin(115200); // initialize serial port with baud rate 115200
17 Serial.println("Input hour,minute,second to set time.");
18 }
19
20 void loop() {
21 // Get temperature
22 tempVal = getTemp();
23 if (second >= 60) { // when seconds is equal to 60, minutes plus 1
24 second = 0;
25 minute++;
26 if (minute >= 60) { // when minutes is equal to 60, hours plus 1
27 minute = 0;
28 hour++;
29 if (hour >= 24) { // when hours is equal to 24, hours turn to zero
30 hour = 0;
31 }
32 }
33 }
34 lcdDisplay(); // display temperature and time information on LCD
35 delay(200);

█ Need help? Contact [email protected]


150 Chapter 16A LCD1602 www.freenove.com █

36 }
37
38 void startingAnimation() {
39 for (int i = 0; i < 16; i++) {
40 lcd.scrollDisplayRight();
41 }
42 lcd.print("starting...");
43 for (int i = 0; i < 16; i++) {
44 lcd.scrollDisplayLeft();
45 delay(300);
46 }
47 lcd.clear();
48 }
49
50 // the timer interrupt function of FlexiTimer2 is executed every 1s
51 void timerInt() {
52 second++; // second plus 1
53 }
54
55 // serial port interrupt function
56 void serialEvent() {
57 int inInt[3]; // define an array to save the received serial data
58 while (Serial.available()) {
59 for (int i = 0; i < 3; i++) {
60 inInt[i] = Serial.parseInt(); // receive 3 integer data
61 }
62 // print the received data for confirmation
63 Serial.print("Your input is: ");
64 Serial.print(inInt[0]);
65 Serial.print(", ");
66 Serial.print(inInt[1]);
67 Serial.print(", ");
68 Serial.println(inInt[2]);
69 // use received data to adjust time
70 hour = inInt[0];
71 minute = inInt[1];
72 second = inInt[2];
73 // print the modified time
74 Serial.print("Time now is: ");
75 Serial.print(hour / 10);
76 Serial.print(hour % 10);
77 Serial.print(':');
78 Serial.print(minute / 10);
79 Serial.print(minute % 10);

Need help? Contact [email protected]


█ www.freenove.com Chapter 16A LCD1602 151

80 Serial.print(':');
81 Serial.print(second / 10);
82 Serial.println(second % 10);
83 }
84 }
85
86 // function used by LCD1602 to display time and temperature
87 void lcdDisplay() {
88 lcd.setCursor(0, 0); // set the cursor to (0,0) (first column,first row).
89 lcd.print("TEMP: "); // display temperature information
90 lcd.print(tempVal);
91 lcd.print("C");
92 lcd.setCursor(0, 1); // set the cursor to (0,1) (first column,second row)
93 lcd.print("TIME: "); // display time information
94 lcd.print(hour / 10);
95 lcd.print(hour % 10);
96 lcd.print(':');
97 lcd.print(minute / 10);
98 lcd.print(minute % 10);
99 lcd.print(':');
100 lcd.print(second / 10);
101 lcd.print(second % 10);
102 }
103
104 // function used to get temperature
105 float getTemp() {
106 // Convert analog value of tempPin into digital value
107 int adcVal = analogRead(tempPin);
108 // Calculate voltage
109 float v = adcVal * 5.0 / 1024;
110 // Calculate resistance value of thermistor
111 float Rt = 10 * v / (5 - v);
112 // Calculate temperature (Kelvin)
113 float tempK = 1 / (log(Rt / 10) / 3950 + 1 / (273.15 + 25));
114 // Calculate temperature (Celsius)
115 return tempK - 273.15;
116 }

In the code, we define 3 variables to represent time: second, minute, hour.


9 int hour, minute, second; // define variables to store hour,minute,seconds

█ Need help? Contact [email protected]


152 Chapter 16A LCD1602 www.freenove.com █

Define a timer with cycle of 1000 millisecond (1 second). And each interrupt makes the second plus 1. When
setting the timer, you need to define a function and pass the function name that works as parameter to
FlexiTimer2::set () function.
14 FlexiTimer2::set(1000, timerInt); // configure timer and timer interrupt function
15 FlexiTimer2::start(); // start timer
After every interrupt, the second plus 1.
51 void timerInt() {
52 sec++; // second plus 1
53 }

:: operator
"::" is scope operator. The function behind "::" belongs to the scope of the front. If we want to call the
function defined in the FlexiTimer2 scope outside, we need to use the "::". It can be global scope operator,
class scope operator and namespace scope operator. It is a namespace scope operator here. Because
functions of FlexiTimer2 library is defined in the namespace of FlexiTimer2, so we can find them in
FlexiTimer2 library file.

In the loop () function, the information on the LCD display will be refreshed at set intervals.
20 void loop() {
… …
34 lcdDisplay(); // display temperature and time information on LCD
35 delay(200);
36 }
In the loop function, we need to control the second, minute, hour. When the second increases to 60, the
minute adds 1, and the second is reset to zero; when the minute increases to 60, the hour adds 1, and the
minute is reset to zero; when the hour increases to 24, reset it to zero.
23 if (second >= 60) { // when the second increases to 60, the minute adds 1
24 second = 0;
25 minute++;
26 if (minute >= 60) { //when the minute increases to 60, the hour adds 1
27 minute = 0;
28 hour++;
29 if (hour >= 24) { // when the hour increases to 24, turn it to zero
30 hour = 0;
31 }
32 }
33 }
We define a function lcdDisplay () to refresh the information on LCD display. In this function, use two bit to
display the hour, minute, second on the LCD. For example, hour/ 10 is the unit, hour% 10 is the tens.
87 void lcdDisplay() {
88 lcd.setCursor(0, 0); // set the cursor to (0,0) (first column,first row).
89 lcd.print("TEMP: "); // display temperature information
90 lcd.print(tempVal);
91 lcd.print("C");

Need help? Contact [email protected]


█ www.freenove.com Chapter 16A LCD1602 153

92 lcd.setCursor(0, 1); // set the cursor to (0,1) (first column,second row)


93 lcd.print("TIME: "); // display time information
94 lcd.print(hour / 10);
95 lcd.print(hour % 10);
96 lcd.print(':');
97 lcd.print(minute / 10);
98 lcd.print(minute % 10);
99 lcd.print(':');
100 lcd.print(second / 10);
101 lcd.print(second % 10);
102 }

Serial port interrupt function is used to receive the data sent by computer to adjust the time, and return the
data for confirmation.
56 void serialEvent() {
57 int inInt[3]; // define an array to save the reveived serial data
58 while (Serial.available()) {
59 for (int i = 0; i < 3; i++) {
60 inInt[i] = Serial.parseInt(); // receive 3 integer data
61 }
62 // print the received data for confirmation
63 Serial.print("Your input is: ");
64 Serial.print(inInt[0]);
65 Serial.print(", ");
66 Serial.print(inInt[1]);
67 Serial.print(", ");
68 Serial.println(inInt[2]);
69 // use the received data to adjust time
70 hour = inInt[0];
71 minute = inInt[1];
72 second = inInt[2];
73 // print the modified time
74 Serial.print("Time now is: ");
75 Serial.print(hour / 10);
76 Serial.print(hour % 10);
77 Serial.print(':');
78 Serial.print(minute / 10);
79 Serial.print(minute % 10);
80 Serial.print(':');
81 Serial.print(second / 10);
82 Serial.println(second % 10);
83 }
84 }

█ Need help? Contact [email protected]


154 Chapter 16A LCD1602 www.freenove.com █

We also define a function that displays a scrolling string when the control board has been just started.
38 void startingAnimation() {
39 for (int i = 0; i < 16; i++) {
40 lcd.scrollDisplayRight();
41 }
42 lcd.print("starting...");
43 for (int i = 0; i < 16; i++) {
44 lcd.scrollDisplayLeft();
45 delay(300);
46 }
47 lcd.clear();
48 }

Verify and upload the code. The LCD screen will display a scrolling string first, and then displays the
temperature and time. We can open Serial Monitor and enter time in the sending area, then click the Send
button to set the time.

Need help? Contact [email protected]

You might also like