这些小活动你都参加了吗?快来围观一下吧!>>
电子产品世界 » 论坛首页 » 活动中心 » 板卡试用 » 【换取手持数字示波器】RTduino驱动ssd1306+sht31温湿度计

共5条 1/1 1 跳转至

【换取手持数字示波器】RTduino驱动ssd1306+sht31温湿度计

助工
2024-04-12 21:07:39   被打赏 40 分(兑奖)     打赏

【编程环境】

1、RT-Thread stiduo

2、RTduino

【硬件】

1、HMIBoard开发板

2、ssd1306 OLED显示屏

3、sht31温湿度计

【实现步骤】

1、在工程目录下面打env配置开启RTuino下面的sensor的sht31。

image.png

2、打开RTuino下面的ssd1306

image.png

3、然后保存参数,使用pkg --update来更新工程包:

image.png

4、使用RT-Thread Studio打开工程,如果不是第一次还需要打开rasc重新生成一下工程:

image.png


5、添加路径到工程中,由于是在env下面生成的,RTunio新添加进来的工程没有包含到编译工程中去。按下图所示,分别添加c\c++的文件包含路径。

image.png

6、复制SHT31test.ino中的所有内容到arduino_main.cpp中去。

image.png

7、添加ssd1306的宏定义、头文件以及初始的代码到工程中;

//ssd1306

#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels

#define OLED_RESET     -1 // Reset pin # (or -1 if sharing Arduino reset pin)
#define SCREEN_ADDRESS 0x3C ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);


//end ssd1306

image.png


8、在setup中添加ssd1306的初始化以及显示LOGO函数,函数中display.begin是初始化ssd1306。

  //start ssd1306
  // SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
  if(!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
    Serial.println(F("SSD1306 allocation failed"));
    for(;;); // Don't proceed, loop forever
  }

  // Show initial display buffer contents on the screen --
  // the library initializes this with an Adafruit splash screen.
  display.display();
  delay(2000); // Pause for 2 seconds

  // Clear the buffer
  display.clearDisplay();

  display.display();
  display.setTextSize(2); // Draw 2X-scale text
  display.setTextColor(SSD1306_WHITE);
  display.setCursor(10, 0);
  display.println(F("SHT31DEMO"));
  display.display();      // Show initial text

image.png

9、在loop中,我们获取sht31的数据,如果成功就显示到oled屏上面:

void loop() {
  float t = sht31.readTemperature();
  float h = sht31.readHumidity();
  display.clearDisplay();
  display.setCursor(10, 0);
  display.println(F("SHT31DEMO"));
  if (! isnan(t)) {  // check if 'is not a number'
    Serial.print("Temp *C = "); Serial.print(t); Serial.print("\t\t");
    display.setCursor(2, 20);
    display.print("Temp:");
    display.println(t,2);
  } else {
    Serial.println("Failed to read temperature");
  }

  if (! isnan(h)) {  // check if 'is not a number'
    Serial.print("Hum. % = "); Serial.println(h);
    display.setCursor(16, 40);
    display.print("Hum:");
    display.println(h,2);
  } else {
    Serial.println("Failed to read humidity");
  }

  display.display();
  delay(1000);

  // Toggle heater enabled state every 30 seconds
  // An ~3.0 degC temperature increase can be noted when heater is enabled
  if (loopCnt >= 30) {
    enableHeater = !enableHeater;
    sht31.heater(enableHeater);
    Serial.print("Heater Enabled State: ");
    if (sht31.isHeaterEnabled())
      Serial.println("ENABLED");
    else
      Serial.println("DISABLED");

    loopCnt = 0;
  }
  loopCnt++;
}

10、实验效果:

fadf3bff6d25a998a51ec645450f250.jpg




关键词: RTduino     湿度计    

高工
2024-04-12 21:48:30     打赏
2楼

谢谢分享


专家
2024-04-19 08:42:21     打赏
3楼

谢谢分享


助工
2024-04-27 17:29:14     打赏
4楼

昨天在想这个怎么弄


工程师
2024-04-28 08:49:45     打赏
5楼

学习了。谢谢分享。


共5条 1/1 1 跳转至

回复

匿名不能发帖!请先 [ 登陆 注册 ]