Welcome to Hackster!
Hackster is a community dedicated to learning hardware, from beginner to pro. Join us, it's free!
tf
Created June 9, 2024 © Apache-2.0

Audible UV Index Meter w/ UNIHIKER

A simple UV audbible index meter implemented on the DFRobot UNIHIKER ....

IntermediateFull instructions provided1.41421356237 hours33
Audible UV Index Meter w/ UNIHIKER

Things used in this project

Story

Read more

Schematics

Wiring ...

Setup ...

Code

UNIHIKER - Audible UV Index Meter

Python
#!/usr/bin/env python
# coding: utf-8

# Wiring: Connect a DFRobot Gravity:Analog UV Sensor V2 to the UNIHIKER P21 pin

import time
from pinpong.board import Board, Pin
from unihiker import GUI
from pinpong.extension.unihiker import *

# Function to convert sensor output voltage (mV) to UV index
# for a GUVA-S12D based analog UV sensor based on a conversion table.
# See http://www.esp32learning.com/code/esp32-and-guva-s12sd-uv-sensor-example.php
# for conversion table ...
def uv_index(mv):
    uvi_mv_thresholds = [50, 227, 318, 408, 503, 606, 696, 795, 881, 976, 1079, 1170];
    
    for threshold in uvi_mv_thresholds:
        if (mv < threshold ):
            break
            
    i = uvi_mv_thresholds.index(threshold)
    
    if (i < 11 ):
        uvi = mv/threshold*(i+1)
    else:
        uvi = 11
        
    return uvi

# Function to convert sensor output voltage (mV) to mW/m^2
# for a GUVA-S12D based analog UV sensor: "The output voltage is: Vo = 4.3 * Diode-Current-in-uA.
# So if the photocurrent is 1uA (~ 9 mW/cm^2), the output voltage is 4.3V."
def uv_level(mv):
    return ( mv / 43 * 9 ) # 1 uA per 9 mW/cm^2 at 4.3V

def uv_risk_level(uvi):
    # UV risk level mapping (UV index threshold,risk level,display color)
    risk_levels=[(2,"low","green"),(5,"moderate","yellow"),(7,"high","orange"),(10,"very high","red"),(11,"extreme","violet")]
    
    for risk_level in risk_levels:
        if (uvi <= risk_level[0]):
            break
    
    return risk_level

def buzzer_play_uv_index_melody(uvi):
    # Play the corresponding tone sequence for the measured UV index on the buzzer:
    # C5 (F5){UV index} for an UV index < 6,
    # C5 (F5){5} (A5){UV index - 5} for an UV index 6 - 10 and
    # C5 (F5){5} (A5){5} C6 for UV index 11 ...
    buzzer.set_tempo(4, 160)
    buzzer.pitch(523, 4) # C5
    for i in range(0,uvi):
        if i < 5:
            buzzer.pitch(698, 4) # F5
        elif i < 10:
            buzzer.pitch(880, 4) # A5
        else:
            buzzer.pitch(1046, 4) # C6

def btn_a_rasing_handler(pin):  # Interrupt event callback function for button A rising edge
    global btn_a_pressed
    btn_a_pressed = True
    
Board().begin() # Initialize the UNIHIKER
gui = GUI() # Instantiate the GUI class

adc21 = Pin(Pin.P21, Pin.ANALOG)  # Initialize the pin as an analog input 

btn_a_pressed = False
button_a.irq(trigger=Pin.IRQ_RISING, handler=btn_a_rasing_handler)  # Trigger on rising edge


# GUI: 
#   Display a background image with the color touch of the risk level,
#   the UV index and the risk level text ...
bg = gui.draw_image(x=0, y=0, h=320, w=240, image='img/background_green.jpg')
title = gui.draw_text(x=120, y=80, text="UV Index", origin="center", color="white", font_size=25)
uv_index_text = gui.draw_digit(x=120, y=160, text="UVI", origin="center", color="white", font_size=50) # Display UV index using 7-segment font
uv_risk_text = gui.draw_text(x=120, y=240, text="N/A", origin="center", color="white", font_size=25)


i0 = 0
l0 = 0
r0 = None

while True:
    # Read the sensor value ...
    v = adc21.read_analog()  # Read the analog signal value from pin A0 
        
    # Calculate UV index, level and risk ...
    i = uv_index(v)
    l = uv_level(v)
    r = uv_risk_level(i)
    
    # Update UI ...
    uv_index_text.config(text="%.2f" % i)
    if r0 != r: # Update the background image and the risk level text only if the risk level changed ...
        bg.config(image='img/background_'+r[2]+'.jpg')
        uv_risk_text.config(text=r[1])
    
    # If the button has been pressed, play the UV index 'melody' ...
    if  btn_a_pressed:
        btn_a_pressed = False
        buzzer_play_uv_index_melody(round(i))
    
    i0 = i
    l0 = l
    r0 = r
    
    time.sleep(1) # Wait for a second ..

Jupyter Notebook: Audible UV Index Meter

Credits

tf
14 projects • 3 followers
Contact

Comments

Please log in or sign up to comment.