UV index meter on an Arduino Uno
A GUVA-S12SD UV sensor and a 16x2 I2C LCD: the UV index and how risky it is, from low to extreme. Drag the sunshine. The whole build, an Arduino Uno and 2 more parts, runs here in your browser on the firmware below; open it in the editor to change the wiring or the code and run it again.
Teaching? Assign it to your class in one click.
Beginner Runs in your browser. Free, and no account needed.
The parts retail for about $34. On Mokxi they're free.
The code
The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.
// UV index meter: a GUVA-S12SD UV sensor module and the UV index on a 16x2
// I2C LCD, with the WHO's name for the band it is in.
//
// A0 the module's OUT (its VCC on 5 V, GND on GND)
// LCD SDA -> A4 SCL -> A5 VCC -> 5 V GND -> GND
//
// The photodiode makes 26 nA per UV index (its datasheet) and the module's
// op-amp makes 4.3 V per microamp, so OUT is 4.3 x 0.026 = 0.1118 V per UV
// index. Many sketches divide by 0.1 V instead, a round figure that reads
// about 12% high; this one divides by the datasheet's number.
//
// It sees 240 to 370 nm only, so a desk lamp reads 0 and the sun does not.
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
const float VOLTS_PER_UVI = 4.3 * 0.026;
LiquidCrystal_I2C lcd(0x27, 16, 2);
const char *band(float uvi) {
if (uvi < 2.5) return "Low ";
if (uvi < 5.5) return "Moderate ";
if (uvi < 7.5) return "High ";
if (uvi < 10.5) return "Very high";
return "Extreme ";
}
void setup() {
Serial.begin(115200);
Serial.println("Mokxi Arduino Uno: UV index meter");
lcd.init();
lcd.backlight();
}
void loop() {
// Average 32 readings: the index moves slowly and this steadies the last digit.
long sum = 0;
for (int i = 0; i < 32; i++) sum += analogRead(A0);
float volts = sum / 32.0 * 5.0 / 1024.0;
float uvi = volts / VOLTS_PER_UVI;
lcd.setCursor(0, 0);
lcd.print("UV index ");
lcd.print(uvi, 1);
lcd.print(" ");
lcd.setCursor(0, 1);
lcd.print(band(uvi));
Serial.print("mV: ");
Serial.print(volts * 1000.0, 0);
Serial.print(" UV index: ");
Serial.print(uvi, 1);
Serial.print(" ");
Serial.println(band(uvi));
delay(500);
}
Parts list
3 parts, plus the jumper wires. They retail for about $34; on Mokxi they're free.
How it is wired
6 connections, pin by pin, read from the circuit itself. Each line is one set of pins joined together, by a jumper wire or a breadboard strip.
- Arduino Uno R3 pin 5V; UV sensor, GUVA-S12SD pin VCC; Character LCD with I2C backpack pin VCC
- Ground: Arduino Uno R3 pin GND; UV sensor, GUVA-S12SD pin GND
- Ground: Arduino Uno R3 pin GND; Character LCD with I2C backpack pin GND
- Arduino Uno R3 pin A0; UV sensor, GUVA-S12SD pin OUT
- Arduino Uno R3 pin A4; Character LCD with I2C backpack pin SDA
- Arduino Uno R3 pin A5; Character LCD with I2C backpack pin SCL
Change it and keep it
Open it in the editor, change the circuit or the code, and keep your version in a free account.