Download as pdf or txt
Download as pdf or txt
You are on page 1of 13

See discussions, stats, and author profiles for this publication at: https://www.researchgate.

net/publication/322530363

Digital Thermometer Using Arduino and MCP9808 Digital Temperature Sensor

Technical Report · January 2018


DOI: 10.13140/RG.2.2.21101.49125

CITATION READS
1 7,013

2 authors:

J. Martinos Themis Martinos

39 PUBLICATIONS 70 CITATIONS 33 PUBLICATIONS 41 CITATIONS

SEE PROFILE SEE PROFILE

All content following this page was uploaded by J. Martinos on 16 January 2018.

The user has requested enhancement of the downloaded file.


Data Sheet MartinosTEAM 16/01/2018

Digital Thermometer
Using Arduino and MCP9808 Digital Temperature Sensor
John Martinos
Introduction ioannismartinos@gmail.com
The purpose of this project was to learn how to program a http://www.researchgate.net/profile/J_Martinos
microcontroller and build pc software which communicates with LinkedIn: John Martinos
the microcontroller. Send commands to the microcontroller and Themis Martinos
receive data from a sensor. As a platform for the microcontroller themismartinos@gmail.com
we used the Arduino UNO R3 development board which is the http://www.researchgate.net/profile/Themis_Martinos
most popular and documented product of the whole Arduino LinkedIn: Themis Martinos
family. The Arduino UNO is a microcontroller board based on the
ATmega328P. It has 14 digital input/output pins, 6 analog inputs,
a USB connection, a power jack and everything needed to
support the microcontroller. For the scope of this project we
used the Arduino UNO to control the MCP9808 digital
temperature sensor, with a typical/maximum accuracy of
±0.25°C/±0.5°C over the sensor's -40°C to +125°C range. The
MCP9808 comes from Adafruit as a breakout board PCB with a
library for the Arduino, which makes it very easy to connect and
get readings from the sensor. We used the Arduino IDE software
to write the code for the microcontroller and uploaded it to the
board. For the PC software we used the Visual Studio
Community edition and wrote a windows form application in C#
which communicates with the Arduino UNO via the COM port.
Figure 1 shows the GUI for the PC software. We also, made an
extension board "module" (see Figure 2) which connects to the
Arduino Uno via pin headers. The circuit for the "module" was Figure 1. Graphical User Interface "GUI" for the PC software.
made on a PCB universal prototyping board 70X90mm. On the
board we placed a 16X2 LCD character display which connects to
the board via pin headers, the temperature sensor which is also
connects to the board via pin headers, a Potentiometer to
control the contrast of the LCD, a buzzer, 5 DIP switches, a LED
and 7 resistors.

Following in the datasheet


you will find the codes for the
Microcontroller and the PC
software. You can freely copy
and modify those codes, in
order to fit in your projects.

Page 2: Schematic and connection


diagrams for the "module"
Page 3: User guide for the "module"
Page 4: User guide for the PC software
Page 5: Code for the microcontroller
Page 6: Code for the PC software
Figure 2. Parts for the project.

[1]
Martinos TEAM Schematic and Connections Diagrams for the "Module"
16/01/2018 Published on Research Gate

8 11
9 1: Arduino Uno R3
2: LCD 16X2 Characters
3: MCP9808 Temp. Sensor
4: Buzzer 5V, 12mm
5: 220Ω Resistor
6: 220Ω Resistor
1 7: 4kΩ Potentiometer
0 8: LED
10 9: 10kΩ Resistor
00 10: 1kΩ Resistor
00 11: DIP Switch 5 Position

5 We used the Fritzing software to


6
design the schematic diagram for
the module (see Figure 3) which is
connected to the Arduino UNO.
Figure 4, shows the connections
for the "module". Figure 5, shows
the names for the pin headers of
7 Arduino UNO.
4 3

Figure 3. Schematic diagram for


2 the "module".
Connections
PIN9 RS DB7 → Digital Pin 2
PIN10 E DB6 → Digital Pin 3
PIN11
DB5 → Digital Pin 4
DB4
DB4 → Digital Pin 5
PIN12 DB5 E → Digital Pin 12
PIN13 DB6 RS → Digital Pin 13
PIN9 → Digital Pin 6
PIN14 DB7
PIN10 → Digital Pin 7
PIN15 5V PIN11/SDA → Analog Pin A4
PIN16 GND PIN12/SCL → Analog Pin A5
PIN13 → Digital Pin 8
PIN14 → Digital Pin 9
PIN15 → Digital Pin 10
PIN16 → Digital Pin 11

Figure 4. Connections diagram for the "module". Figure 5. Arduino UNO R3.

Martinos John and Martinos Themis [2]


Martinos TEAM User Guide for the "Module"
16/01/2018 Digital Thermometer Using Arduino and MCP9808 Digital Temperature Sensor
At the startup the UNO prints the message "Digital Thermometer" If the switch 3 is ON the reading speed is 2 sec.
on the LCD display.

If the switch 4 is ON the reading speed is 1 sec.


After a delay of 1 sec the UNO search for the MCP9808 temperature
sensor and check if the connection is correct. If the connection is
correct, then it prints the message "Connection to MCP9808
Success" to the LCD.

If the switch 5 is ON the reading speed is 0.01 sec.

If the UNO cannot find the MCP9808 for some reason, then it prints
the message "Connection to MCP9808 Failed" to the LCD.

If the switch 1 is ON the UNO waits for commands by the PC


software.

After a delay of 1 sec the UNO checks whether the switch 1 is ON or


OFF. If switch 1 is OFF then the UNO run as a standalone device and
prints the readings from the MCP9808 to the LCD both for Celsius
and Fahrenheit values. Also, in this case it is not allowed to the PC
Software to connect with the UNO. The USB cable now it is used as a
power supply for the UNO. Instead of USB cable you can use the 9V
battery with the jack adapter to power the device. In this state the
UNO reads the MCP9808 every 0.5 sec.

If we want to change the reading speed of the MCP9808, then we


turn ON the switches 2, 3, 4 & 5. Every switch corresponds to
different speed. If the switch 2 is ON the reading speed is 3 sec.

Switch 1 Switch 5

Switch 4

Switch 3

Switch 2

Martinos John and Martinos Themis [3]


Martinos TEAM User Guide for the PC Software
16/01/2018 Digital Thermometer Using Arduino and MCP9808 Digital Temperature Sensor
Using Visual Studio Community edition we wrote a windows form line represents the number of the loops. We control the delay for
application in C# which communicates with the UNO via the COM the loops via the track bar. The minimum value for the "Track Bar" is
port. The figure below shows the GUI for the PC software at the 1 (lowest speed) and the maximum value is 3000 (maximum speed).
startup. By default the program has preselected the Celsius and the The current value of the temperature is printed on the right and
reading speed at 50%. above the graph. If we select the Fahrenheit ratio button, then the
graph is cleared and the values now are on Fahrenheit.

As the "Connect" button is pressed, the software is searching all the


available COM ports for the UNO. First the software gets all the When the ratio button Celsius is selected it appears the following
available COM ports names. Then it sends a buffer with 3 bytes (4, 8 menu on the LCD display.
and 16) to each port until it receives the message "I'M ARDUINO". If
the software cannot find the UNO then it prints to message line "Not
connected". This will happen either because the UNO is not
connected to the PC, or if it is connected the switch 1 on the
"module" is OFF.

When the ratio button Fahrenheit is selected it appears the


following menu on the LCD display.

When the connection to the UNO is established, then the software


prints in the message line the text "Connection Successful -
With the "Clear" button we can clear the current graph and start a
Connected to COM5". In our case it is COM5, but it can vary from PC
new one. Finally, as long we control the UNO via the PC software,
to PC.
the control switches 2, 3, 4 & 5 on the "module" are not used and we
can control the speed only with the "track bar".

The readings from the temperature sensor are printed on the graph
as it shows the figure above. The vertical line of the graph represents
the value of the temperature on Celsius or Fahrenheit. The
horizontal line represents the number of the loops. The horizontal

Martinos John and Martinos Themis [4]


Martinos TEAM Code for the microcontroller
16/01/2018 Digital Thermometer Using Arduino and MCP9808 Digital Temperature Sensor
We used the Arduino IDE software to write the code for the B01100,
microcontroller and uploaded it to the board. Following you can see B01100,
the code. B01111,
B00111,
/* B00000,
Project: Digital Thermometer Using Arduino and B00000,
MCP9808 Digital Temperautre Sensor B00000
};
Team: John & Themis Martinos
Date: 10/01/2018 byte usb2[] = {
B01111,
We wrote the code for the Arduino in order to operate in two ways. B11111,
When switch 1 is OFF, the Arduino run as a standalone device. In B11111,
this case we can change the speed at which we get the readings B11111,
from the sensor by controlling the switches 2, 3, 4 & 5. When B11111,
switch 1 is ON, the Arduino wait for commands from the PC B11111,
software. In this case the speed at which we get the readings from B11111,
the sensor is controlled by the PC software. B01111
*/ };

#include <Wire.h> byte usb3[] = {


#include "Adafruit_MCP9808.h" B00000,
B11111,
// Create the MCP9808 temperature sensor object B11011,
Adafruit_MCP9808 tempsensor = Adafruit_MCP9808(); B11111,
B11111,
// include the library code: B11011,
#include <LiquidCrystal.h> B11111,
B00000
// initialize the library by associating any needed LCD interface pin };
// with the arduino pin number it is connected to
const int rs = 13, en = 12, d4 = 5, d5 = 4, d6 = 3, d7 = 2; byte LoopSpeed1[] = {
LiquidCrystal lcd(rs, en, d4, d5, d6, d7); B00000,
B00000,
//Setup message bytes B00000,
byte inputByte_0; B00000,
byte inputByte_1; B00000,
byte inputByte_2; B00000,
B11111,
const int buzzer = 7; // buzzer to arduino pin 9 B11111
int buzzerOnOff1 = true; };
int buzzerOnOff2 = true;
int buzzerOnOff3 = true; byte LoopSpeed2[] = {
int buzzerOnOff4 = true; B00000,
int buzzerOnOff5 = true; B00000,
int buzzerOnOff6 = true; B00000,
B00000,
int val = 0; B11111,
int speed1 = true; B11111,
int speed2 = true; B11111,
int speed3 = true; B11111
};
byte usb1[] = {
B01100, byte LoopSpeed3[] = {

Martinos John and Martinos Themis [5]


Martinos TEAM Code for the microcontroller
16/01/2018 Digital Thermometer Using Arduino and MCP9808 Digital Temperature Sensor
B00000, lcd.clear();
B00000, lcd.setCursor(0,0);
B11111, lcd.print("Conection to");
B11111, lcd.setCursor(0,2);
B11111, lcd.print("MCP9808 Failed");
B11111, while (1);
B11111, }
B11111 lcd.setCursor(0,0);
}; lcd.print("Conection to");
lcd.setCursor(0,1);
byte LoopSpeed4[] = { lcd.print("MCP9808 Success");
B11111, delay(1000);
B11111, lcd.clear();
B11111, }
B11111,
B11111, void loop() {
B11111,
B11111, lcd.display();
B11111 float c = tempsensor.readTempC();
}; float f = c * 9.0 / 5.0 + 32;

void setup() { if (digitalRead(6)==true){


lcd.createChar(1, usb1); if (buzzerOnOff1==true){
lcd.createChar(2, usb2); tone(buzzer, 3000);
lcd.createChar(3, usb3); delay(20);
lcd.createChar(4, LoopSpeed1); noTone(buzzer);
lcd.createChar(5, LoopSpeed2); buzzerOnOff1 = false;
lcd.createChar(6, LoopSpeed3); buzzerOnOff2 = true;
lcd.createChar(7, LoopSpeed4); lcd.clear();
lcd.setCursor(0,0);
pinMode(buzzer, OUTPUT); // Set buzzer - pin 9 as an output lcd.print("PC connection");
tone(buzzer, 3000); // Send 1KHz sound signal... //
delay(20); // ...for 0.02 sec lcd.setCursor(13,0);
noTone(buzzer); // Stop sound... lcd.write(1); // write byte "1" to lcd
lcd.setCursor(14,0);
pinMode(6, INPUT); lcd.write(2); // write byte "2" to lcd
pinMode(8, INPUT); lcd.setCursor(15,0);
pinMode(9, INPUT); lcd.write(3); // write byte "3" to lcd
//
// set up the LCD's number of columns and rows: lcd.setCursor(0,1);
lcd.begin(16, 2); lcd.print("Temp/ure");
Serial.begin(9600); lcd.setCursor(12,1);
analogReference(INTERNAL); lcd.print("Wait");
Serial.println("MCP9808 module"); }

lcd.setCursor(0,0); if (Serial.available()> 2)
lcd.print(" Digital "); {
lcd.setCursor(0,1); inputByte_0 = Serial.read();
lcd.print(" Thermometer "); delay(10);
delay(1000); inputByte_1 = Serial.read();
lcd.clear(); delay(10);
inputByte_2 = Serial.read();
if (!tempsensor.begin()) { delay(10);
Serial.println("Couldn't find MCP9808!");

Martinos John and Martinos Themis [6]


Martinos TEAM Code for the microcontroller
16/01/2018 Digital Thermometer Using Arduino and MCP9808 Digital Temperature Sensor
//Check for start of Message case 128:
if(inputByte_0 == 4) if (buzzerOnOff5==true){
{ tone(buzzer, 3000);
//Detect Command type delay(20);
switch (inputByte_1) noTone(buzzer);
{ buzzerOnOff5 = false;
case 8: buzzerOnOff6 = true;
//Set PIN and value }
switch (inputByte_2) lcd.setCursor(10,1);
{ lcd.print(" Wait");
case 16: break;
Serial.print("I'M ARDUINO"); }
if (buzzerOnOff6==true){ }
tone(buzzer, 3000); }
delay(20); }
noTone(buzzer); //Clear Message bytes
buzzerOnOff6 = false; inputByte_0 = 0;
buzzerOnOff3 = false; inputByte_1 = 0;
buzzerOnOff4 = false; inputByte_2 = 0;
} buzzerOnOff5=true;
break; }
{ }
case 32: else{
if (buzzerOnOff3==true){ if (buzzerOnOff2==true){
tone(buzzer, 3000); tone(buzzer, 3000);
delay(20); delay(20);
noTone(buzzer); noTone(buzzer);
buzzerOnOff3 = false; buzzerOnOff2 = false;
} buzzerOnOff1 = true;
buzzerOnOff4 = true; lcd.clear();
Serial.println(c); lcd.setCursor(0,0);
lcd.setCursor(10,1); lcd.print("Temperature");
lcd.print(c); lcd.setCursor(5,1);
lcd.setCursor(15,1); lcd.print("C or");
lcd.print("C"); lcd.setCursor(15,1);
break; lcd.print("F");
} }
{ lcd.setCursor(0,1);
case 64: lcd.print(c);
if (buzzerOnOff4==true){ lcd.setCursor(10,1);
tone(buzzer, 3000); lcd.print(f);
delay(20);
noTone(buzzer); val=500;
buzzerOnOff4 = false; ////////////////////////////////////////////////// [ 1 ]
} if (digitalRead(11)==true){
buzzerOnOff3 = true; val = 3000;
Serial.println(f); lcd.setCursor(12,0);
lcd.setCursor(10,1); lcd.write(4); // write byte "4" to lcd
lcd.print(f); }
lcd.setCursor(15,1); else{
lcd.print("F"); if (speed1==true){
break; lcd.setCursor(12,0);
} lcd.print(" ");
{ }

Martinos John and Martinos Themis [7]


Martinos TEAM Code for the microcontroller
16/01/2018 Digital Thermometer Using Arduino and MCP9808 Digital Temperature Sensor
} speed3 = true;
////////////////////////////////////////////////// [ 2 ] }
if (digitalRead(10)==true){ //////////////////////////////////////////////////
val = 2000; delay(val);
lcd.setCursor(12,0); }
lcd.write(4); // write byte "4" to lcd }
lcd.setCursor(13,0);
lcd.write(5); // write byte "5" to lcd
speed1 = false;
}
else{
if (speed2==true){
lcd.setCursor(13,0);
lcd.print(" ");
speed1 = true;
}
}
////////////////////////////////////////////////// [ 3 ]
if (digitalRead(9)==true){
val = 1000;
lcd.setCursor(12,0);
lcd.write(4); // write byte "4" to lcd
lcd.setCursor(13,0);
lcd.write(5); // write byte "5" to lcd
lcd.setCursor(14,0);
lcd.write(6); // write byte "6" to lcd
speed1 = false;
speed2 = false;
}
else{
if (speed3==true){
lcd.setCursor(14,0);
lcd.print(" ");
speed2 = true;
}
}
/////////////////////////////////////////////////// [ 4 ]
if (digitalRead(8)==true){
val = 10;
lcd.setCursor(12,0);
lcd.write(4); // write byte "4" to lcd
lcd.setCursor(13,0);
lcd.write(5); // write byte "5" to lcd
lcd.setCursor(14,0);
lcd.write(6); // write byte "6" to lcd
lcd.setCursor(15,0);
lcd.write(7); // write byte "7" to lcd
speed1 = false;
speed2 = false;
speed3 = false;
}
else{
lcd.setCursor(15,0);
lcd.print(" ");

Martinos John and Martinos Themis [8]


Martinos TEAM Code for the PC software
16/01/2018 Digital Thermometer Using Arduino and MCP9808 Digital Temperature Sensor

We used the Visual Studio Community edition and wrote a windows form application in C# which communicates with the Arduino UNO via
the COM port. This program consists of three main tabs. In the tab "Form1.cs" we have the main code (and main class) for the program. In the
tab "Form1.cs [Design]" we have designed the graphical user interface "GUI" for the program. The part of the code which reads the COM
ports and establishes the connection with the device, it is written in a second class. This part of the code it is written in the tab
"Connector.cs". First step for the code is to get all the available COM port names. Then to each COM port name it sends a buffer and wait for
a specific response. If the response is the message " I'M ARDUINO " then it saves the name of this port in a variable. On the next step it opens
this port and sends a buffer. As a response it gets back the data from the sensor.

The main code for "Form1.cs" tab


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO.Ports;
using System.Threading;

namespace WindowsFormsApp1
{
public partial class Form1 : Form
{

Connector comport = new Connector();


bool TF;

public Form1()
{
InitializeComponent();
chart2.Series[0].ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Line;
Celsius.Checked = true;
Fahrenheit.Checked = false;
trackBar.Value = 1500;
TimerPlot.Text = trackBar.Value + "";
}

private void button1_Click(object sender, EventArgs e)


{
if (comport.connect(9600, "I'M ARDUINO", 4, 8, 16) & (button1.Text == "Connect"))
{
Status.Text = "Connection Successful - Connected to " + comport.port;
timer1.Start();
button1.Text = "Disconnect";
TF = true;
}
else
{
Status.Text = "Not connected . . . ";
timer1.Stop();
button1.Text = "Connect";
String TempMessage = comport.message(4, 8, 128);
}
}

private void timer1_Tick(object sender, EventArgs e)


{
timer1.Interval = 3005 - trackBar.Value;
TimerPlot.Text = trackBar.Value + "";

Martinos John and Martinos Themis [9]


Martinos TEAM Code for the PC software
16/01/2018 Digital Thermometer Using Arduino and MCP9808 Digital Temperature Sensor

try
{
Status.Text = "Connection Successful - Connected to " + comport.port;
if (Celsius.Checked==true)
{
if (TF == true)
{
chart2.Series[0].Points.Clear();
TF = false;
Units.Text = "C";
}
String TempMessage = comport.message(4, 8, 32);
DataOut.Text = "";
DataOut.Text = TempMessage;
chart2.Series[0].Points.AddY(TempMessage.Substring(0, 5));
}
else
{
if (TF == false)
{
chart2.Series[0].Points.Clear();
TF = true;
Units.Text = "F";
}
String TempMessage = comport.message(4, 8, 64);
DataOut.Text = "";
DataOut.Text = TempMessage;
chart2.Series[0].Points.AddY(TempMessage.Substring(0, 5));
}
}
catch
{
Status.Text = ("Connection was lost - Searching . . .");
}
}
private void chart2_Click(object sender, EventArgs e)
{
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void DataOut_TextChanged(object sender, EventArgs e)
{
}
private void Status_TextChanged(object sender, EventArgs e)
{
}
private void Celsius_CheckedChanged(object sender, EventArgs e)
{
Units.Text = "C";
}
private void Fahrenheit_CheckedChanged(object sender, EventArgs e)
{
Units.Text = "F";
}
private void Units_Click(object sender, EventArgs e)
{
}

Martinos John and Martinos Themis [10]


Martinos TEAM Code for the PC software
16/01/2018 Digital Thermometer Using Arduino and MCP9808 Digital Temperature Sensor

private void Clear_Click(object sender, EventArgs e)


{
chart2.Series[0].Points.Clear();
DataOut.Text = "";
}
private void trackBar_Scroll(object sender, EventArgs e)
{
TimerPlot.Text = trackBar.Value + "";
}
private void panel1_Paint(object sender, PaintEventArgs e)
{
}
private void TimerPlot_TextChanged(object sender, EventArgs e)
{
}
}
}

The code for the "Connector.cs" tab


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO.Ports;
using System.Threading;

namespace WindowsFormsApp1
{
class Connector
{
public string port = "";
static SerialPort currentPort;
public Boolean connect(int baud, string recognizeText, byte paramone, byte paramtwo, byte paramthree)
{
try
{
byte[] buffer = new byte[3];
buffer[0] = Convert.ToByte(paramone);
buffer[1] = Convert.ToByte(paramtwo);
buffer[2] = Convert.ToByte(paramthree);
int intReturnASCII = 0;
char charReturnValue = (Char)intReturnASCII;
string[] ports = SerialPort.GetPortNames();
foreach (string newport in ports)
{
currentPort = new SerialPort(newport, baud);
currentPort.Open();
currentPort.Write(buffer, 0, 3);
Thread.Sleep(200);
int count = currentPort.BytesToRead;
string returnMessage = "";
while (count > 0)
{
intReturnASCII = currentPort.ReadByte();
returnMessage = returnMessage + Convert.ToChar(intReturnASCII);
count--;
}
currentPort.Close();
port = newport;
if (returnMessage.Contains(recognizeText))
{

Martinos John and Martinos Themis [11]


Martinos TEAM Code for the PC software
16/01/2018 Digital Thermometer Using Arduino and MCP9808 Digital Temperature Sensor

return true;
}
}
return false;
}
catch (Exception e)
{
return false;
}
}
public string message(byte paramone, byte paramtwo, byte paramthree)
{
try
{
byte[] buffer = new byte[3];
buffer[0] = Convert.ToByte(paramone);
buffer[1] = Convert.ToByte(paramtwo);
buffer[2] = Convert.ToByte(paramthree);
currentPort.Open();
currentPort.Write(buffer, 0, 3);
int intReturnASCII = 0;
char charReturnValue = (Char)intReturnASCII;
Thread.Sleep(200);
int count = currentPort.BytesToRead;
string returnMessage = "";
while (count > 0)
{
intReturnASCII = currentPort.ReadByte();
returnMessage = returnMessage + Convert.ToChar(intReturnASCII);
count--;
}
currentPort.Close();
return returnMessage;
}
catch (Exception e)
{
return "";
}
}
}
}

Martinos John and Martinos Themis [12]

View publication stats

You might also like