Skip to content

Instantly share code, notes, and snippets.

@hsinghao
Created March 28, 2019 09:09
Show Gist options
  • Save hsinghao/e8436907ffc7120670f3f67fa58f4e0a to your computer and use it in GitHub Desktop.
Save hsinghao/e8436907ffc7120670f3f67fa58f4e0a to your computer and use it in GitHub Desktop.
模組介紹-faya 紫外線感應模組
// 2018/03/28
// Faya-Nugget 範例程式 (uv_1.ino)
// 單元: 模組介紹-faya 紫外線感應模組
// 網址: https://fayalab.blogspot.com/2019/03/uv.html
// 目標: (1) 利用UV感測器測得當前UV強度
// (2) 將結果顯示在串列傳輸監視視窗與LCD顯示模組
// 接線: Arduino ==> faya模組
// A0 ==> 紫外線模組_Vout
// A1 ==> 紫外線模組_Vref3.3V
// A4 ==> LCD顯示模組_SDA
// A5 ==> LCD顯示模組_SCL
#include <Wire.h> //引入I2C函式庫
#include <LiquidCrystal_I2C.h> //引入串列LCD模組函式庫
//定義LCD的位址與參數
LiquidCrystal_I2C faya_LCD(0x20, 16, 2); // LCD位址:0x20 ; LCD規格:16x2
void setup()
{
Serial.begin(9600); //啟動串列傳輸介
faya_LCD.init(); //初始化LCD
faya_LCD.backlight(); //啟動背光
}
void loop()
{
int portUVout = analogRead(A0); //儲存Vout的類比值
int portVref33 = analogRead(A1); //儲存Vref3.3V類比值
float vOut = (3.33 / portVref33) * portUVout; //儲存電壓值
float uvIntensity = (mapFloat(vOut,0.99,2.8,0,15)); //儲存UV強度
//顯示結果於串列監控視窗
Serial.println("===== fayaLab UV Sensor =====");
Serial.print("Vout (analog value) = "); //顯示Vout類比值
Serial.println(portUVout);
Serial.print("vREF33 (analog value) = "); //顯示Vref3.3V類比值
Serial.println(portVref33);
Serial.print("uv Voltage = "); //顯示Vout電壓值
Serial.print(vOut);
Serial.println(" (V)");
Serial.print("uv Intensity = "); //顯示UV強度
Serial.print(uvIntensity);
Serial.println(" (mW/cm2)\r\n");
//顯示結果於LCD顯示器
faya_LCD.setCursor(0, 0); //座標移到第0列第0行
faya_LCD.print("uvIntensity = "); //印出文字
faya_LCD.setCursor(0, 1); //座標移到第0行第1列
faya_LCD.print(uvIntensity); //印出UV值
faya_LCD.print(" (mW/cm2)"); //印出單位
delay(3000);
faya_LCD.clear();
}
//map float副程式
float mapFloat(float value, float fromLow, float fromHigh, float toLow, float toHigh) {
return (value - fromLow) * (toHigh - toLow) / (fromHigh - fromLow) + toLow;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment