本帖最后由 wulisys 于 2018-10-9 22:40 编辑
实验室里有汞灯,有的同学担心紫外线辐射,某宝上搜了下,买了个紫外线传感器做了个检测仪。
一直在论坛里逛,也分享点代码出来,没什么技术含量
首先上传感器,不是广告啊!
UV指数,国际标准值
先上汞灯的测试结果吧,实际紫外线辐射可以忽略不计。
传感器接线很简单:
VCC-为电源正极输入口,接入3.3V-5V的电压;GND-GND为电源负极输入口; OUT-OUT为模拟信号输出口,链接MCU的I/O口;
数字7脚接了个按键,下拉电阻,按下第七脚高电平,切换“测量/自检”模式。
上代码: - <p> // 紫外线传感器示例代码
- // 检测波长:200-370nm
- // 接线方式:
- // VCC-5V
- // GND-GND
- // OUT-Analog pin 0
- #include <SPI.h>
- #include <Wire.h>
- #include <Adafruit_GFX.h>
- #include <Adafruit_SSD1306.h>
- #define OLED_RESET 4
- Adafruit_SSD1306 display(OLED_RESET);
-
- //#define LOGO16_GLCD_HEIGHT 16 //定义显示高度
- //#define LOGO16_GLCD_WIDTH 16 //定义显示宽度
- #if (SSD1306_LCDHEIGHT != 64)
- #error("Height incorrect, please fix Adafruit_SSD1306.h!");
- #endif
- int test=1;
- void setup()
- {
- Serial.begin(9600);// open serial port, set the baud rate to 9600 bps
- display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // initialize with the I2C addr 0x3D (for the 128x64)
- // init done
- pinMode(7,INPUT);
- }
- void loop()
- {
- int sValue;
- String ss;
-
- //按下按键test变量取反一次(数字脚7)
- if (digitalRead(7)) {
- test=test*-1;}
-
- //判断test状态,切换测试和自检模式
- if (test==1) {
- sValue = analogRead(0);//connect UV sensors to Analog 0
- } else
- {
- sValue=random(0,244);
- }
- ss=GetUVindex(sValue); //取得UV指数,string类型</p><p>
- //以下串口输出
- Serial.print("Value=");
- Serial.print(sValue);//print the value to serial
- Serial.print(" UV=");
- Serial.println(ss);
- //以下OLED12864屏显示
- display.clearDisplay();
- //切记,OLED显示中不要有运算函数,会影响显示效果!
- display.setTextSize(1); //设置字体大小
- display.setTextColor(WHITE); //设置字体颜色白色
- display.setCursor(0,0); //设置字体的起始位置
- if (test==1){
- display.println("UV index Test(xubin)");//输出字符并换行
- } else
- {
- display.println("Self checking...");//输出字符并换行
- }
- display.println("");
- display.setTextSize(2);
- display.print("UV:");
- display.println(ss);
- display.setTextSize(1);
- display.println("");
- display.print("Value=");
- display.println(sValue);
- display.println("");
- display.print("wavelength:200-370nm");
- display.display(); //显示以上
- delay(300);
- }
- //输出UV指数国际标准值1~11级,输出string类型
- String GetUVindex(int value)
- {
- float uv;
- if (Value<10) {uv=0;} else
- if (Value>=10 && Value<=46) {uv=0+(Value-10)/36.0;} else
- if (Value>=47 && Value<=65) {uv=1+(Value-47)/18.0;} else
- if (Value>=66 && Value<=83) {uv=2+(Value-66)/17.0;} else
- if (Value>=84 && Value<=103) {uv=3+(Value-84)/19.0;} else
- if (Value>=104 && Value<=124) {uv=4+(Value-104)/20.0;} else
- if (Value>=125 && Value<=142) {uv=5+(Value-125)/17.0;} else
- if (Value>=143 && Value<=162) {uv=6+(Value-143)/19.0;} else
- if (Value>=163 && Value<=180) {uv=7+(Value-163)/17.0;} else
- if (Value>=181 && Value<=200) {uv=8+(Value-181)/19.0;} else
- if (Value>=201 && Value<=221) {uv=9+(Value-201)/20.0;} else
- if (Value>=222 && Value<=240) {uv=10+(Value-222)/18.0;} else
- if (Value>241) {uv=12;}
- char a[]="";
- dtostrf(uv,2,1,a);
- return(a)
- }
- </p>
复制代码
|