UV Sensor Project
The UV Sensor project showcases how to interface a UV sensor with an Arduino Mega to measure UV intensity levels. UV sensors are essential for monitoring UV exposure levels, which is crucial for applications involving UV radiation detection and safety.
How It Works
A UV sensor module is built around a photodiode whose response is deliberately restricted to the ultraviolet band. The two parts common in hobby kits are the GUVA-S12SD, sensitive from roughly 240–370 nm and covering both UVA and UVB, and the ML8511, which responds to 280–390 nm and outputs an analog voltage proportional to intensity.
Both report irradiance, measured in milliwatts per square centimetre, which is then mapped to the familiar UV Index. The GUVA carrier typically produces a voltage that rises with intensity and is converted with an approximately linear relationship across the normal outdoor range.
The Arduino Mega 2560 samples with a 10-bit ADC, so analogRead() returns 0–1023 across 5 V. A practical consequence is that the useful outdoor range — UV Index 0 to about 11 — spans only part of the ADC range, so resolution is adequate but not generous. The ML8511 is the better choice when accuracy matters, since it has an internal amplifier and a cleaner transfer characteristic.
Ordinary window glass blocks most UVB, so a sensor indoors behind glass reads far lower than one outdoors even in bright sunshine. That is physics, not a fault.
Components Needed
- Arduino Mega 2560
- UV Sensor Module
- Jumper Wires
- Arduino Mega 2560
- Power Supply
Wiring to the Arduino Mega 2560
Connect OUT to A0, VCC to the 5 V rail and GND to ground. If you are using an ML8511, tie its EN pin high as well or the output stays dormant.
Many UV carriers output a maximum around 1 V even on a 5 V supply, which wastes most of a 5 V ADC range. Using the internal 1.1 V analog reference recovers that resolution.
Mount the sensing window facing the sky with nothing above it. A clear acrylic cover is acceptable only if it is UV-transmissive — standard acrylic and glass both attenuate heavily.
| Module pin | Arduino Mega 2560 pin | Function |
|---|---|---|
| OUT / SIG | A0 | Analog voltage proportional to UV intensity |
| VCC | 5V | Supply (3.3–5 V) |
| GND | GND | Common ground |
| EN (ML8511) | 5V | Enable — tie high for continuous operation |
Build and Upload
Open the Arduino IDE and create a new sketch.
Copy and paste the provided Arduino code into the sketch.
Upload the code to the Arduino Mega.
Open the Serial Monitor in the Arduino IDE (set to 9600 baud).
Observe UV intensity values displayed in the Serial Monitor.
Place the UV sensor under different lighting conditions to observe changes in UV intensity readings.
Example Code
Converting a GUVA-S12SD reading into UV Index with averaging. Upload it with the board set to Arduino Mega 2560 and open the Serial Monitor at 9600 baud.
const int UV_PIN = A0;
const float VREF = 5.0;
const int ADC_MAX = 1023;
float readMilliVolts() {
long total = 0;
for (int i = 0; i < 32; i++) { total += analogRead(UV_PIN); delay(2); }
return (total / 32.0) * VREF * 1000.0 / ADC_MAX;
}
const char* riskLevel(float index) {
if (index < 3) return "Low";
if (index < 6) return "Moderate";
if (index < 8) return "High";
if (index < 11) return "Very High";
return "Extreme";
}
void setup() { Serial.begin(9600); }
void loop() {
float mv = readMilliVolts();
float uvIndex = mv / 100.0; // GUVA-S12SD: ~100 mV per index unit
Serial.print("UV: ");
Serial.print(mv, 0);
Serial.print(" mV Index ");
Serial.print(uvIndex, 1);
Serial.print(" (");
Serial.print(riskLevel(uvIndex));
Serial.println(")");
delay(2000);
}
Applications
A ultraviolet sensor turns up in a wide range of projects. These are the uses where it is the right choice rather than a compromise:
- Weather stations reporting a local UV Index alongside temperature and humidity
- Sun-exposure warnings for wearables and outdoor signage
- Verifying UV sterilisation lamps are actually emitting
- Monitoring UV curing in resin printers and coating processes
- Greenhouse and horticultural light monitoring
Working with the Arduino Mega 2560
The Arduino Mega 2560 is built around the ATmega2560 and runs on 5 V logic with 8 KB of SRAM and 256 KB of program flash. These details change how this circuit is wired and what the sketch can do, so they are worth stating plainly before you build.
The Mega’s 8 KB of SRAM is four times the Uno’s, so larger buffers and lookup tables are practical.
I²C lives on D20/D21 rather than A4/A5 — wiring copied from an Uno tutorial will not work unchanged.
Four hardware UARTs mean a serial sensor can have its own port instead of fighting SoftwareSerial.
| Arduino Mega 2560 characteristic | Value | Why it matters here |
|---|---|---|
| Logic voltage | 5 V | Matches most hobby modules directly |
| ADC resolution | 10-bit (0–1023) | Sets how finely an analog reading can be resolved |
| Analog inputs | A0–A15 (sixteen channels) | Determines how many analog sensors can share the board |
| PWM outputs | D2–D13 and D44–D46 | Needed for brightness, speed and tone control |
| I²C pins | D20 (SDA) and D21 (SCL) | Fixed by hardware — wiring copied from another board may not match |
| Interrupt pins | D2, D3, D18, D19, D20 and D21 | Required for counting fast or asynchronous events |
| Serial | four independent hardware UARTs | Monitor runs at 9600 baud by default |
Troubleshooting
Most problems with this module fall into a handful of categories. Work through these before suspecting the part itself:
- The reading is near zero outdoors — the sensor is behind glass, which blocks UVB. Move it outside.
- The index seems far too high indoors — fluorescent and some LED lamps emit stray UV; compare against a known outdoor reading.
- Values are noisy — average heavily. UV changes slowly, so a 32-sample average costs nothing.
- Readings saturate at full scale in sunlight — the module output exceeds the ADC reference; add a divider or choose a lower reference.
- Code written for an ESP board gives odd analog values — the Arduino Mega 2560 uses a 10-bit ADC returning 0–1023; rescale any constant taken from a 12-bit example.
- An I²C sensor is not found after copying wiring from another Arduino — on the Arduino Mega 2560 I²C is on D20 (SDA) and D21 (SCL).
Taking It Further on the Arduino Mega 2560
Once the basic reading works, where you go next depends very much on which board you are using. These are the directions that suit the Arduino Mega 2560 specifically:
The Mega’s sixteen analog inputs make it the right board when several of these sensors must run at once. Where an Uno would need an external multiplexer, the Mega simply reads A0 through A15 directly.
Four hardware UARTs mean a GPS, a serial display and a debug console can coexist without SoftwareSerial, which is unreliable above 38400 baud and blocks interrupts while it transmits.
With 8 KB of SRAM there is room to buffer readings and do real processing — a rolling average over several hundred samples, or holding a full display frame in memory, both of which are impractical on an Uno.
Notes and Practical Limits
UV Index is defined for sunlight. Pointing one of these sensors at an artificial UV source gives a number, but calling it a UV Index is meaningless — the spectral weighting does not apply.
Readings drift as the sensor window ages and clouds with prolonged exposure. For long-running installations, plan to replace the sensor periodically rather than trusting a years-old calibration.