This is an Arduino library for interfacing with the ML8511 UV sensor, a compact ultraviolet (UV) sensor that measures UV intensity and calculates the UV Index. The library provides a simple and flexible API to monitor UV exposure with support for multiple units and calibration options.
- Measure UV intensity in various units (e.g., mW/cm², W/m², μW/cm², MED, etc.).
- Calculate the UV Index based on sensor readings.
- Configurable over-sampling for improved measurement consistency.
- Custom calibration with user-defined voltage values.
- Support for multiple microcontroller platforms (e.g., ESP32, RP2040, STM32, SAM3X8E).
- Optional serial logging for debugging.
- Arduino (AVR-based boards)
- ESP32
- Raspberry Pi Pico (RP2040)
- STM32F1 and STM32F4
- Arduino Due (SAM3X8E)
- Download the latest release as a
.zip
file from the Releases page. - Open the Arduino IDE.
- Go to Sketch > Include Library > Add .ZIP Library.
- Select the downloaded
.zip
file and click Open. - The library will now be available under Sketch > Include Library > ML8511.
- Clone or download this repository.
- Copy the folder containing
ML8511.h
andML8511DEFS.h
to your Arduinolibraries
folder:- Windows:
Documents\Arduino\libraries\
- macOS/Linux:
~/Documents/Arduino/libraries/
- Windows:
- Restart the Arduino IDE.
- OUT Pin: Connect to an analog input pin on your microcontroller (e.g.,
A0
). - EN Pin: Connect to a digital output pin for enabling/disabling the sensor (e.g.,
D2
). - VCC: Connect to 3.3V (or as per your microcontroller's logic level).
- GND: Connect to ground.
#include <ML8511.h>
// Define pins
const uint8_t ANALOG_PIN = A0;
const uint8_t ENABLE_PIN = 2;
// Create sensor object
ML8511 uvSensor(ANALOG_PIN, ENABLE_PIN);
void setup() {
Serial.begin(9600);
uvSensor.setActiveUnit(mW_Per_Sq_cm); // Set unit to mW/cm²
}
void loop() {
float uvIntensity = uvSensor.getUVIntensity();
uint8_t uvIndex = uvSensor.getUVIndex();
Serial.print("UV Intensity: ");
Serial.print(uvIntensity);
Serial.println(" mW/cm²");
Serial.print("UV Index: ");
Serial.println(uvIndex);
delay(1000);
}
#include <ML8511.h>
ML8511 uvSensor(A0, 2);
void setup() {
Serial.begin(9600);
// Calibrate with custom voltage values
if (uvSensor.calibrateSensor(0.9, 2.1, 2.8)) {
Serial.println("Calibration successful!");
} else {
Serial.println("Calibration failed!");
}
uvSensor.setActiveUnit(W_Per_Sq_m); // Set unit to W/m²
}
void loop() {
float uvIntensity = uvSensor.getUVIntensity();
Serial.print("UV Intensity: ");
Serial.print(uvIntensity);
Serial.println(" W/m²");
delay(1000);
}
#include <ML8511.h>
ML8511 uvSensor(A0, 2);
void setup() {
Serial.begin(9600);
uvSensor.showLogs(true); // Enable serial logging
uvSensor.setActiveUnit(MED); // Set unit to MED
}
void loop() {
float uvIntensity = uvSensor.getUVIntensity();
Serial.print("UV Intensity: ");
Serial.print(uvIntensity);
Serial.println(" MED");
delay(1000);
}
ML8511(uint8_t analog, uint8_t enable)
: Initializes the sensor with the analog and enable pins.
float getUVIntensity()
: Returns the UV intensity in the currently set unit.uint8_t getUVIndex()
: Returns the calculated UV Index (0-11+).void set_V0(float v0)
: Sets the zero-UV voltage (default: 0.87V).void setOverSampling(uint8_t oss)
: Sets the over-sampling factor (options:ML8511_OSS_0
,ML8511_OSS_1
,ML8511_OSS_2
,ML8511_OSS_3
).bool calibrateSensor(float UV0_voltage, float UV10_voltage, float UV15_voltage)
: Calibrates the sensor with custom voltage values.void setActiveUnit(uint8_t unit)
: Sets the unit for UV intensity (e.g.,mW_Per_Sq_cm
,W_Per_Sq_m
,MED
).void showLogs(bool show)
: Enables or disables serial logging.
Constant | Description | String Representation |
---|---|---|
mW_Per_Sq_cm |
Milliwatts per cm² | mW/cm2 |
W_Per_Sq_m |
Watts per m² | W/m2 |
microW_Per_Sq_cm |
Microwatts per cm² | μW/cm2 |
Joules_s_Per_Sq_m |
Joules/s per m² | Js/m2 |
mJoules_s_Per_Sq_cm |
Millijoules/s per cm² | mJs/cm2 |
Joules_s_Per_Sq_cm |
Joules/s per cm² | Js/cm2 |
MED |
Minimal Erythemal Dose | MED |
- The default ADC resolution is 10-bit (0-1023), but it switches to 12-bit (0-4095) for ESP32, RP2040, STM32, and SAM3X8E platforms.
- Ensure the sensor is powered with a stable 3.3V supply for accurate readings.
- Calibration voltages should align with the sensor's characteristics for reliable results.
This library is released under the MIT License. See the LICENSE
file for details.
- Saurav Sajeev
Contributions are welcome! Please submit a pull request or open an issue on the GitHub repository.