Luqmaan Baboo
Published © MIT

UV Index Alert Using XinaBox ☒CHIPS and Arduino

This project measures the UV Index using ☒CHIPS. A simple project to remind you to remain protected in the sun.

EasyShowcase (no instructions)15 minutes209
UV Index Alert Using XinaBox ☒CHIPS and Arduino

Things used in this project

Hardware components

CW01
XinaBox CW01
☒CHIP Wi-Fi Core based on ESP8266 Wi-Fi Module
×1
IP01
XinaBox IP01
☒CHIP USB Programmer based on FT232R From FTDI Limited
×1
OD01
XinaBox OD01
☒CHIP 128x64 Pixel OLED Display
×1
PU01
XinaBox PU01
☒CHIP USB (Type A) Power Supply
×1
XinaBox SL01
☒CHIP UV and Light Sensor based on VEML6075 & TSL4531 respectively
×1
USB Power Supply
Power Bank or similar product
×1

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Code

UV_Alert.ino

Arduino
This code measures the UV Index using SL01 and outputs a message on the OD01 OLED display accompanied with an RGB LED
#include <xCore.h>                  // include core library
#include <xSL01.h>                  // include sensor library
#include <xOD01.h>                  // include OLED display library

int LED_state; 
bool flag;
float uv_Index;                     // uv index variable
unsigned long previousMilli = 0;    // timing variable for LEDS
unsigned long previousMilli_1 = 0;  // timing variable for OLED
const long interval_1= 5000;        // time delay for OLED 

// define RGB LED pin numbers
#define RED 12        
#define GREEN 13
#define BLUE 5

void setup() {
  // put your setup code here, to run once:
  
  // set RGB LED pins
  pinMode(RED, OUTPUT);
  pinMode(GREEN, OUTPUT);
  pinMode(BLUE, OUTPUT);

  // initialize variables
  LED_state = LOW;
  flag = false;
  uv_Index = 0;
  
  // start the I2C communication
  Wire.begin(2, 14);

  // start the SL01 xCHIP
  SL01.begin();

  // start OLED Display
  OLED.begin();
  // clear OLED display
  OD01.clear();

  // delay for normalization
  delay(5000);
}

void loop() {
  // put your main code here, to run repeatedly:

  // constantly read uv measurements
  SL01.poll();

  // retrieve uv measurement from sensor
  uv_Index = SL01.getUVIndex();

  // call main program
  uv_categories();

}

// main program contains different categories of uv index
void uv_categories() {
  
  // low uv index boundaries
  if((uv_Index >= 0) && (uv_Index < 3)){
    display_uv("LOW ");
    digitalWrite(RED, LOW);
    digitalWrite(BLUE, LOW);
    digitalWrite(GREEN, HIGH);    // green led on when low
  }
  
  // moderate uv index boundaries
  else if((uv_Index >= 3) && (uv_Index < 6 )){
    display_uv("MODERATE ");
    digitalWrite(GREEN, LOW);
    digitalWrite(BLUE, LOW);
    led_time_delay(800, GREEN);  // flash green LED 800ms when moderate
  }
  
  // high uv index boundaries
  else if((uv_Index >= 6) && (uv_Index < 8)){
    display_uv("HIGH ");
    digitalWrite(RED, LOW);
    digitalWrite(BLUE, LOW);
    digitalWrite(RED, HIGH);      // red LED on when high
  }
  
  // very high uv index boundaries
  else if((uv_Index >= 8) && (uv_Index < 11)){
    display_uv("VERY HIGH ");
    digitalWrite(GREEN, LOW);
    digitalWrite(BLUE, LOW);
    led_time_delay(600, RED);     // flash red LED when 600ms when very high
  }
  
  // extreme uv index boundaries
  else if((uv_Index > 11)){
    display_uv("EXTREME ");
    digitalWrite(GREEN, LOW);
    digitalWrite(BLUE, LOW);
    led_time_delay(200, RED);     // flash red LED when 200ms when very high 
  }
  
  // sensor error or disconnected
  else{
    display_uv("No measurements available");
    digitalWrite(RED, LOW);
    digitalWrite(GREEN, LOW);
    digitalWrite(BLUE, HIGH);     // blue LED on when sensor gives strange readings
  }
}

// time delay function for multitasking
// do not use the delay() function as it will pause all operations
void led_time_delay(const long interval, int LED) {
  
  unsigned long currentMilli = millis();
  if(currentMilli - previousMilli >= interval){
    previousMilli = currentMilli;
    if(LED_state == LOW){
      LED_state = HIGH;
    }
    else{
      LED_state = LOW;
    }
    digitalWrite(LED, LED_state);
  }
}

// prints message on OLED display
void display_uv(String category) {

  // timing delay according to interval_1 variable
  // do not use the delay() function as it will pause all operations
  unsigned long currentMilli_1 = millis();
  if(currentMilli_1 - previousMilli_1 >= interval_1){
    previousMilli_1 = currentMilli_1;
    if(flag == false){
      flag = true;
    }
    else{
      flag = false;
    }

    // prints on OLED display
    OD01.print(category);       // display category of UV Index on OLED
    OD01.print("UV Index: ");
    OD01.println(uv_Index);     // display UV Index on OLED
  }
}

Credits

Luqmaan Baboo

Luqmaan Baboo

9 projects • 2 followers
Electrical Engineering Student with a fascination for automation.

Comments