如何在 Arduino Uno 中使用 i2c 16x2 LCD 为 DHT11、pH 传感器编码?

How to code for DHT11, pH Sensor with i2c 16x2 LCD in Arduino Uno?

我正在做我的大学项目,我必须在 16x2 LCD 上测量温度并根据温度切换冷却装置,我还必须使用 pH 传感器并显示它的温度16x2 的值。目前我正在使用下面的代码进行温度和切换,但它无法正常工作。它在屏幕上显示温度,但也有一些垃圾值,而且切换不正常。

 #include <Wire.h>
 #include <LCD.h>
 #include <LiquidCrystal_I2C.h>
 #include <dht.h>

 dht DHT;
 #define DHT11_PIN 7
 LiquidCrystal_I2C  lcd(0x27,2,1,0,4,5,6,7);
 const int ledPin = 6;

 void setup()
 {
  lcd.setBacklightPin(3,POSITIVE);
  lcd.setBacklight(HIGH);
  lcd.begin(16, 2);
  lcd.clear();
  Serial.begin(9600);

  for (int DigitalPin = 8; DigitalPin <= 8; DigitalPin++) {
   pinMode(DigitalPin, OUTPUT);
  }  

 }

 void loop()
 {
  int chk = DHT.read11(DHT11_PIN);
  float temp=(DHT.temperature);
  float Hum=(DHT.humidity);
  lcd.print("Temp         C  ");
  lcd.setCursor(6,0);
  lcd.print(temp);
  lcd.setCursor(0,1);
  lcd.println("Humid        %  ");
  lcd.setCursor(6,1);
  lcd.print(Hum);

  delay(1000);

  if (temp <= 29)
   digitalWrite(8, LOW);
  else if (temp>30)
  digitalWrite(8, HIGH);
 }

我会把我的回答分为两部分:

  1. 硬件:

请描述您的硬件配置以缩小问题范围:您如何切换继电器?是光耦合的吗?你用的是晶体管吗?如果有,是什么类型?你的继电器有反向二极管保护吗? (不要不要将继电器直接连接到 Arduino 引脚)

  1. 代码

2.1。您的 setup() 中的这个 for 循环不正确:

for (int DigitalPin = 8; DigitalPin <= 8; DigitalPin++) {
   pinMode(DigitalPin, OUTPUT);
}

您的索引是从 8 到 8,因此您不需要 for 循环来将单个引脚声明为输出。

2.2。通常,您可以将输出引脚分配给变量,或者像为 DHT 所做的那样作为宏。只要保持一致。此外,您的代码需要进行一般整理:

一个。使用所有调用

的库开始您的代码
#include <Wire.h>    // The Wire.h library is already called by the LiquidCrystal_I2C library, so you don't need to call it if you use the I2C one
#include <LCD.h>    // You are using the LCD library...
#include <LiquidCrystal_I2C.h>    // but also the I2C lcd library. You don't need both.
#include <dht.h>    // Is this the latest version? Use Adafruit's

b。尝试在开始时对所有宏进行分组。在此处声明引脚或常量:

// Using macros
#define DHT11_PIN 7
#define DigitalPin 8
#define DHT11_PIN 7

或使用引脚常量。选择一个,而不是两个:

// Group all your const int together, for the sake of clarity
const int DHT_PIN = 7;
const int DigitalPin = 8;
const int ledPin = 6;

c。我相信此构造函数不适合您选择的库。检查 this 库中的示例(假设它是相同的,我相信它是)

LiquidCrystal_I2C  lcd(0x27,2,1,0,4,5,6,7); 

尝试使用此构造函数,而不是:

// Set the LCD address to 0x27 for a 16 chars and 2 line display
LiquidCrystal_I2C lcd(0x27, 16, 2);
 

d。 setup() 函数也需要一些更改:

 void setup()
 {
   // You don't seem to be using the serial library. However, if you use it, it is 
   // preferable to start it first.
  Serial.begin(9600);  

  // LCD configuration
  lcd.begin(16, 2);    // Always call the begin() method first
  lcd.setBacklightPin(3,POSITIVE);    // Then, use additional configuration methods.
  lcd.setBacklight(HIGH);
  lcd.clear();
  // Once again, please check if you are using the right library. 
  // It might be the cause of the garbage you see.
   
  // Initialize the type of pin
   pinMode(DigitalPin, OUTPUT);
    
 }

e。根据 Adafruit 的 library(请参阅示例),您缺少一个宏来声明您正在使用的 DHT 传感器的类型:

#define DHTTYPE DHT11

f。在您的主循环()中,您正在读取带有一些奇怪括号的值:

int chk = DHT.read11(DHT11_PIN);
float temp=(DHT.temperature);
float Hum=(DHT.humidity);

根据最新的库,应该是:

// The latest library does not have this prototype: int chk = DHT.read11(DHT11_PIN); What is it for in your dht lib?
  
float temp = DHT.readTemperature(); // instead of: float temp=(DHT.temperature); --> which should be DHT.temperature(); in any case
float hum = DHT.readHumidity(); // instead of: float Hum=(DHT.humidity);

TL;DR:共享您的硬件以便为您提供帮助。更新您的库,清理您的代码,并使用最新库中的正确方法。

非常感谢您的指导,下面是我的工作代码:

#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <DHT.h>
#define DHTPIN 7 //digital pin for sensor

#define DHTTYPE DHT11      // DHT 11
DHT dht(DHTPIN, DHTTYPE);

LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);

void setup() 

{
  pinMode(8, OUTPUT); //for configuring relay output
  lcd.begin(16, 2);  
  dht.begin();
}

void loop() {

 delay(1000);
 float h = dht.readHumidity();
  
  // Read temperature as Celsius (the default)
  float t = dht.readTemperature();
  
  // Read temperature as Fahrenheit (isFahrenheit = true)
  float f = dht.readTemperature(true);

  // Check if any reads failed and exit early (to try again).
  if (isnan(h) || isnan(t) || isnan(f)) 

{
   lcd.print("NO CONNECTION");
    return;
}

//Hum code
  lcd.setCursor(0, 0);
  lcd.print("Hum:");
  lcd.print(h);
  lcd.print(" % ");
//Temp code
  lcd.setCursor(0, 1);
  lcd.print("Temp:");
  lcd.print(t);
  lcd.print(" C");


//To operate relay
{
if (t <= 26)
digitalWrite(8, LOW);
else if (t>27)
digitalWrite(8, HIGH);
}
  
}