LDR (Light Dependent Resistor

For Flowcode users to discuss projects, flowcharts, and any other issues related to Flowcode 7.

Moderator: Benj

Frank607
Posts: 192
Joined: Mon Mar 04, 2013 8:07 pm
Has thanked: 29 times
Been thanked: 15 times
Contact:

Re: LDR (Light Dependent Resistor

Post by Frank607 »

Hi Martin,

Thanks for you input. But,… I don’t understand your program. Sorry.

I have 8 LEDs connected to PORTC
I have 8 Flash Patterns (8 Macros)
I have a DIP Switch with 8 positions on PORTA with 10K Pull Down resistors

This program work fine. The only thing that I want to have is an LDR which is controlling the brightness of the LEDs in all Macros. In other words; the flash pattern Macros must look to the LDR all the time to determine the brightness of the LEDs.

I have an LDR connected to B5 (An13) 5V and a 10K resistor to GND.

So, I have uploaded a simple version of the original program to show what I have. (see above, LDR TEST - 01.fcfx) In this program must be an LDR.

Frank

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: LDR (Light Dependent Resistor

Post by medelec35 »

Hi Frank,
Frank607 wrote:I don’t understand your program. Sorry.

I have 8 LEDs connected to PORTC
I have 8 Flash Patterns (8 Macros)
I have a DIP Switch with 8 positions on PORTA with 10K Pull Down resistors
The flowchart I looked at called LDR TEST -01.fcfx showed:
LDR_TEST_01.png
(26.83 KiB) Downloaded 1494 times
Two patterns with only 4LEDs flashing, so that is what I did.
One pattern flashes every second and the other every 50ms.
Does the brightness of LED vary depending on light falling on the LDR?

If you want more patterns I need to know what they are so can implement them.
Martin

Frank607
Posts: 192
Joined: Mon Mar 04, 2013 8:07 pm
Has thanked: 29 times
Been thanked: 15 times
Contact:

Re: LDR (Light Dependent Resistor

Post by Frank607 »

medelec35 wrote:Hi Frank,

Does the brightness of LED vary depending on light falling on the LDR?
Yes, at daylight the LEDs must have a full brightness of 100%. When it gets evening to night the LEDs must dimming to 70% or 60%.

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: LDR (Light Dependent Resistor

Post by medelec35 »

Hi frank, I was referring to your actual hardware using the flowchart I sent you.
When you shade the LDR did the LED's dim?
I can modify flowchart so duty drops to 60% in the shade.
Martin

Frank607
Posts: 192
Joined: Mon Mar 04, 2013 8:07 pm
Has thanked: 29 times
Been thanked: 15 times
Contact:

Re: LDR (Light Dependent Resistor

Post by Frank607 »

Hi Martin,

Your program works now on my hardware. The LEDs are dimming when I shade the LDR.
Only the LEDs are flashing very fast. Where can I change this? This is not clear to me.

PWM is new to me so I thought that it would be easy to dims the LEDS with a “central PWM Macro”
I have made some programs in FlowCode to see what PWM does and it works great. Slowly I understand PWM.

If I could use PWM for my program It looks also that I must have a PIC with at least 8 PWM outputs. This PIC16F1788 does have 3 (or 4?) PWM outputs. But that is not a big problem I can change from PIC.

So why is this test program not working in hardware?
LDR Test - pwm plus led 01 - led array.fcfx
(14.69 KiB) Downloaded 281 times
Frank

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: LDR (Light Dependent Resistor

Post by medelec35 »

Frank607 wrote:Only the LEDs are flashing very fast.
That will be pattern 2 (when A0 = high) as you have got a 50ms delay which is very fast!
To change to pattern 1 = 1 sec delay, A0 must be low.

If you look in main you will see

Code: Select all

MilliDividedby10 = 500 //50ms
for when A0 is high.
The value is for ms/10 so 500 is for 50ms delay and

Code: Select all

 MilliDividedby10 = 10000
is for a 1sec delay.
You can use the switch component for different values depending value of PORTA.
Using my method is the easiest way to control all 8 LEDs independently.
Frank607 wrote:So why is this test program not working in hardware?
If you select View chip, you will see CCP1 (PWM1 )on pin 13 & CCP2(PWM2) on pin 12.
These are the only two pins that will have a controllable PWM available.
Therefore, you will only able to control brightness of an LED connected to RC1 or RC2 only.
If you want to use PWM and not my method (which will dim LEDs on any pin) then you will need to connect pin 13 to a bipolar NPN transistor, via a current limiting resistor.
Then connect all LEDs anodes to +5V via current limiting resistors & connect All LEDs cathodes to the collector of the transistor.
Connect the emitter of transistor to 0V.
Instead of bipolar transistor you could use a logic level N-type MOSFET.

Alternatively there maybe a way of using just 1 PWM module and being able to control each LED independently so you can have 1 to 8 enabled.
It gets more complex.
See schematic:
LDR Bright Control1.png
(41.8 KiB) Downloaded 1478 times
Note Left inputs off for clarity.
With this method the LED is turned off by outputting a low (e.g RC0 = Low for LED1), or LED is enabled by turning the output to an input(e.g RC0 = input for LED1)
Martin

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: LDR (Light Dependent Resistor

Post by medelec35 »

Hi Frank,
Looking at schematic you PM'd me,
The only way you can control LEDs individually is by bit banging PWM like I did with LDR TEST - 02.fcfx
That means you can't use PWM module.
Only way you can use PWM module is by connecting it to control all LEDs at the same time or wire up the same as LDR Bright Control1.png but using MOSFETS.
It's your call.
Martin

Frank607
Posts: 192
Joined: Mon Mar 04, 2013 8:07 pm
Has thanked: 29 times
Been thanked: 15 times
Contact:

Re: LDR (Light Dependent Resistor

Post by Frank607 »

Hi Martin,

Please, can you show me how to implement your program “LDR TEST - 02.fcfx” into my program?

I have been trying for one and half day but maybe I don’t understand your program.

Btw, to use PWM on all 8 outputs does not work. It stops by adding output 3. With 1 output it works great but in my opinion it is also a short on the PWM pin.
Original - 0.3.fcfx
(14.08 KiB) Downloaded 276 times
Many thanks,

Frank

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: LDR (Light Dependent Resistor

Post by medelec35 »

Frank607 wrote:Please, can you show me how to implement your program “LDR TEST - 02.fcfx” into my program?
When I get some time free I will help you with that.
Frank607 wrote:Btw, to use PWM on all 8 outputs does not work. It stops by adding output 3.
You should be able to have PWM on all outputs if using bitbang method.
For all 8 outputs what flowchart did you use and what was all the connections to the microcontroller?
Martin

Frank607
Posts: 192
Joined: Mon Mar 04, 2013 8:07 pm
Has thanked: 29 times
Been thanked: 15 times
Contact:

Re: LDR (Light Dependent Resistor

Post by Frank607 »

Hi Martin,

Thanks, I will wait for it.

I have made it on a breadboard based on your “LDR Bright Control1.png”. I have used logic level NPN mosfets.

This example shows 1 mosfet. When adding mosfet 3 it looks like the PWM1 pin can’t handle it anymore and PWM1 stops working.(The 0R resistor is just in the schematic to separate nets)
Parts.jpg
Parts.jpg (16.19 KiB) Viewed 14837 times
In this flowchart I’m just testing the PWM part. The nicest thing is that I can give a certain Brightness level and not goes lower than that I also tried it with a TC4427 (mosfet driver) in the PWM1 line but no luck.

Frank

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: LDR (Light Dependent Resistor

Post by medelec35 »

Hi Frank,
The resistance values are wrong, look at the circuit I uploaded.
That's why is not working for you.
The resistance in my circuit between PWM and each channel I have as 3K3.
You have got it as 0R i.e a short.
You will damage the microcontroller as PWM is and output and CH1 is also an output.
When CH1 for example is low (shorted to 0V) , it will disabled the LED, so even if PWM output is high (Shored to 5V).
You are effectively shorting the PWM output!
Instead of NPN bipolars you are using N channel MOSFETS.
I have modified flowchart based on you original based on Original - 0.3.fcfx but with dimming all of portC via bitbang method.
Hopefully it will work for you as rushed it a bit.
Attachments
LDR TEST - 03.fcfx
(14.89 KiB) Downloaded 266 times
Martin

Frank607
Posts: 192
Joined: Mon Mar 04, 2013 8:07 pm
Has thanked: 29 times
Been thanked: 15 times
Contact:

Re: LDR (Light Dependent Resistor

Post by Frank607 »

When I use a 3K3 resistor (in R25) between the PWM1 line and the mosfet the mosfet does not dim anymore.
I use “TEST PWM PIC16F1788 - 01 - LDR Treshold in Macro.fcfx”

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: LDR (Light Dependent Resistor

Post by medelec35 »

When using PWM component, the channel that is connected to each pin needs to disable only.
Therefore the LED should be off.
So when the LED is to be on the channel needs to be turned to an input.
Then the PWM is allowed to control the LED.
Therefore you have to alter flowchart accordingly.
If you are struggling with that I can modify when get a chance.
Martin

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: LDR (Light Dependent Resistor

Post by medelec35 »

Hi Frank,
Try the attached Flowchart.
It's using one PWM component which of course is common to all channels.
I have set the min PWM to 60%
Port (0 to 7) is used for each channel.
Don't forget R25 must be at least 3K3 for each channel.
If it does not work for you, then I will try on hardware when I get a chance.
Attachments
LDR TEST - 4 PWM component.fcfx
(17.05 KiB) Downloaded 196 times
Martin

Frank607
Posts: 192
Joined: Mon Mar 04, 2013 8:07 pm
Has thanked: 29 times
Been thanked: 15 times
Contact:

Re: LDR (Light Dependent Resistor

Post by Frank607 »

Martin,

Thanks !

Last Saturday I have tested your “LDR TEST - 03.fcfx” and It works as you have designed.
I have made a video from it: (on my garbage / test channel on YouTube)
https://youtu.be/wz4fTK4Omy0

I will try your “LDR TEST - 4 PWM component.fcfx” tomorrow. And let you know.

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: LDR (Light Dependent Resistor

Post by medelec35 »

Hi Frank, that does look good.
Thank you for sharing it.
The PWM lower and upper amounts can be precisely controlled if required.
Fingers crossed for the latest version, although not strictly required as bitbang method works.
Martin

Frank607
Posts: 192
Joined: Mon Mar 04, 2013 8:07 pm
Has thanked: 29 times
Been thanked: 15 times
Contact:

Re: LDR (Light Dependent Resistor

Post by Frank607 »

Hi Martin,

The program “LDR TEST - 4 PWM component.fcfx” works on all the 6 outputs including 3K3 resistors on the PWM line :-)

But when I dim the LDR the LEDs starts to flicker. On the 5mm LEDs you can’t barely see it but the bigger LEDs clearly flicker. They do this all the time. In 100% brightness and on 60% brightness.

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: LDR (Light Dependent Resistor

Post by medelec35 »

Can you disconnect a channel and see if LED connected to the channel flickers?
The only thing I can suggest is short out the resistor from channel to gate.
Make sure the resistor between PWM output and channel is high enough to prevent excessive current. 2K2 to 4K7?
Are you able to scope the voltage across gate & GND and across LED that's flickering?
Martin

Frank607
Posts: 192
Joined: Mon Mar 04, 2013 8:07 pm
Has thanked: 29 times
Been thanked: 15 times
Contact:

Re: LDR (Light Dependent Resistor

Post by Frank607 »

I have done some measurements and came to the following conclusion. (Measured with a SDM 3055)

The power lines have thick wires, not breadboard wires.
I have only test it with 1 channel.

I need in daylight 12V on the LED Lamp. If you look to Table 3 than the LED Lamp gets only 10V because the Gate from the mosfet gets 3.7V from the PIC. For me this is strange because the PIC runs on 5V. But that is something to me to find out why.

Table 1 and 2 shows the program “LDR TEST - 4 PWM component.fcfx”

To be sure: I have used in all measurements an old school 35m photo film box and a led torch.

(The colors in Table 1 and 2 are a reference.)
Measurement results.jpg
Measurement results.jpg (65.81 KiB) Viewed 14782 times

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: LDR (Light Dependent Resistor

Post by medelec35 »

Hi Frank,
You not got a scope or logic analyser?
You are measuring the average voltage, wharas im intreasted in the whole of on/off waveform.
You need to remember that when the LED is on, it is actually being pulsed as a frequency of 1KHz.
Are the MOSFETS logic level types?
There will be a drop in voltage at the gate due to 27K Resistor connected to GND (along with 3K3 connected to PWM output) and also the output is a PWM with a variable duty.
Martin

Frank607
Posts: 192
Joined: Mon Mar 04, 2013 8:07 pm
Has thanked: 29 times
Been thanked: 15 times
Contact:

Re: LDR (Light Dependent Resistor

Post by Frank607 »

Hi Martin,

I’m working on it. But I have also to do other things in my job.

But in the meantime I have built the mosfet part on the separate breadboard on a new better breadboard. Suddenly two mosfets where dead because of a short in the old separate breadboard. (see YouTube)

Your “LDR TEST - 4 PWM component.fcfx” is now running very well in test with normal 5mm LEDs.

I have made new PCBs and they have the possibility to change to your option or to a standard option with no dimming. This PCB have also a connector for measuring points.

I will wait when the PCBs will arrive and then I will be back here.

Thanks !!

Frank

Frank607
Posts: 192
Joined: Mon Mar 04, 2013 8:07 pm
Has thanked: 29 times
Been thanked: 15 times
Contact:

Re: LDR (Light Dependent Resistor

Post by Frank607 »

Hi,

(I have FlowCode 8 )

I will come back to this topic. I've been helped a lot but it still doesn't work properly.

This concerns four Road Safety Lights (LED 200mm) This lamps are full of lenses, so every flicker is verry noticeable.
When it starts to get dark, the lamps flicker when they are dimmed by the LDR. The flickering is minimal but very noticeable.

Each LED lamp has its own MOSFET (Logic Level IRLZ34N) The circuit diagram is simple:
ON: LED 1 and 2: 1 second
OFF: half second
ON: LED 3 and 4: 1 second
OFF: half second
Repeat

The LDR is an NORPS-12 with a 10K resistor.

What could this be due to? Maybe it should be completely different with different hardware (CPU and more). I've been working on this for a while now but can't figure it out.

I hope there is still someone who can help me with this.

----------
Maybe is not an good idea to use for this project PWM. Maybe it is better to do something voltage 12V to 10V or something? Just my thinking.

I see for example this video n YouTube with an Arduino:
https://www.youtube.com/watch?v=0p1B74TosTs
So, in my eyes it must be simpel to do. I could be totaly wrong on this !
---------

I have tried something else but I don't understand the "Light Sensor component" and the settings. Maybe I'm thinking to simple.
Test - 01.fcfx
(12.88 KiB) Downloaded 102 times
Current situation including Martin's LDR TEST - 4 PWM component.fcfx:
2023-07-09_10-54-00.jpg
2023-07-09_10-54-00.jpg (97.37 KiB) Viewed 7732 times
2023-07-09_10-40-33.jpg
2023-07-09_10-40-33.jpg (101.26 KiB) Viewed 7732 times

chipfryer27
Valued Contributor
Valued Contributor
Posts: 617
Joined: Fri Jun 06, 2014 3:53 pm
Has thanked: 184 times
Been thanked: 195 times
Contact:

Re: LDR (Light Dependent Resistor

Post by chipfryer27 »

Hi

In your attached file "Test" you set the LDR to "average" the readings but only set it to 1. If you set to 10 (for example) it will take ten readings and return the average of such. The more you average the more stable readings will appear (generally speaking).

However although you set this you don't do anything further with it.

In your loop you have your On time at 1 second but your Off time at 50mS. That should perhaps be 500mS and is an easy typo to make. I've done similar numerous times.

Regards

Frank607
Posts: 192
Joined: Mon Mar 04, 2013 8:07 pm
Has thanked: 29 times
Been thanked: 15 times
Contact:

Re: LDR (Light Dependent Resistor

Post by Frank607 »

Thanks for answering me. I will write tomorrow an answer. I hope you will take a look to it.

(I have Flowcode V8, so maybe this topic can move to the V8 section)

chipfryer27
Valued Contributor
Valued Contributor
Posts: 617
Joined: Fri Jun 06, 2014 3:53 pm
Has thanked: 184 times
Been thanked: 195 times
Contact:

Re: LDR (Light Dependent Resistor

Post by chipfryer27 »

Hi

I'll certainly help where I can and from your post it doesn't look too difficult really.

There were a few other issues in the chart but as I thought it was "just a test" I didn't address, such as clock settings etc.

It would be better if you did create a new post in the v8 forum if you are using v8.

Regards

Post Reply