hi welcome to an Arduino step-by-step course by Robojax
in this lecture I’m going to introduce this mcp980 temperature sensor from microchip
this is the actual chip mcp9808 and it has been offered in this module format so it can be used
easily with Arduino unlike any other temperature sensor so far that this is far the best with high
accuracy and greater range this can display from -40 to up to 125 degrees Celsius the accuracy is
0.25 degrees it has greater selectable resolution without further ado let's get started with this
you receive the module you will be receiving it with this pin header and we have to solder it
let me explain the pins we have vcc which will be connected to 5 volts we have ground
will be connected to the ground scl and sda for I square c communication sda and Arduino uno will
be connected to a4 and scl will be connected to a5 rest of the pins will not be connected
this is a data sheet for microchip mcp9808 temperature sensor and the accuracy is
0.5 degrees Celsius the range for the temperature have been shown the typical with plus minus 0.25
is from minus 40 to 125 degrees flexible resolution is 0.5 plus 0.5 or 0.25 or 0.125 and
0.0625 and it has user programmable temperature alert output which has not been used in this
library the operating voltage is 2.7 to 5.5 volts so it can work with 3.3 volts in case
you run out of 5 volts or you can put it at 5 volts so that's very nice amount of current
that it consumes is 200 milliamp typical but you can also do a shutdown whenever
that temperature measurement is not needed you can shut it down this is very important
especially when the device is used in a mobile devices and portable devices that run on battery
and budget consumption matters so we can shut it down whenever not needed
the communication and reading of the data will be using the two-wire of I-square-c
if you want to look for further information I will provide you the length for this data sheet
here is the wiring diagram for microchip mcp9808 module with Arduino from the left side vcc is
connected to five volts ground have been connected using the orange wire to ground and from clock
that's a cl the clock a signal for clock have been connected using red wire to a5 sda the data pin
have been connected to af4 here is a wiring on the Arduino from the left the vcc have been connected
using the yellow wire to 5 volts the ground have been connected using orange to the ground
this is the scl the clock have been connected to a5 and sda the data have been connected to a4
these are four other wires are left unconnected
let me explain the code the include wire dot h is part of the Arduino ide once you have the software
running this is already included you don't need to include anything else this line include adafruit
mcp9808.h that's a header file which is part of the library from adafruit I will provide you the
link and once you click and download it as a zip file save it and then include it with this library
and you can include it by clicking on the sketch include library add zip then point it to where
you have downloaded in this case it has been downloaded in this folder select it and click
open once you click open then close your Arduino ide and open it again once you reopen it
click on file examples and then you will see mcp9808 library and click on this
there is only one example and open it which is this file and this is a class and after that we
are creating an instance of the class or an object needs this uh constructor inside the
setup we initialize the serial monitor with 9600 bot so it can display the information for us and
this line serial parent ln it just prints the text temperature temp sensor.begin is a function
within the class and it returns true if everything works fine and if it returns false if something
goes wrong in terms of measurement or connectivity and this exclamation mark means not true so this
returns true if it is not true then we say serial printer n could not find mcp9808 this is a text
this will be printed while one means stay forever once there is an error the screen will stay here
and you will see this text and the program will stack and the because this device uses I square cs
communication there is an address for that and by default this is 0.18 on the device
I’m going to show you to run I2C scanner to see the address but you don't have to put it
by default it works with this device and inside the loop we read the temperature using the temp
sensor dot read temp c that's a centigrade this is the Celsius temperature and the value
for this will be stored in a float variable c and then we create another variable f for
Fahrenheit and c multiplied by this will give us the Fahrenheit value so now we have Fahrenheit and
Celsius and this line will print the text temp with a double quote this is a text and then we
print the c and Celsius so after this you will see that temperature in Celsius and after that
we will see this asterisk or a star with c this backslash t means tab and there will be like this
much space long tab after that we will see the f the Fahrenheit and after that you'll see this text
asterisk with f and because this has ln all of this will be in one line and this will create
a new line and we wait one second and the loop continues for the next iteration and again we'll
display the temperature and it continues like that here is a feature for shutdown in case if
you want to shut it down and it's not needed to save the battery then use temp sensor dot shutdown
and here this is how you take action let's say you want to do something with the temperature
and Celsius is above 60 degrees when it is bigger than 60 then you can do something we could put f
we can use f open parenthesis and c bigger than 60 and inside this curly braces we can put our
code whatever you want you can turn off something let's say the temperature increases and you want
to turn on a fan to reduce the temperature of some liquid or some machine or device whatever it is
and this can turn on or you can go the reverse anytime temperature is below 60 you can turn
on a heater or relay or whatever you have so this way you can take action
now the code is running this is a Celsius and that's the Fahrenheit
now I’m running this heat gun to apply
heat
as you can see the temperature increased up to 71 degrees 161 degrees Fahrenheit and it's
slowly dropping and as you can see we also see the decimal points it's very nice
if I hold it the temperature will dry quickly if you did not connect the wiring properly you
should see on your serial monitor something like this check your wires and try again
here is the updated code with the custom function that I have written
whenever you need to call a temperature for example get temp
put inside a single code c it will give you the Celsius and if you need Fahrenheit just
use this one and you can get that you can do some calculation with it or in case I’m printing it I
just put it inside this print opening and closing so this is the actual value in Celsius and this is
the actual value in Fahrenheit just put f and if you need it in Kalvin also you get the kelvin now
this function written is below here I’ve explained it but let me explain it good function this is
the beginning and that is the end and the type is float we pass that character c f or k and we read
the temperature stored in c that's a Celsius and then using this calculation we make it Fahrenheit
and we check the type if it is Fahrenheit return f else f if it is k add this number and convert
it to kelvin else if it's not Fahrenheit or kelvin it must be Celsius and return c
here is a demonstration with the custom function as you can see it prints
Celsius Fahrenheit and also kelvin now let me apply some heat with this heat gun
and here it shows Celsius Fahrenheit and kelvin it's very hot now 85 4 degrees
I’m now going to move the temperature sensor outside the window is a little
open as you can see the temperature dropped to 11.
so we got the minus 15 and it can go lower
I’m bringing it back inside the room
and as you can see the module is getting warmer
thanks for your attention this was how to use this microchip mcp9808 temperature sensor
with Arduino please make sure to subscribe by clicking at the subscribe button in here
thank you thank you for watching tutorial from Robojax if you learned something and
found this useful please thumb up as this will help my video in the search algorithm of YouTube
if you have comment or question please post it at the comment section below
I try to answer and reply and don't forget to subscribe so you get updates on my upcoming videos