Need help with LDR

Hello everyone. I’m new to programing and I’m trying to write sketch for LDR value so I can save it to graph history. I tied few examples from arduino but without any luck. If anybody could help me I would greatly appreciate it.

Please provide more details, which hardware, what is the setup? OS version, list of examples you are trying.

Be advised that just storing values between certain periods can be erratic. The LDR is not really a pretty thing to just “use”. You may need to address debouncing and calculate average values to get nice graphs and to do certain things with it. Maybe even calculate values based on a longer period to get consistent results.

I’ve experimented with that and it added up to a lot of frustration, so I’m gonna try again someday lol.

latest blynk andoid app
arduino uno + ethernet ,
ds18b20 +LDR

heres my sketch

    #include <OneWire.h>*emphasized text*
    #include <DallasTemperature.h>
    #include <Ethernet.h>
    #include <BlynkSimpleEthernet.h>
    #include <SimpleTimer.h>
    #include <SPI.h>
    #define ONE_WIRE_BUS 7
    OneWire oneWire(ONE_WIRE_BUS);
    DallasTemperature sensors(&oneWire);
    char auth[] = "xxxxx";
    int LDR = A0;
    SimpleTimer timer;
    
    void setup()
    {
      Serial.begin(9600);
      Blynk.begin(auth);
      sensors.begin();
      pinMode(LDR, INPUT);
      timer.setInterval(1000, readTemp);
      timer.setInterval(1000, readLDR);
    }
    void readLDR()
    {
      LDR = analogRead(LDR);
      // You can send any value at any time.
      // Please don't send more that 10 values per second.
      Blynk.virtualWrite(0, LDR);
    }
    
    
    void readTemp()
    {
      sensors.requestTemperatures();
      float floatTempC = sensors.getTempCByIndex(0);
      char t_buffer[15];
      dtostrf(floatTempC, 8, 9, t_buffer);
      Blynk.virtualWrite(1, t_buffer);
    }
    void loop()
    {
      Blynk.run();
      timer.run();
    }

That looks about right. Do you get the graph in your dashboard? I’d say this should work just fine.

I have no idea how to access the history of the graph though. Maybe @Pavel should enlighten us about that :wink:

So the problem is you don’t see history in history graph? History graph is updated once in an hour. This could be a reason.

The problem is that I see some crazy values at steady light. Going up and down. Plus I tried led widget with it and it doesn’t work.

I’ll try different LDR since you guys telling me that sketch looks good. Another question. Is there a way to access history graph on computer ?

You also may just print LDR data to realtime widget like “value display” to see it is working correctly.

Not at the moment.

Could you please post a screenshot?

I;m guessing you are running into what I told. LDR’s are really weird and sensitive. You might get away with using a capacitor or adding a resistor.

But I’m figuring you need a formula to calculate a balanced average value. It could be something like:

  • Measure 7x the LDR with a 1s interval and store all these values.
  • Remove the highest and lowest values
  • Calculate the average of what’s left (a.k.a. Sum of values divided by 5).

This should get you a much more stable result.

-edit-

Damn me, I’m at work, but hey, what’s that? An Arduino!!! I made some example code for you:

int value[19];
int sizeOfArray = sizeof(value) / sizeof(int);

void setup() 
{
  Serial.begin(9600);
  pinMode(A0, INPUT);

  Serial.print("Number of measurements: ");
  Serial.println(sizeOfArray);
}

void loop() 
{
    
  for(int i=0;i<sizeOfArray;i++)
  {
    value[i] = analogRead(A0);
    delay(250);
  }
  
  int maxIndex = 0;
  int minIndex = 0;
  int max = value[maxIndex];
  int min = value[minIndex];

  for(int i=0;i<sizeOfArray;i++)
  {
     if (max<value[i])
     {
       max = value[i];
       maxIndex = i;
     }

     if (min>value[i])
     {
       min = value[i];
       minIndex = i;
     }     
  }
  
  Serial.println("==========");
  
  Serial.print("Max: " );
  Serial.println(value[maxIndex]);

  Serial.print("Min: " );
  Serial.println(value[minIndex]);

  Serial.println("==========");
  
  int totalValue = 0;
  
  for(int i=0;i<sizeOfArray;i++)
  {
    totalValue = totalValue + value[i];
  }

  Serial.print("Average is: ");
  Serial.println((totalValue - (value[minIndex] + value[maxIndex])) / (sizeOfArray - 2));
}

This should get you started. It takes measurement every X (delay paramater in first for-loop) seconds and averages it out without the minimum and maximum numbers.

-edit- for a bit better code. You can decide how much measurements to take with changing a single parameter at the top (the array initializer).

heres the screenshot . it looks like analog0 port showing stable values …

Thanx man. I’ll give it a try one day. After all this idea might be useless because history graph is storing data every hour. For example I won’t see what time there’s sunrise or sunset or is my external ds18b20 at direct sunlight or not. You get the picture. I might have to move to another website or something to do that I guess.

My personal opinion is also that there should be more ways and intervals to store history with a minimum of five minutes or so. For these kinds of applications an hour is just too big of a gap. On the other hand storing data costs money so I fo get why there is such a big interval.

In your case, could you do a webrequest to fetch sunrise/sundown from a website? Would that help you out?

1 Like

i’m not sure ,for now im trying to learn some programing and than i’ll see what i really want …

1 Like