By Leff
#9224
I tested this sensor with uno, and a IIC lcd module using the code (attached in the end), and got these results.

Indoor
(84.2 KiB) Downloaded 304 times


Outside, balcony UV sensor toward inside
(108.3 KiB) Downloaded 304 times


Outside, balcony UV sensor toward outside1
(87.68 KiB) Downloaded 304 times


Outside, balcony UV sensor toward outside2
(96.83 KiB) Downloaded 304 times


Code: Select all
#include <Wire.h>
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27, 20, 4);

int ReadUVintensityPin = A0; //Output from the sensor

void setup() {
  lcd.init();
  lcd.backlight();

  lcd.setCursor(0, 0);
  lcd.print("Booting...");
  delay(1000);
  pinMode(ReadUVintensityPin, INPUT);
  Serial.begin(9600); //open serial port, set the baud rate to 9600 bps
  Serial.println("Starting up...");
}

void loop() {
  int uvLevel = averageAnalogRead(ReadUVintensityPin);

  float outputVoltage = 5.0 * uvLevel / 1024;
  float uvIntensity = mapfloat(outputVoltage, 0.99, 2.9, 0.0, 15.0);

  lcd.clear();
  lcd.setCursor(0, 0);
  Serial.print("UV: "); lcd.print("UV: ");
  Serial.print(uvIntensity); lcd.print(uvIntensity);
  Serial.print(" mW/cm^2"); lcd.print(" mW/cm^2");

  Serial.println();
  delay(100);
}

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);

}

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;
}