Build it step by step
0 of 7 doneWire the sensor
VCC to 3V3, GND to GND, OUT to GPIO34.
Wire the OLED
3V3, GND, SDA to GPIO21, SCL to GPIO22.
Install the libraries
Adafruit SSD1306 and Adafruit GFX.
Upload the sketch
Indoors it shows a low number.
Take it outdoors
Hold the sensor flat towards the sky.
Move into shade
The value falls.
Note the peak
Highest value since power-up.
How do you build an ESP32 sun exposure meter?
An ESP32 reads a GUVA-S12SD UV sensor and shows a rough UV index and a sun-care reminder on an OLED, with the highest value of the session. You need ESP32 DevKit V1, GUVA-S12SD UV sensor module, SSD1306 OLED Display and Breadboard 830 Tie Points. It takes 7 steps and about 130 minutes.
- Level
- Intermediate
- Time
- About 130 min
- Parts
- 4
Parts list
- Development BoardsBuyESP32 DevKit V1 × 1
- SensorsBuy
- DisplaysBuy
- PrototypingBuy
Tick what you already own. It saves to My Garage, so “Build with what I have” knows.
Wiring
| From | To | Why |
|---|---|---|
| GUVA module VCC and OLED VCC | ESP32 3V3 | Power at 3.3 V |
| GUVA module GND and OLED GND | ESP32 GND | Common ground |
| GUVA module OUT | ESP32 GPIO34 | Analog reading (input only pin) |
| OLED SDA | ESP32 GPIO21 | I2C data |
| OLED SCL | ESP32 GPIO22 | I2C clock |
Code
The GUVA-S12SD is a diode that responds to ultraviolet light, with an amplifier on the module that turns its tiny current into a voltage. In the usual module the output is about 100 millivolts for each step of the UV index, so a reading of 500 millivolts is roughly index 5. The ESP32 reads that voltage with its analog converter, averages 32 readings to calm the noise, and divides by 100. The numbers are only a rough guide, because modules differ and the sensor responds to a wide band of UV rather than the sunburn curve the real index is based on, so it can read higher or lower than the official figure. The bands follow the standard scale from low to extreme. Use it as a way to learn how UV varies through the day and with cloud and shade, and follow official sun-safety advice for real decisions. Glass blocks most UV, so test it outdoors, point the sensor at the sky and never look at the sun.
Runs on a virtual ESP32 Dev Module 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).
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
Adafruit_SSD1306 oled(128, 64, &Wire, -1);
const int UV_PIN = 34;
float peakIndex = 0;
float readMilliVolts() {
long sum = 0;
for (int i = 0; i < 32; i++) { sum += analogReadMilliVolts(UV_PIN); delay(5); }
return sum / 32.0;
}
const char* band(float uv) {
if (uv < 3) return "Low";
if (uv < 6) return "Moderate";
if (uv < 8) return "High";
if (uv < 11) return "Very high";
return "Extreme";
}
const char* advice(float uv) {
if (uv < 3) return "Little cover needed";
if (uv < 6) return "Hat, shade at noon";
if (uv < 8) return "Cover up, sunscreen";
return "Avoid midday sun";
}
void setup() {
Wire.begin(21, 22);
if (!oled.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { while (true) {} }
}
void loop() {
float mv = readMilliVolts();
float uv = mv / 100.0; // the module gives about 100 mV for each index step
if (uv > peakIndex) peakIndex = uv;
oled.clearDisplay();
oled.setTextColor(SSD1306_WHITE);
oled.setTextSize(1);
oled.setCursor(0, 0);
oled.print("UV index (rough guide)");
oled.setTextSize(3);
oled.setCursor(0, 14);
oled.print(uv, 1);
oled.setTextSize(1);
oled.setCursor(0, 44);
oled.print(band(uv)); oled.print(" peak "); oled.print(peakIndex, 1);
oled.setCursor(0, 55);
oled.print(advice(uv));
oled.display();
delay(500);
}Test checklist
What you should see at each check. If a reading is far off, stop and use the troubleshooting list.
| Check | Expected |
|---|---|
| Indoors under lamps | Near 0 |
| Outdoors in the sun | A clearly higher number |
| Move into shade | The value falls |
| Cover the sensor | Near 0 |
| Behind window glass | Much lower than outdoors |
If it does not work
Always zero
Check OUT is on GPIO34 and the module has 3.3 V.
Always at the maximum
The output may be above 3.3 V, check your module's supply.
Reading differs from a weather app
This is a rough guide, the sensor and the official index use different light bands.
Display blank
Check the I2C wires and the address 0x3C.
Readings noisy
Keep the wires short and away from the display wires.
Still stuck? Find the fault step by step with your multimeter →
Common questions
What parts do I need for the ESP32 sun exposure meter?
ESP32 DevKit V1, GUVA-S12SD UV sensor module, SSD1306 OLED Display and Breadboard 830 Tie Points.
How long does the ESP32 sun exposure meter take to build?
About 130 minutes for a intermediate maker, in 7 steps.
How do I know the ESP32 sun exposure meter works?
Indoors under lamps: Near 0. Outdoors in the sun: A clearly higher number. Move into shade: The value falls.
Always zero: what should I check?
Check OUT is on GPIO34 and the module has 3.3 V.
Always at the maximum: what should I check?
The output may be above 3.3 V, check your module's supply.
Reading differs from a weather app: what should I check?
This is a rough guide, the sensor and the official index use different light bands.
Display blank: what should I check?
Check the I2C wires and the address 0x3C.
