Projects / UV exposure badge

UV exposure badge

Intermediate2 hGuided buildReviewed

Build it step by step

0 of 7 done
  1. ESP3217.8 mA0.91

    Wire the sensor and display

    Check both on I2C.

  2. ESP32

    Read the UV index

    Outdoors in the sun.

  3. ESP32

    Compare with a UV forecast

    Should be close.

  4. ESP32

    Add the dose sum

    Check it climbs.

  5. ESP32

    Set your limit

    Be cautious.

  6. ESP32

    Add the buzzer

    Test with a low limit.

  7. ESP32

    Fit it in a case

    Window facing out.

In short

How do you build an UV exposure badge?

A badge measures UV through the day and warns when you have had enough sun, with total dose on an OLED. You need Seeed XIAO ESP32C3, LTR390 UV sensor, 0.91 inch OLED 128x32 (I2C), Li-Po battery 3.7 V 500 mAh and Active buzzer 5 V. It takes 7 steps and about 120 minutes.

Level
Intermediate
Time
About 120 min
Parts
5

Parts list

Tick what you already own. It saves to My Garage, so “Build with what I have” knows.

Wiring

FromToWhy
LTR390 SDA and SCLXIAO D4 (GPIO6) and D5 (GPIO7)I2C (address 0x53)
LTR390 VIN and GNDXIAO 3V3 and GNDPower
OLED SDA and SCLXIAO D4 and D5Same I2C bus (address 0x3C)
OLED VCC and GNDXIAO 3V3 and GNDPower
Buzzer plus and minusXIAO D3 (GPIO5) and GNDTime-for-shade alert
LiPoXIAO battery pads BAT plus and minus (check polarity)Power and on-board charging

Code

The LTR390 measures ultraviolet light directly, and with 18x gain and 20-bit resolution the datasheet gives about 2300 counts for each UV index unit, so the sketch divides by 2300. One UV index unit is 25 milliwatts per square metre, so the dose in joules per square metre is the index times 0.025 times the seconds, added up through the day. The OLED shows the running total against a limit, and the buzzer sounds once when you reach it. The limit in the code is a cautious starting figure for fair skin, not medical advice: sunburn depends on your skin, your medicines and the day, so use the badge as a reminder to seek shade, cover up and use sunscreen, and do not use it as a reason to stay out. Keep the sensor window uncovered and facing outward, and remember glass and clothing block UV, so a badge under a jacket reads low. There is no clock chip, so the total resets when you power cycle it. LiPo safety: keep the cell away from heat, do not puncture it, charge it only through the board, and stop using a swollen cell. Arduino IDE with the ESP32 boards package 2.0.x, board "XIAO_ESP32C3".

Runs on a virtual Seeed XIAO ESP32-C3 in your browser, with sliders for the sensors. Happy with it? Verify and upload to your real board from the same page (Chrome or Edge on a computer).

// UV exposure badge: reads UV index, adds up the dose, shows it on the OLED and beeps near your daily limit.
// Libraries: Adafruit LTR390, Adafruit SSD1306, Adafruit GFX. Board: Seeed XIAO ESP32C3.
// One UV index unit is 25 mW per square metre of erythemal irradiance. Dose in J per square metre = UVI x 0.025 x seconds.
#include <Wire.h>
#include <Adafruit_LTR390.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
Adafruit_LTR390 ltr;
Adafruit_SSD1306 oled(128, 32, &Wire, -1);
const uint8_t BUZ = D3;
const float LIMIT_J_M2 = 200.0;           // a cautious limit for very fair skin; about 300 for medium skin. Not medical advice
float dose = 0;                            // J per square metre
unsigned long lastRead = 0;
bool warned = false;
void setup() {
  Wire.begin(D4, D5);
  ltr.begin();
  ltr.setMode(LTR390_MODE_UVS);
  ltr.setGain(LTR390_GAIN_18);            // with 18x gain and 20 bit resolution, UVI = reading / 2300
  ltr.setResolution(LTR390_RESOLUTION_20BIT);
  oled.begin(SSD1306_SWITCHCAPVCC, 0x3C);
  oled.setTextColor(SSD1306_WHITE);
  pinMode(BUZ, OUTPUT);
  lastRead = millis();
}
void loop() {
  if (millis() - lastRead >= 10000) {      // read every 10 seconds
    float seconds = (millis() - lastRead) / 1000.0;
    lastRead = millis();
    float uvi = ltr.readUVS() / 2300.0;
    dose += uvi * 0.025 * seconds;
    oled.clearDisplay(); oled.setTextSize(1); oled.setCursor(0, 0);
    oled.print("UV index "); oled.println(uvi, 1);
    oled.print("Dose "); oled.print(dose, 0); oled.print(" of "); oled.print(LIMIT_J_M2, 0);
    oled.display();
    if (dose >= LIMIT_J_M2 && !warned) {
      warned = true;
      for (int i = 0; i < 4; i++) { tone(BUZ, 2500, 200); delay(400); }
    }
  }
}

Test checklist

What you should see at each check. If a reading is far off, stop and use the troubleshooting list.

CheckExpected
IndoorsUV index near zero
Outdoors at middayUV index of several
DoseClimbs steadily
At the limitFour beeps once

If it does not work

Reads zero outdoors

Check the I2C address and that the window is clear.

Reads very high indoors

Check the gain and the divide by 2300.

OLED blank

Check the I2C address and pins.

Still stuck? Find the fault step by step with your multimeter →

Ask Lark AI

Common questions

What parts do I need for the UV exposure badge?

Seeed XIAO ESP32C3, LTR390 UV sensor, 0.91 inch OLED 128x32 (I2C), Li-Po battery 3.7 V 500 mAh and Active buzzer 5 V.

How long does the UV exposure badge take to build?

About 120 minutes for a intermediate maker, in 7 steps.

How do I know the UV exposure badge works?

Indoors: UV index near zero. Outdoors at midday: UV index of several. Dose: Climbs steadily.

Reads zero outdoors: what should I check?

Check the I2C address and that the window is clear.

Reads very high indoors: what should I check?

Check the gain and the divide by 2300.

OLED blank: what should I check?

Check the I2C address and pins.