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
}
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.
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.
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?
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.