Arduino Mega - GUVA-S12SD UV Sensor

The Arduino Mega has sixteen analog inputs, so one board can serve a whole rooftop weather station: temperature, humidity, rain, wind, light - and, with a GUVA-S12SD module, the strength of the sun's ultraviolet rays. The UV level is the number that tells you when to put on sunscreen, and it can be high even on a cool, cloudy day. This tutorial instructs you how to use the Arduino Mega to measure UV light from the GUVA-S12SD UV sensor and express it as a UV index. In detail, we will learn:

Arduino Mega GUVA-S12SD UV sensor

We suggest:

Hardware Preparation

1×Arduino MEGA
1×Alternatively, DIYables MEGA Development Board
1×USB 2.0 cable type A/B (for USB-A PC)
1×USB 2.0 cable type C/B (for USB-C PC)
1×GUVA-S12SD UV Sensor Module
1×Jumper Wires
1×Recommended: Screw Terminal Block Shield for Arduino Uno/Mega
1×Recommended: Sensors/Servo Expansion Shield for Arduino Mega
1×Recommended: Breadboard Shield for Arduino Mega
1×Recommended: Enclosure for Arduino Mega

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 and Its Pinout

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

The DIYables GUVA-S12SD module is a small purple board, roughly 11mm x 27mm, with two holes for screws. Two chips do all the work. The small white square is the GUVA-S12SD photodiode, which reacts to UV-A and UV-B light between 240nm and 370nm. Next to it sits a VLM358 op-amp. When ultraviolet light lands on the photodiode, it lets through a very small current. The op-amp makes this current bigger and turns it into a voltage on the SIG pin, and the Arduino Mega reads that voltage with analogRead(). More UV means a higher voltage. The header comes already soldered, and the product is sold as a pack of two.

GUVA-S12SD UV Sensor Module
Supply Voltage 2.5V to 5V DC
Light Range UV-A and UV-B, 240nm to 370nm
Viewing Angle 130 degrees
Output Type Analog voltage on the SIG pin
Typical Output About 0V with no UV, up to about 1V or a bit more in strong sun
Board Size About 11mm x 27mm, two mounting holes

Lamps and window light hold almost no UV, so inside a room the module stays close to 0V. That is normal - it is not broken. Carry it outdoors into direct sun, or shine a UV flashlight on it, to see the output rise.

The three pins are printed on the board in this order:

  • SIG pin: must be connected to an analog input pin of the Arduino Mega. It carries the UV voltage.
  • GND pin: must be connected to GND (0V)
  • VCC pin: must be connected to the 3.3V pin or the 5V pin of the Arduino Mega. The module works on both.

※ NOTE THAT:

The arrangement of pins on a module may differ from one manufacturer to another. Check the SIG, GND and VCC labels on your own board and connect each wire by its label.

To turn the voltage into a UV index, most hobby projects use a simple rule: each 0.1V on SIG is about 1 point of UV index. So UV index ≈ SIG voltage / 0.1. This is a rough estimate for fun projects and reminders. It is not a calibrated UV meter.

The WHO sorts UV index numbers into five bands:

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

Wiring Diagram

Only three jumper wires are needed: SIG to A0, GND to a GND pin, and VCC to the 3.3V pin of the Arduino Mega. The 5V pin works for VCC too.

The wiring diagram between Arduino Mega GUVA-S12SD UV sensor

This image is created using Fritzing. Click to enlarge image

GUVA-S12SD UV Sensor Arduino Mega
SIG A0
GND GND
VCC 3.3V (or 5V)

How To Program For GUVA-S12SD UV Sensor

  • The first step is to give a name to the analog pin that receives the SIG wire:
#define UV_SIG_PIN A0
  • Read the pin. The Arduino Mega has a 10-bit ADC, so the result is a whole number from 0 to 1023:
int adcValue = analogRead(UV_SIG_PIN);
  • Change the number into volts. The ADC of the Mega measures against 5V, so 1023 stands for 5V. This stays true even when the module itself is powered from 3.3V:
float sigVoltage = adcValue * 5.0 / 1023.0;
  • Divide by 0.1 to get the approximate UV index:
float uvIndex = sigVoltage / 0.1;

Arduino Mega Code for GUVA-S12SD UV Sensor - Raw ADC Value and Voltage

Start with the plain numbers. Once per second, this sketch prints the ADC value from A0 and the matching SIG voltage. It is a good way to check the wiring before you add any math.

/* * This Arduino Mega code was developed by newbiely.com * * This Arduino Mega code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-mega/arduino-mega-guva-s12sd-uv-sensor */ #define UV_SIG_PIN A0 // The Arduino Mega analog pin wired to the SIG pin of the GUVA-S12SD module void setup() { Serial.begin(9600); // No pinMode() is needed: analogRead() sets the pin up as an analog input by itself } void loop() { int adcValue = analogRead(UV_SIG_PIN); // 10-bit result: 0 to 1023 float sigVoltage = adcValue * 5.0 / 1023.0; // the Mega ADC uses 5V as its reference Serial.print("ADC: "); Serial.print(adcValue); Serial.print(" | SIG: "); Serial.print(sigVoltage, 3); // three decimals, because the UV signal is small Serial.println(" V"); delay(1000); // one reading per second }

Detailed Instructions

  • If this is the first time you use the Arduino Mega, see how to set up the environment for Arduino Mega in the Arduino IDE.
  • Wire the GUVA-S12SD module to the Arduino Mega as shown in the diagram.
  • Connect the Arduino Mega board to your computer using a USB cable. A laptop is handy here, because you will take the setup outside.
  • Open Arduino IDE on your computer.
  • Select the Arduino Mega or Mega 2560 board and the right COM port.
  • Paste the code above into a new sketch and click Upload.
  • Open the Serial Monitor. Watch a few readings indoors, then walk out into the sun and point the white sensor chip at the sky.
∞
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
Arduino Mega Or...
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'Arduino Mega Or...' on 'COM15')
New Line
9600 baud
ADC: 0 | SIG: 0.000 V ADC: 2 | SIG: 0.010 V ADC: 3 | SIG: 0.015 V ADC: 71 | SIG: 0.347 V ADC: 104 | SIG: 0.508 V ADC: 127 | SIG: 0.621 V ADC: 133 | SIG: 0.650 V
Ln 11, Col 1
Arduino Mega Or... on COM15
2

The first three lines were taken next to a window, and the ADC value hardly moves above zero. The last four were taken outside around midday, where the voltage climbs to more than half a volt. Notice how small the numbers are: even in bright sun, the SIG pin uses only a small part of the 0 to 1023 range.

Arduino Mega Code for GUVA-S12SD UV Sensor - UV Index and Risk Level

A single analogRead() can jump by one or two steps from one reading to the next. This sketch takes 16 readings in a row and uses their average, so the result is steadier. Then it changes the average into volts, divides by 0.1 to get the UV index, and picks the WHO level name with a short helper function, uvLevelName().

/* * This Arduino Mega code was developed by newbiely.com * * This Arduino Mega code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-mega/arduino-mega-guva-s12sd-uv-sensor */ #define UV_SIG_PIN A0 // The Arduino Mega analog pin wired to the SIG pin of the GUVA-S12SD module #define SAMPLE_COUNT 16 // how many readings are averaged for one result void setup() { Serial.begin(9600); } // Return the WHO name of a UV index value const char* uvLevelName(float uvIndex) { if (uvIndex < 3) return "Low"; if (uvIndex < 6) return "Moderate"; if (uvIndex < 8) return "High"; if (uvIndex < 11) return "Very High"; return "Extreme"; } void loop() { // Add up several readings to smooth out small jumps long total = 0; for (int i = 0; i < SAMPLE_COUNT; i++) { total += analogRead(UV_SIG_PIN); delay(5); } float averageAdc = total / (float)SAMPLE_COUNT; float sigVoltage = averageAdc * 5.0 / 1023.0; // ADC value -> volts (5V reference) float uvIndex = sigVoltage / 0.1; // rough rule: every 0.1V is about 1 UV index point Serial.print("SIG: "); Serial.print(sigVoltage, 2); Serial.print(" V -> UV index: "); Serial.print(uvIndex, 1); Serial.print(" ("); Serial.print(uvLevelName(uvIndex)); Serial.println(")"); delay(1000); }

Detailed Instructions

  • Keep the same wiring.
  • Replace the previous sketch with the code above and click Upload.
  • Open the Serial Monitor. Start in the shade, then move into full sunlight.
∞
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
Arduino Mega Or...
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'Arduino Mega Or...' on 'COM15')
New Line
9600 baud
SIG: 0.01 V -> UV index: 0.1 (Low) SIG: 0.18 V -> UV index: 1.8 (Low) SIG: 0.46 V -> UV index: 4.6 (Moderate) SIG: 0.67 V -> UV index: 6.7 (High) SIG: 0.84 V -> UV index: 8.4 (Very High)
Ln 11, Col 1
Arduino Mega Or... on COM15
2

In the shade the index is low. In open summer sun at noon it moves into the High and Very High levels. Your own numbers depend on the season, the time of day, the clouds and the angle of the sensor.

From here, the Arduino Mega can do more with the result. For example, it can sound a buzzer when the index reaches 6, show the level on an LCD, close a smart window blind, or check if a UV lamp or UV LED still gives enough light.

Additional Knowledge

How is the GUVA-S12SD different from the LDR light sensor in the other Arduino Mega tutorials?

LDR Light Sensor GUVA-S12SD UV Sensor
Reacts to Visible light Ultraviolet light only (240nm to 370nm)
Indoor lamp Big change in reading Almost no change
Output Changes with resistance (needs a resistor or a module) Ready-to-read voltage on SIG
Typical use Day/night switch, room brightness UV index, sunburn warning

It is evident that the two sensors answer different questions: the LDR tells you how bright it is, and the GUVA-S12SD tells you how strong the sunburn-causing part of the light is.

A tip on resolution: with the default 5V reference, one ADC step of the Arduino Mega is about 4.9mV, which is about 0.05 UV index. That is fine for hobby use. If you want finer steps, the Mega also has an internal 2.56V reference. Add analogReference(INTERNAL2V56); in setup() and change 5.0 to 2.56 in the voltage formula. The SIG voltage stays well below 2.56V, so nothing is cut off.

Finally, keep the sensor dry if you mount it outdoors. A clear window in front of it must let UV through - normal glass and many plastics block a lot of UV, and the readings will drop.

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!