How to differentiate Led brightness between stages (fade in/out , slow blinking and fast blinking)

Hi all,
l have led that show 4 stages and l do not have direct access to led.

  • Fade in/out- the brightness of light go up and down
  • Slow blinking-1hz
  • Fast blinking -4hz
  • Solid on - led on
    l have used a color sensor or photoresistor for getting data from led.
    l want to know how can l differentiate the stages in between? It is a little bit difficult to find a threshold between fade in/ out and slow blinking for differentiating. The values of brightness or color intensity are the same level.
    l want to make a live test and check these stages and identify how long these stages continue.
    l would be happy if someone can share his/her ideas about this task. if you know the topic name related to this task please share it with me.

photoresistor code :

void setup() {
Serial.begin(9600);
}
//Then, we read the analog value coming from photoresistor and we define it as " value " :

void loop() {
int value = analogRead(A0);
//And we write the value on the serial monitor :

//Serial.println("Analog Value :",value);
Serial.print(value);

Hello mengcp
Post your sketch, well formated, with comments and in so called code tags "</>" and schematic to see how we can help.
Have a nice day and enjoy coding in C++.

1 Like

I would try to use a photodiode instead of a photoresistor. As a diode is quicker than a LDR when going from illuminated to dark again, this should allow to measure the pulse timing to differentiate fade in/out and the blink frequencies ...

I have discriminated slow blinking (about 1 or 2Hz) from LED off and LED on
by LDR to monitor the telephone LED but that does quite likely not solve the fading task.

Fading LEDs is normally done using pulse width modulation (PWM). Often you can detect this by eye, especially in a dark room, by scanning your eyes around the room but keeping the led in view. With PWM, your eye will see a series of dots or dashes. Is that true for the led you want to detect?

If so, fading is just another frequency of flashing, but a frequency too high for the eye to see when looking straight at the led.
Generally the frequency is several hundred Hz. That maybe too fast also for an LDR, I'm not sure. But as suggested, a photo-diode or photo-transistor will be fast enough to detect these flashes easily.

So maybe you could use pulseIn() function to measure the length of flashes. If the length is only a few milliseconds, the led is fading. If the pulses are around 250ms or 1000ms, then the led is flashing.

1 Like

If pulseIn() returns 0 (timeout) the LED is either full on or full off. Check the state of the sensor to tell which.

2 Likes

Yes, that is what I recon: LDR are quicker (10 msec) when going from darkness to illumination, but recover with (I read) about 200 kOhm/sec.... This does not fit for PWM.

This is what wikipedia says:

Photoresistors also exhibit a certain degree of latency between exposure to light and the subsequent decrease in resistance, usually around 10 milliseconds. The lag time when going from lit to dark environments is even greater, often as long as one second. This property makes them unsuitable for sensing rapidly flashing lights, but is sometimes used to smooth the response of audio signal compression.

see here

https://en.wikipedia.org/wiki/Photoresistor

1 Like

Hmm, photo-transistors no good either then. That leaves us with photodiodes I suppose. Something must be fast enough. IR receivers are sensitive to 30~50KHz for example. And what do they use to receive multi-gigahertz signals from optical fibres?

1 Like

I second that. If you read

https://en.wikipedia.org/wiki/Photodiode

the article also mentions phototransistors but says [in comparison to photodiodes]:

Phototransistors also have significantly longer response times.

1 Like

yes, l can see the fade in /out with eyes. These stages happened sequentially. l want to differentiate when the stages start and finish. l can collect data with the sensor now l want to differentiate the stages.
l have used color sensor the data range is same with the graph above. l think this data will be the same even if l use another sensor. l think due to data l should use some methods to differentiate them. currently, l am checking the topic of regular and irregular time series data.

I understand now that the graph in post # 1 is representing the data that you measured with the photoresistor. Is that correct?

1 Like

yes, l have measured with photoresisitor

Sorry I missed that important point ...

Could you give some additional information regarding the graph? What LED state produces the data

  • 22:31:04 (is the time line here correct?)
  • 22:31:06 to 22:31:50
  • 22:31:51 to 22:32:06
  • 22:32:08 to 22:32:18
  • 22:32:18 to 22:32:29
    and so on?

Are the stages repeating from 22:32:30?

fade in/out
slow blinking
fast blinking
solid on
then repeat again

You may try these algorithms to check if they can solve at least some of your requirements:

#include "Streaming.h"

const int DeltaThreshold = 200;
const int MaxThreshold = 350;
const int MinThreshold =  20;

int data[10 * 10]; // 10 data per second for 10 seconds
int dataLength = sizeof(data) / sizeof(data[0]);

void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  CreateData(8, 5, 405);
  PrintData();
  EvaluateDataChanges();
  EvaluateTimeBetweenMaxMins();
}

void loop() {
  // put your main code here, to run repeatedly:

}

void PrintData() {
  for (int i = 0; i < dataLength; i++) {
    if (i % 10 == 0) Serial << "\n";
    Serial << data[i] << "\t";
  }
  Serial << "\n\n";
}

void EvaluateDataChanges() {
  int Count   = 0;
  int CountChanges = 0;
  for (int i = 0; i < dataLength - 1; i++) {
    if (abs(data[i] - data[i + 1]) > DeltaThreshold) CountChanges++;
  }
  Serial << "No of Data Changes = " << CountChanges << "\n";
  if (CountChanges == 0) {
    if (data[0] > MaxThreshold ) Serial << "Data are HIGH";
    if (data[0] < MinThreshold ) Serial << "Data are LOW";
  }
  Serial << "\n";
}

void EvaluateTimeBetweenMaxMins() {
  int lastMaxPos         = -1;
  int lastMinPos         = -1;
  for (int i = 1; i < dataLength - 1; i++) {
    if (abs(data[i] - data[i + 1]) > DeltaThreshold) {
      if (data[i] > MaxThreshold) {
        if (lastMaxPos >= 0) Serial << "MaxDiff = " << i - lastMaxPos << "\n";
        lastMaxPos = i;
      }
      if (data[i] < MinThreshold) {
        if (lastMinPos >= 0) Serial << "MinDiff = " << i - lastMinPos << "\n";;
        lastMinPos = i;
      }
    }
  }
  Serial << "\n";
}



boolean CreateData(int Periode, int Min, int Max) { // Periode = 10 -> 1 Hz Periode
  // assuming a measurement every 100 msec
  int count = Periode / 2;
  boolean writeMin = true;
  for (int i = 0; i < dataLength; i++) {
    count--;
    if (count <= 0) {
      count = Periode / 2;
      writeMin = !writeMin;
    }
    data[i] = writeMin ? Min + random(10) : Max + random(10);
  }
}

I have programmed a simple routine to create some test data (which might not really cover your raw data!). You can play around with this routine to create different kinds of "wave forms" ...

CreateData(8, 5, 405);

The first parameter gives a periode of the same number of highs and lows in 100 msecs (so 10 means 500 msec high, 500 msec low). The second is the low value , the third is the max value (which both get a random(10) added).

The data are stored in an array of 10 data per second for 10 seconds (10x10 = 100 data).

EvaluateDataChanges()

Counts the numbers of "edges" which are defined by a difference between two adjacent data of more than DeltaThreshold.

if no changes are detected, it returns whether data[0] is higher than MaxThreshold or lower than MinThreshold (should equal LED constantly on or off).

EvaluateTimeBetweenMaxMins()

Returns the difference between edges high to high and low to low. which is a measure for slow or quick blinking.

It works with artificial data and is a "quick shot", success or not also depends on your raw data. At least I hope you get the idea and may develop further from here ...

If you still have problems, feel free to post your raw data.

I created and checked the test sketch here:

https://wokwi.com/projects/326101695584010835

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.