Author Topic: PWM not working with ATTINY84 / alternative (more flexible) MCUs?  (Read 6295 times)

0 Members and 2 Guests are viewing this topic.

Offline T_guttataTopic starter

  • Regular Contributor
  • *
  • Posts: 198
  • Country: 00
Hi

I 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.

« Last Edit: August 03, 2025, 07:23:14 am by T_guttata »
 

Offline ComradeXavier

  • Contributor
  • Posts: 24
  • Country: us
Re: PWM not working with ATTINY84
« Reply #1 on: August 01, 2025, 01:48:09 pm »
Per the ATTiny84 Datasheet, PA2 is not a Timer/Counter compare output pin. See section 1: Pin Configurations and section 10.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.
 

Offline T_guttataTopic starter

  • Regular Contributor
  • *
  • Posts: 198
  • Country: 00
Re: PWM not working with ATTINY84
« Reply #2 on: August 02, 2025, 06:01:56 am »
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 simple |O |O |O

But 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.
« Last Edit: August 02, 2025, 06:33:44 am by T_guttata »
 

Offline ozcar

  • Frequent Contributor
  • **
  • Posts: 405
  • Country: au
Re: PWM not working with ATTINY84
« Reply #3 on: August 02, 2025, 06:34:18 am »
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.
 

Offline T_guttataTopic starter

  • Regular Contributor
  • *
  • Posts: 198
  • Country: 00
Re: PWM not working with ATTINY84
« Reply #4 on: August 02, 2025, 06:55:20 am »
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.
 

Offline brucehoult

  • Super Contributor
  • ***
  • Posts: 6464
  • Country: nz
Re: PWM not working with ATTINY84
« Reply #5 on: August 02, 2025, 11:15:42 am »
But I might then use an ATTINY 3226 instead of the 84. I think the newer versions are more flexible.

Do 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?
 

Offline Psi

  • Super Contributor
  • ***
  • Posts: 12738
  • Country: nz
Re: PWM not working with ATTINY84
« Reply #6 on: August 02, 2025, 11:39:51 am »
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.
« Last Edit: August 02, 2025, 11:44:10 am by Psi »
Greek letter 'Psi' (not Pounds per Square Inch)
 

Offline Peabody

  • Super Contributor
  • ***
  • Posts: 2783
  • Country: us
Re: PWM not working with ATTINY84
« Reply #7 on: August 02, 2025, 02:26:12 pm »
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.
 

Offline T_guttataTopic starter

  • Regular Contributor
  • *
  • Posts: 198
  • Country: 00
Re: PWM not working with ATTINY84
« Reply #8 on: August 02, 2025, 08:56:28 pm »
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…
« Last Edit: August 02, 2025, 08:59:40 pm by T_guttata »
 

Offline Psi

  • Super Contributor
  • ***
  • Posts: 12738
  • Country: nz
Re: PWM not working with ATTINY84
« Reply #9 on: August 02, 2025, 09:56:21 pm »
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.
« Last Edit: August 02, 2025, 10:00:01 pm by Psi »
Greek letter 'Psi' (not Pounds per Square Inch)
 

Offline Doctorandus_P

  • Super Contributor
  • ***
  • Posts: 5389
  • Country: nl
Re: PWM not working with ATTINY84
« Reply #10 on: August 02, 2025, 10:00:05 pm »
Start with a working example. The ATTinies are popular, so there must be plenty of examples around.

I went with the ATTINY84, but in the meantime I found out that this is no longer a good choice in 2025.

As 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...
 

Offline Psi

  • Super Contributor
  • ***
  • Posts: 12738
  • Country: nz
Re: PWM not working with ATTINY84
« Reply #11 on: August 02, 2025, 10:03:10 pm »
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.
Yeah, 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.
Greek letter 'Psi' (not Pounds per Square Inch)
 

Offline brucehoult

  • Super Contributor
  • ***
  • Posts: 6464
  • Country: nz
Re: PWM not working with ATTINY84
« Reply #12 on: August 02, 2025, 11:19:37 pm »
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…

Several 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 examples

https://github.com/cnlohr/ch32fun

- WCH have provided Arduino library support, if you want to stick with that

Our host reviewed the CH32V003  2 1/2 years ago:



- search "CH32V003" on YouTube. There's a lot of stuff!
 

Offline ozcar

  • Frequent Contributor
  • **
  • Posts: 405
  • Country: au
Re: PWM not working with ATTINY84
« Reply #13 on: August 03, 2025, 12:30:13 am »
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, to change the speed of the motor that quickly?
 

Offline T_guttataTopic starter

  • Regular Contributor
  • *
  • Posts: 198
  • Country: 00
Re: PWM not working with ATTINY84 / alternative (more flexible) MCUs?
« Reply #14 on: August 03, 2025, 07:49:29 am »
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!

@ozcar

Regarding 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).
 

Offline eugene

  • Frequent Contributor
  • **
  • Posts: 602
  • Country: us
Re: PWM not working with ATTINY84 / alternative (more flexible) MCUs?
« Reply #15 on: August 03, 2025, 05:12:56 pm »
Since much of this seems new to you, I want to verify that the fan you're attempting to control has a brushed type 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 Document

Note 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.
90% of quoted statistics are fictional
 

Offline T_guttataTopic starter

  • Regular Contributor
  • *
  • Posts: 198
  • Country: 00
Re: PWM not working with ATTINY84 / alternative (more flexible) MCUs?
« Reply #16 on: August 03, 2025, 07:20:23 pm »
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.
 

Offline eugene

  • Frequent Contributor
  • **
  • Posts: 602
  • Country: us
Re: PWM not working with ATTINY84 / alternative (more flexible) MCUs?
« Reply #17 on: August 03, 2025, 08:53:50 pm »
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 you simply want to create a regulated buck converter to bring 12-24V down to 6-12V.

Quote
the fan should not see the PWM signal.

But 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.
90% of quoted statistics are fictional
 

Offline ozcar

  • Frequent Contributor
  • **
  • Posts: 405
  • Country: au
Re: PWM not working with ATTINY84 / alternative (more flexible) MCUs?
« Reply #18 on: August 04, 2025, 05:55:42 am »
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.
 

Offline T_guttataTopic starter

  • Regular Contributor
  • *
  • Posts: 198
  • Country: 00
Re: PWM not working with ATTINY84 / alternative (more flexible) MCUs?
« Reply #19 on: August 04, 2025, 10:17:33 am »
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?
 

Online Ian.M

  • Super Contributor
  • ***
  • Posts: 14134
Re: PWM not working with ATTINY84 / alternative (more flexible) MCUs?
« Reply #20 on: August 04, 2025, 10:48:34 am »
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.
« Last Edit: August 04, 2025, 11:40:34 am by Ian.M »
 

Offline Psi

  • Super Contributor
  • ***
  • Posts: 12738
  • Country: nz
Greek letter 'Psi' (not Pounds per Square Inch)
 

Offline T_guttataTopic starter

  • Regular Contributor
  • *
  • Posts: 198
  • Country: 00
Re: PWM not working with ATTINY84 / alternative (more flexible) MCUs?
« Reply #22 on: August 04, 2025, 11:09:47 am »
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?
 

Offline Psi

  • Super Contributor
  • ***
  • Posts: 12738
  • Country: nz
Re: PWM not working with ATTINY84 / alternative (more flexible) MCUs?
« Reply #23 on: August 04, 2025, 11:28:13 am »
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?

Search 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.
« Last Edit: August 04, 2025, 11:30:58 am by Psi »
Greek letter 'Psi' (not Pounds per Square Inch)
 

Online Ian.M

  • Super Contributor
  • ***
  • Posts: 14134
Re: PWM not working with ATTINY84 / alternative (more flexible) MCUs?
« Reply #24 on: August 04, 2025, 11:50:44 am »
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.
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf

 

- - -
-->