I did some more testing, it only happens on ATMega8, ATMega328 works fine so text below can be ignored in case of it.
In short, when I use PWM output on arduino pin 9 with ATMega8 MCU freezes, digitalWrite works, analogWrite doesn’t. I don’t know what could be the issue, ATMega328 works fine with same circuit, configuration and code.
Hi, I’ve stumbled on a problem (as expected) with using PWM on ATMega8/328 while using internal 8MHz oscillator. Simply put, I can’t get PWM to work. On ATMega8 it freezes MCU and on ATMega328 it doesn’t seem to do anything. Are there some limits on using PWM (analogWrite) while using internal oscillator?
That’s my problem in short, now the whole story :). I’m trying to make custom Lego train motor controller. I used Arduino Uno for development, it’s rather simple actually, IR 1838B reciever for remote controll, few leds to be used as front and back lights on the train, and L293D to drive DC motor of Lego Train Power Functions.
With remote I can turn on and off front and rear LEDs separately and I wanted for DC motor to have several speeds that I will control via PWM on pin 9 (0%, 50%, 75% and 100%). While everything is connected on breadboard it works fine.
It should go inside Lego train so I need it to be as small as possible. So I’ve made minimalistic PCB and I wanted to use Atmega8 (that’s why I chose pin 9, it’s OC1A on both Atmega8 and Atmega328) as code fits in it and I have quite few of them in TQFP package, to save some space I decided to use internal RC. I’ve made the PCB, soldered the components, recompiled it for ATMega8 and flashed it to MCU via USBAsp. It seemed to work fine until I tried to start the motor, nothing happened and when I tried to start front/read LED’s also nothing happened. Like MCU froze.
I changed the code to simple on/off (digitalWrite instead of analogWrite) and it works as expected. No more lockups, I can turn it on, reverse it, turn LEDs on and off…
After that I built similar test circuit on breadboard with simple leds so I can test it on another ATMega8 to see if the one I soldered was faulty. I again tested this simple code on Uno and it worked fine. Recompiled and flashed it to ATMega8 and it froze again. Simple led on/off works, but PWM one freezes MCU. I used to LEDs, one remote button toggles on/off of one of these LEDs, and the other one changes PWM value of analogWrite for the other LED. On/off works fine, but as soon as I press button to trigger analogWrite on the other LED it freezes, I can’t even control on/off LED until I reset MCU.
I tried the same thing with one ATMega328p I had around, the same as one used on Uno, except I configured it to use internal 8MHz oscillator instead of external 16MHz as it’s on Arduino and PWM similarly didn’t work. Only improvement was that MCU didn’t lock up, PWM LED didn’t react, but other LED still reacted. I also added test code to PWM part to see if it even triggers (simple ++ counter that counts how many times I pressed remote button and then it blinks the other LED that number of times) and that also works, so it’s not that it “skips” that part of the code.
Sorry for the long story