DEV Community

Cover image for ESP32 MicroPython GUVA-S12SD UV Sensor
Arduino ESP32 Raspberry Pi Tutorials
Arduino ESP32 Raspberry Pi Tutorials

Posted on Originally published at newbiely.com

ESP32 MicroPython GUVA-S12SD UV Sensor

The GUVA-S12SD is a small UV sensor board that outputs a voltage proportional to how much ultraviolet light hits it. Wire its SIG pin to an ESP32 ADC pin, and with a few lines of MicroPython you get a raw reading, a voltage, and eventually a UV index with a friendly label like "High" or "Extreme". It is a nice first analog sensor project if you have only done digital GPIO so far.

The full tutorial, with every detail and the line-by-line code explanation, is on our site: ESP32 MicroPython GUVA-S12SD UV Sensor

What you need

Wiring

ESP32 MicroPython GUVA-S12SD UV Sensor wiring diagram

The sensor has three pins. SIG goes to an ADC pin on the ESP32, specifically GPIO36, which belongs to ADC1 and keeps working even when WiFi is active. GPIO36 is input-only, which is fine since the ESP32 only needs to read it. GND connects to the ESP32's GND, and VCC connects to the 3.3V pin, not VIN, since the sensor runs at 3.3 volts same as the board.

How it works

A tiny white square on the board is a UV photodiode. UV light makes it pass a very small current, too small for the ESP32 to measure on its own. A VLM358 op-amp chip next to it amplifies that current into a voltage on SIG: more UV means a higher voltage. A rough rule of thumb turns that voltage into a UV index: every 0.1V is about one index point, so 0.5V is roughly a UV index of 5. Indoors or behind glass, expect near-zero readings, since lamps and windows block most UV.

Code

The first script sets up ADC(Pin(36)) with atten(ADC.ATTN_11DB), which widens the ADC's range to about 3.3V since sunlight could otherwise push SIG past the default 1V limit. It then prints both read_u16(), a raw 0-65535 number, and read_uv(), a calibrated microvolt reading converted to volts. The second script builds on this: read_sig_voltage() averages 16 readings for a steadier value, and uv_level_name() converts the resulting UV index into a label like Low, Moderate, High, Very High or Extreme, printed once per second.

esp32-micropython-guva-s12sd-uv-sensor.py

"""
This ESP32 MicroPython code was developed by newbiely.com
This ESP32 MicroPython code is made available for public use without any restriction
For comprehensive instructions and wiring diagrams, please visit:
https://newbiely.com/tutorials/esp32-micropython/esp32-micropython-guva-s12sd-uv-sensor
"""

from machine import ADC, Pin
import time

# The SIG pin of the GUVA-S12SD UV sensor is wired to GPIO36 (ADC1, input only)
uv_adc = ADC(Pin(36))

# 11 dB attenuation lets the ADC measure up to about 3.3V
# (the sensor gives only about 0V to 1V+, so this range is more than enough)
uv_adc.atten(ADC.ATTN_11DB)

while True:
    raw = uv_adc.read_u16()                # 0 to 65535
    voltage = uv_adc.read_uv() / 1_000_000  # read_uv() gives microvolts, calibrated by the ESP32

    print("raw:", raw, "| SIG:", "{:.3f}".format(voltage), "V")

    time.sleep_ms(800)
Enter fullscreen mode Exit fullscreen mode

Things that go wrong

If readings stay near zero, check that you are not testing indoors or through a window, since both block UV and will fool you into thinking the sensor is broken. Make sure VCC is wired to 3.3V and not VIN. If raw values look capped or clipped in strong sunlight, confirm the attenuation is set with atten(ADC.ATTN_11DB), otherwise the ADC's default range is too narrow to capture higher SIG voltages.

Full tutorial

The complete tutorial - wiring diagram, code explanation and troubleshooting - is here: https://newbiely.com/tutorials/esp32-micropython/esp32-micropython-guva-s12sd-uv-sensor

Top comments (0)