UV ML8511 Sensor

Please help on this code how to include a buzzer that can buzzer when UV intensity is greater > 11.0 , the buzzer should buzz else be low I have tried to include if statement but still I am getting an error.

#include <LiquidCrystal_I2C.h>
 
LiquidCrystal_I2C lcd(0x27,16,2);
//Hardware pin definitions
int UVOUT = A0; //Output from the sensor
int REF_3V3 = A1; //3.3V power on the Arduino board
int buzzer = 13;
void setup()
{
 
  // Initialize the LCD connected 
  lcd. init ();
  
  // Turn on the backlight on LCD. 
  lcd. backlight ();
 
  pinMode(UVOUT, INPUT);
  pinMode(REF_3V3, INPUT);
  pinMode(buzzer, OUTPUT);
 
  
  
}
 
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);
  lcd.clear();
  lcd.print("UV Ray Intensity");
  lcd.setCursor(0, 1);
  lcd.print(uvIntensity);
  lcd.print(" mW/cm^2");
  
  Serial.println();
  
  delay(200);
}
 
//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;
 
  return(runningValue);
}
 
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;
}

Hi @Ntshukumbana.

Since you want some help, I'll make a proposal for you.

you read the topic:

then correct your initial post and enclose the code in </> tags.
ArduinoForum

Then I do my best to help you, and in exchange, you read the topic.

What about ? Done deal?

RV - mineirin

You have posted your question in the Introductory Tutorials forum rather than the
Programming Questions forum :grimacing:

@Ntshukumbana , your post moved to a more appropriate category.

Thank you

Hi
This line needs to end f with ";" "int buzzer = 13"
What arduino are you using in your project?

RV - mineirin

Arduino Uno. thanks I did not notice that.

Hi
I didn't find in your code at what moment you trigger the buzzer

RV - mineirin

Yes that is where I am encountering challenges I tried but this too is not working.

 {
  int uvLevel = averageAnalogRead(UVOUT);
  int refLevel = averageAnalogRead(REF_3V3);
  float uvIntensity = 15.0;
   if (uvIntensity > 11.0)
    {
  //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.9, 0.0, 15.0);
  digitalWrite (buzzer, HIGH);
  Serial.print("MP8511 output: ");
  Serial.print(uvLevel);
  Serial.print(" MP8511 voltage: ");
  Serial.print(outputVoltage);
  lcd.setCursor(4,1);
  lcd.print("UV Intensity:");
  lcd.setCursor(0,1);
  lcd.print(uvIntensity);
  delay(1000);
  lcd.clear();
  }

 else

 {
    
       digitalWrite(buzzer, LOW);
 }


  
  }

Try this code:

#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27, 16, 2);
//Hardware pin definitions
int UVOUT = A0; //Output from the sensor
int REF_3V3 = A1; //3.3V power on the Arduino board
int buzzer = 13;
//----------------------------------------------
void setup()
{
  // Initialize the LCD connected
  lcd. init ();
  // Turn on the backlight on LCD.
  lcd. backlight ();
  pinMode(UVOUT, INPUT);
  pinMode(REF_3V3, INPUT);
  pinMode(buzzer, OUTPUT);
}
//----------------------------------------------
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);
  lcd.clear();
  lcd.print("UV Ray Intensity");
  lcd.setCursor(0, 1);
  lcd.print(uvIntensity);
  lcd.print(" mW/cm^2");
  Serial.println();
  delay(200);
  if(uvIntensity >= 11.0)
    digitalWrite(buzzer, HIGH);
  else
    digitalWrite(buzzer, LOW);
}
//----------------------------------------------
//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;
  return (runningValue);
}
//----------------------------------------------
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;
}

Thank you it finally responded.