LDR (Light Dependent Resistor -> FC V8

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

Moderator: Benj

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

LDR (Light Dependent Resistor -> FC V8

Post by Frank607 »

As a result of this thread, I'm going to ask the question here again.
viewtopic.php?f=63&t=19023

Because I have Flowcode V8 I was advised to continue posting the question here.

The LED Lamp continues to flicker while dimming with an LDR. The LED Lamp is used for road works and is equipped with numerous "lenses". This allows you to clearly see every flicker. Of course you don't see these flickers on a 5mm LED.

Four of these LED lamps are needed for this project, so also four outputs on the PCB.

I myself have the idea that a direct PWM solution will not work with this type of LED lamp.

The specs of this lamp are 12V / 940mA.

With a LAB power supply I have experimented with lowering the milliamperes and this works very well.

I looked at an LED driver and ordered it to experiment with:
https://nl.farnell.com/1793219

I have no idea if this will work. Other than that, this one is very expensive if I'm going to put four on a circuit board.

So I'm looking for another solution and as I wrote above it goes well with lowering the number of milliamperes.
Last edited by Frank607 on Wed Jul 12, 2023 10:26 am, edited 1 time in total.

User avatar
Steve
Matrix Staff
Posts: 3422
Joined: Tue Jan 03, 2006 3:59 pm
Has thanked: 114 times
Been thanked: 422 times
Contact:

Re: LDR (Light Dependent Resistor -> FC V8

Post by Steve »

You've probably tried this, but increasing the PWM frequency might be an easy way to reduce flicker.

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

Re: LDR (Light Dependent Resistor -> FC V8

Post by chipfryer27 »

Hi

Can you post your code please?

Things to keep in mind when using an LDR (assuming with another fixed value resistor forming a voltage divider) is that these have tolerances and can vary, however I would not have thought that would have made much of a difference in your application.

How are you sampling the value and how often do you read?

I would probably not be reading too many times, perhaps as little as once every minute or longer and I would average a number of readings when I did sample (maybe average 100 samples or the like). If your sensor is constantly sampling and not averaging then the value will most likely vary quite a bit. Also if a car's headlights illuminate the area briefly the value will instantly change dramatically. Similarly if a cloud or something passes over, or perhaps even a bird, this would cause a constantly sampling sensor to react, really when there is no great need to. Reading the value every minute or longer would eliminate many transients as you only care about the ambient light level. There are other "averaging" techniques you could employ too that would help filter out "fliers".

With that in mind I can recommend an Ambient Light sensor, the BH1750VFI and this component is available in FC v10. I did a couple of tests with it here.

https://www.flowcode.co.uk/forums/viewt ... f=3&t=1512

It runs over I2C so you may be able to interface with v8.

I would be tempted to break your project into two parts, sensing and output, testing each before marrying together.

Whilst testing I would include a display so that you can see the current sample value (or send the values out on UART and monitor using your PC running a terminal program). This would enable you to see how often your sample value changes and by what factor.

I would then have a range of values that you respond to. For example lets say you are sampling the LDR using a byte returning 0-255. If your output is to be 25% / 50% / 75% / 100% then I would have any sensor value up to 64 output 25%, any value between 65 and 128 output 50%, any value between 129 and 192 output 75% with anything above 193 outputting 100%. This too will help to eliminate constant change per reading. Note that you will need to collect empirical data to establish these threshold values, but you will no doubt get the idea (that sensor I mentioned outputs in Lux so you could easily obtain threshold levels to use using a cheap light meter).

As for output make sure that your supply can handle the required current and your PWM frequency values are high enough. To test I would create a simple chart that you can set at any of your values (25% - 100%) and monitor your output. Most PICs have at least two hardware PWM channels and these can be used to control your LEDs. As mentioned I'd set the PWM at a fixed value and see if the LEDs are flickering. If so I'd investigate further.

Hope this helps.

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 -> FC V8

Post by Frank607 »

My current code is written by Martin.
This is the last code I use.
LDR TEST - 4 PWM component.fcfx
(17.85 KiB) Downloaded 86 times
-
These are the associated hardware schematics
2023-07-09_10-40-33.jpg
2023-07-09_10-40-33.jpg (101.26 KiB) Viewed 83422 times
-
2023-07-09_10-54-00.jpg
2023-07-09_10-54-00.jpg (97.37 KiB) Viewed 83422 times
-
I already changed the PWM Prescaler from 16 to 1 which gives me a Frequency of 16KHz. Flickering is still visible.
-
The LDR is constantly read out. I see that when it darkens, the light immediately reacts to this. This has to change anyway and indeed maybe to one minute. Would be nice 10 seconds.

User avatar
Steve
Matrix Staff
Posts: 3422
Joined: Tue Jan 03, 2006 3:59 pm
Has thanked: 114 times
Been thanked: 422 times
Contact:

Re: LDR (Light Dependent Resistor -> FC V8

Post by Steve »

I would rule out the LDR as the source of the fluctuations and replace it with a potentiometer, just in case the LDR is not providing a constant resistance.

If the flicker still exists, then I guess it's the switching of the LEDs or their drivers that is causing the issue. Maybe consider driving the LEDs from a variable current source instead of using PWM.

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 -> FC V8

Post by Frank607 »

Steve wrote:
Wed Jul 12, 2023 12:15 pm
I would rule out the LDR as the source of the fluctuations and replace it with a potentiometer, just in case the LDR is not providing a constant resistance.

If the flicker still exists, then I guess it's the switching of the LEDs or their drivers that is causing the issue. Maybe consider driving the LEDs from a variable current source instead of using PWM.
Thank you

I just replaced the LDR with a potentiometer. The flickering is still there. So it must have something to do with PWM and these LED lamps.

I think we should just forget about the whole PWM thing. What is a good solution to do this with an adjustable power source?

Are ready-made ICs available for this purpose?

The specs of the LAMP:
12V / 940mA

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.

User avatar
Steve
Matrix Staff
Posts: 3422
Joined: Tue Jan 03, 2006 3:59 pm
Has thanked: 114 times
Been thanked: 422 times
Contact:

Re: LDR (Light Dependent Resistor -> FC V8

Post by Steve »

I got me electronics degree a long time ago and since then I've mainly been a software engineer, so I'm not in the best place to answer that. Perhaps others on the forum can.

I did have a quick search of the internet and there seem to be a lot of possibilities though.

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 -> FC V8

Post by Frank607 »

I just made a video with the LED Lamp and a LAB power supply. If I turn back the current, the lamp will burn less brightly. This without flickering.

So this could be the solution.

https://youtu.be/vrQWPdb_Jy0

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

Re: LDR (Light Dependent Resistor -> FC V8

Post by chipfryer27 »

Hi

I've just looked at the drivers from Farnel you referred to in your first post. Are these the ones you are now using with success?

They have option to control either manually or by PWM but confusingly (well at least on initial glance) it shows a PWM frequency of 200Hz but later indicates 10KHz. I've only glanced so I may have misread.

I would be very tempted to play with this feature by enabling a PWM channel then setting it to varying pulse widths either manually or stepping through at set periods (e.g. ten seconds at each level). If the LEDs flicker then time to think again.

Do you have the link for the LEDs?

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 -> FC V8

Post by Frank607 »

I've come a bit further with this LED Driver. I started from scratch with both the schematic and the flowchart.

I am now using the RCD-24-0.70 (max 700mA) for testing. The LED lamp has a specification of 12V/940mA. So far it looks perfect. No more flickering.

I used the following ports of the LED Driver:
PWM Dimming and ON/OFF Control for switching the LED Lamp on and off (CH1)
Analog Dimming Control for dimming. Via PWM -> Filter -> Analog (PWM-DIM)

I want to use the RCD-24-1.00 and it can handle up to 1000mA. But this is too much. Max 880mA control would be nice.

Can this be done through settings in the flowchart?

Btw, the LDR works the other way around. When it gets dark, the lamp will burn brighter.
LDR - RCD-24-0.70.fcfx
(11.62 KiB) Downloaded 88 times

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 -> FC V8

Post by Frank607 »

I'm also looking at the RCDE-48-1.05. It has a common PIN for “PWM DIMMING AND ON/OFF CTRL”. This is half the price of the RCD-24-1.00. Recom also suggested this to me to use.

So it would be nice to be able to use this shared PIN for brightness and on and off.

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

Re: LDR (Light Dependent Resistor -> FC V8

Post by chipfryer27 »

Hi

Glad to hear you are having success.

My impression is that the part number references the maximum current you can draw, so if your LED is 940mA then the 1000mA should be fine to use.

Do you want the lamp to be brighter when it gets dark, or dimmer when it gets dark?

I don't know details of your LDR but typically they decrease in resistance with more light. High resistance in darkness, low in bright light. I assume you have the LDR in series with a fixed value resistor and measure at the junction. You will need to establish the ratio between such.

Say the LDR had a resistance of 2M in darkness and 200 in full daylight and you had a fixed value of 1000 Ohms

If you connect +5v to LDR to resistor to ground, and measure ADC at the LDR/resistor junction then in darkness you would have V/R = 5v / 2000000+1000 = 2.5uA, which then gives IxR of 2.5uA x 1000 = 2.5mV at ADC pin. In full daylight it would be 5v / 200 +1000 = 4.1mA, which then works out at 4.1V.

If using the returned ADC to control PWM then duty would increase in relation to brightness.

If you connected +5v to resistor to LDR. then using the above you would now have an ADC value of 5v in darkness and 0.82v in bright light, meaning the PWM duty would decrease in bright light.

Hope this helps.

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 -> FC V8

Post by Frank607 »

Thank you!!

Isn't it true that in Flowcode you can reverse the reading of the LDR? Then would be nice.

The lamp must of course burn fully in daylight. When it gets dark, it should dim. From 100% to about 60%.

The LED Driver RCD-24-1.00 goes up to max 1050mA so really too much.
EDIT: 1000mA

PWM should be adjustable to approximately 85% of Duty Cycle. I don't know how to do this.

You also received a schematic as it is now on my breadboard.

Thanks for helping me ! :D
Last edited by Frank607 on Sun Jul 16, 2023 10:55 am, edited 1 time in total.

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

Re: LDR (Light Dependent Resistor -> FC V8

Post by chipfryer27 »

Hi

The PWM part is really not too difficult and from the chart you attached you seem to be able to use the PWM component..

Do you have the data sheets for the LEDs?

Using an LDR is doable but you will have to gather a lot of data to establish threshold values and ensure your LDR/Fixed Resistor combination give you reliable readings throughout the intended "daylight" range.

How bright is full daylight to you? You may think that each day at 10am is much the same but the LDR won't, therefore you will need to establish ADC values for Dim, Bright and inbetween.

Have you considered upgrading to Flowcode v10? All components and many "chips" are free to use, and it has a BH1750 ambient light sensor, which returns the ambient light level in Lux. You could then use a simple Lux meter to establish "brightness" levels and then enter these values within your chart as thresholds.

Anyway, irrespective of method, you will obtain a reading that you will use to alter the duty cycle. Depending on range, you may need to use the "Map" component. Say your sensor outputs 614 to 1023 through the range you are interested in (10-bit ADC). With the Map component this can be transposed to a byte value, 0 to 255 (or whatever you wish). 614 would equal 60% with 1023 equalling 100%. Map would transpose to set Duty Cycle to 153 (=60%) to 255 (=100%).

As mentioned before, I'd probably only take readings every minute or so, which would help avoid transient levels being reflected in the brightness.

Hope this helps a bit.

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 -> FC V8

Post by Frank607 »

Hi chip fryer27,

Thanks for your reply. The reading of the LDR will indeed be once every 2 minutes in a later flowchart. Constant reading does not make sense and will certainly affect the entire operation.

I also understand your thoughts about daylight, clouds and night. But this is also not very close.

I'm going to use the RCD-24-1.00. According to the datasheet it can go from 0 to 1000mA.

The manufacturer of the lamp specifies: 12v / 940mA. If I connect the lamp to my LAB power supply and set it to 12V, a current of 880mA flows.

The lamp must be dimmed between 100% during the day and about 60% when it is dark. This is not very close and will have to be proven experimentally. But I will have to set this somewhere in the flowchart.

Suppose I want to run the lamp at 900mA. Then I should use max 90% Duty Cycle and set it in the PWM Component. Never higher than 90%. Is this possible ?

Can you make an example of the map component in my flowchart I posted above?

That would help me a lot on my way.

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

Re: LDR (Light Dependent Resistor -> FC V8

Post by chipfryer27 »

Hi

The driver won't supply more power than the component can draw, and your specs are usually either Maximum or Typical values. Your Lab supply value is within about 5% or so of spec so it's most likely fine.

What will be your target chip and ADC resolution?

I'm sure I could knock up a quick example to give you a guide, but you will need to establish best value of fixed resistor (use a 1% if available) and subsequent values for day/night.

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 -> FC V8

Post by Frank607 »

The target chip is an: PIC16F18346
ADC is 10Bit

The LDR is an NORPS-12 - https://www.farnell.com/datasheets/3759787.pdf

You have my schematic and the flowchart is above in this topic.

Thanks !

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

Re: LDR (Light Dependent Resistor -> FC V8

Post by chipfryer27 »

Hi

I've noticed a couple of issues with v8.

The "Map" function I spoke of is not (yet) an official component. However I believe it was Medelec35 that created it and he made it available in either the v7 or v8 forum. You will need to do a search of the forum to find the component then download it etc.

Next, there seems to be a bug of sorts with the PWM component. You can either SetDutyCycle or SetDutyCycle10Bit to give a range of 0 to either 255 or 1023 (0 - 100%)

If you use the 10Bit option you only use a range of 0 to 486 (this equates to 0-100%) I've no idea why. Note this is in simulation, I don't have your chip to see what's going on in Hardware.

Once I'd discovered that little jewel, because I was using the Map component I could set the Map component for an input of 0 - 1023 to give an output of 243 to 486 (roughly 60 -100%).

As I moved the Slider, irrespective of how low the ADC read the PWM output was never lower than ~60% and increased accordingly to give 100%.

The chart is very basic, just to show using the Slider/ADC mapping to the PWM. If you find the Map component and install I'll post the chart. Do note that I used the full range of ADC, you will need to establish your own ADC values based on ambient light levels. It's unlikely your LDR/Resistor combination will give an ADC range of 0-1023 though.

Regards

Edit...
I'd forgotten that I'd adjusted postscaler / period overflow to give me a specific frequency, so my comment re potential bug is solved... :)

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 -> FC V8

Post by Frank607 »

@chipfryer27:
I'm working on it. Please be patient ! :-)

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

Re: LDR (Light Dependent Resistor -> FC V8

Post by chipfryer27 »

Hi

I'm sure you are.

I'm quite interested to know how you get on with the drivers as they look quite capable.

Regards

PS
The "map"component isn't strictly necessary as you can always just make a decision. If light level is between x and y, then PWM equals z and so forth,

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 -> FC V8

Post by Frank607 »

Hi chip fryer27,

I'm sorry I didn't respond, but I've started all over again. I hope you are still present in this forum. The LED Drivers work but not well enough whether it is 700mAor 1000mA. Both are not good. Technically this works well, but this is a very expensive solution.

I therefore started again with the IRLZ34N Logic Level Mosfet. This should also be flicker free.

Just to be clear:

I have four LED lamps that have to dim via an LDR when it gets dark. These must also turn on and off at a certain frequency. The dimming must be from 100% to about 60%.

Attached is a basic schematic of the hardware I have now and a piece of flowchart that I use as a base. The lamps do not switch on and off yet and that is where the problem lies. How do I do this?

The LDR does work if I connect the lamp to the PWM output.

I don't understand how to dim the four outputs to the lamps via an LDR and PWM. It seems very simple… 

How can I have the readout from the LDR go to the lamps? I must be able to say somewhere in the flowchart to the LED lamp; Ok, you go on and off and with the value that the LDR gives.

Please help me!
2023-07-23_17-38-59.jpg
2023-07-23_17-38-59.jpg (107.42 KiB) Viewed 83148 times
LDR - IRLZ34N - ON-OFF.fcfx
(10.86 KiB) Downloaded 83 times

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

Re: LDR (Light Dependent Resistor -> FC V8

Post by chipfryer27 »

Hi

I assume that when C0 is on you want the PWM output to be active at whatever Duty Cycle, and when C0 is off you want the PWM disabled.

One way to do that is to just set the Duty Cycle to zero straight after making C0 go off.

LDR - IRLZ34N - ON-OFF_disable.fcfx
(10.86 KiB) Downloaded 79 times

In you chart you are using the potentiometer to create the value for the Duty Cycle. This works, so just replace this with your LDR.

Most PICs have at least two PWMs so enable these to operate at whatever frequency. The output pin(s) can feed more than one FET so you could have PWM1 feeding two FETS, powering LED 1 & 2, and PWM2 feeding two FETs powering LED 3 & 4.

Whatever happens on PWM1 will now control LED 1 & 2
Whatever happens on PWM2 will now control LED 3 & 4

To flash alternatively, say for one second you could

Set Duty Cycle of PWM1 to ADCvalue and PWM2 to zero
Wait one second
Set Duty Cycle of PWM2 to ADCvalue and PWM1 to zero
Wait one second
Repeat
Every minute or so update the ADCvalue based on LDR.

Alternatively just have a PWM operate the FETs, but have another FET switch the actual 12v feed to LED string 1&2, and another for string 3&4. These can simply be controlled by any free pins.

Sorry to be brief,

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 -> FC V8

Post by Frank607 »

Thanks. Great information !

I don't see any changes in the posted flowchart.

I have it working now with one mosfet and via this way:
PWM1 and the mosfet ( -> LED Lamp ) are now both on C0)

Set Duty Cycle of PWM1 to ADCvalue
Wait one second
Set Duty Cycle of PWM1 to zero
Wait one second
Repeat

This now works very nicely and well.

I am using the PIC16F18346 (20-PIN). In Flowcode I see there are 4 possibilities to use for PWM. Channel 1 to 4. In the datasheet of this PIC I also see four CCP channels. Can I use all four for PWM 1 to 4?

The other option is a good one. Use a second mosfet to drive the 12V. I'm going to take a look at this too.

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

Re: LDR (Light Dependent Resistor -> FC V8

Post by chipfryer27 »

Hi

Oops... See attached :)
LDR - IRLZ34N - ON-OFF_disable.fcfx
(11.05 KiB) Downloaded 81 times
The new icon is after you switch C0 off.

Regards

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

Re: LDR (Light Dependent Resistor -> FC V8

Post by chipfryer27 »

Hi

Yes, you can use all four PWM channels. If you set them all to run from the same timer then they will each have the same frequency but can be independently controlled (if desired).

So if running on Timer 2 set for 1KHz then each PWM channel would be set for 1KHz, but each could have different Duty Cycles. PWM1 = 25% / PWM2 = 50% / PWM3 = 75% / PWM4 = 100% although you will probably have them all set the same.

Regards

Post Reply