Lesson 33: Using the MCP9808 High-Accuracy I2C Temperature Sensor with an LCD | Arduino Step-by-Step Course
In this lesson, we learn how to use the MCP9808 high-accuracy temperature sensor and display it on an LCD1602 or LCD2004 LCD screen. There are two other videos and code examples; one is an introduction to the MCP9808, and the other is about the TM1637 LED display.
[ad] temperature sensor
515-Lesson 33: Using the MCP9808 High-Accuracy I2C Temperature Sensor with an LCD | Arduino Step-by-Step Course
语言: C++
/*
* Robojax Arduino Step-by-Step Course
* Part 3: Temperature Sensors
* Lesson 33: MCP9808 with LCD1602-I2C
* Updated by Ahmad Shamshiri for Robojax (Robojax.com)
* on March 23, 2019 in Ajax, Ontario, Canada
Please watch the video instruction here: https://youtu.be/ljPrJKAokUA
This code is available at: http://robojax.com/course1/?vid=lecture33
with over 100 lectures free on YouTube. Watch it here: http://robojax.com/L/?id=338
Get the code for the course: http://robojax.com/L/?id=339
If you found this tutorial helpful, please support me so I can continue creating.
Make a donation using PayPal: http://robojax.com/L/?id=64
* Code is available at: http://robojax.com/learn/arduino
* This code is "AS IS" without warranty or liability. Free to be used as long as you keep this note intact.
* This code has been downloaded from Robojax.com
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
/**************************************************************************/
/*!
This is a demo for the Adafruit MCP9808 breakout.
----> http://www.adafruit.com/products/1782
Adafruit invests time and resources providing this open source code,
please support Adafruit and open-source hardware by purchasing
products from Adafruit!
*/
/**************************************************************************/
#include <Wire.h>
#include "Adafruit_MCP9808.h"
// Create the MCP9808 temperature sensor object
Adafruit_MCP9808 tempsensor = Adafruit_MCP9808();
// start of settings for LCD1602-I2C
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// Set the LCD address to 0x3F for a 16 chars and 2 line display
LiquidCrystal_I2C lcd(0x3F, 16, 2);
// end of settings for LCD1602-I2C
void setup() {
//YouTube Watch it here: http://robojax.com/L/?id=338
// initialize the LCD,
lcd.begin();
// Turn on the backlight and print a message.
lcd.backlight();
lcd.clear();
lcd.setCursor (0,0); //
// Make sure the sensor is found, you can also pass in a different i2c
// address with tempsensor.begin(0x19) for example
if (!tempsensor.begin()) {
lcd.print("MCP9808 Error");
}
lcd.print("MCP9808 Test");
delay(2000);
}
void loop() {
//YouTube Watch it here: http://robojax.com/L/?id=338
//Serial.println("wake up MCP9808.... "); // wake up MCP9808 - power consumption ~200 mikro Ampere
//tempsensor.wake(); // wake up, ready to read!
lcd.clear();// clear previous values from screen
lcd.setCursor (0,0); //character zero, line 1
lcd.print("MCP9808 Sensor"); // print text
// print distance in degrees C
lcd.setCursor (0,1); //character 0, line 2
lcd.print(getTemp('C'));// print Temperature Celsius
lcd.setCursor (5,1); //character 6, line 2
lcd.print((char)223);// print degree
lcd.setCursor (6,1); //character 7, line 2
lcd.print("C");// print C
// print distance degrees F
lcd.setCursor (8,1); //character 9, line 2
lcd.print(getTemp('F'));// print distance in cm
lcd.setCursor (14,1); //character 15, line 2
lcd.print((char)223);// print degree
lcd.setCursor (15,1); //character 16, line 2
lcd.print("F");// print F
//Serial.println("Shutdown MCP9808.... ");
//tempsensor.shutdown(); // shutdown MCP9808 - power consumption ~0.1 micro Ampere
delay(1000);
}
/*
* Written by Ahmad Shamshiri for Robojax.com
* January 22, 2019 in Ajax, Ontario, Canada
*
* getTemp(char type)
* returns temperature in either C, F, or K
* @param type is a character of upper case
* C is used to get Celsius
* F is used to get Fahrenheit
* K is used for Kelvin
*/
float getTemp(char type) {
//YouTube Watch it here: http://robojax.com/L/?id=338
float c = tempsensor.readTempC();//get main temperature in C
float f = c * 9.0 / 5.0 + 32;// convert to fahrenheit
if(type =='F')
{
return f;// fahrenheit
}else if(type =='K')
{
return c + 274.15;// return Kelvin
}else{
return c; //return Celsius
}
}//getTemp ends
资源与参考
-
外部Adafruit MCP9808 库(来自 GitHub)github.com
-
外部下载 MCP9808 数据表(PDF)ww1.microchip.com
文件📁
没有可用的文件。