风筝
发表于: 2019-1-16 14:59:30 | 显示全部楼层

在本篇文章中,我们将使用Arduino开发板制作一个带有OLED显示屏的温度计和湿度计。我们将从DHT22传感器读取温度和湿度,然后在OLED屏上显示数据。


OLED表示有机发光二极管,它们有许多不同的尺寸。我们要使用的尺寸是128X64(1.3英寸)。 OLED通过SPI或I2C通信与Arduino配合使用,但在本文中,我们使用的是SPI通信。

SPICom.jpeg


所需的硬件

●    Arduino Uno开发板

●    单色OLED显示屏12864

●    DHT22温湿度传感器

●    连接导线

●    面包板


电路原理图

首先,我们将OLED与Arduino开发板连接起来。 OLED可以连接到Arduino中的I2C和SPI引脚。以I2C方式连接OLED的连接更容易,但SPI通信比I2C快。因此,我们将使用SPI将OLED与Arduino连接起来。使用Arduino连接OLED,如下所示:

◾     将OLED上的CS引脚连接到Arduino上的引脚10

◾     将OLED上的DC引脚连接到Arduino上的引脚9

◾     将OLED上的RST引脚连接到Arduino上的引脚8

◾     将OLED上的D1或CLK引脚连接到Arduino上的引脚11

◾     将OLED上的D0或DIN引脚连接到Arduino上的引脚13


我们已将OLED连接到引脚13、11、10、9和8,因为这些引脚用于SPI通信。接下来,将DHT22与Arduino连接。 DHT22传感器与Arduino的连接如下:

◾     将DHT22上的VCC连接到Arduino上的5V引脚

◾     将DHT22上的GND连接到Arduino上的GND

◾     将DHT22的数据引脚连接到Arduino上的引脚7

connection.jpeg


代码说明

首先,我们包含用于DHT22传感器和OLED的库。 'U8glib'库用于OLED,它使代码非常容易。 我们将使用'U8glib'库的函数在OLED上显示数据。

  1. #include   
  2. #include "DHT.h"
复制代码

接下来,我们定义了连接DHT22传感器数据引脚的引脚,然后定义了DHT传感器的类型。 市场上还有一些其他类型的DHT传感器。 之后,我们初始化了连接OLED的引脚。

  1. #define DHTPIN 7
  2. #define DHTTYPE DHT22
  3. DHT sensor(DHTPIN, DHTTYPE);
  4. U8GLIB_SH1106_128X64 oled(13, 11, 10, 9, 8);
复制代码

setup()函数中,我们给出命令以开始从DHT22传感器接收值。 然后我们设置字体并在OLED上打印“Welcome to DIYHACKING”,,并持续5秒钟。 如果您不喜欢,可以更改字体大小。 您可以在此处找到不同的字体大小。

  1. sensor.begin();
  2. oled.firstPage();  
  3. do
  4. {
  5. oled.setFont(u8g_font_fur14);           //setting the font size
  6. //Printing the data on the OLED   
  7. oled.drawStr(20, 15, "Welcome");        
  8. oled.drawStr(40, 40, "To");
  9. oled.drawStr(5, 60, "DIYHACKING");  
  10. }  while( oled.nextPage() );
  11. delay(5000);
  12. }
复制代码

loop()函数中,我们从DHT22传感器读取湿度和温度值,然后使用温度和湿度计算热指数。

  1. float h = sensor.readHumidity();                 //Reading the humidity value
  2. float t = sensor.readTemperature();                 //Reading the temperature value
  3. float fah = sensor.readTemperature(true);        //Reading the temperature in Fahrenheit
  4. if (isnan(h) || isnan(t) || isnan(fah)) {                //Checking if we are receiving the values or not
  5. Serial.println("Failed to read from DHT sensor!");
  6. return;
  7. float heat_index = sensor.computeHeatIndex(fah, h);        //Calculating the heat index in Fahrenheit
  8. float heat_indexC = sensor.convertFtoC(heat_index);    //Calculating the heat index in Celsius
复制代码

最后,我们再次设置字体大小并在OLED上打印温度、湿度和热指数。 您可以按照上面讨论的链接更改字体大小,也可以在不同的位置设置数据。

  1. oled.firstPage();  
  2. do
  3. {
  4. oled.setFont(u8g_font_fub11);           //setting the font size
  5. //Printing the data on the OLED
  6. oled.drawStr(0, 15, "Temp: ");
  7. oled.drawStr(0, 40, "Hum: ");
  8. oled.drawStr(0, 60, "Hi: ");
  9. oled.setPrintPos(72, 15);                     //setting the dimensions to print the temperature
  10. oled.print(t, 0);
  11. oled.println("C");
  12. oled.setPrintPos(72, 40);                //setting the dimensions to print the humidity
  13. oled.print(h, 0);     
  14. oled.println("%");
  15. oled.setPrintPos(72, 60);                //setting the dimensions to print the heat index
  16. oled.print(heat_indexC, 0);
  17. oled.println("%");
  18. }  
  19. while( oled.nextPage() );
  20. delay(2000);  
  21. }
复制代码

Arduino代码

以下是本篇文章使用的完整代码: main.rar (771 Bytes, 下载次数: 228)


跳转到指定楼层
1750913958
发表于: 2020-1-22 11:20:56 | 显示全部楼层

这程序出现错误啊
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题 700 | 回复: 1479



手机版|

GMT+8, 2024-3-29 16:23 , Processed in 0.171843 second(s), 6 queries , Gzip On, MemCache On. Powered by Discuz! X3.5

YiBoard一板网 © 2015-2022 地址:河北省石家庄市长安区高营大街 ( 冀ICP备18020117号 )

快速回复 返回顶部 返回列表