UV meter
Print Profile(0)
Description
My girlfriend got sunburned in a tanning salon because new tubes had been installed in the machine without informing customers. Therefore, I designed and built a measuring device using an Arduino Nano controller and a UV light sensor. The instrument displays the raw measured value and the corresponding UV index on a small OLED screen.
This makes it possible to check the intensity of the tanning bed before starting a session. This is the first version, developed according to her request, and the program is still under development. In the meantime, I am providing the basic source code below.
#include
#include
#include
#include
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 32
#define OLED_RESET -1
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
const int uvPin = A0;
// Időzítés
const unsigned long sampleInterval = 500; // 0,5 másodperc
unsigned long lastSampleTime = 0;
// Súlyozott mozgóátlag (2 másodperc = 4 minta)
int samples[4] = {0, 0, 0, 0};
int sampleIndex = 0;
bool bufferFilled = false;
// Súlyok (régi → új)
const int weights[4] = {1, 2, 3, 4};
void setup() {
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
while (true);
}
// 👋 Bejelentkezési képernyő
display.clearDisplay();
display.setTextColor(SSD1306_WHITE);
display.setTextSize(1);
display.setCursor(0, 0);
display.println("Hi");
display.setTextSize(2);
display.setCursor(0, 12);
display.println("Vanda");
display.display();
delay(2000);
}
void loop() {
unsigned long currentMillis = millis();
// 📥 Mintavételezés 0,5 másodpercenként
if (currentMillis - lastSampleTime >= sampleInterval) {
lastSampleTime = currentMillis;
int uvRaw = analogRead(uvPin); // Minta tárolása körkörös pufferben samples[sampleIndex] = uvRaw; sampleIndex = (sampleIndex + 1) % 4; if (sampleIndex == 0) bufferFilled = true; // Súlyozott átlag számítása long weightedSum = 0; long totalWeight = 0; int count = bufferFilled ? 4 : sampleIndex; for (int i = 0; i < count; i++) { int idx = (sampleIndex - count + i + 4) % 4; weightedSum += samples[idx] * weights[i]; totalWeight += weights[i]; } int uvAverage = (totalWeight > 0) ? weightedSum / totalWeight : 0; // 🌞 Feszültség számítása float voltage = uvAverage * (5.0 / 1023.0); // ☀️ UV Index (adatlap alapján) float uvIndexFloat = voltage / 0.1; int uvIndex = round(uvIndexFloat); uvIndex = constrain(uvIndex, 0, 11); // 📊 Grafikus sáv szélessége int barWidth = map(uvAverage, 0, 1023, 0, SCREEN_WIDTH - 1); barWidth = constrain(barWidth, 0, SCREEN_WIDTH); // 🖥️ Kijelzés display.clearDisplay(); // Bal oldal: súlyozott átlag display.setTextSize(2); display.setCursor(0, 0); display.println(uvAverage); display.setTextSize(1); display.setCursor(0, 22); display.println("DAC"); // Jobb oldal: UV index display.setTextSize(2); display.setCursor(90, 0); display.println(uvIndex); display.setTextSize(1); display.setCursor(80, 22); display.println("UV INDEX"); // 🔻 Alsó grafikus sáv (legalsó pixelsor) display.drawFastHLine(0, SCREEN_HEIGHT - 1, barWidth, SSD1306_WHITE); display.display();
}
}
Parts:
* Arduino Nano
* I2C OLED Display Module 0.91 Inch I2C SSD1306 OLED Display Module White / BLUE I2C OLED Screen Driver DC 3.3V~5V for Arduino
* A1918 analog UV Light Sensor - GUVA-S12SD












Comment & Rating (0)