Home » ESP32 and ML8511 sensor example

ESP32 and ML8511 sensor example

by shedboy71

The ML8511 measures the amount of ultra violet rays contained in sunlight, and it is used for the equipment which displays the suntan by a ultra violet rays, the guidance for UV care of skin, etc

The sensor detects 280-390nm light most effectively. This is categorized as part of the UVB (burning rays) spectrum and most of the UVA (tanning rays) spectrum.

It outputs a analog voltage that is linearly related to the measured UV intensity (mW/cm2). If your microcontroller can do an analog to digital signal conversion then you can detect the level of UV!



Parts Required

Here are the parts I used


Name Link
Connecting cables


A simple connection this one using the module above

 LOLIN32  ML8511 module
 3v3  3V3
 Gnd  GND
 A0  OUT



No external libraries required for this one


int UVsensorIn = A0; //Output from the sensor
void setup()
  pinMode(UVsensorIn, INPUT);
  Serial.begin(9600); //open serial port, set the baud rate to 9600 bps
void loop()
  int uvLevel = averageAnalogRead(UVsensorIn);
  float outputVoltage = 3.3 * uvLevel/4095;
  float uvIntensity = mapfloat(outputVoltage, 0.99, 2.9, 0.0, 15.0);
  Serial.print(" UV Intensity: ");
  Serial.print(" mW/cm^2"); 
//Takes an average of readings on a given pin
//Returns the average
int averageAnalogRead(int pinToRead)
  byte numberOfReadings = 8;
  unsigned int runningValue = 0; 
  for(int x = 0 ; x < numberOfReadings ; x++)
    runningValue += analogRead(pinToRead);
  runningValue /= numberOfReadings;

float mapfloat(float x, float in_min, float in_max, float out_min, float out_max)
  return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;



Open the serial monitor and you should see something which looks like this

UV Intensity: 10.72 mW/cm^2
UV Intensity: 10.83 mW/cm^2
UV Intensity: 10.97 mW/cm^2
UV Intensity: 11.05 mW/cm^2
UV Intensity: 11.10 mW/cm^2
UV Intensity: 11.21 mW/cm^2
UV Intensity: 11.17 mW/cm^2
UV Intensity: 11.22 mW/cm^2
UV Intensity: 11.27 mW/cm^2
UV Intensity: 11.28 mW/cm^2
UV Intensity: 11.29 mW/cm^2



You may also like

Leave a Comment

Adblock Detected

Please support us by disabling your AdBlocker extension from your browsers for our website.