- /*
- 【Arduino】168種傳感器模塊系列實(shí)驗(yàn)(資料代碼+圖形編程+仿真編程)
- 實(shí)驗(yàn)一百五十四:ML8511紫外線傳感器模塊 模擬量輸出UV Sensor Breakou
- 實(shí)驗(yàn)接線
- ML8511 / Arduino
- 3.3V = 3.3V
- OUT = A0
- GND = GND
- EN = 3.3V
- Arduino 3.3V = Arduino A1
- 實(shí)驗(yàn)之二:串口顯示ML8511紫外線傳感器數(shù)值(帶3.3V基準(zhǔn)校準(zhǔn))
- */
- //Hardware pin definitions
- int UVOUT = A0; //Output from the sensor
- int REF_3V3 = A1; //3.3V power on the Arduino board
- void setup()
- {
- Serial.begin(9600);
- pinMode(UVOUT, INPUT);
- pinMode(REF_3V3, INPUT);
- Serial.println("MP8511 example");
- }
- void loop()
- {
- int uvLevel = averageAnalogRead(UVOUT);
- int refLevel = averageAnalogRead(REF_3V3);
- //Use the 3.3V power pin as a reference to get a very accurate output value from sensor
- float outputVoltage = 3.3 / refLevel * uvLevel;
- float uvIntensity = mapfloat(outputVoltage, 0.99, 2.9, 0.0, 15.0);
- Serial.print("MP8511 output: ");
- Serial.print(uvLevel);
- Serial.print(" MP8511 voltage: ");
- Serial.print(outputVoltage);
- Serial.print(" UV Intensity (mW/cm^2): ");
- Serial.print(uvIntensity);
- Serial.println();
- delay(100);
- }
- //Takes an average of readings on a given pin
- //Returns the average
- int averageAnalogRead(int pinToRead)
- {
- byte numberOfReadings = 8;
- unsigned int runningValue = 0;
- for (int x = 0 ; x < numberOfReadings ; x++)
- runningValue += analogRead(pinToRead);
- runningValue /= numberOfReadings;
- return (runningValue);
- }
- //The Arduino Map function but for floats
- //From: http://forum.arduino.cc/index.php?topic=3922.0
- float mapfloat(float x, float in_min, float in_max, float out_min, float out_max)
- {
- return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
- }
復(fù)制代碼
|