Arduino MicroPython GUVA-S12SD UV Sensor

Clouds hide the sun, but they do not stop all of its ultraviolet light. That is why people still get sunburned on a grey summer day. With a GUVA-S12SD UV sensor and a few lines of MicroPython, your Arduino Giga R1 WiFi can put a number on that invisible light. Step by step, this page shows:

https://newbiely.com/images/cover/arduino-guva-s12sd-uv-sensor-micropython.jpg

Hardware Preparation

1×Arduino Giga R1 WiFi
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×Recommended: Screw Terminal Block Shield for Arduino Uno/Mega/Giga
1×Recommended: Sensors/Servo Expansion Shield for Arduino Mega/Giga
1×Recommended: Breadboard Shield for Arduino Mega/Giga
1×Recommended: Enclosure for Arduino Giga
1×Recommended: Power Splitter for Arduino Giga

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 Pinout

This is a tiny purple PCB, roughly 11mm by 27mm, with a mounting hole at each end. Two parts do all the work. The small white square is the GUVA-S12SD photodiode. The 8-pin chip next to it is a VLM358 op-amp. When UV light hits the photodiode, it lets a very small current flow. That current is much too weak to measure directly, so the op-amp turns it into a voltage on the SIG pin. The Giga reads that voltage with its ADC. Brighter UV means a higher voltage. That is all you need to know to use it.

GUVA-S12SD UV Sensor Pinout
image source: diyables.io
Feature Value
UV range 240nm to 370nm (UV-A and UV-B)
Field of view 130 degrees
Power 2.5V to 5V DC
SIG output about 0V with no UV, up to about 1V or a bit more in strong sun
Size about 11mm x 27mm
Comes as pack of 2, header already soldered

The header has three pins. From one end they read SIG, GND, VCC:

  • SIG pin: the analog output. It goes to pin A0 of the Giga.
  • GND pin: the ground. It goes to any GND pin of the Giga.
  • VCC pin: the power input. The module is happy with 2.5V to 5V, but on the Giga we use 3.3V, the same level as the Giga's own pins.

Do not worry if the reading is close to zero at your desk. The sensor ignores visible light, and LED or fluorescent room lamps send out almost no UV. Even a sunny window is a poor test, because glass blocks a big part of the UV. To see real numbers, take the board outside, or point a UV flashlight at the white square.

From voltage to UV index

Hobby makers use a simple rule with this sensor: UV index ≈ SIG voltage ÷ 0.1. In other words, each 0.1V on SIG is about one UV index point. A reading of 0.42V means roughly UV index 4. This rule is good enough for a fun project or a reminder, but the module is not a calibrated UV meter. The levels below come from the World Health Organization:

UV index Level Sun advice
0 to 2 Low no special care needed
3 to 5 Moderate sunscreen, seek shade near noon
6 to 7 High hat, sunglasses and sunscreen
8 to 10 Very High keep out of the midday sun
11 and above Extreme avoid the sun if you can

Wiring Diagram

No breadboard and no extra parts are needed. Plug three female-to-male jumper wires from the module header into the Giga.

The wiring diagram between Arduino MicroPython GUVA-S12SD UV Sensor

This image is created using Fritzing. Click to enlarge image

GUVA-S12SD UV Sensor Arduino Giga R1 WiFi
SIG A0
GND GND
VCC 3.3V

Keep the white photodiode on top and nothing in front of it. Your fingers, a plastic case or a sticker over the square will cut the reading.

How To Program GUVA-S12SD UV Sensor With MicroPython

You do not need any library. The machine module already has everything.

  • Create an ADC object on A0, the pin that listens to SIG:
uv_sensor = ADC(Pin('A0'))
  • Take one reading. read_u16() always gives a 16-bit number, where 0 means 0V and 65535 means 3.3V:
raw = uv_sensor.read_u16()
  • Scale it to volts:
volts = raw * 3.3 / 65535
  • Divide by 0.1 to get the approximate UV index:
uv_index = volts / 0.1

Want to try it before you write a file? Type these two lines into the Thonny Shell, one at a time, and press Enter after each. The second line prints one reading right away:

from machine import ADC, Pin ADC(Pin('A0')).read_u16()

Arduino MicroPython Code - GUVA-S12SD Raw Reading and Voltage

This first script is a wiring test. Every second it shows the 16-bit number from A0 next to the voltage it stands for.

""" This Arduino MicroPython script was developed by newbiely.com This Arduino MicroPython script is made available for public use without any restriction For comprehensive instructions and wiring diagrams, please visit: https://newbiely.com/tutorials/arduino-micropython/arduino-micropython-guva-s12sd-uv-sensor """ from machine import ADC, Pin import time # The Arduino Giga WiFi pin A0 connected to the SIG pin of the GUVA-S12SD UV sensor uv_sensor = ADC(Pin('A0')) while True: raw = uv_sensor.read_u16() # 16-bit reading: 0 (0V) to 65535 (3.3V) volts = raw * 3.3 / 65535 # Change the reading into the SIG voltage print("raw = {:5d} SIG = {:.3f} V".format(raw, volts)) time.sleep(1) # One line per second

Detailed Instructions

Here’s instructions on how to run the above MicroPython code on Arduino with Thonny IDE:

  • Make sure Thonny IDE is installed on your computer.
  • Make sure MicroPython firmware is installed on your Arduino board.
  • If you are new to Arduino with MicroPython, see the Getting Started with Arduino and MicroPython.
  • Wire SIG to A0, GND to GND and VCC to 3.3V, as in the diagram above.
  • Plug the Arduino Giga into your computer with the USB Type-C cable. A laptop is best, because you will carry it outside.
  • Open Thonny IDE and go to Tools Options.
  • Under the Interpreter tab, select MicroPython (generic) from the dropdown menu.
  • Select the COM port corresponding to your Arduino board (e.g., COM33 on Windows or /dev/ttyACM0 on Linux).
  • Copy the provided Arduino MicroPython code and paste it into Thonny's editor.
  • Save the MicroPython code to your Arduino by:
    • Clicking the Save button or pressing Ctrl+S.
    • In the save dialog, choose MicroPython device and name the file main.py.
  • Click the green Run button (or press F5) to execute the code.
  • Read a few lines indoors. Then go out into the sun, turn the white square toward the sky, and watch the Shell.
Shell x
>>> %Run -c $EDITOR_CONTENT
MPY: soft reboot raw = 152 SIG = 0.008 V raw = 214 SIG = 0.011 V raw = 176 SIG = 0.009 V raw = 9134 SIG = 0.460 V raw = 9870 SIG = 0.497 V raw = 10208 SIG = 0.514 V raw = 12880 SIG = 0.649 V raw = 13542 SIG = 0.682 V
MicroPython (generic) • Giga Virtual Comm Port in FS Mode @ COM33 ≡

The first three lines were taken next to a desk lamp: only around 10 millivolts. The rest were taken outside around noon, where SIG rose to about 0.5V to 0.7V. If the raw value stays near zero even in bright sun, check that the SIG wire sits on A0 and that VCC really goes to 3.3V.

※ NOTE THAT:

read_u16() always returns a number from 0 to 65535, even though the hardware ADC inside the Giga may use fewer bits. MicroPython scales the result for you, so the formula raw * 3.3 / 65535 stays the same.

Arduino MicroPython Code - GUVA-S12SD UV Index With Level Name

A single reading can jump a little because of electrical noise or a moving shadow. This script takes 20 readings in a row, uses their average, and then prints the voltage, the approximate UV index (one decimal) and the WHO level.

""" This Arduino MicroPython script was developed by newbiely.com This Arduino MicroPython script is made available for public use without any restriction For comprehensive instructions and wiring diagrams, please visit: https://newbiely.com/tutorials/arduino-micropython/arduino-micropython-guva-s12sd-uv-sensor """ from machine import ADC, Pin import time # The Arduino Giga WiFi pin A0 connected to the SIG pin of the GUVA-S12SD UV sensor uv_sensor = ADC(Pin('A0')) SAMPLES = 20 # How many readings to average for one result def read_sig_volts(): # Read the pin several times and return the average voltage total = 0 for _ in range(SAMPLES): total += uv_sensor.read_u16() time.sleep_ms(5) return (total / SAMPLES) * 3.3 / 65535 def uv_level(index): # UV index groups from the World Health Organization if index < 3: return "Low" elif index < 6: return "Moderate" elif index < 8: return "High" elif index < 11: return "Very High" return "Extreme" while True: volts = read_sig_volts() uv_index = round(volts / 0.1, 1) # About 1 UV index point for every 0.1V (approximation) print("SIG {:.3f} V | UV index {:.1f} | {}".format(volts, uv_index, uv_level(uv_index))) time.sleep(1)

Detailed Instructions

  • Do not change the wires.
  • Paste this code into Thonny and save it to the Arduino as main.py, replacing the first script.
  • Click Run (F5).
  • Outside, hold the sensor in the shade of a tree, then in open sun, and then put your palm over it for a second.
Shell x
>>> %Run -c $EDITOR_CONTENT
MPY: soft reboot SIG 0.318 V | UV index 3.2 | Moderate SIG 0.462 V | UV index 4.6 | Moderate SIG 0.641 V | UV index 6.4 | High SIG 0.697 V | UV index 7.0 | High SIG 0.823 V | UV index 8.2 | Very High SIG 0.041 V | UV index 0.4 | Low SIG 0.705 V | UV index 7.1 | High
MicroPython (generic) • Giga Virtual Comm Port in FS Mode @ COM33 ≡

Shade gives a Moderate level, full sun pushes it to High or even Very High, and your palm brings it straight back to Low. Check your numbers against the UV index in a local weather app. If your sensor always reads a bit high or low, change the 0.1 in the code until the two agree.

Project Ideas With the UV Sensor

The Giga has WiFi and plenty of pins, so the UV value can drive many small projects:

  • Sunburn reminder: blink an LED or sound a buzzer when the index stays above 5 for a few minutes.
  • UV lamp checker: see if the UV lamp in a reptile tank or a resin-curing box still gives out enough UV.
  • Smart window blind: move a servo motor to close the blind when strong UV hits the window side of the house.
  • Garden weather station: add the UV index to temperature and humidity readings and log them every few minutes.

To measure normal visible light instead of UV, see the Arduino MicroPython Light Sensor tutorial.

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!