World of Electronics and Cyber Consultancy

DIY – UV Index Detector Device – Arduino – Full Code

DIY - UV Index Detector Device - Arduino - Full Code

Items Needed:

  1. Arduino UNO
  2. UV Sensor Module
  3. LCD 2 Lines with i2c module
  4. Jumper Wires Male – Female
  5. Lamp 1090

Code:


#include "LCD.h"
#include <LiquidCrystal_I2C.h>

#define I2C_ADDR 0x27 // <<----- Add your address here. Find it from I2C Scanner
#define BACKLIGHT_PIN 3
#define En_pin 2
#define Rw_pin 1
#define Rs_pin 0
#define D4_pin 4
#define D5_pin 5
#define D6_pin 6
#define D7_pin 7


LiquidCrystal_I2C lcd(I2C_ADDR,En_pin,Rw_pin,Rs_pin,D4_pin,D5_pin,D6_pin,D7_pin);



void setup() {
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);

lcd.begin(20,4);
lcd.setBacklightPin(BACKLIGHT_PIN,POSITIVE);
lcd.setBacklight(HIGH);
lcd.setCursor(0,0);

lcd.setCursor(0,0);
lcd.print("C.B.ELECTRONICS");
delay(500);
lcd.setCursor(0,1);
lcd.print("Lebanese Park");
delay(500);
lcd.clear();

}

// the loop routine runs over and over again forever:
void loop() {
// read the input on analog pin 0:
int sensorValue = analogRead(A0);
Serial.println(sensorValue);
lcd.setCursor(0,0);
lcd.print(sensorValue);
lcd.setCursor(0,1);
lcd.print("UV Index: ");
if(sensorValue<10)
{
lcd.print(0);
}
else if (sensorValue<46)
{
lcd.print(1);
}
else if (sensorValue<65)
{
lcd.print(2);
}
else if (sensorValue<83)
{
lcd.print(3);
}
else if (sensorValue<103)
{
lcd.print(4);
}
else if (sensorValue<124)
{
lcd.print(5);
}
else if (sensorValue<142)
{
lcd.print(6);
}
else if (sensorValue<162)
{
lcd.print(7);
}
else if (sensorValue<180)
{
lcd.print(8);
}
else if (sensorValue<200)
{
lcd.print(9);
}
else if (sensorValue<221)
{
lcd.print(10);
}
else if (sensorValue>221)
{
lcd.print(11);
}
delay(1000);
lcd.clear();
}