I used code from this source (https://www.circuitschools.com/uv-index-meter-interfacing-arduino-or-esp32-with-ml8511/) to read UV intensity with an ML8511 sensor and display it on an OLED. It worked perfectly.
After adding code to send the data to Google Sheets using HTTPClient, the UV intensity result started showing NAN, even though the sensor setup and logic were unchanged.
Could the added WiFi or HTTP request code be affecting the sensor readings?
Here’s the modified code:
`#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <WiFi.h>
#include <HTTPClient.h>
int UVOUT = 15;
int REF_3V3 = 4;
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET -1
Adafruit_SSD1306 display(OLED_RESET);
// Network credentials.
const char* ssid = "*****";
const char* password = "*****";
String googleScriptID = "...";
void setup()
{
Serial.begin(115200);
pinMode(UVOUT, INPUT);
pinMode(REF_3V3, INPUT);
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
display.clearDisplay();
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");
}
//Takes an average of readings on a given pin
//Returns the average
int averageAnalogRead(int pinToRead)
{
byte numberOfReadings = 8;
unsigned int runningValue = 0;
for(int x = 0 ; x < numberOfReadings ; x++)
runningValue += analogRead(pinToRead);
runningValue /= numberOfReadings;
return(runningValue);
}
float mapfloat(float x, float in_min, float in_max, float out_min, float out_max)
{
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}
void loop()
{
int uvLevel = averageAnalogRead(UVOUT);
int refLevel = averageAnalogRead(REF_3V3);
//Use the 3.3V power pin as a reference to get a very accurate output value from sensor
float outputVoltage = 3.3 / refLevel * uvLevel;
float uvIntensity = mapfloat(outputVoltage, 0.99, 2.8, 0.0, 15.0);
// Serial Output
Serial.print("output: ");
Serial.print(refLevel);
Serial.print(" / ML8511 output: ");
Serial.print(uvLevel);
Serial.print(" / Voltage: ");
Serial.print(outputVoltage);
Serial.print(" / UV Intensity (mW/cm^2): ");
Serial.println(uvIntensity);
// OLED Display Output
display.clearDisplay();
display.setCursor(20, 0);
display.setTextSize(1);
display.setTextColor(WHITE);
display.println("UV Ray Intensity");
display.setCursor(25, 15);
display.setTextSize(1.9);
display.println(uvIntensity);
display.setCursor(30, 25);
display.setTextSize(1.5);
display.println("mW/cm^2");
display.display();
delay(3000);
display.clearDisplay();
if (WiFi.status() == WL_CONNECTED) {
HTTPClient http;
String googleFormsURL = "https://script.google.com/.../exec";
String Intensity = String(uvIntensity);
String url = googleFormsURL + "?Intensity=" + Intensity;
http.begin(url);
int httpResponseCode = http.GET();
if (httpResponseCode == 302) {
// Perform the request to the redirected URL
String redirectURL = http.getLocation();
http.end();
http.begin(redirectURL);
httpResponseCode = http.GET();
}
if (httpResponseCode > 0) {
Serial.print("HTTP Response code: ");
Serial.println(httpResponseCode);
String payload = http.getString();
Serial.println(payload);
} else {
Serial.print("Error code: ");
Serial.println(httpResponseCode);
}
http.end();
}
delay(10000);
}`
Any suggestions to fix this issue? Thanks!