Attiny13a and 2 PWM (dimmer and fan) is not working.


I’m doing my project and here is the schematics atteched. It simply doesn’t generate any PWM. I’ve started with just one PWM controlling a fan depending on LM35 sensor and it worked well. Now I’m trying to add one more PWM for a LED panel dimmer. But it’s not working. Also fan now starts from the very beginning. I’ve measured the voltage on LM35 and it is twice higher than it does sed to be without the pot. When I rotate the pot LM35 starts to oscillate too. I test it without the led panel and MOSFET. There is no any PWM on pin 0. It is just always 4.5V. I have a 120W 12V power source.

#include <avr/io.h>
#include <util/delay.h>

void setup()
 TCCR0A = 2<<COM0A0 |  3<<WGM00; // clear 0c0a at compare match //enable fast pwm
 TCCR0B = 1 <<WGM02 | 1<<CS00; //enable fast pwm mode // not to use prescaler
 OCR0A = 79;  // aiming for desired frequency 


void loop() {
    analogWrite(0, map(analogRead(2), 0, 1023, 0, 72));  
    int t = analogRead(3)/2;
    if (t>80) {
      int value = (t-80)+55;  
      analogWrite( 1, value);
    else {
      analogWrite( 1, 0); 


You should post the picture here.
The problem is likely in the code you did not post at all.

Sorry, it never attaches photos from phone.

Please see the schematics and code in the first post attached.

Another strange thing I found out. As I touch the pin connected to the fan with the oscilloscope it starts sending PWM.

Sorry, it was a soldering problem. Attiny was missing the ground.

But it still doesn't work. When I turn on the power, my fan starts, after few seconds it stops. Then it works according to the temperature. The pot is set to the minimum. But as soon as I start to rotate the pot the fan starts at maximum speed. I've measured the voltage on LM35 at this moment. it changes twice when I rotate the pot. What could be wrong?

The CTC mode uses OCR0A as TOP. You cannot use it to generate PWM this way. I don't know how analogWrite is implemented but I guess it simply sets the corresponding OCR. So analogWrite (0, x) sets OCR0A to x - it changes PWM frequency changing duty of pin 1 (OC0B) but does nothing with pin 0. It will be always high.

Ok. Is it possible generate fast (>25kHz) PWM on pin 0 and 1 separately?

Default RC oscillator speed is 9600kHz, using this clock without prescaler you get PWM speed 9600/256 ~ 38 kHz. If you need some other frequency tuning the oscillator via OSCCAL register is your only option (or use an external clock ofc).

I'm trying to understand how to modify this registry, but it seems too complicated for me. Can you tell me how to change it that I can use pin 1 and pin 0 separately to generate fast PWM 38kHz with different duty cycles.

Use Fast PWM with 0xff as TOP instead of OCR0A. Waveform Generation Mode 3, not 7. In other words don't set WGM02.

But how should I implement it with code?

Try deleting “1 <<WGM02 |” in the code you have posted :wink:

Thank you! It seems PWM works fine, but still the problem of pot affecting LM35 is still there. I rotate the pot and the fan starts to rotate. It doesn't look like a noise it's more like a raising the voltage on both pins.

If I read the schematic and code correctly the pot affects fan while LED is controlled by LM35. You should swap pins.
Also the LED is drawn backward.

No, the connection is fine. Cause it the fan is controlled by LM35 if the pot is set to 0, but when I rotate the pot it starts working full speed. I've measured the voltage on LM35. It changes twice when I rotate the pot.

No, the connection is fine.

Double check it - either your schematic or the posted code is wrong.

Maybe there is some noise coupled to the LM35. Try to add a decoupling cap close to its power pins and keep all connections short. But I have little experience with LM35.

Why do you want fast PWM for this project? I think motors and light are fine with much slower PWM.

Also the 220u cap across the motor is huge. I think it will cause you trouble. And if it is polarised it will suffer from motor reversed polarity when you turn the motor off. Usually a small ceramic caps are used for motors.

I need fast PWM cause it is a video light setup. It has to be more than 14kHz and also quit. So I did it faster than 25. Otherwise it produces an unbearable noise.

I will try to take out a 220uF cap.