"UV Sensor Data Turns NAN After Adding Google Sheets Integration"

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!

This problem does not sound like it is related in any way to Avrdude, stk500 or bootloaders, so I'm going to move your topic to a more appropriate forum section. Please pay more attention in future to where you post your topics. There is a sticky post at the top of most sections that describes what the section is about.

Please share what model of Arduino you are using, as you seem to have forgotten to mention this basic piece of information.

Some ADC pins are not usable when WiFi is used. Try using GPIO36, 37, 38, or 39.
Any ADC1 pin should work

I use ESP WROOM 32

I'v tried but the result is negative

Did you change both the UVOUT and REF_3V3 pins to one of the ADC1 GPIOs?

Which pins are ADC1 are shown here
https://docs.espressif.com/projects/arduino-esp32/en/latest/boards/ESP32-DevKitC-1.html

FYI:
The ADC on the ESP32 has very poor accuracy and a limited range.
It can only accuratrly measure voltages between 0.15V and 3.1V.

I suggest you use analogReadMilliVolts(pin) instead of analogRead(pin). It will give you a calibrated reading in milivolts.

No need to do float outputVoltage = 3.3 / refLevel * uvLevel since it has already been properly scaled.

Where?
Can you post your serial monitor output.

this result from pin 15 and 4

this result from pin 32 and 33

and this result without connect to spreadsheet use pin 15 and 4
image

Is that before or after you used the correct pins?
What pins did you use?
Did you use the analogReadmillivolts?

I thought you said it was giving a Negative number.

They are ADC2 pins.

ADC1_CH0 (GPIO 36)
ADC1_CH1 (GPIO 37)
ADC1_CH2 (GPIO 38)
ADC1_CH3 (GPIO 39)
ADC1_CH4 (GPIO 32)
ADC1_CH5 (GPIO 33)
ADC1_CH6 (GPIO 34)
ADC1_CH7 (GPIO 35)
ADC2_CH0 (GPIO 4)
ADC2_CH1 (GPIO 0)
ADC2_CH2 (GPIO 2)
ADC2_CH3 (GPIO 15)
ADC2_CH4 (GPIO 13)
ADC2_CH5 (GPIO 12)
ADC2_CH6 (GPIO 14)
ADC2_CH7 (GPIO 27)
ADC2_CH8 (GPIO 25)
ADC2_CH9 (GPIO 26)

correct.

The reading for the ADC1 pins seem reasonable but the ML8511 is low
Follow my instructions in post #8 regarding using analogMilliVolts and the ADC1 pins

The ADC1 pin selection is given in the linlkI provided in Post #7

Is the UV sensor near the WiFi antenna? If yes, try moving it.

it's work, but so I just use 3 pin from the sensor, right?

Not sure what you mean by 3pin?
Connect 3.3 to esp32 3.3V pin
Connect Gnd to ESP32 GND.
Connect OUT to ESP32 ADC1 pin.

You can leave EN disconnected.

yeah I do that, 3.3, GND, and OUT.

That is all you need.

Thank you. Do you have any suggestions on how to calibrate this sensor?

Not without thousands of dollars of test equipment.
Most people just use the graph provided in the datasheet.