Ultraviolet sensor ML8511 DIP

For the safety of our skin, during our holidays by the sea and not only, it is good to check the values of ultraviolet rays to which we expose ourselves. We will see how to implement a system for the detection of UV-A and UV-B rays using an Arduino Yun and a ML8511 DIP sensor.The ML8511 sensor is very easy to use. It outputs a analog voltage that is linearly related to the measured UV intensity (mW/cm2).   
What do we need

  • an Arduino Yun board
  • UV-A UV-B sensor – ML8511
  • a breadboard
  • a bunch of wire

The circuit


Be sure to follow the connections shown in the fritzing circuit  and in the next table :

  • GND             ---> GND
  • EN                ---> 3.3V
  • +V                ---> A1
  • VO                ---> A0  
  • Arduino 3.3V ---> Arduino A1

The code

//Hardware pin definitions
int UVOUT = A0; //Output from the sensor
int REF_3V3 = A1; //3.3V power on the Arduino board

void setup()

  pinMode(UVOUT, INPUT);
  pinMode(REF_3V3, INPUT);


void loop()
  int uvLevel = averageAnalogRead(UVOUT);
  int refLevel = averageAnalogRead(REF_3V3);
  //Use the 3.3V power pin as a reference to get a very accurate output value from sensor
  float outputVoltage = 3.3 / refLevel * uvLevel;
  float uvIntensity = mapfloat(outputVoltage, 0.99, 2.8, 0.0, 15.0); //Convert the voltage to a UV intensity level

  Serial1.print("output: ");

  Serial1.print("ML8511 output: ");

  Serial1.print(" / ML8511 voltage: ");

  Serial1.print(" / UV Intensity (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;


//The Arduino Map function but for floats
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;
Write or paste code in Arduino IDE, then load the sketch on Yun board.

Python Serial Test

Read data from shell. 
To do this we used the included library “pyserial”. You must create a python script to be launched from a terminal. Then connect to your board with ssh, create file serialTest.py with the following code and start it with the command  “python serialTest.py”. Read data now.
import serial
     print "Trying..."
     arduino = serial.Serial('/dev/ttyATH0',230400)
     print "Failed to connect on"
while True:
     print x

Referral link:

Ultraviolet sensor ML8511 DIP
Ultraviolet sensor ML8511 DIP Sample Code