I've been working on a project involving UV sensor readings using both an Arduino Uno and an ATTiny85. However, I've encountered discrepancies in the UV index values obtained from the two different microcontrollers. Below, I'll explain the setup, the code used, and the differences observed.
Setup Overview
Arduino Uno Setup:
- Reference Voltage: External 3.3V
- UV Sensor Pin: A0
ATTiny85 Setup:
- Reference Voltage: Internal 1.1V
- UV Sensor Pin: Analog pin 3 (corresponds to physical pin 2)
ADAFRUIT GUVA-S12SD
Issue
Using the same UV sensor, the Arduino Uno provides a UV index of 10.25, while the ATTiny85 gives a reading of 8.25 for the same light conditions. Here are the detailed codes and explanations for both setups.
Arduino Uno Code
#ifdef __AVR_ATtiny85__
#include <TinyWireM.h>
#define Wire TinyWireM
#else
#include <Wire.h>
#endif
#include <Tiny4kOLED.h>
const int sampleCount = 20;
const int delayTime = 50;
float voltageSum, current, power, sum;
const float referenceVoltage = 3.3; // External reference of 3.3V
const int UVPin = A0; // Analog pin used
void setup() {
// Serial.begin(9600);
analogReference(EXTERNAL); // Use external reference of 3.3V
oled.begin();
oled.setFont(FONT8X16);
oled.clear();
oled.switchRenderFrame();
oled.clear();
oled.switchRenderFrame();
oled.deactivateScroll();
oled.on();
}
void loop() {
measureADA();
delay(1000);
}
void measureADA() {
oled.clear();
voltageSum = 0;
for (int i = 0; i < sampleCount; i++) {
voltageSum += analogRead(UVPin);
delay(delayTime);
}
voltageSum = (voltageSum / sampleCount) * referenceVoltage / 1023.0;
float correctedVoltage = voltageSum;
if (correctedVoltage < 0) correctedVoltage = 0;
current = correctedVoltage / 4.3; // I in uA
power = current * 1000.0 / 113.0; // UV power in mW/cm²
float uvIndex = (current * 1000.0 - 83.0) / 21.0;
if (uvIndex < 0) uvIndex = 0;
oled.setCursor(1,1);
oled.print(uvIndex);
}
ATTiny85 Code
#ifdef __AVR_ATtiny85__
#include <TinyWireM.h>
#define Wire TinyWireM
#else
#include <Wire.h>
#endif
#include <Tiny4kOLED.h>
const int sampleCount = 20;
const int delayTime = 50;
float voltageSum, current, power, sum;
const float referenceVoltage = 1.1; // Internal reference of 1.1V
const int UVPin = 3; // Analog pin used
void setup() {
// Serial.begin(9600);
analogReference(INTERNAL); // Use internal reference of 1.1V
oled.begin();
oled.setFont(FONT8X16);
oled.clear();
oled.switchRenderFrame();
oled.clear();
oled.switchRenderFrame();
oled.deactivateScroll();
oled.on();
}
void loop() {
measureADA();
delay(1000);
}
void measureADA() {
oled.clear();
voltageSum = 0;
for (int i = 0; i < sampleCount; i++) {
voltageSum += analogRead(UVPin);
delay(delayTime);
}
voltageSum = (voltageSum / sampleCount) * referenceVoltage / 1023.0;
float correctedVoltage = voltageSum;
if (correctedVoltage < 0) correctedVoltage = 0;
current = correctedVoltage / 4.3; // I in uA
power = current * 1000.0 / 113.0; // UV power in mW/cm²
float uvIndex = (current * 1000.0 - 83.0) / 21.0;
if (uvIndex < 0) uvIndex = 0;
oled.setCursor(1,1);
oled.print(uvIndex);
}
Do you see any mistakes?
Thanks