LDR average of the last 20ms

Hey guys,

I am using a white LED in combination with a regular LDR to have some kind of a simple photoelectric barrier.
the device is also connected to a stepper and a servo. So from time to time it seems to be that the LED which is used for the photoelectric barrier does some kind of flickering. So the barrier gets activated by accident what should not happen.
Can you show me how to change a simple code of the LDR to get the average of the last 10ms or 20ms (or what ever makes sense; I do not know at wich frequence the LDR gets his readings) to make the decision if there is an object moving into the barrier or not.

int LDRValue;
 
void setup()
{
  Serial.begin(9600); // starts the serial port at 9600
}
 
void loop()
{
  LDRValue = analogRead(A3); // read analog input pin 0
  Serial.print(LDRValue); // prints the value read
  Serial.print(" \n"); // prints a space between the numbers
  }

Here is a simple code part which I am using.

Thank you very much.

You're seeking to implement a software fix for a hardware fault?
Why not isolate and remedy the hardware fault?

LDR's are very slow and leds flicker if you give them unsteady power.
A phototransistor is magnitudes faster and more sensitive.

If the light detector has wide view, many light sources can affect it.
My guess is that you're getting unexpected interference.

The view of the sensor should define your "beam".
You don't need a led at the far end, a brightly illuminated wall will do.

LEDs shouldn't "flicker". It's so weird, that it's almost difficult to make them do it, if you wanted to make them flicker. So that should be your starting point.

If you post schematics and context for the LED circuit, I'm sure that we can help you eliminate that.

ok guys,

thank you very much for your responses. The project is working now. The flickering was not the reason for the problem.
But indeed, if the SG90 servo is moving, the LED at GPIO23 on the Wemos Lolin32 board is flickering.
And by the way, the servo does not move smoothly. I don't know why. I have to do some researches on it.
Thanks again.

What servo? Oh, that servo…

How are you providing power to the servo?

a7

Lack of power for the servo causes led V to dip.

It's a SG90. Yes, I think that's the reason why the LED is flickering during it's movement. It is powered by the 5V output of the board. But the server is also not moving smoothly if I use a external power source to power it. maybe it is broken? I don't know.

The servo power should not go through the A) board that has 200mA max total or B) I/O pin that should not source or sink more than 30mA.

What to do is get a good power source for the servo at voltage the servo is rated for and wire that power to the servo with a high efficiency power transistor, a logic-level MOSFET as the switch flipped by an I/O pin and 220 ohm resistor.

The transistor is fast enough for PWM and normally can switch over 24VDC.
What voltage does the servo want?

hi,

thanks a lot. is there an example somewhere in the internet? I found examples where this SG90 servo is connected directly to the board.
The servo needs voltage from 4,8-6V (datasheet). But of course, I feared that the current maybe to low.

My power source is a 2 amps 5V power adapter. I am still using a step up converter to power the stepper with 12 volts.
I tried to connect the servo directly to the power source but it does not change anything. it is still vibrating and shaking while moving.

I would really appreciate if you could show me a solution of that. :pensive:

That servo takes 4.8V to 6V. 12V is way too much.

I said the power source for the servo is 5V. The power source for the stepper is 12V.

I found the reason for all the problems. The servo is crap. I replaced it with another SG90 from another manufacturer and it works like a charm.

THanks a lot!!

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