UV sensor Bargraph

Hi people I’ve combined these codes to try and get a Bargraph controlled by a UV sensor but… not sure if its my faulty wiring job, but its not behaving correctly. getting silly readings from the UV sensor for starters.

float Vsig;

// these constants won’t change:
const int analogPin = A0; // the pin that the UV sensor is attached to
const int ledCount = 5; // the number of LEDs in the bar graph

int ledPins = {
11, 10, 9, 8, 5
}; // an array of pin numbers to which LEDs are attached

void setup() {
Serial.begin(9600);

// loop over the pin array and set them all to output:
for (int thisLed = 0; thisLed < ledCount; thisLed++) {
pinMode(ledPins[thisLed], OUTPUT);
}
}

void loop() {
int sensorValue;
long sum=0;
for(int i=0;i<1023;i++)
{
sensorValue=analogRead(A0);
sum=sensorValue+sum;
delay(2);
}
sum = sum >> 10;
Vsig = sum*4980.0/1023.0; // Vsig is the value of voltage measured from the SIG pin of the Grove interface
Serial.print("The voltage value: “);
Serial.print(Vsig);
Serial.print(” mV – ");

if (Vsig < 50) {
Serial.print("UV Index: 0 “); Serial.println(” Exposure level - NONE (You’re probably at home!) ");
}
if (Vsig > 50 && Vsig < 227) {
Serial.print("UV Index: 1 “); Serial.println(” Exposure level - LOW (You’re probably at home!) ");
}
if (Vsig > 227 && Vsig < 318) {
Serial.print("UV Index: 2 “); Serial.println(” Exposure level - LOW (You can go outside and have fun!) ");
}
if (Vsig > 318 && Vsig < 408) {
Serial.print("UV Index: 3 “); Serial.println(” Exposure level - MODERATE (Sun starts to annoy you) ");
}
if (Vsig > 408 && Vsig < 503) {
Serial.print("UV Index: 4 “); Serial.println(” Exposure level - MODERATE (Sun starts to annoy you) ");
}
if (Vsig > 503 && Vsig < 606) {
Serial.print("UV Index: 5 “); Serial.println(” Exposure level - MODERATE (Sun starts to annoy you) ");
}
if (Vsig > 606 && Vsig < 696) {
Serial.print("UV Index: 6 “); Serial.println(” Exposure level - HIGH (Get out from the sunlight! get out now!) ");
}
if (Vsig > 696 && Vsig < 795) {
Serial.print("UV Index: 7 “); Serial.println(” Exposure level - HIGH (Get out from the sunlight! get out now!) ");
}
if (Vsig > 795 && Vsig < 881) {
Serial.print("UV Index: 8 “); Serial.println(” Exposure level - VERY HIGH (Get out from the sunlight! get out now!) ");
}
if (Vsig > 881 && Vsig < 976) {
Serial.print("UV Index: 9 “); Serial.println(” Exposure level - VERY HIGH (If you value your health, don’t go outside, just stay at home!) ");
}
if (Vsig > 976 && Vsig < 1079) {
Serial.print("UV Index: 10 “); Serial.println(” Exposure level - VERY HIGH (If you value your health, don’t go outside, just stay at home!) ");
}
if (Vsig > 1079 && Vsig < 1170) {
Serial.print("UV Index: 11 “); Serial.println(” Exposure level - EXTREME (If you value your health, don’t go outside, just stay at home!) ");
}
if (Vsig > 1170) {
Serial.print("UV Index: 11+ “); Serial.println(” Exposure level - EXTREME (You will probably die in 3, 2, 1… Just JOKING, don’t be scared…) - intensity of sunlight is really at maximum ");
}

// read the UV sensor:
int sensorReading = analogRead(analogPin);
// map the result to a range from 0 to the number of LEDs:
int ledLevel = map(sensorReading, 0, 1023, 0, ledCount);

// loop over the LED array:
for (int thisLed = 0; thisLed < ledCount; thisLed++) {
// if the array element’s index is less than ledLevel,
// turn the pin for this element on:
if (thisLed < ledLevel) {
digitalWrite(ledPins[thisLed], HIGH);
}
// turn off all pins higher than the ledLevel:
else {
digitalWrite(ledPins[thisLed], LOW);
}
}
}

actually what I would prefer to happen is to take the average reading and calibrate each LED to turn on at a certain value

something like this
(0,1023,0,255)

Tell us about the silly readings. Are they high, low or fluttering around erratically? Or something else?

Your debug Serial.print statements look good. What are they telling you? What are they printing for Vsig?

Tell us more about the UV sensor module. A link to a datasheet would be useful. Show us how it’s connected to your Arduino.

This is the UV sensor^^

output: 346ML8511 output: 252 / ML8511 voltage: 2.40 / UV Intensity (mW/cm^2): 11.71
output: 224ML8511 output: 141 / ML8511 voltage: 2.08 / UV Intensity (mW/cm^2): 9.01
output: 299ML8511 output: 235 / ML8511 voltage: 2.59 / UV Intensity (mW/cm^2): 13.29
output: 701ML8511 output: 809 / ML8511 voltage: 3.81 / UV Intensity (mW/cm^2): 23.36
output: 819ML8511 output: 891 / ML8511 voltage: 3.59 / UV Intensity (mW/cm^2): 21.55
output: 675ML8511 output: 711 / ML8511 voltage: 3.48 / UV Intensity (mW/cm^2): 20.60
output: 351ML8511 output: 254 / ML8511 voltage: 2.39 / UV Intensity (mW/cm^2): 11.59
output: 234ML8511 output: 177 / ML8511 voltage: 2.50 / UV Intensity (mW/cm^2): 12.48
output: 443ML8511 output: 456 / ML8511 voltage: 3.40 / UV Intensity (mW/cm^2): 19.95
output: 717ML8511 output: 821 / ML8511 voltage: 3.78 / UV Intensity (mW/cm^2): 23.11
output: 758ML8511 output: 851 / ML8511 voltage: 3.70 / UV Intensity (mW/cm^2): 22.50
output: 499ML8511 output: 460 / ML8511 voltage: 3.04 / UV Intensity (mW/cm^2): 17.01
output: 271ML8511 output: 181 / ML8511 voltage: 2.20 / UV Intensity (mW/cm^2): 10.06
output: 300ML8511 output: 239 / ML8511 voltage: 2.63 / UV Intensity (mW/cm^2): 13.58
output: 596ML8511 output: 682 / ML8511 voltage: 3.78 / UV Intensity (mW/cm^2): 23.09
output: 737ML8511 output: 839 / ML8511 voltage: 3.76 / UV Intensity (mW/cm^2): 22.93
output: 630ML8511 output: 679 / ML8511 voltage: 3.56 / UV Intensity (mW/cm^2): 21.27
output: 324ML8511 output: 236 / ML8511 voltage: 2.40 / UV Intensity (mW/cm^2): 11.72
output: 241ML8511 output: 161 / ML8511 voltage: 2.20 / UV Intensity (mW/cm^2): 10.07
output: 448ML8511 output: 463 / ML8511 voltage: 3.41 / UV Intensity (mW/cm^2): 20.06
output: 720ML8511 output: 823 / ML8511 voltage: 3.77 / UV Intensity (mW/cm^2): 23.06
output: 759ML8511 output: 853 / ML8511 voltage: 3.71 / UV Intensity (mW/cm^2): 22.53
output: 488ML8511 output: 445 / ML8511 voltage: 3.01 / UV Intensity (mW/cm^2): 16.73
output: 266ML8511 output: 180 / ML8511 voltage: 2.23 / UV Intensity (mW/cm^2): 10.30
output: 311ML8511 output: 251 / ML8511 voltage: 2.66 / UV Intensity (mW/cm^2): 13.87
output: 610ML8511 output: 704 / ML8511 voltage: 3.81 / UV Intensity (mW/cm^2): 23.36
output: 745ML8511 output: 839 / ML8511 voltage: 3.72 / UV Intensity (mW/cm^2): 22.59
output: 617ML8511 output: 656 / ML8511 voltage: 3.51 / UV Intensity (mW/cm^2): 20.87
output: 308ML8511 output: 212 / ML8511 voltage: 2.27 / UV Intensity (mW/cm^2): 10.62
output: 231ML8511 output: 161 / ML8511 voltage: 2.30 / UV Intensity (mW/cm^2): 10.86
output: 465ML8511 output: 492 / ML8511 voltage: 3.49 / UV Intensity (mW/cm^2): 20.73
output: 721ML8511 output: 827 / ML8511 voltage: 3.79 / UV Intensity (mW/cm^2): 23.16
output: 747ML8511 output: 835 / ML8511 voltage: 3.69 / UV Intensity (mW/cm^2): 22.37
output: 466ML8511 output: 415 / ML8511 voltage: 2.94 / UV Intensity (mW/cm^2): 16.15
output: 283ML8511 output: 182 / ML8511 voltage: 2.12 / UV Intensity (mW/cm^2): 9.38
output: 255ML8511 output: 185 / ML8511 voltage: 2.39 / UV Intensity (mW/cm^2): 11.64
output: 533ML8511 output: 603 / ML8511 voltage: 3.73 / UV Intensity (mW/cm^2): 22.74
output: 727ML8511 output: 835 / ML8511 voltage: 3.79 / UV Intensity (mW/cm^2): 23.21
output: 665ML8511 output: 734 / ML8511 voltage: 3.64 / UV Intensity (mW/cm^2): 21.98
output: 367ML8511 output: 286 / ML8511 voltage: 2.57 / UV Intensity (mW/cm^2): 13.11
output: 244ML8511 output: 174 / ML8511 voltage: 2.35 / UV Intensity (mW/cm^2): 11.30
output: 420ML8511 output: 418 / ML8511 voltage: 3.28 / UV Intensity (mW/cm^2): 19.01
output: 704ML8511 output: 818 / ML8511 voltage: 3.83 / UV Intensity (mW/cm^2): 23.57
output: 770ML8511 output: 851 / ML8511 voltage: 3.65 / UV Intensity (mW/cm^2): 22.02
output: 514ML8511 output: 480 / ML8511 voltage: 3.08 / UV Intensity (mW/cm^2): 17.33
output: 279ML8511 output: 189 / ML8511 voltage: 2.24 / UV Intensity (mW/cm^2): 10.32
output: 312ML8511 output: 246 / ML8511 voltage: 2.60 / UV Intensity (mW/cm^2): 13.36
output: 607ML8511 output: 696 / ML8511 voltage: 3.78 / UV Intensity (mW/cm^2): 23.15
output: 746ML8511 output: 843 / ML8511 voltage: 3.73 / UV Intensity (mW/cm^2): 22.70
output: 617ML8511 output: 657 / ML8511 voltage: 3.51 / UV Intensity (mW/cm^2): 20.92
output: 310ML8511 output: 211 / ML8511 voltage: 2.25 / UV Intensity (mW/cm^2): 10.41
output: 235ML8511 output: 172 / ML8511 voltage: 2.42 / UV Intensity (mW/cm^2): 11.81
output: 480ML8511 output: 518 / ML8511 voltage: 3.56 / UV Intensity (mW/cm^2): 21.31
output: 726ML8511 output: 831 / ML8511 voltage: 3.78 / UV Intensity (mW/cm^2): 23.10
output: 725ML8511 output: 816 / ML8511 voltage: 3.71 / UV Intensity (mW/cm^2): 22.58
output: 433ML8511 output: 373 / ML8511 voltage: 2.84 / UV Intensity (mW/cm^2): 15.35
output: 261ML8511 output: 185 / ML8511 voltage: 2.34 / UV Intensity (mW/cm^2): 11.18
output: 373ML8511 output: 343 / ML8511 voltage: 3.03 / UV Intensity (mW/cm^2): 16.94
output: 677ML8511 output: 787 / ML8511 voltage: 3.84 / UV Intensity (mW/cm^2): 23.59
output: 769ML8511 output: 857 / ML8511 voltage: 3.68 / UV Intensity (mW/cm^2): 22.27
output: 554ML8511 output: 552 / ML8511 voltage: 3.29 / UV Intensity (mW/cm^2): 19.04
output: 279ML8511 output: 191 / ML8511 voltage: 2.26 / UV Intensity (mW/cm^2): 10.52
output: 260ML8511 output: 195 / ML8511 voltage: 2.48 / UV Intensity (mW/cm^2): 12.31
output: 552ML8511 output: 627 / ML8511 voltage: 3.75 / UV Intensity (mW/cm^2): 22.86
output: 726ML8511 output: 834 / ML8511 voltage: 3.79 / UV Intensity (mW/cm^2): 23.21
output: 647ML8511 output: 703 / ML8511 voltage: 3.59 / UV Intensity (mW/cm^2): 21.51
output: 340ML8511 output: 253 / ML8511 voltage: 2.46 / UV Intensity (mW/cm^2): 12.15

the leds aren’t working with this one for some reason…

I really wanted to use this code because it has the option to calibrate the leds can’t seem to get it to work though

int runTimer = 1;
int runFor = 60; // time in seconds
int buzzerPin = 5;
int relayPin = 4;
int data = 0;

/*
Temperature Alarm
*/
float sinVal;
int toneVal;
unsigned long tepTimer ;
int UVOUT = A4; //Output from the sensor
int REF_3V3 = A1; //3.3V power on the Arduino board

/*
UV sensor to LED
*/
int yellowPin = 10; // Y - digital 9
int redPin = 8; // R – digital 10
int greenPin = 11; // G – digital 11
int orangePin = 9; // O – digital 12
int purplePin = 7; // P - digital 13
int potRedPin = A0; // UV sensor
int potGreenPin = A0; // UV sensor
int potOrangePin = A0; // UV sensor
int potPurplePin = A0; // UV sensor
int potYellowPin = A0; // UV sensor

//setup up accerleation variables
int xAcc;
int yAcc;
int zAcc;

int zero = 282;//this was the zero point value for my accelerometer, your board may be slightly different. see the comment below for calculating this with your own board.

void setup() {
pinMode(8, OUTPUT);
Serial.begin(9600);
Serial.begin(9600);

pinMode(UVOUT, INPUT);
pinMode(REF_3V3, INPUT);

Serial.println(“ML8511 example”); //UV to LED setup
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(orangePin, OUTPUT);
pinMode(yellowPin, OUTPUT);
pinMode(purplePin, OUTPUT);
Serial.begin(9600);

pinMode(buzzerPin, OUTPUT); // 2hr Timer
pinMode(relayPin, OUTPUT);

Serial.begin(9600);//set up serial
}

void loop() {
int val;
double data;
val = analogRead(0);
data = (double) val * (5 / 10.24); // convert the voltage to temperture

if (data > 27) { // If the temperture is over 27 degree, buzzer will alarm.
for (int x = 0; x < 180; x++) {
sinVal = (sin(x * (3.1412 / 180)));
toneVal = 2000 + (int(sinVal * 1000));
tone(8, toneVal);
delay(2);
}
} else { // If the temperturn is below 27 degree, buzzer will not alarm
noTone(8);
}

if (millis() - tepTimer > 500) { // output the temperture value per 500ms
tepTimer = millis();
Serial.print("temperature: ");
Serial.print(data);
Serial.println(“C”);
}
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); //Convert the voltage to a UV intensity level

Serial.print("output: ");
Serial.print(refLevel);

Serial.print("ML8511 output: ");
Serial.print(uvLevel);

Serial.print(" / ML8511 voltage: ");
Serial.print(outputVoltage);

Serial.print(" / UV Intensity (mW/cm^2): ");
Serial.print(uvIntensity);

Serial.println();

delay(100);
int potRed = analogRead(potRedPin);
int potGreen = analogRead(potGreenPin);
int potOrange = analogRead(potOrangePin);
int potPurple = analogRead(potPurplePin);
int potYellow = analogRead(potYellowPin);

int val1 = map(potRed, 0, 1023, 0, 255); // UV sensor output converts divides into colours
int val2 = map(potGreen, 0, 1023, 0, 255);
int val3 = map(potOrange, 0, 1023, 0, 255);
int val4 = map(potPurple, 0, 1023, 0, 255);
int val5 = map(potYellow, 0, 1023, 0, 255);

Serial.print(“Red:”);
Serial.print(val1);
Serial.print(“Green:”);
Serial.print(val2);
Serial.print(“Orange:”);
Serial.println(val3);
Serial.print(“Purple:”);
Serial.print(val4);
Serial.print(“Yellow:”);
Serial.print(val5);

}

//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);
}

//The Arduino Map function but for floats
//From: http://forum.arduino.cc/index.php?topic=3922.0
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;

}