Cant output Voltage from Arduino Code to LCD

66 views Asked by At

Ok, SO I am trying to create a UV meter and output a UV index to a Nokia 5110 LCD. I am using an arduino Nano. Have a GY 8511 UV Sensor that I need to output the voltage and use the If/esle string at the end to output the UV index onto the diplay.

Currently I am outputting these values to the serial monitor so I can see what the code and sensor is seeing.

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

The one that I want to use in the If/else string at the end is the "ML8511 voltage"; this is a voltage from 1.00v-3.3v

Currently the code has voltage as what is being compared and output the UV Index but that is not the value I need. I want to compare the values that are being output by

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

I have tried to change "Voltage" to "outputVoltage", "UVlevel" I have tried to move the math to before that string... Im kinda lost at this point

Here is the code... Its a mess, I know, I am not a code person and I am struggling through this trying to get this to work so please try to be nice...

Hopefully you guys can help me out and its a simple fix.

#include <Adafruit_PCD8544.h>
#include <LCD5110_Graph.h>



LCD5110 lcd(8,9,10,12,11);
extern unsigned char BigNumbers[];
extern uint8_t splash[];
extern uint8_t ui[];
//////////////////////////////////////////////////////////////////////////////////
int UVOUT = A0; //Output from the sensor
int REF_3V3 = A1; //3.3V power on the Arduino board
/////////////////////////////////////////////////////////////////////////////////
String UV = "0"; 


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

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

  Serial.println("ML8511 example");
  ////////////////////////////////////////////////////////////////// 
 lcd.InitLCD();
 lcd.setFont(BigNumbers);
 lcd.clrScr();
 lcd.drawBitmap(0, 0, splash, 84, 48);
 lcd.update();  
 delay(3000);
 
}

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); //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 stringLength = 0; 
  
 UV = readSensor();
 lcd.clrScr();
 lcd.drawBitmap(0, 0, ui, 84, 48);
 
 stringLength = UV.length();
 printUV(stringLength);
 lcd.update();
 delay(150);
}
//////////////////////////////////////////////////////////////////////////////////////
//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;
}
/////////////////////////////////////////////////////////////////////////////////////////
void printUV(int length)
{
  switch(length)
  {
    case 1:  lcd.print(UV,38,19); break;
    case 2:  lcd.print(UV,24,19); break;
    default:  lcd.print(UV,0,19); break;
  }
}

String readSensor()
{
  String UVIndex = "0";
  int sensorValue = 0;
  
  sensorValue = analogRead(0);                        //connect UV sensor to Analog 0   
  int voltage = (sensorValue); //* (3.3 / 1023.0))*1000;  Voltage in miliVolts
  
 Serial.print("LCD OUTPUT: ");
  Serial.print(voltage);
 delay(100);
 
  if(voltage<50)
  {
    UVIndex = "0";
  }else if (voltage>50 && voltage<=250)
  {
    UVIndex = "0";
  }else if (voltage>250 && voltage<=350)
  {
    UVIndex = "1";
  }
  else if (voltage>350 && voltage<=400)
  {
    UVIndex = "2";
  }else if (voltage>400 && voltage<=500)
  {
    UVIndex = "3";
  }
  else if (voltage>500 && voltage<=600)
  {
    UVIndex = "4";
  }else if (voltage>600 && voltage<=700)
  {
    UVIndex = "5";
  }else if (voltage>700 && voltage<=800)
  {
    UVIndex = "6";
  }else if (voltage>800 && voltage<=900)
  {
    UVIndex = "7";
  }
  else if (voltage>900 && voltage<=1000)
  {
    UVIndex = "8";
  }
  else if (voltage>1000 && voltage<=1100)
  {
    UVIndex = "9";
  }
  else if (voltage>1100 && voltage<=1200)
  {
    UVIndex = "10";
  }else if (voltage>1200)
  {
    UVIndex = "11";
  }
  return UVIndex;
}
1

There are 1 answers

0
Yukonelius On

So I got the correct output to work... Not sure what clicked, and I thought that I was doing this already but here is the part of the code that finally got what I wanted to be output correctly.

It was changing voltage to outputVoltage (since thats the value I wanted) but not sure what it worked.

  int voltage = outputVoltage; 
  
 Serial.print("LCD OUTPUT: ");
  Serial.print(outputVoltage);
 delay(100);
 
  if(outputVoltage<1.0)
  {
    UVIndex = "0";
  }else if (outputVoltage>1.0 && outputVoltage<=1.015)
  {
    UVIndex = "0.0";
  }else if (outputVoltage>1.015 && outputVoltage<=1.03)
  {
    UVIndex = "0.1";
  }
  else if (outputVoltage>1.03 && outputVoltage<=1.045)
  {
    UVIndex = "0.2";
  }else if (outputVoltage>1.045 && outputVoltage<=1.06)
  {```

as you may notice in the new code, I want to be able to output to the LCD from 0.0 to 10 in increments of 0.1.  Is there a (relatively) easy (For a beginner) to sort of use a lookup table or something?  The datasheet for the sensor shows a linear relationship between voltage and the UVI I am trying to output.  

Thanks