GUVA-S12SD UV sensor sketch
-
Re: [0_1534265394169_UVSensor.ino](Uploading 100%) GUVA-S12SD UV sensor sketch required
[0_1534265424506_UVSensor.ino](Uploading 100%)
-
/**
- DESCRIPTION
- Sensor: CJMCU-GUVA-S12SD/CJMCU-S12D
- Sensor Arduino
- SIG -> A0
- GND -> GND
- VCC -> 5V or 3.3v
- part created by Henrik Ekblad henrik.ekblad@mysensors.org
- Copyright (C) 2013-2015 Sensnology AB
- Full contributor list: https://github.com/mysensors/Arduino/graphs/contributors
- part Original link : CSDN 博客链接.
- part by Tonbor august 2018
*/
// Enable debug prints to serial monitor
#define MY_DEBUG// Enable and select radio type attached
#define MY_RADIO_NRF24
//#define MY_RADIO_NRF5_ESB
//#define MY_RADIO_RFM69
//#define MY_RADIO_RFM95#include <MySensors.h>
#define UV_SENSOR_ANALOG_PIN 0
#define CHILD_ID_UV 0
uint32_t SLEEP_TIME = 30*1000; // Sleep time between reads (in milliseconds)
MyMessage uvMsg(CHILD_ID_UV, V_UV);
int lastSend =0;
int sensorValue;
long sum = 0;
int vout = 0;
int uv = 0;
int lastUV = -1;void presentation()
{
// Send the sketch version information to the gateway and Controller
sendSketchInfo("UV Sensor", "1.3");// Register all sensors to gateway (they will be created as child devices) present(CHILD_ID_UV, S_UV);
}
void setup() {
Serial.begin(115000);
}
void loop()
{
uint32_t currentTime = millis();
sensorValue = 0;
sum = 0;
for (int i = 0 ; i < 1024 ; i++ ) {
sensorValue = analogRead(UV_SENSOR_ANALOG_PIN);
sum = sensorValue + sum;
delay(2);
}
vout = sum >> 10;
vout = vout * 4980.0 / 1024;
Serial.print("The Photocurrent value:");
Serial.print(vout);
Serial.println("mV");
if (vout < 50) {
uv = 0;
}
else if (vout < 227) {
uv = 1;
}
else if (vout < 318) {
uv = 2;
}
else if (vout < 408) {
uv = 3;
}
else if (vout < 503) {
uv = 4;
}
else if (vout < 606) {
uv = 5;
}
else if (vout < 696) {
uv = 6;
}
else if (vout < 795) {
uv = 7;
}
else if (vout < 881) {
uv = 8;
}
else if (vout < 976) {
uv = 9;
}
else if (vout < 1079) {
uv = 10;
}
else {
uv = 11;
}//Send value to gateway if changed, or at least every 5 minutes if ((uv != lastUV)||(currentTime-lastSend >= 5UL*60UL*1000UL)) { lastSend=currentTime; send(uvMsg.set(uv)); lastUV = uv; } sleep(SLEEP_TIME);
}
Suggested Topics
-
Welcome
Announcements • • hek