Funpack第八期 Arduino nano 33 ble sense简易环境监测站
Funpack第八期 Arduino nano 33 ble sense简易的环境监测站,主要通过Arduino nano 33 ble sense开发板内部搭载的温度湿度传感器等多个传感器来实时测量周边环境的湿度、温度、光照强度、噪声等。
标签
嵌入式系统
Sunkx
更新2021-05-14
801

Funpack第八期 Arduino nano 33 ble sense简易环境监测站

任务二:环境监测站

利用NANO-33 BLE的传感器, 搭建一个小型环境监测站用于监测户外环境。待监测的参数包括:

  • 周边环境温度(精度:±0.1°C, ±0.1°F)

  • 周边环境湿度(精度:±1%)

  • 大气压强(精度:±0.1kPa, ±0.1psi)

  • 日照强度(用于判断白天/夜晚)

  • 周边平均噪声(精度:±1dB)

具体实现

需要器件:

  • Arduino Nano 33 BLE sense 开发板一块
  • OLED显示屏一块(SPI协议)

接线示意图

Fpn2MFznEZVppHy6pc9pXgTVbSdX

接线关系表

OLED屏管脚 开发板管脚
VCC 板子VCC
GND 板子GND
MOSi(D1) 11号管脚
CLK(D0) 13号管脚
RES(没有这个脚可以不管) 8号管脚
DC 9号管脚
CS 10号管脚

源码实现 读取温度和湿度

#include <Arduino_HTS221.h>
//读取温度湿度传感器所需要的库
void setup() {

  //init HTS to get Temperature and humidity
  if (!HTS.begin()) {
    Serial.println("Failed to initialize humidity temperature sensor!");
    while (1);
  }
}
void loop() {

  // read all the HTS sensor values
  float temperature = HTS.readTemperature();
  float humidity    = HTS.readHumidity();

}

读取大气压强

#include <Arduino_LPS22HB.h>
//读取大气压强传感器数据所需要的库
void setup() {

  //init LPS22HB to get air pressure
   if (!BARO.begin()) {
    Serial.println("Failed to initialize pressure sensor!");
    while (1);
  }
}
void loop() {

  //read the pressure of air
  float pressure = BARO.readPressure();

}

读取光照强度传感器

光照传感器首先读取的是周围环境的RGB值,我们通过RGB转灰度也是亮度的近似公式

Gray = 0.3*R +  0.6*G + 0.1*B

//rgb color
#include <Arduino_APDS9960.h>
// 读取光照强度所需的库
void setup() {
  
   Serial.begin(9600);
  //init APDS9960 to get rgb value
  if (!APDS.begin()) {
    Serial.println("Error initializing APDS9960 sensor.");
  }
}
void loop() {

  //read APDS sensor values
  while (! APDS.colorAvailable()) {
    delay(5);
  }
  int r, g, b;
  // read the color
  APDS.readColor(r, g, b);
  float gray = 0.3*r+0.6*g+0.1*b;  //得到光照值
}

读取噪声值

//noise 
#include <PDM.h>

#include<math.h>
// default number of output channels
static const char channels = 1;

// default PCM output frequency
static const int frequency = 16000;

// Buffer to read samples into, each sample is 16-bits
short sampleBuffer[512];

// Number of audio samples read
volatile int samplesRead;
void setup() {

// Configure the data receive callback
  PDM.onReceive(onPDMdata);
  PDM.setGain(20);
  //init PDM to get data of noise
  if (!PDM.begin(channels, frequency)) {
    Serial.println("Failed to start PDM!");
    while (1);
  }
}
void loop() {

//read the noise
  double noise = get_noise();   //获取噪音值
}
void onPDMdata() {
  // Query the number of available bytes
  int bytesAvailable = PDM.available();

  // Read into the sample buffer
  PDM.read(sampleBuffer, bytesAvailable);

  // 16-bit, 2 bytes per sample
  samplesRead = bytesAvailable / 2;
}
double get_noise()
{
  double amp=0,sum=0;
  int cnt =0;
  while(cnt<200)
  {
    if (samplesRead) {
        for (int i = 0; i < samplesRead; i++) 
        {
           sum+=double(sampleBuffer[i])*sampleBuffer[i];
        }
        cnt+=samplesRead;
        samplesRead=0;
  }
  amp = 20 *log10(10*sqrt(sum/cnt));
  return amp;
}
}

最终OLED显示

#include <Arduino.h>
#include <U8g2lib.h>

#ifdef U8X8_HAVE_HW_SPI
#include <SPI.h>
#endif
#ifdef U8X8_HAVE_HW_I2C
#include <Wire.h>
#endif

U8G2_SSD1306_128X64_NONAME_F_4W_SW_SPI u8g2(U8G2_R0, /* clock=*/ 13, /* data=*/ 11, /* cs=*/ 10, /* dc=*/ 9, /* reset=*/ 8);


void setup() {
  // put your setup code here, to run once:
  u8g2.begin();
  u8g2.enableUTF8Print();
}
void loop() {
  u8g2.clearBuffer();          // clear the internal memory
  u8g2.setFont(u8g2_font_wqy12_t_gb2312a ); // choose a suitable font
  u8g2.setCursor(0,10);
  u8g2.print("环境湿度:");
  u8g2.print(humidity);
  u8g2.print(" %");
  
  u8g2.setCursor(0,23);
  u8g2.print("环境温度:");
  u8g2.print(temperature);
  u8g2.print(" °C");
  
  u8g2.setCursor(0,36);
  u8g2.print("大气压强:");
  u8g2.print(pressure);
  u8g2.print(" kPa");
    
  u8g2.setCursor(0,49);
  u8g2.print("光照强度:");
  if(int(gray)>=20)
    u8g2.print("白天");
  else
    u8g2.print("夜晚");
  
  u8g2.setCursor(0,62);
  u8g2.print("平均噪声:");
  u8g2.print(noise);
  u8g2.print("dB");
  u8g2.sendBuffer();          // transfer internal memory to the display
  
  delay(1000);  


}

最终效果示意图

FkINxMm6arkFVHqhyLZLwacSgU0B

心得体会

Arduino 牛皮!

 

软硬件
电路图
附件下载
environment.ino
environment.ino.ARDUINO_NANO33BLE.bin
团队介绍
评论
0 / 100
查看更多
目录
硬禾服务号
关注最新动态
0512-67862536
info@eetree.cn
江苏省苏州市苏州工业园区新平街388号腾飞创新园A2幢815室
苏州硬禾信息科技有限公司
Copyright © 2023 苏州硬禾信息科技有限公司 All Rights Reserved 苏ICP备19040198号