 # Grove UV Sensor: Understanding Grove Sketch

Hi guys,

Im working with this UV sensor that came with the Grove Kit. I looked up its datasheet here:

http://www.seeedstudio.com/wiki/Grove_-_UV_Sensor

and using their sample sketch:

``````void setup(){

Serial.begin(9600);
}

void loop()
{
int sensorValue;
long  sum=0;
for(int i=0;i<1024;i++)
{
sum=sensorValue+sum;
delay(2);
}
sum = sum >> 10;
Serial.print("The voltage value:");
Serial.print(sum*4980.0/1023.0);
Serial.println("mV");
delay(20);
Serial.print("\n");

}
``````

I get mV values ranging from 1660-3091.

First Im not sure why it loops to 1024, reading the value each time and adding it to the previous read. So basically its reading one time, delaying for 2ms, then reading again, adding the second value to the first value and then looping again. Then there is that >>. Is that a bitwise operator? Im not to clear on what those do. But then we multiply that value by 4980 and over 1023 to get mV.

Whatever all that is, I took the mV and tried to get the UV Index. So they state that:

illumination intensity = 307 * Vsig

where Vsig is the voltage reading given by their grove UV sensor. So a 2477mV reading should be multiplied by 307 to get the illumination intensity, which gives me 760,439 which would then be /200 to give the UV index = 3,802.

Has anyone used this to get a UV Index that we can read?

sum = sum >> 10;

is a bit-shift right by 10 places which equals a division by 1024 == #samples.

simplified the code without averaging:

``````void setup()
{
Serial.begin(9600);
Serial.println(__FILE__);
}

void loop()
{
// measure
// process
float voltage = sensorValue * 4980.0 / 1023.0;
float ilumination = 307* voltage;
// display
Serial.print("The voltage value: ");
Serial.print(voltage, 1);  // 1 decimal
Serial.println("mV");
Serial.print("      Ilumination: ");
Serial.print(ilumination, 1);  // 1 decimal
Serial.println();
Serial.println();
// wait
delay(100);
}
``````