Problem with Coding for Waveshare UV sensor using Arduino Uno

I am currently working on a school project to make a wearable device for health and I need to use this UV sensor in our code to input the UV index. We have the hardware finished and everything is plugged in we just need help on coding the sensor to the LCD.

Capture.JPG

here's the code. The AOUT is plugged to A0, VCC is to 3.3V, and GRD is to GRD

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

void setup()
{
  Serial.begin(9600);

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

  Serial.println("ML8511 example");
}

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

  Serial.print("output: ");
  Serial.print(refLevel);

  Serial.print("ML8511 output: ");
  Serial.print(uvLevel);

  Serial.print(" / ML8511 voltage: ");
  Serial.print(outputVoltage);

  Serial.print(" / UV Intensity (mW/cm^2): ");
  Serial.print(uvIntensity);
  
  Serial.println();
  
  delay(100);
}

//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(A0);
  runningValue /= numberOfReadings;

  return(runningValue);  
}

//The Arduino Map function but for floats
//From: http://forum.arduino.cc/index.php?topic=3922.0
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;
}

What LCD?

Do you have any of the examples which came with the LCD woeking?

ashleyokp, I'm interested in how you arrived at the values you did for the mapping the voltage to the intensity level. I've the same sensor but haven't found any documentation as to what the output voltage actually equates to.
Why 0.99 and not 0, 2.8 and not 3.3. and how did you derive 15 ?

mapfloat(outputVoltage, 0.99, 2.8, 0.0, 15.0)

thanks very much in advance

lazybaer:
ashleyokp, I'm interested in how you arrived at the values you did for the mapping the voltage to the intensity level. I've the same sensor but haven't found any documentation as to what the output voltage actually equates to.
Why 0.99 and not 0, 2.8 and not 3.3. and how did you derive 15 ?

mapfloat(outputVoltage, 0.99, 2.8, 0.0, 15.0)

thanks very much in advance

Ashley has been gone from the forum for over a year. likely found more interesting subjects.

Paul

Paul_KD7HB:
Ashley has been gone from the forum for over a year. likely found more interesting subjects.

Paul

I noticed. I was hoping, on an off chance, that she might get an email notice or something and pop back with some insight on this. I've not found an answer anywhere to what the voltage actually indicates on this sensor and this was the closest thing I've found to this mystery. =)

lazybaer:
I noticed. I was hoping, on an off chance, that she might get an email notice or something and pop back with some insight on this. I've not found an answer anywhere to what the voltage actually indicates on this sensor and this was the closest thing I've found to this mystery. =)

I am experiencing the same problem. The code included in the official Waveshare documents is a very basic one with only two outputs. You can go out in the sun or don't go out :slight_smile:
I wonder if you have found anything more relevant to the specific sensor.

btw the code posted by ashleyokp is totally wrong. Is meant for another, digital sensor (ML8511) and it will not give you any meaningful results with this one.