PWM not working with ATTINY84 / alternative (more flexible) MCUs? - Page 1
Forum section: electronics / general

author: T_guttata
author_rank: Regular Contributor
timestamp: August 01, 2025, 12:31:21 pm
content: HiI try to control a fan with an ATTINY84. But I just can not get the PWM to work.I use physical pin 3 (PA2) to switch a N-Channel Mosfet (IRLML0030).I can program the microcontroller and I can do PWM in software code (100Hz for example), but I just can not get the HW PWM to work. The pin just acts as digital.From what I googled, this pin should be PWM capable. I just use "analogWrite(PIN_PWMFAN, pwmValue); // Hardware-PWM auf PA2/OC1B"Do I have to somehow prepare that pin for PWM to work? How?Thanks for your help.

author: ComradeXavier
author_rank: Contributor
timestamp: August 01, 2025, 01:48:09 pm
content: Per theATTiny84 Datasheet, PA2 is not a Timer/Counter compare output pin. See section1: Pin Configurationsand section10.2: Alternate Port Functions.Take note that the physical package pin number doesn't always map to the same logical pin. QFN/MLF pin3 is PA2; PDIP/SOIC pin3 is PB1.Also, since you are using an Arduino library function, the implementation determines how the Arduino pin number maps to the logical pin and it might not match the physical package numbering.

author: T_guttata
author_rank: Regular Contributor
timestamp: August 02, 2025, 06:01:56 am
content: I'm aware, that physical pin numbering is not the same as logical pins and arduino pin numbering.After programming with ATMEL ICE, I have experience the full nightmare regarding pin mapping. Never experienced such a huge mess for something that simpleBut programming of the microcontroller worked and since I can switch on/off the PA2, it seems that the numbering is correct. Only PWM is not working.I have attached a screenshot of the table you are referring to. I don't understand it. The manual of the attiny84 is full of abbreviations I have never heard of. I have no clue how to find out, if it is possible to use that pin for PWM, and how it needs to be done.So, any help is appreciated.EDIT: I have attached a Pinout document I have found (not in the official datasheet). If I understand it correctly, PA2 can not do PWM.

author: ozcar
author_rank: Frequent Contributor
timestamp: August 02, 2025, 06:34:18 am
content: That table confirms what ComradeX told you about PA2 (no PWM) and so does the pinout in Fig 1-1 - look for pins that include "OCxy", like say "OC1A" for PA6.If you can't change to using PA5, PA6, PA7 or PB2 (whatever those map to in Arduino terms), maybe you could do a software PWM, depending on required frequency.

author: T_guttata
author_rank: Regular Contributor
timestamp: August 02, 2025, 06:55:20 am
content: Yes, I have tested that, but this does not work in my case.I want to control a 12V Fan with 12-24V voltage. I have added a filter. I have tested with SW PWM 30-1000Hz. The fan works, but the noise is horrible^^I expected that it should work with 31kHz, however, I have not tested it yet.Therefore, I will need to do another iteration. But I might then use an ATTINY 3226 instead of the 84. I think the newer versions are more flexible.

author: brucehoult
author_rank: Super Contributor
timestamp: August 02, 2025, 11:15:42 am
content: Quote from: T_guttata on August 02, 2025, 06:55:20 amDo you particularly need AVR? Why? If you're just using the Arduino library anyway, then you can use anything. Do you need the 512 bytes of EEPROM?

author: Psi
author_rank: Super Contributor
timestamp: August 02, 2025, 11:39:51 am
content: The vast majority of MCUs have limitations on what features you can use on what pins. PWM output included.Some MCUs, like nRF52xxx, can remap practically anything to any GPIO pin, but this is not typical.Newer MCUs are often better with more remap options. AVR mcus are pretty old now, so pretty limited in what can be remapped.Arduino often tries to be 'smart' and hide a lot of the hardware limitations from the user. It makes getting started easier but makes it more and more annoying as you learn and want to do more technical things. Like caring about software vs hardware PWM.

author: Peabody
author_rank: Super Contributor
timestamp: August 02, 2025, 02:26:12 pm
content: PB2, not PA2, would be a PWM output for timer0. Also, the datasheet says you have to configure the pins as outputs to get PWM output.In your schematic, the mosfet appears to be inverted. I assume it's not inverted in the circuit.

author: T_guttata
author_rank: Regular Contributor
timestamp: August 02, 2025, 08:56:28 pm
content: Yes, I have figured out the hard way that the mosfet is flipped, I will need to change that.It is the first time I build a complete PCB with a microcontroller. I went with the ATTINY84, but in the meantime I found out that this is no longer a good choice in 2025.My idea is to go with 2 types of microcontrollers, which I can use in the future for various projects. One should be very small (3x3mm) for small boards and one should be larger with more IOs and more functionality.So, I think I will try something different from the ATTINY families. They have two many devices with no significant differences. The product portfolio is large in number of variants but small in terms of meaningful alternatives.I might look at the STM32 family. Its 32 bit, but even cheaper than the attiny family.But this would mean again a lot of effort, since I would need to build another cable for programming. So far, I have used a TC2030 plug with a customized PCB to map the pins to the Atmel ICE programmer.What kind of MCUs would you prefer for simpler tasks such as controlling a fan in 2025? It should be somehow future proof, as I don‘t want to change everything for every project…

author: Psi
author_rank: Super Contributor
timestamp: August 02, 2025, 09:56:21 pm
content: I would go with an ATMega over an ATTiny if you want to future proof it while sticking with the same brand of mcu.The tiny's are quite limited in many ways.Nothing wrong with an ATMega even today, they're still very capable for a lot of things.They tend to get slow if you try and do a ton of floating point math, but that's about it.For simple projects like controlling a fan, flashing LEDs, etc.. they are plenty good enough, maybe even overkill.STM32 has its own learning curve but is a big step up in terms of features, speed, configurability and reduced costs.If you're just playing around with simple stuff a ATTiny/ATMega is better suited than a STM32.

author: Doctorandus_P
author_rank: Super Contributor
timestamp: August 02, 2025, 10:00:05 pm
content: Start with a working example. The ATTinies are popular, so there must be plenty of examples around.Quote from: T_guttata on August 02, 2025, 08:56:28 pmAs far as 15 years back I got quite annoyed by the differences between the ATMEGA's and the ATTINY's. Back then I was programming for the AT90S2313 and the ATMEGA8. I bought a bunch of ATTINY2313 and only later discovered that they have no normal UART, but some other weird serial device. As I had no interest in re-writing and debugging my UART based custom network protocol, I never used those ATTINY's. I still have 20 or so of them rotting in a drawer somewhere.When I wanted something faster then the ATMEGA's, I had a brief look at the Xmega's. but the whole architecture was so different that I may as well look further. So I never looked very deep into those. Another annoyance with atmel back then is that they invented a new programming interface every 3 years or so. They don't start with well thought out peripherals, but over their line they add a register here, and a few bits in a register over there, and that makes it really hard to write a library that runs on a bunch of their IC's.I also don't have a real need to go to a smaller physical size then an QFP32 for the ATMEGA's.STM32 has a whole lot of choice, from quite small to plenty big. And because STM32 is very popular, and also used in a lot of Chinese products (For example you can buy an FX2N PLC on Aliexpress for around EUR 30. I despise PLC programming, but Hey, it's an STM32 with hardened I/O and built in SMPS power supply for EUR 30!!! With STM32 there is also much more commonality of peripherals over the series. In the datasheet they describe a timer, and then there is a line that says: there are 5 of these (timers, uarts, whatever) in the IC.I also did make a big miscalculation with STM32. The processors are powerful, but also difficult to use, and ST is pushing really hard for everybody to use their (freely available, Eclipse based) IDE. There is a very long thread here about the shittynes of that software, and it's a real bummer.https://www.eevblog.com/forum/microcontrollers/is-st-cube-ide-a-piece-of-buggy-crap/For DIY, the quality of the tools, ease of use and widespread usage by other hobbyists (for examples and such) are far more important factors then the cost of the hardware. I do not understand the craze around the Padauk stuff for example. Why would I care whether a uC costs 3ct or 80ct? That price difference is not worth a week of my life.From what I've read on this forum The LPC line from NXP may be worth looking into, but I never used them myself. Arm-Cortex seems to be pretty much universal. Risc-V is apparently still slowly growing. The Raspi RP2040 and consorts may be one of the most attractive for DIY. It's a very capable processor, and there are quite affordable (<EUR 10) development boards, and there is lots of community support. Bare IC's cost less then EUR 1. They are 7mm by 7mm QFN. Do you really have a need to go smaller then 7 by 7mm? Ti has some BGA uC's that are 2 by 3mm or so these day's...

author: Psi
author_rank: Super Contributor
timestamp: August 02, 2025, 10:03:10 pm
content: Quote from: Doctorandus_P on August 02, 2025, 10:00:05 pmYeah, don't fall into the ATXMega vs ATMega trap.Anyone thinking about moving from ATMega/ATTiny onto ATXMega should move to a more common 32bit platform like STM32 instead.

author: brucehoult
author_rank: Super Contributor
timestamp: August 02, 2025, 11:19:37 pm
content: Quote from: T_guttata on August 02, 2025, 08:56:28 pmSeveral years ago I switched to using CH32V003 for the things I previously used ATTiny85 and even ATMega328 for.- 2k RAM, 16k flash, 18 GPIOs covers almost anything an Uno can do. The flash is smaller, but RISC-V code is also a lot smaller, especially for 16 bit or 32 bit values and pointers/arrays. 48 MHz speed is also a lot more.- priced at $0.10 to $0.20 in qty 50 ($5 or $10 investment) for 8, 16, 20 pin packages (but no DIP), while 85s are $1+ and 328's several dollars.https://www.aliexpress.com/item/1005005036714708.html(official WCH store)- works on 2.7V - 5.5V, 5V is rare for newer MCUs- supports proper debugging, using 1 pin- peripherals are copies of STM32, you can literally use STM's documentation (though WCH's is good too), just with a RISC-V CPU instead of Arm.- official kit for $8 with a dev board, a WCHLinkE USB programmer, and 5x 20 pin chips.https://www.aliexpress.com/item/1005004895791296.html- a lot of unofficial dev boards. I like the $1.50 nanoCH32V003 from Muse Lab. They also have a bundle with their own version of the WCHLinkE USB programmer for $6.50.https://www.aliexpress.com/item/1005005221751705.html- improved CH32V002 (4k RAM, 16k flash) and CH32V004 (6k RAM, 32k flash) now seem to be easily available. They also have expanded voltage range (down to 2V), 12 bit ADC instead of 10 bit. Also multiply instructions.- easy to upgrade to bigger, faster, WCH chips with more pins, USB, 2.4 GHz radios, etc.- They've become very popular, there's a large and active community. One great resource is very lightweight header files and example code by chlohr. Do have a browse around that, look at the exampleshttps://github.com/cnlohr/ch32fun- WCH have provided Arduino library support, if you want to stick with thatOur host reviewed the CH32V003 2 1/2 years ago:- search "CH32V003" on YouTube. There's a lot of stuff!

author: ozcar
author_rank: Frequent Contributor
timestamp: August 03, 2025, 12:30:13 am
content: For your current project ATTiny84 should be capable of controlling a fan, even it requires a bit of track cutting and a wire or two to re-arrange the pin usage.But only if you have to. You mentioned trying software PWM, but did not show how you filtered the output? Is it really needed, or even possible, tochangethe speed of the motor that quickly?

author: T_guttata
author_rank: Regular Contributor
timestamp: August 03, 2025, 07:49:29 am
content: Thanks for your replies. Interesting, since that kind of information you will not find in data sheets.Of course I can fix my current project with the ATTINY84. But since I must do another PCB anyway to correct a few bugs, there is no need to stick with the ATTINY84.My initial question is answered; the PA2 pin I have used is not PWM capable. Learning: don't trust AI in terms of pin functionality. I was too lazy to dig into the data sheet and instead relied on the answer of AI.I searched for differences among MUCs and initially I could not find to many relevant ones. After using you get to know a few short comings, for example that the Serial Monitor in the Arduino IDE will not work with the ATTINY84, even though I thought I bought a good programmer /debugger (ATMEL ICE for rouhly $100).So, my requirements for an MCU for simpler tasks are the following ones:-must work with Arduino IDE. -> I don't program daily and therefore, I want the simplest solution possible. I will only switch / upgrade, if I hit a limit (something I need to do, which I can not with the IDE).-the MCU must be available in a small package (3x3 or 4x4mm). -> I want flexibility to use it for really tiny boards.-It must be possible to use the Serial Monitor in arduino IDE for debugging without tinkering.-it must be reasonnably priced (<$1) -> for prototyping I don't care. But maybe I want to use one of my designs comercially in the future, and then it matters if the MCU is 50 cents or $5.-it must be somehow future proof, as I don't want to start from scratch every time I do a project.-it should have 5V logic levels -> more flexibility to use mosfets. (I might have to sacrifice this requirement for others)-I want to use a small footprint to program the MCU. TC2030 6 pin (tag connect) is currently my favorite. You can get to work that for every MCU, but the effort might be different (depending on what cables are readily available). -> the required cable from tag connect to program the attiny84 with ATMEL ICE would have cost me $150 as it is only available in the tag connect store. I used a cable which is available on digikey and did a custom PCB to map the pins. This was then maybe $40 in total, but it cost me a lot of effort to find out how to do the mapping. -> You don't want to do that for every project, it is a waste of lifetime.So, I will have a closer look at the STM32 and CH32 families. Currently I don't have the overview yet regarding debugging functionality, which I found again is absolutely crucial to work efficiently!@ozcarRegarding required speed for PWM: I want to power a 12V fan with 12-24V input power without using a buck. Since the fan makes horrible noise with low frequency, I want to test with higher frequency (see the attached schematic).

author: eugene
author_rank: Frequent Contributor
timestamp: August 03, 2025, 05:12:56 pm
content: Since much of this seems new to you, I want to verify that the fan you're attempting to control has abrushedtype motor with just two wires: PWR and GND.If the motor is Brush-Less DC (BLDC) with built in commutation then controlling it via PWM is problematic because all of the commutation circuitry gets powered down then back up with every PWM cycle.If your fan is a common 3-wire or 4-wire computer cooling fan, then there is a dedicated wire to be used for PWM input. You would connect one of the wires solidly to GND, another one solidly to 12V (without PWM), then apply a simple 25kHz PWM signal to the PWM wire through a small MOSFET connected as open drain. Intel wrote the specification in 2005 and most fan manufacturers adhere to this spec. The most difficult part about using an Arduino for this is generating a 25kHz PWM. It requires manually setting up some timer registers, which is not standard Arduino procedure, but it's all covered in multiple online sources.Intel Fan Specification DocumentNote that the document includes what I believe is a mistake. The document states that the controller (i.e. computer motherboard) is to provide a pullup to 12VDC for the tachometer output. I've worked with "technical writers" at large companies that didn't really understand the original engineering draft and "fixed" it so that it was incorrect. Anyway, I have used pullups to whatever logic level I was using (3.3V or 5V) without any problems reading the tach signal.

author: T_guttata
author_rank: Regular Contributor
timestamp: August 03, 2025, 07:20:23 pm
content: Thanks, for the spec. Did not know this was initially from intel and that it is available online.I want to use a 92mm fan. I assume that most of the fans used for PCs are BLDC. There are fans with 2 wires, 3 wires (Tacho) and 4 wires (Tacho + PWM). I don't use the tacho signal. My idea was to use a 3 wire fan, since they are available for roughly $4 on aliexpress.The idea was to use a filter (inductance + 2 caps as in the drawing) to create a lower voltage (6-12V) from a 12-24V input. The idea is to use a PWM frequency which is high enough, such that the voltage should be pretty constant.So, the fan should not see the PWM signal. That's the idea. I'm open for other suggestions, but I don't want to use a buck just for that. And from my understanding, my idea should work.

author: eugene
author_rank: Frequent Contributor
timestamp: August 03, 2025, 08:53:50 pm
content: Quote from: T_guttata on August 03, 2025, 07:20:23 pmSo you simply want to create a regulated buck converter to bring 12-24V down to 6-12V.QuoteBut your proposed circuit that switches ground absolutely will subject the fan to the PWM. To accomplish what you want you would need to switch the high side and put the filter after the switching transistor. That is essentially how buck converters work.All of that could work, I suppose. But if it were me, I would use buck converter chip to regulate the 12-24V supply down to 12V. Then, If I did want to control fan speed I would use a 4-wire fan for PWM input. But that's just me; there are many solutions to most problems.

author: ozcar
author_rank: Frequent Contributor
timestamp: August 04, 2025, 05:55:42 am
content: I have lots of PC fans here, two and three wire. I tested several and found that all were quite happy at 6V dc, with some of them running (even starting) with the voltage as low as 3V. The current draw was approximately proportional to the voltage over the range that the fans would run on. YMMV, or perhaps YFMV.I then tried them with a circuit as proposed, basically with them exposed to the unsmoothed PWM and they worked surprisingly well, even on frequencies down to below 100Hz. There was a kick-back voltage spike when the mosfet turned off, so I added a reverse diode across the fan. At low kHz there was a bit of squealing, but my hearing is not so good.As already said, the proposed circuit does not smooth the PWM out, but I don't see why it would be necessary to change to high side switching. Just connecting a capacitor across the fan and adding some series resistance in the drive made a big difference.I think I would also look at the four-wire fan option though - no mosfet or filtering required.

author: T_guttata
author_rank: Regular Contributor
timestamp: August 04, 2025, 10:17:33 am
content: The point is that I want to power and control a 12V fan with a power supply voltage between 12-24V. The input voltage will be constant but might be anything between 12-24V.Simplest solution would be to just vary the duty cycle. But since I don't know if 24V peaks will harm a 12V fan on the long run, I added the filter (inductor + cap). The idea of the filter is to make sure, that the fan will not see peaks >12V.If 24V peaks are no issue, I could off course use a 4 pin fan. But since I don't know that I went with the other solution.Maybe someone can tell me how to calculate what voltage the fan wil actually see based on the duty cycle, PWM frequency and filter?

author: Ian.M
author_rank: Super Contributor
timestamp: August 04, 2025, 10:48:34 am
content: Your 'filter' has a resonant frequency of 2.4KHz (assuming capacitors are nominal value, not true for high-K ceramics under significant bias vs their voltage rating). We have no way of telling how that will interact with the pulsed load of a fan's internal BLDC driver, or with your PWM. If the fundamental or low order odd harmonics of either are near the resonant frequency and the fan load during commutation provides insufficient damping, its possible for the peak voltage seen by the fan to significantly exceed the supply voltage, possibly reaching double the supply voltage. High K ceramics loose capacitance with increasing voltage so may act as parametric amplifiers, 'pumping' the peaks eve higher. No 12V BLDC fan is going to like peaks of over 50V even if they only occur transiently at startup or shutdown.TLDR: your circuit is not fit for purpose.

author: Psi
author_rank: Super Contributor
timestamp: August 04, 2025, 10:49:27 am
content: pwm RC filter calchttp://sim.okawa-denshi.jp/en/PWMtool.php

author: T_guttata
author_rank: Regular Contributor
timestamp: August 04, 2025, 11:09:47 am
content: Ok, I see. I will have to invest more in the topic. Most certainly I'm not the first one who tries to lower a voltage using PWM and a filter?

author: Psi
author_rank: Super Contributor
timestamp: August 04, 2025, 11:28:13 am
content: Quote from: T_guttata on August 04, 2025, 11:09:47 amSearch for "PWM DAC". Its often used when you want to generate an analog output using a digital output and you dont have a proper DAC.

author: Ian.M
author_rank: Super Contributor
timestamp: August 04, 2025, 11:50:44 am
content: Its certainly *possible* to reduce voltage by a software controlled buck converter, but the low max. PWM frequency an 8 bit MCU without a dedicated SMPS peripheral can achieve will force you to use a much much larger and more expensive inductor vs a dedicated buck converter IC with an operating frequency in the high hundreds of KHz to low MHz range. Also assuming you are using a free-wheeling diode, writing a fast enough control loop for it to stabilise the output voltage is non-trivial as is protection against overcurrent during startup and when running.TLDR: use a single IC buck converter, save money and your life gets much much easier, even if you need to make the MCU adjust its output voltage to implement speed control.

author: T_guttata
author_rank: Regular Contributor
timestamp: August 04, 2025, 12:29:04 pm
content: I hoped I had found a small simple and cheap solutionMy idea was not to create a closed loop. The fan takes 150mA and I thought I can adjust the voltage between roughly 6-12V by changing the duty cylce.It takes much more board space to implement a proper step down converter and it is also more expensive (maybe $5 instead of only 50 cents).

author: Psi
author_rank: Super Contributor
timestamp: August 04, 2025, 12:37:50 pm
content: Your PWM idea will work to some degree, you can make it easier if you don't care about wasting some power.Put a resistor across the fan to turn it into more of a resistive load and use a big cap to smooth out the PWM.Then you should get better control of the output voltage.Since you have a better idea how much current will be always be flowing out of the cap due to known resistor value.You do need some feedback for voltage across the cap. Or at the very least to measure the input voltage so you can calculate max pwm to get 12V on the fan

author: T_guttata
author_rank: Regular Contributor
timestamp: August 04, 2025, 12:55:07 pm
content: If I understand you correctly you propose to put a resistor in series to the fan to reduce voltage peaks, and measure the input voltage with an analog input of the MCU. Then I could reduce the duty cycle such, that I have a voltage between 6-12V.So, rather than assuming that 50% duty cycle with 12V will give 6V, I should measure the input voltage and set the duty cycle accordingly?

author: Psi
author_rank: Super Contributor
timestamp: August 04, 2025, 01:02:15 pm
content: na, i was meaning put both a resistor and a capacitor in parallel with the fan. Then feed PWM into it via a series resistor from your NFET in the negative line. (or could be resistor from fan to the +12-24 and nfet to gnd, doesnt matter)Your PWM source needs to be powerful enough to run the fan at full speed plus the resistor load.

author: T_guttata
author_rank: Regular Contributor
timestamp: August 04, 2025, 01:06:51 pm
content: The PWM source can deliver 4A. But what value should the resistor have in parallel to the fan? Depending on the amount of power wasted, you could just go to the simplest solution and put a resistor in series to the fan and burn 12V?

author: Psi
author_rank: Super Contributor
timestamp: August 04, 2025, 01:10:21 pm
content: Quote from: T_guttata on August 04, 2025, 01:06:51 pmNot sure, but I would start with a resistor that draws a constant load equal to the fan at 12V. All the resistor is trying to do is make the capacitor discharge a bit more consistent and predictable.How big is this fan, I'm assuming it's like a small PC fan, so a watts or 2. If it's a big fan its probably not the best method.

author: Psi
author_rank: Super Contributor
timestamp: August 04, 2025, 01:14:22 pm
content: Another option is, as you say, resistor in series with fan with a value that makes the fan run at 12V from 24V.Then put the cap across the fan and use the nFET PWM to short out the series resistor (resistor in ground line)The more PWM duty the more you allow bursts of current into the cap/fan and increases the avg voltage speeding up the fan. You do that based on how much the input voltage is above 12V.So at 100% PWM the fan on full 12V with resistor shorted, and 0% PWM the fan has the resistor and is running from 24V but only seeing 12V

author: T_guttata
author_rank: Regular Contributor
timestamp: August 04, 2025, 01:16:46 pm
content: It is a small 92mm fan, 12V @ 150mA. Wasting 2W is too much. Wasting 0.5W would be somehow ok.

author: Psi
author_rank: Super Contributor
timestamp: August 04, 2025, 01:21:07 pm
content: Do you need full speed control or just for the fan to run safely from between 12V and 24V ?

author: T_guttata
author_rank: Regular Contributor
timestamp: August 04, 2025, 01:49:48 pm
content: The plan was to control the fan to reduce the noise level. I maybe don't need full control (0-100%) but more than just one speed.

author: Ian.M
author_rank: Super Contributor
timestamp: August 04, 2025, 01:53:34 pm
content: One approach that *will* work open loop is a MOSFET half H-bridge feeding a LC filter with resonant frequency much lower than the PWM frequency, and sufficient inductance and saturation current rating that the inductor cant saturate in a single PWM cycle. The duty cycle then sets the output voltage as a proportion of the input voltage. Some care must be taken during startup to avoid inductor saturation by ramping the duty cycle up slowly.Active inductor current sensing (with hardware support for terminating the PWM on-time early if the current is over a threshold) would let you use a somewhat physically smaller cheaper inductor, at the expense of needing a software control loop to manage the currentHowever it certainly wont be less board area than a dedicated buck converter IC as the much lower operating frequency needs much bigger (and thus physically larger) inductor and capacitor.

author: Psi
author_rank: Super Contributor
timestamp: August 04, 2025, 02:09:05 pm
content: Quote from: T_guttata on August 04, 2025, 01:49:48 pmIf you only need a small number of fan levels you can use like 4 mosfets controlled by 4 outputs to switch 4 ground resistors. And by combinations you can get an off setting plus 15 different amounts of switchable series resistance. Or with 3 mosfets off+7 levels etc..

author: T_guttata
author_rank: Regular Contributor
timestamp: August 04, 2025, 02:49:21 pm
content: Lots of creative options but many are not energy efficient.😏 The required board space for my initial idea is rougly 10x5mm.

author: Atlan
author_rank: Frequent Contributor
timestamp: August 04, 2025, 03:37:26 pm
content: Make a stepdown converter, just a choke, a diode, a capacitor and a transistor. Predict that the fan will be a stable load so that there is no need for feedback.Or install a dps with a converter on top of the basic dps.

author: eugene
author_rank: Frequent Contributor
timestamp: August 04, 2025, 08:54:02 pm
content: Ok, I think I might have a solution that would work for you assuming you can get a PWM signal from somewhere (which was actually the original topic of this thread.)Below I have drawn three different concepts. The first is what you proposed in your opening post. I draw it a little funny with the transistor sideways, but that was on purpose. The problem with that circuit is that when the PWM transistor opens, the current through the fan stops completely. That is not your goal.The second drawing shows (conceptually) how a buck converter is configured. Notice that during the part of the PWM cycle when transistor is open, the fan continues to draw current from the capacitor.The third circuit is one that I am proposing. It's exactly the same as yours except that the capacitor is connected to the fan side of the transistor. In this configuration the fan continues to draw current from the cap during the low part of the PWM cycle, but unlike the buck converter configuration, the transistor is referenced to ground. You can use an N channel MOSFET and drive it directly from a MCU GPIO pin, or 555 or whatever.I'm counting on people smarter than me to tell me why this wouldn't work.EDIT: add explicit GND to schematics

author: PCB.Wiz
author_rank: Super Contributor
timestamp: August 04, 2025, 09:57:20 pm
content: Quote from: T_guttata on August 02, 2025, 08:56:28 pmIf you already know AVR, look at thenewest AVRlike the AVRxxDD series, that comes in 3x3 packages and larger and includes useful extras like DAC and Config Logic.

author: Psi
author_rank: Super Contributor
timestamp: August 04, 2025, 11:47:25 pm
content: Could go with an off the shelf opamp that has high current drive output. You can probably find one that will do 150MA at 24V. The problem is that it won't be cheap.Can drive the opamp with a simple PWM DAC since he no longer needs the DAC to produce any power.And can probably add a zener and some extra parts to make it stop at 12V output all done in hardware. Or use a dual package and make one into a comparator.

author: brucehoult
author_rank: Super Contributor
timestamp: August 05, 2025, 12:39:57 am
content: Quote from: PCB.Wiz on August 04, 2025, 09:57:20 pmSomeone using the Arduino libraries/API does not "know AVR".This is not a knock on the Arduino library! It is really great at making microcontroller code machine-independent and portable within a certain limited but very useful range of functionality and performance.

author: T_guttata
author_rank: Regular Contributor
timestamp: August 05, 2025, 12:41:15 pm
content: In the meantime I also tried to draw a few alternative circuits. I basically found the same options as @eugene showedIf I use a STM32 in the future, PWM frequency can be much higher, maybe 100kHz?In principle I have to use a buck topology with either high side or low side mosfet, right?

author: PCB.Wiz
author_rank: Super Contributor
timestamp: August 05, 2025, 08:00:32 pm
content: Quote from: T_guttata on August 05, 2025, 12:41:15 pmIt can be much higher on any modern 8 bit MCU too.Most PWM timers these days allow any division setting, & you have said not many steps are needed, so you can get eg 100kHz with 200 steps from 20MHz.Less common are PWM timers with dithering, which allow 12+ bits of averaged power control still above 100kHzQuoteYes, tho low side usually places the inductor next to the drain, so the fan body is not radiating switching square waves.

author: PCB.Wiz
author_rank: Super Contributor
timestamp: August 06, 2025, 02:24:31 am
content: Quote from: T_guttata on August 04, 2025, 01:49:48 pmEven a Tiny84 is fine for controlling a FAN speed.You can nudge the CLK up to ~ 16MHz using OSCCAL, then divide by ~ 100 gives 160kHz of PWM frequency.If 1% steps are too coarse, you can use various techniques to get better averages.If youvary both PWM and PER, you can get finer control - this example at ~ 80% modulation, gives 5 more steps with a +/- 3% PER changeCode:PWM/PER Calc Rounded80/101 = 0.7920792 79.277/97 = 0.7938144 79.478/98 = 0.7959183 79.679/99 = 0.7979797 79.880/100 = 0.8 80.0081/101 = 0.8019801 80.282/102 = 0.8039215 80.483/103 = 0.8058252 80.680/99 = 0.8080808 80.8Or you can dither the PWM value too, by alternating between two choices in that table, on a rate-multiplier basis.3 bits of dither averages over 50us for another 8 steps, for a total of appx 40, and your ~7 bit PWM has become close to 12bA modest amount of assembler can manage the table lookups and choices.You might like to monitor the fan voltage to get better control.

author: Ian.M
author_rank: Super Contributor
timestamp: August 06, 2025, 03:54:37 am
content: For under $0.25 USD (100 qty) you can get a >2MHz buck converter, e.g.AP64060Qwhich lets you use over an order of magnitude smaller inductors and capacitors than you need for a 120KHz MCU driven PWM solution, with no external MOSFET or free-wheeling diode required. That throws the board area and cost arguments right out of the window.How do you then get two wire fan speed control? Simply inject a small variable current to the buck converter's feedback pin, which fools the converter into 'thinking' its output voltage is too high, so reducing it proportionally. Injecting the current can be as simple as feeding filtered PWM from a MCU through a resistor, value chosen with respect to the buck feedback divider resistors to get the desired output voltage range. Most such buck converter ICs will also have an logic level enable input pin which can be used to switch the fan off completely.

author: T_guttata
author_rank: Regular Contributor
timestamp: August 06, 2025, 04:45:10 am
content: Another interesting solution. I thought, that to control a buck with a MCU, you would need a current DAC.The solution would be superior, no doubt. But you still need some more components and board space? The LC filter I have used so far consists of 2 22uF ceramic caps and one inductor (5x5mm). The required board space is very small.I would still need a filter + the small buck converter circuit?

author: Ian.M
author_rank: Super Contributor
timestamp: August 06, 2025, 06:05:28 am
content: The integrated buck converter should take far less board area than the MCU controlled one @120KHz due to its much higher operating frequency. One package replaces the MOSFET and free-wheeling diode, and the inductor and output capacitor should be physically less than half the linear dimensions.The AP64060Q feedback reference voltage is nom. 800mV, and if you follow the datasheet exactly and use a 22.1K lower resistor in the feedback divider, the standing current in the divider is thus about 36uA. I doubt you want to reduce the fan voltage below 6V so you need to inject a maximum of 18uA. The MCU pin IO high level is 3.3V, so the total drop across the PWM filter and injection resistor is 2.5V. The total resistance of the RC + injection resistor thus needs to be about 138K. Split this evenly between the filter and injection resistor and 68K is near enough. Assuming a 31.25KHz PWM (8MHz/256), a filter cutoff frequency a couple of order of magnitude lower can be expected to give good results. A 10nF filter capacitor gives a cutoff frequency of about 467Hz and about 537nA peak to peak ripple on the injected current at 9V buck output, and about 0.13V pk-pk ripple on the buck output at the PWM frequency. As the control PWM filter is low power you can use very small passives, even as small as 0201 (if you can assemble them!) so the extra board area is negligible. If you can cram on an extra RC stage, use 47K resistors and two 10nF capacitors giving a 308Hz cutoff frequency and the ripple will be negligible (much less than the existing ripple and noise from the buck converter).

author: T_guttata
author_rank: Regular Contributor
timestamp: August 06, 2025, 06:40:51 pm
content: Cool. I have tried to rebuild the buck circuit based on AP6406Q. I managed to squeeze everything in a 9.9x7.6mm packageIs this a common way to create a variable voltage with a microcontroller? I search for that some time ago, and did not come across that solution. I thought you would need a current DAC.Edit: Based on your recommendation I have attached a complete sketch.

author: Ian.M
author_rank: Super Contributor
timestamp: August 08, 2025, 11:06:23 am
content: Yes, that should get you 'in the ballpark'. There is a potential startup issue if you want to start the fan at 12V - to avoid Vout overshoot, set the PWM to 24% to precharge C6,C7 a few ms before enabling the buck converter, then set the PWM for the desired output voltage once its running, a few ms later.Here's a LTspice sim using a LTC buck converter and a LTC Timerblox PWM generator in place of your MCU that will give you a general idea of what to expect. Because the sim runs extremely slowly, I've lowered the buck switching frequency, and am also ramping the PWM duty cycle too fast for the low pass filter to keep up properly. If you've got a very fast PC, you can increase the runtime, reduce the control voltage ramp rate (source V3) and even increase the buck switching frequency (R1,L1) for more realistic results.