# PWM didn't work on Atmega8

**URL:** <https://forum.arduino.cc/t/pwm-didnt-work-on-atmega8/248564>\
**Category:** 3rd Party Boards\
**Created:** [July 28, 2014, 11:03am UTC](https://forum.arduino.cc/t/pwm-didnt-work-on-atmega8/248564 "2014-07-28T11:03:15Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [July 28, 2014, 11:03am UTC](https://forum.arduino.cc/t/pwm-didnt-work-on-atmega8/248564/1 "2014-07-28T11:03:15Z")

</div>

Hi, im trying to use my PWM pins in atmega8. According to the image below, atmega8 has 3 PWM pins (9, 10 and 11) So, I created simple test for checking this pwm pins, I connected LED with resistor and I pushed code with ` analogWrite(9, 254);` instruction and nothing happen. If i use `digitalWrite(9, HIGH) ` the LED is working. Also i measured voltage when i'm using` analogWrite(9, 254)` and its about ~0.35V. If I use 255 value instead of 254 in analogWrite, LED is working. I searched google but I can't find any usefull informations.

![Arduino-To-Atmega8-Pins.png](https://europe1.discourse-cdn.com/arduino/original/4X/2/0/d/20ddf932e2d093b3af19636bfda01df50f6e9928.png)

---

<div class="post-metadata">

**Author:** ![Riva](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/riva/32/1013044_2.png) [@Riva](https://forum.arduino.cc/u/Riva)\
**Post date:** [July 28, 2014, 11:10am UTC](https://forum.arduino.cc/t/pwm-didnt-work-on-atmega8/248564/2 "2014-07-28T11:10:35Z")

</div>

Might be better to post your whole code instead of the single line.  
Other posts I have read on this forum say you don't need to do pinMode when using a pin for analogue write but this may not be the case on older cores as used in the mega8. Did you set pin 9 to output?  
Do you have a current limiting resistor on the LED else you may damage the arduino pin the LED is connected to. How is the LED wired?

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [July 28, 2014, 11:22am UTC](https://forum.arduino.cc/t/pwm-didnt-work-on-atmega8/248564/3 "2014-07-28T11:22:42Z")

</div>

I always set pinModes, my whole code

> void setup() {  
> pinMode(9, OUTPUT);
> 
> }
> 
> void loop() {  
> analogWrite(9, 254);  
> }

and circuit below

 ![atmega.png_bb.jpg](https://europe1.discourse-cdn.com/arduino/original/4X/5/c/9/5c9e2b2d0569688bffae62a755a6e7334a017515.jpeg)

---

<div class="post-metadata">

**Author:** ![Riva](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/riva/32/1013044_2.png) [@Riva](https://forum.arduino.cc/u/Riva)\
**Post date:** [July 28, 2014, 2:34pm UTC](https://forum.arduino.cc/t/pwm-didnt-work-on-atmega8/248564/4 "2014-07-28T14:34:56Z")

</div>

> evelan:  
> I always set pinModes, my whole code

Nothing stands out to me as to why that should not work. If I get chance tomorrow I will dig out a mega8 and try the same test.

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [July 28, 2014, 5:26pm UTC](https://forum.arduino.cc/t/pwm-didnt-work-on-atmega8/248564/5 "2014-07-28T17:26:28Z")

</div>

Okey, thanks Im waiting for results. In meanwhile I will try burn bootloader on my other mega8 and check the same code and circuit

EDIT  
So I burned bootloader again on both mega8 and now I think it works fine, but if I put mega8 to my project it behaves as said before. I think (even I'm sure) it because I have weak power filtration (but in my opinion it looks quite well) :~

---

<div class="post-metadata">

**Author:** ![Riva](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/riva/32/1013044_2.png) [@Riva](https://forum.arduino.cc/u/Riva)\
**Post date:** [July 29, 2014, 7:34am UTC](https://forum.arduino.cc/t/pwm-didnt-work-on-atmega8/248564/6 "2014-07-29T07:34:01Z")

</div>

I can confirm it works for me and you also don't need the pinMode command to set a analogWrite pin to output.  
Attached is an image that shows the simple setup. The LED is pulsing as per the sketch.  
For you LED to work as expected when not in the final circuit points to a wiring error. Can you post the full final circuit your testing in that does not work?

```cpp
void setup() {
//  pinMode(9, OUTPUT);
}

void loop() {
  for(byte x = 0; x < 255; x++){
  analogWrite(9, x);
  delay(5);
  }
}

```

 ![photo.JPG](https://europe1.discourse-cdn.com/arduino/original/4X/9/b/5/9b55187e3912165d802b1f5352f9b6054c5ae407.jpeg)

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [May 6, 2021, 4:56pm UTC](https://forum.arduino.cc/t/pwm-didnt-work-on-atmega8/248564/7 "2021-05-06T16:56:05Z")

</div>

Closed on May 6, 2021, 4:56pm UTC


