UV sensor模組
UV Index
電路圖
程式列表
Blockly
Python
from machine import Pin,ADC
from machine import Switch
import utime
from machine import RTC
x = None
sensorSum = None
UV_Index = None
sensorValue = None
sensorVoltage = None
adc0 = None
KeyA = None
rtc = None
"""描述此函式...
"""
def GetIndex(x):
global sensorSum, UV_Index, sensorValue, sensorVoltage
if sensorVoltage < 50:
UV_Index = 0
elif sensorVoltage < 227:
UV_Index = 1
elif sensorVoltage < 318:
UV_Index = 2
elif sensorVoltage < 408:
UV_Index = 3
elif sensorVoltage < 503:
UV_Index = 4
elif sensorVoltage < 606:
UV_Index = 5
elif sensorVoltage < 696:
UV_Index = 6
elif sensorVoltage < 795:
UV_Index = 7
elif sensorVoltage < 881:
UV_Index = 8
elif sensorVoltage < 976:
UV_Index = 9
elif sensorVoltage < 1079:
UV_Index = 10
else:
UV_Index = 11
adc0 = ADC(Pin.board.AIN0)
KeyA = Switch('keya')
rtc = RTC()
while True:
sensorSum = 0
for count in range(16):
sensorValue = adc0.read()
sensorSum = sensorSum + sensorValue
sensorValue = sensorSum / 4
sensorVoltage = (sensorValue * 3300) / 16384
GetIndex(None)
print(''.join([str(x2) for x2 in ['sensor reading = ', sensorValue, ', sensor voltage = ', sensorVoltage, 'mV', ', UV index = ', UV_Index]]))
if (KeyA.value()) == True:
break
utime.sleep_ms( 500 )
執行結果