搜索代码

Lesson 30: Using a BMP280 Temperature Sensor with an LCD1602 | Arduino Step-by-Step Course

Lesson 30: Using a BMP280 Temperature Sensor with an LCD1602 | Arduino Step-by-Step Course

In this lesson, we learn how to use the BMP280 to measure temperature and display it on an LCD1602 as Celsius, Fahrenheit, and Kelvin. There is separate code for the LCD2004.

518-Lesson 30: Using a BMP280 Temperature Sensor with an LCD1602 | Arduino Step-by-Step Course
语言: C++
/*
 /* 
 * Robojax Arduino Step-by-Step Course
 * Lesson 30, Using BMP280 with LCD1602 display
 
 *  
   Please watch video instructions here https://youtu.be/4IqMqveK38U
 This code is available at http://robojax.com/course1/?vid=lecture11
 
 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  

 *  You can get this code from Robojax.com
 *  
 *  Original library source: https://github.com/ozhantr/DigitLedDisplay


 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 library for the BMP280 humidity, temperature & pressure sensor

  Designed specifically to work with the Adafruit BMP280 Breakout
  ----> http://www.adafruit.com/products/2651

  These sensors use I2C or SPI to communicate; 2 or 4 pins are required
  to interface.

  Adafruit invests time and resources providing this open source code,
  please support Adafruit and open-source hardware by purchasing products
  from Adafruit!

  Written by Limor Fried & Kevin Townsend for Adafruit Industries.
  BSD license, all text above must be included in any redistribution
 ***************************************************************************/

#include <Wire.h>
#include <SPI.h>
#include <Adafruit_BMP280.h>

#define BMP_SCK  (13)
#define BMP_MISO (12)
#define BMP_MOSI (11)
#define BMP_CS   (10)

Adafruit_BMP280 bmp; // I2C
//Adafruit_BMP280 bmp(BMP_CS); // hardware SPI
//Adafruit_BMP280 bmp(BMP_CS, BMP_MOSI, BMP_MISO,  BMP_SCK);

// LCD1602 or LCD2004 settings

#include <LiquidCrystal_I2C.h>
// Set the LCD address to 0x3F for a 20 chars and 4 line display
LiquidCrystal_I2C lcd(0x3F, 16, 2);
// end of LCD1602 or LCD2004 settings

void setup() {
// initialize the LCD, 
  lcd.begin();
  // Turn on the backlight and print a message.
  lcd.backlight();
  lcd.clear();
   lcd.setCursor (0,0); //

  if (!bmp.begin()) {
      lcd.print("BMP280 Error"); 
    while (1);
  }
  lcd.print("BMP280 Test"); 
  /* Default settings from datasheet. */
  bmp.setSampling(Adafruit_BMP280::MODE_NORMAL,     /* Operating Mode. */
                  Adafruit_BMP280::SAMPLING_X2,     /* Temp. oversampling */
                  Adafruit_BMP280::SAMPLING_X16,    /* Pressure oversampling */
                  Adafruit_BMP280::FILTER_X16,      /* Filtering. */
                  Adafruit_BMP280::STANDBY_MS_500); /* Standby time. */
}

void loop() {
  lcd.clear();// clear previous values from screen
  lcd.setCursor (0,0); //character zero, line 1
  lcd.print("BMP280 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'));// F
  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 
    delay(1000);
}// loop end

/*
 * Written by Ahmad Shamshiri 
 * 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
 * Written by Ahmad Shamshiri for Robojax.com
 */
float getTemp(char type) {
    float c = bmp.readTemperature();//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

资源与参考

文件📁

没有可用的文件。