ESP8266 - GUVA-S12SD UV Sensor

This tutorial instructs you how to use the ESP8266 NodeMCU to read the strength of ultraviolet light from a GUVA-S12SD UV sensor. Sunburn is caused by UV, not by heat or brightness, so a normal light sensor cannot warn you about it. This tiny module can, and the NodeMCU needs only its single analog pin to read it. Later you can push the value to your phone over WiFi. In detail, we will learn:

ESP8266 NodeMCU NodeMCU GUVA-S12SD UV sensor

We suggest:

Hardware Preparation

1×ESP8266 NodeMCU ESP-12F
1×Alternatively, ESP8266 D1 Mini NodeMCU ESP-12F
1×Alternatively, ESP8266 NodeMCU ESP-12E (Uno-form)
1×Micro USB Cable
1×Alternatively, ESP8266 NodeMCU ESP-12F (SMD-form)
1×USB Cable Type-A to Type-C (for USB-A PC)
1×USB Cable Type-C to Type-C (for USB-C PC)
1×GUVA-S12SD UV Sensor Module
1×Jumper Wires
1×Breadboard
1×Recommended: Screw Terminal Expansion Board for ESP8266

Or you can buy the following kits:

1×DIYables Sensor Kit (18 sensors/displays)
Disclosure: Some of the links provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you.
Additionally, some of these links are for products from our own brand, DIYables .

Overview of GUVA-S12SD UV Sensor

GUVA-S12SD UV sensor module pinout
image source: diyables.io

The module is a small purple board, about 11mm x 27mm, with two mounting holes. Look for the small white square near one end: that is the GUVA-S12SD photodiode, the part that sees UV. Next to it sits a VLM358 op-amp chip. The header is already soldered, and DIYables sells the module as a pack of two.

How does it work? When UV light lands on the photodiode, it lets a very small current flow. This current is far too weak for the ESP8266 to notice. The op-amp turns it into a voltage on the SIG pin, and the NodeMCU reads that voltage on A0 with analogRead(). More UV gives a higher voltage.

The sensor only cares about UV-A and UV-B light, from 240nm to 370nm. Ceiling lights, phone screens and most window light give it almost nothing, so do not worry if it shows nearly 0 on your desk. That is normal. Carry it outside into the sun, or shine a UV flashlight at it, to see real numbers.

GUVA-S12SD UV Sensor Module
Sensor chip GUVA-S12SD UV photodiode + VLM358 op-amp
Light it detects UV-A and UV-B, 240nm to 370nm
Viewing angle 130 degrees
Supply voltage 2.5V to 5V DC
Output analog voltage on SIG
SIG with no UV about 0V
SIG in strong sunlight about 1V or a bit more
Board size about 11mm x 27mm, two mounting holes

Most makers use a simple rule to turn the voltage into a UV index: UV index ≈ SIG voltage / 0.1. In other words, every 0.1V on SIG is about one UV index point, so 0.5V means a UV index of about 5. This is a good estimate for hobby projects, but the module is not a calibrated UV meter. Do not trust it for medical or safety decisions.

The UV index levels defined by the WHO are:

UV Index Level What it means
0 - 2 Low Little risk for most people
3 - 5 Moderate Wear a hat and sunscreen at midday
6 - 7 High Stay in the shade around noon
8 - 10 Very High Skin can burn quickly
11+ Extreme Avoid the midday sun

The GUVA-S12SD UV Sensor Pinout

The three pins are printed on the board in this order: SIG, GND, VCC.

  • SIG pin: must be connected to the A0 pin of the NodeMCU. It carries the analog UV voltage.
  • GND pin: must be connected to a G (GND) pin of the NodeMCU (0V)
  • VCC pin: must be connected to the 3V3 pin of the NodeMCU. The module also runs on 5V, but 3.3V matches the logic level of the ESP8266.

※ NOTE THAT:

The arrangement of pins on a module may differ from one manufacturer to another. It is essential to always refer to the labels printed on the module when using it. Take a close look!

Wiring Diagram

The ESP8266 has just one analog input, A0, so the SIG wire has only one place to go. Take 3V3 and G from the same side of the NodeMCU as A0, which keeps the three wires short and tidy.

The wiring diagram between ESP8266 NodeMCU and NodeMCU GUVA-S12SD UV sensor

This image is created using Fritzing. Click to enlarge image

See more in ESP8266's pinout and how to supply power to the ESP8266 and other components.

GUVA-S12SD UV Sensor ESP8266 NodeMCU
SIG → A0
GND → G (GND)
VCC → 3V3

If you want to take readings outdoors, use longer jumper wires so the sensor can face the sky while the NodeMCU stays in the shade, or power the NodeMCU from a USB power bank.

How To Program For GUVA-S12SD UV Sensor

No library is required. The analogRead() function from the ESP8266 core is all we need.

  • The first step is to give a name to the analog pin that is wired to SIG:
#define UV_SIG_PIN A0
  • Then, read the pin with the analogRead() function. On the NodeMCU, the result is a number from 0 to 1023:
int raw = analogRead(UV_SIG_PIN);
  • To get the SIG voltage, scale the count. The bare ESP8266 chip only accepts 0V to 1V on its ADC, but the NodeMCU board adds a voltage divider in front of A0. Because of that, 1023 stands for about 3.3V:
float voltage = raw * 3.3 / 1023.0;
  • Finally, estimate the UV index with the 0.1V-per-point rule:
float uvIndex = voltage / 0.1;

One A0 count is about 3.2mV, which is roughly 0.03 UV index points. So the reading can move by a few counts even when the sunlight stays the same. The second sketch averages 16 readings to calm this down.

ESP8266 NodeMCU Code for GUVA-S12SD UV Sensor - Raw Value and Voltage

Start with this sketch to check the wiring. Once per second, it prints the A0 count and the SIG voltage with three decimals.

/* * This ESP8266 NodeMCU code was developed by newbiely.com * * This ESP8266 NodeMCU code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/esp8266/esp8266-guva-s12sd-uv-sensor */ #define UV_SIG_PIN A0 // ESP8266 NodeMCU pin A0 connected to the SIG pin of the GUVA-S12SD module void setup() { Serial.begin(9600); // A0 is the only analog input of the ESP8266, no pinMode() is needed for it } void loop() { int raw = analogRead(UV_SIG_PIN); // 0 to 1023 // The NodeMCU board has a voltage divider on A0, so 1023 means 3.3V on the pin float voltage = raw * 3.3 / 1023.0; Serial.print("A0 raw: "); Serial.print(raw); Serial.print(" | SIG: "); Serial.print(voltage, 3); // 3 decimals, one count is about 0.003V Serial.println(" V"); delay(1000); }

Detailed Instructions

  • If this is the first time you use ESP8266, see how to setup environment for ESP8266 on Arduino IDE.
  • Wire the UV sensor to the NodeMCU as shown in the table and diagram above.
  • Connect the ESP8266 NodeMCU board to your computer using a USB cable.
  • Open Arduino IDE on your computer.
  • Choose the board NodeMCU 1.0 (ESP-12E Module) and the COM port of your NodeMCU.
  • Copy the code above into a new sketch.
  • Click the Upload button and wait for the upload to finish.
  • Open the Serial Monitor at 9600 baud. On your desk, the numbers stay close to zero.
  • Take the laptop and the NodeMCU near a sunny window that is open, or outside, and point the white square at the sky. The values rise:
∞
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
Nodemcu 1.0 (ESP-12E Module)
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'Nodemcu 1.0 (ESP-12E Module)' on 'COM15')
New Line
9600 baud
A0 raw: 3 | SIG: 0.010 V A0 raw: 5 | SIG: 0.016 V A0 raw: 4 | SIG: 0.013 V A0 raw: 102 | SIG: 0.329 V A0 raw: 167 | SIG: 0.539 V A0 raw: 214 | SIG: 0.690 V A0 raw: 211 | SIG: 0.681 V A0 raw: 248 | SIG: 0.800 V
Ln 11, Col 1
Nodemcu 1.0 (ESP-12E Module) on COM15
2

Normal window glass blocks most UV-B and part of UV-A. If the value barely moves behind a closed window, open it or go outside.

ESP8266 NodeMCU Code for GUVA-S12SD UV Sensor - UV Index and Risk Level

This sketch turns the voltage into something people understand. It averages 16 quick readings, divides the voltage by 0.1 to get the UV index, rounds it, and prints the WHO level name next to it.

/* * This ESP8266 NodeMCU code was developed by newbiely.com * * This ESP8266 NodeMCU code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/esp8266/esp8266-guva-s12sd-uv-sensor */ #define UV_SIG_PIN A0 // ESP8266 NodeMCU pin A0 connected to the SIG pin of the GUVA-S12SD module #define SAMPLE_COUNT 16 // number of readings to average void setup() { Serial.begin(9600); } // Read A0 several times and return the average SIG voltage float readUvVoltage() { long sum = 0; for (int i = 0; i < SAMPLE_COUNT; i++) { sum += analogRead(UV_SIG_PIN); delay(2); // short pause between ADC readings } float raw = sum / (float)SAMPLE_COUNT; return raw * 3.3 / 1023.0; // 0-1023 on the NodeMCU A0 pin = 0-3.3V } // Turn a UV index number into a WHO level name const char* uvLevel(float uvIndex) { int uv = round(uvIndex); // the WHO scale uses whole numbers if (uv <= 2) return "Low"; if (uv <= 5) return "Moderate"; if (uv <= 7) return "High"; if (uv <= 10) return "Very High"; return "Extreme"; } void loop() { float voltage = readUvVoltage(); // Rough hobby conversion: every 0.1V on SIG is about 1 UV index point float uvIndex = voltage / 0.1; Serial.print("SIG = "); Serial.print(voltage, 2); Serial.print(" V -> UV index "); Serial.print(uvIndex, 1); Serial.print(" ("); Serial.print(uvLevel(uvIndex)); Serial.println(")"); delay(1000); }

Detailed Instructions

  • Keep the same wiring. SIG stays on A0.
  • Paste the code above into a new sketch and click Upload.
  • Open the Serial Monitor (9600 baud).
  • Start in the shade, then hold the sensor in direct sunlight. Shade it with your hand for a moment to see the level drop.
  • A sample from a sunny late morning:
∞
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
Nodemcu 1.0 (ESP-12E Module)
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'Nodemcu 1.0 (ESP-12E Module)' on 'COM15')
New Line
9600 baud
SIG = 0.01 V -> UV index 0.1 (Low) SIG = 0.02 V -> UV index 0.2 (Low) SIG = 0.31 V -> UV index 3.1 (Moderate) SIG = 0.47 V -> UV index 4.7 (Moderate) SIG = 0.64 V -> UV index 6.4 (High) SIG = 0.66 V -> UV index 6.6 (High) SIG = 0.83 V -> UV index 8.3 (Very High) SIG = 0.22 V -> UV index 2.2 (Low)
Ln 11, Col 1
Nodemcu 1.0 (ESP-12E Module) on COM15
2

The last line is a reading taken with a hand over the sensor. Clouds have a smaller effect than you might expect: thin clouds let a lot of UV through.

※ NOTE THAT:

The ESP8266's ADC is fine for a rough UV estimate like this one, but it is not very precise. If your project needs steadier numbers, you can use an external ADC such as the ADS1115 with the ESP8266.

Additional Knowledge

GUVA-S12SD UV Sensor vs Light Sensor (Photoresistor)

Question GUVA-S12SD UV Sensor Light Sensor (LDR)
What it measures UV-A and UV-B (240nm to 370nm) Visible brightness
Reaction to indoor lamps Almost none Strong
Output direction More UV = higher voltage Depends on how the divider is wired
Can estimate the UV index? Yes, roughly (voltage / 0.1) No
Extra resistor needed? No, the op-amp is on the board Yes, for a bare photoresistor

It is evident that the GUVA-S12SD is the right part when you care about sunburn risk, while a light sensor is better for "is it dark or bright?" jobs. The two can even work together in the same weather station.

Ideas for Using the UV Sensor

  • Sunscreen reminder: when the UV index stays at 3 or more, send a message to your phone every two hours.
  • WiFi UV station: show the live UV index on a small web page served by the NodeMCU.
  • UV lamp tester: check if an old UV curing lamp or UV LED still gives out UV light.
  • Weather station: log UV together with temperature and humidity over a whole day.
  • Smart blind: close a window blind with a servo when the UV level becomes High.

Video Tutorial

※ OUR MESSAGES

  • As freelancers, We are AVAILABLE for HIRE. See how to outsource your project to us
  • Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!