How To Build a Flashlight With Perfect Modes (picture heavy)

JackCY:

PWM frequency depends on the CPU frequency and the PWM settings (mode and prescaler). Tido's BLF-VLD is supposed for 1.2MHz as CPU frequency (9.6MHz with CKDIV8), resulting in 2.3kHz PWM frequency.

Phase correct PWM has the advantage of being completely off with a value of 0 and completely on with 255. Fast PWM doesn't switch fully off with 0, but it's faster.

If you change the PWM settings, no other changes are required.
If you change the CPU frequency, you need to change the fuses and the F_CPU line.

Tom E: "Seems like the author thought the code generated would be more efficient if spaces, tabs, and CR/LF's were left out, looks like... "
Offense taken.

Hmm the fuses Tido has there in BLF-VLD 0.4 are 79/ED, so 4.8MHz, no divide, 64ms delay, in code there is phase correct PWM and prescaler of 1. BOD 1.8V and selfprogramming enabled. AH! Found it, he sets the F_CPU in the project settings as a compile parameter. Didn’t notice it gets there, the make files are automatic.

Yes for Luxdrv I can see F_CPU in code, nowhere used but F_CPU setting is needed for the delay.h for sure and maybe even sleep.h?

What motivated you DrJones to write your own drivers instead of modding the BLF-VLD?
I have to say BLF-VLD is big, probably unnecessarily but has tons of things there.
The EEPROM seems to be used for the variable modes I suppose, apart from storing the current mode I guess.

Do BLF-VLD, Luxdrv, nlite use PWM on the max setting? Or do they switch the output on steadily without PWM? Or with PWM but constantly on without the single blip down some firmware has?

<— is getting back in business once again, forgot some of the stuff already

May try to decipher the two drivers available and write some of my own.

TomE: format the code, no big deal. Comments are nice BLF-VLD has tons, sure Luxdrv not so much but the code should be self explanatory hopefully once one understands what is what.

I’ve uploaded nlite so far, will test it, but need some of my own custom drivers for other things.

I thought it was 4.8MHz phase correct which brought PWM to 9.somethingkHz (don't have the code in front of me). And I believe F_CPU should only affect delay.h.

That's what I did. I'm not as good of a programmer as Tido or DrJones, but it's fun and it allows me to customize it how I want.

Where did you find these fuses? I guess only the first ones were used with 1.2MHz then; the lowest implemented modes shouldn't even light up at 9kHz PWM.

Yes, F_CPU should not affect sleep.h.

All 3 mentioned firmwares use a PWM level of 255 for high, which means 100% on, no 'blip' as in QLITE.

> What motivated you DrJones to write your own drivers instead of modding the BLF-VLD?
I just had done a few ATtiny13 projects before and found programming a flashlight myself much more intriguing than using someone else's code.

I have too many things to do and program so I appreciate it when I don’t have to reinvent the wheel completely :wink:

The BLF-VLD fuses are in the Eclipse project setting. And also the same are on wiki I think.

What’s the thing with the blip? What did they do wrong? In Qlite drivers? The drivers some people buy so they get “better” firmware without reprogramming?
PWM 254 instead of 255?

They used inverted fast-PWM, where a value of 255 switches completely off, but 0 only switches on for 255 of 256 CPU cycles.

Thanks, I have to read up on the precise PWM output values for the two modes.
My silly question, why even use PWM for 100% output? It seems all drivers do when I look at code. Why not just flip the output ON? In case of correct PWM no difference, just more code :expressionless: So it’s more of a why yes if PWM can keep the ouput steadily on.

You answered your question :)

And I can tell you, in my advanced firmwares I often have to fight for every byte to get all the functionality into ATtiny13's 1kB flash.

Is there a way to get low mode or moonlight mode even lower?
If I remember correctly, seems like the lowest PWM value you can set in Dr.jones code is 5. Anything lower and the led doesn’t light up. I assume it might could be lowered by reducing PWM frequency. When I take a 2800ma amc7135 driver and add a few amc7135’s (increasing current) the output increases in low mode as with all modes. Can a moonlight mode (1 or 2 lumens) still be achieved some how when increasing the number of amc7135’s on a Nanjg 105c driver.

+1 moderator - got the same Q - I know some other driver designs (non Nanjg) seem to be able to do 5mA regardless, just dunno if possible with a Nanjg driver. Think the doc addressed this though a while back 100's of posts ago? Smile

Dual PWM, cut a trace and add a wires to have what ever the count of 7135s on first PWM and 1x 7135 on the second PWM.

That way you can have 1x 7135 output and PWM it as low as you like.

Reduce PWM frequency, a lot.

Use some other regulator instead of a single 7135 on the first PWM. Something that switches faster and is for lower currents, mainly so it switches faster.

Ghetto style, use GITD ring on the lens, fire it up on max, shut it down, now you have a moonlight :stuck_out_tongue:
Real style, shut the light off, let the moon shine, now you have 100% real moon light 0:)

How much could the frequency be reduced while still be acceptable and how much could be gained. I don’t think pwm is linear, so assuming frequency isn’t either. Half the frequency wouldn’t be half the light, at least that’s not how we would see it.
I have seen some cleaver post with using a second output pin in the code. I just wanted to know if there’s a code change that could help with moonlight modes for a plain Nanjg 106c.

I am just trying to understand the offtime checking:

If I have got this right you only connect a resistor with a diode parallel in series with a capacitor.
Software measures how much it is charged to determine a short or long offphase, have I understand this correct?

Edit:removed wrong pic to prevent confusing others

As I understand it, the software just looks for logic low or logic high upon startup (not a voltage range).

Check out this diagram though, as I don't believe the one above will work - https://budgetlightforum.com/t/-/17286#comment-400487

EDIT: Added image

I wondered what the diode is good for but now it makes sense.
It gets charged through the controller, through the diode.
On startup the controller checks if it is still high or low to determine if short or long off.
Is that right?

Yup. And after it's done checking the state of the I/O pin, it should be set to output in order to charge the capacitor. Without the diode the capacitor would take too long to charge, so there's a good chance a short on/off would not be registered as a tap. And without the resistor the capacitor would empty too quickly when the light is turned off (assuming no resistance on the I/O pin via the MCU).

Now it seems as though you can get lucky just using a capacitor (no resistor or diode), hoping that the internal resistance on the I/O pin when the MCU is turned off is enough to maintain current in the capacitor so it is read as "high" after .5s or so of being off - https://budgetlightforum.com/t/-/744?page=5#comment-14238. I would like to try this as others have been successful, but I just need to source some SMD capacitors.

Today I just received the last part I needed to build my own flashlight.
C8 host, XM-L2 T6-3C and Nangj 105c 2.8A.
I am not a programmer at all and BLF-VLD seems complicated for me. So I took luxdrv 0.3b and make it to my taste.
defining the modes was quite easy and I get rid of the strobe and beacon.
I used that USBASP and that SOIC8 clip (it’s chinese new year, it’s maybe the reason it’s not in stock).
AVR Drivers - Flashlight Wiki page was also a great help to reorder the pin correctly.

I tested many configuration and the wonderful advantage of the C8 is that the driver retainer is adapted to the 8*7135 size while on other host it needs to be soldered to the pill. So I can unscrew the pill and reflash the ATtiny13a.
This is so grateful.
Next I’ll try to understand tido’s code.

I'm building a C8 tonight, and I'm sure I'll try multiple mode setups so that is definitely an advantage!

Congrats on getting it working!!

EDIT: And yeah, that Wiki page is really awesome. It looks like JackCY and Brted contributed everything to that page.

That was a theory I once had. It don't work for reasons you can read in the following post (#171).

What I think is happening is that the condenser gets discharged fast to the protection diode threshold (0.7V) when the light is off and after that slowly bleeds through the input pin resistance after turn on.
I think the Polish project then switch the pin into an ADC input and they can measure the low remaining voltage if not all is bleeded. If that is true then a capacitor is the only component needed.
Unfortunately I don't have the program space needed for this in atTiny13 so I still have to mount the three components. By the way I switch position of the condenser and the parallelled resistor and diode so the two can be soldered firmly to the ground ring. This has no impact on function.

My driver can be found in post #254 (code link in #264 and #467) in the same thread. It has short cycle memory, battery rescue step down, battery voltage read-out and memory that can be switched on-off without soldering.

If I am honest the code is to complicated for me, I probably will hack the off time memory in the luxdrv code myself…I don’t need all the fancy stuff and blinky modes…

I am trying to make a simple 20mm driver with a FET, I have still a bit space on the board left so I thought I can add the parts for off time memory…