Decoupling Capacitors Breaking Atmega328? - Page 1
Forum section: electronics / beginners

author: Sirryan
author_rank: Newbie
timestamp: October 30, 2024, 04:18:48 am
content: Hello,I am using Timer2 on the Atmega328p and running the Atmega off the internal 8Mhz oscillator, but then using an external 32khz crystal to keep Timer2 accurate. The system works very well as long as I don't put in the decoupling capacitors. However, as soon as I plug them in and then power the device, it does the initial LED blink (see code below), but then never flashes the LED again. It still pulls about 10mA the entire time it's on and never fluctuates. Am I just plugging the Capacitors in the wrong spot? They're 15pF capacitors, which the Atmega328p documentation recommends between 12-22pF.Here's my code:Code:#include <avr/io.h>#include <avr/interrupt.h>#include <avr/sleep.h>#include <avr/power.h>#include <util/delay.h>#define LED_PIN PB1 // Pin 9 is PB1 on ATmega328Pvolatile uint8_t timerOverflowCount = 0;void setup_timer2() {// Set Timer2 clock source to external 32.768kHz crystalASSR |= (1 << AS2);// Set Timer2 to normal modeTCCR2A = 0;// Set prescaler to 1024 for maximum power savingTCCR2B = (1 << CS22) | (1 << CS20);// Enable Timer2 overflow interruptTIMSK2 |= (1 << TOIE2);// Wait for Timer2 external crystal to stabilizewhile (ASSR & ((1 << TCN2UB) | (1 << OCR2AUB) | (1 << OCR2BUB) | (1 << TCR2AUB) | (1 << TCR2BUB)));}void setup() {// Disable all digital input buffersDIDR0 = 0x3F;DIDR1 = (1 << AIN1D) | (1 << AIN0D);// Set all pins as inputs with pull-ups disabled to reduce power consumptionDDRB = 0;DDRC = 0;DDRD = 0;PORTB = 0;PORTC = 0;PORTD = 0;// Set LED pin as outputDDRB |= (1 << LED_PIN);// Turn LED onPORTB |= (1 << LED_PIN);// Keep LED on for 1000 ms_delay_ms(1000);// Turn LED offPORTB &= ~(1 << LED_PIN);// Disable all peripheralspower_all_disable();// Re-enable only Timer2power_timer2_enable();setup_timer2();// Enable global interruptssei();}ISR(TIMER2_OVF_vect) {sleep_disable();timerOverflowCount++;if (timerOverflowCount >= 5) { // (32768 / 1024) / 256 * 3 seconds ≈ 93// Wake up the MCU to toggle LEDtoggle_led();timerOverflowCount = 0;}sleep_enable();}void toggle_led() {// Set LED pin as outputDDRB |= (1 << LED_PIN);// Turn LED onPORTB |= (1 << LED_PIN);// Keep LED on for 1000 ms_delay_ms(1000);// Turn LED offPORTB &= ~(1 << LED_PIN);// Set LED pin back to input to save powerDDRB &= ~(1 << LED_PIN);}int main() {setup();while (1) {// Disable ADCADCSRA &= ~(1 << ADEN);// Disable analog comparatorACSR |= (1 << ACD);// Enter sleep mode// Set sleep mode to power save (allows Timer2 to keep running)set_sleep_mode(SLEEP_MODE_PWR_SAVE);sleep_enable();//This portion must stay in the loop***MCUCR |= (3 << 5); //set both BODS and BODSE at the same timeMCUCR = (MCUCR & ~(1 << 5)) | (1 << 6); // then set the BODS bit and clear the BODSE bit at the same time__asm__ __volatile__("sleep");//***}return 0;}And here are the images of my setup:[imghttp://[ Specified attachment is not available ]Thanks!

author: Sirryan
author_rank: Newbie
timestamp: October 30, 2024, 04:20:04 am
content: Looks like a lot of those images didn't work. Here's attempt number 2:

author: Hella_Wini22
author_rank: Contributor
timestamp: October 30, 2024, 04:57:52 am
content: 32 kHz crystals are IIRC usually "lazy" and they don't have caps. Those are usually specified and tuned for faster crystals in MHz range and upthose aren't decoupling caps. They are there to compensate for crystal's parasitic capacitances etc.12 pF is not much in absolute terms and your wireboard adds considerable capacitance itself already.whole circuit around crystal REALLY hates noise and is usually as close to chip as possible and surrounded with GND plane. In your case it is the opposite - it works like a friggin antenna.If there is one thing that is super important when working with digital tech and microcontrollers, it is power plane and quality pof power rail. You have just two wires for power. No fast multilayer decupling cap, where it would be REALLY needed - right at the micro's Vcc and GND lines. ANd perhaps some small electrolytic cap ( 10 uF or so).

author: tggzzz
author_rank: Super Contributor
timestamp: October 30, 2024, 10:28:34 am
content: Quote from: Sirryan on October 30, 2024, 04:18:48 amGood job w.r.t. providing photos; many beginners omit thoseYou have more immediate problems than than the tuning capacitors. For a start you need appropriately connected decoupling capacitors.I would also check the capacitors are 15pF - not 15µF tantalum beads!Then you should be aware of all the stray capacitance and inductance in that type of construction, since it can have Unintended Consequences.One subtleexample of such is shown athttps://entertaininghacks.wordpress.com/2024/03/16/practical-traps-with-a-one-transistor-audio-amplifier-solderless-breadboards-and-oscilloscopes/

author: Kleinstein
author_rank: Super Contributor
timestamp: October 30, 2024, 04:17:51 pm
content: One should have the real decoupling capacitors at the supplies and also connect the analog VCC and analog GND.The bread board can have parasitic capacitance on the order of 10 pF - with additional 15 pF this may already be too much.Form the pictures it is hard to tell the actual size of the capacitors.

author: Sirryan
author_rank: Newbie
timestamp: October 31, 2024, 03:39:41 am
content: Wow! Thank you for the quick replies!Ok, let me summarize what I understand and you can help me fill in the gaps.@Hella_Wini22, "lazy" means that the crystal will oscillate at a lower frequency (i.e. < 32 kHz) because there are no capacitors within the crystal. Meaning I need to add them externally to get it to oscillate at the correct frequency. At least that would normally be the case, but since the breadboard I'm using already has plenty of parasitic capacitance, that's why it works just fine without the additional capacitors. Those capacitors push it over the edge. Is that correct?I'm also understanding that I should add a decoupling capacitor very close to the Vcc and Gnd pins of the Atmega328p? How close is close? Should it be directly soldered to the pins, 0.1 inches away is fine too? I can get away with 0.5 inches?As far as power and ground lines, should I have thicker wires?For the power and ground decoupling capacitor at the Atmega328p, should I have two decoupling capacitors in parallel. One of size 10uF and one of 15pF?@tggzzz, I should be using rats nest (where all the wires are directly soldered together) or a dedicated PCB. In link in one of the articles you posted, you mentioned that matrix boards "arguably... combine PCBs’ and stripboards’ worst features." Is it worth using those? I've got plenty of those on hand. I've included a close up picture of my capacitor here:(Hopefully this works this time. I'm struggling with the inline images. I'll reply again if this doesn't work.)@Kleinstein, Sounds like I should have a decoupling capacitor between the Vcc pin and Gnd pin as well as the AVcc pin and the GND pin of the Atmega.Another question I have is, do I just have the wrong capacitors in general. Is there a difference between a "standard" capacitor and a decoupling capacitor or can I use any old capacitor as a decoupling capacitor?Thanks again for all your help with this!

author: Sirryan
author_rank: Newbie
timestamp: October 31, 2024, 03:40:27 am
content: Hahaha, guess the picture didn't work again. I'll attach it at the bottom of this reply.

author: squadchannel
author_rank: Frequent Contributor
timestamp: October 31, 2024, 04:47:03 am
content: I haven't assembled it with a bare chip on a breadboard in years, but please wire it as per the attached picture. It should work.The jumpers, etc. are too long. layout is probably not good.Jumpers should also be cut and made to match the pitch of the holes.copy this layout, jumpers, and parts placement as is.also, possible that the program is bad.One debugging method I often use is to attach a new LED for debugging and use it like a breakpoint. This is very useful when you don't have a debugger.It can be used when you are stuck in a loop or if else branch etc...Try to build your program so that the debug LED glows after initialization.If it does not light up, something is wrong at the initialization stage. The clock register or output port register settings are incorrect.Also, if you only want to make the LED blink, do not use interrupt, timer, or sleep.If you just want to blink the LED, use delay and while loop. replace _delay with interrupt/timer after blink succeeds.

author: iMo
author_rank: Super Contributor
timestamp: October 31, 2024, 07:05:44 am
content: There are 2 grounds afaik, one Vcc, and one AVcc afaik. You miss one GND (and a decoupling cap at Avcc).The capacitance between breadboard's pins is around 3.5pF, it should work fine, I did it x times on such a board with 328p.. With a 16MHz crystal I would use 12-15pF caps perhaps.With the 32kHz resonators you may add 1Meg resistor in parallel with it.

author: tggzzz
author_rank: Super Contributor
timestamp: October 31, 2024, 09:47:30 am
content: Quote from: Sirryan on October 31, 2024, 03:39:41 amGood to see someone listening and thinkingThat doesn't always happenAll techniques have advantages and disadvantages, and you need to be aware of them. Which is necessary or better depends on what you are trying to do. Choose whatever combination is suitable for the task.People can use solderless breadboards successfully, but equally they can cause subtle intermittent problems which are difficult for a beginner to recognise and avoid. Other construction techniques are, of course, susceptable to poor implementation practices (e.g. long wires, no decoupling!), but it is easier to avoid them.Two other tips: there's a "preview" button, so you can check the formatting/image is OK before posting, and my preference is not to combine multiple replies in a single post (I had to go to the trouble of removing your response to other people).

author: Kleinstein
author_rank: Super Contributor
timestamp: October 31, 2024, 09:50:02 am
content: The decoupling capacitors are not special capacitors, just describing the function of the capacitor in the circuit. The usual type used today are MLCCs, like X7R type, but film type capacitor like MKS would also work. An additional electrolytic capacitor at the supply (like 1 for the board) can also make sense, but would not replace the local decoupling capacitors.Ideally the capacitors should be as close to the chip as reasonable possible (like 0.1 inch). Many chips are not that sensitive and would still work if the capacitors are a bit further away and often also if missing - the main point is more RF emissions and interference inside the circuit, that could cause problems in precision circuits. With many modern chips the supply pins are next to each other and thus easy to add them close.For the bread-board I even went so far as soldering a decoupling capacitor directly to the supply pins (very close to the case to not interfere with the use on the bread board) - this is also a nice reminder on where the supply pins are. For adapter boards (like SOT23 or SO8 to "DIP") for modern OP-amps it also makes sense to have a capacitor on the adapter.For the AVRs they should have AVCC and AGND also connected and a decoupling capacitor there, even if the analog part (ADC, comparator, reference) is not used.

author: tggzzz
author_rank: Super Contributor
timestamp: October 31, 2024, 10:02:37 am
content: Quote from: Kleinstein on October 31, 2024, 09:50:02 amLead inductance is often a problem with modern logic and high-speed opamps.V=L di/dt, dt can be very small, and the rule of thumb is L=1nH/mm of wire. In the mid to late 80s that became a problem with 20/24pin logic ICs - even with proper PCBs and decoupling capacitors.

author: PGPG
author_rank: Frequent Contributor
timestamp: October 31, 2024, 10:43:43 am
content: Quote from: Sirryan on October 31, 2024, 03:39:41 amFist sentence - No!Second - Yes!Crystal don't need capacitors to oscillate. Without capacitors crystal will oscillate at a little (may be 100ppm = 0.01%) higher (not lower!) frequency than its nominal.Crystal to oscillate at its nominal frequency have to 'see' at its pins the right capacitance. This capacitance is the crystal parameter. Typical (for 32768Hz crystals) is 12.5pF, but to make it possible to build generators (in RTC) needing very little current (like 200nA) there are 32768Hz crystals with this capacitance being 6pF (remember this parameter is not the crystal internal capacitance (it also have something like this), but external capacitance to be 'seen'.External capacitors are 'seen' by crystal in serie so to get 12.5pF you expect two 25pF capacitors.But internal to IC generator has its own capacitance (you should be able to read it from datasheet). Let us assume it is 2.5pF. So to calculate the exact capacitors that are needed you subtract 2.5 from 12.5 and get 10pF as a capacitance that is lacking when you connect this crystal to this IC. So in such case if you use two 20pF capacitrors you will ensure the right capacitance needed by capacitor (you should also take in count the PCB track capacitances - so may be two 18pF will be better).If instead of 20pF you will use 33pF the generator will be oscillating at lower frequency. Increasing capacitors you increase the load for generator and at some point it will be overloaded and not able to oscillate.It is possible that in your case when you added capacitors the needed current for oscillate increased so the higher current pulses from supply were needed and as you don't have decoupling capacitors then whenever IC need a current pulse its supply voltage drops down to say 2V (for few ns time) and it make IC stop working.If you will use very fat supply wires it will not help as not their resistance counts but their inductance, and inductance depends mainly on length.When IC need current pulse this pulse going through wires generates voltage drop depending on wire R and L (in this case L is thousand times more important).

author: Kleinstein
author_rank: Super Contributor
timestamp: October 31, 2024, 11:05:26 am
content: The crystal oscillator may very well need some capacitance to work at all. Depening on the chip and crystal type the parasitic / chip internal capacitance can be enough, but it may also be too little. Similar with to high a capacitance at the crystal the oscillation usually stops.The Atmel datasheets usually show the option to switch in internal capacitance to work with a 32 kHz crystal. However in the errata to all the AVRs I know they also say that those internal capacitors don't work. No Idea why the did not simply dropped the internal capacitors from the DS if they don't work anyway.

author: PGPG
author_rank: Frequent Contributor
timestamp: October 31, 2024, 11:17:28 am
content: Quote from: Sirryan on October 31, 2024, 03:39:41 amDecoupling capacitor should have as high capacitance as it is possible but with as low its parasitic serial resistance and inductance as possible.In 80s typically 10nF capacitors were used as decoupling (one per each digital IC). 100nF those time were much bigger or if not bigger were much worse in the sense of parasitic parameters.Now I use 100nF ceramic (MLCC) 0402 capacitors as decoupling.I will try to insert a graphic. Hope I can do itThere I have marked with green circles the decoupling capacitors to answer your question: How close is close?Decoupling capacitors supply current for short pulses needed by digital IC when it changes state (internal registers and output pins).The slower IC it takes longer (like 10ns) but smaller current pulses. The faster IC it takes shorter (like 1ns) but higher current pulses. For shorter and higher pulses the close means closer.What counts is surface surrounded by the IC structure connection to the decoupling capacitor. Because of this modern ICs have VCC and GND together. Also if the current pulse is needed on the left side of structure there is no idea to have it travel through the whole structure so it is better to have capacitor at each side.The IC at my picture is the AtXmega 8-bit microcontroller.I have inserted it (at least it looked so - 'Select File" working here is big riddle for me) but when posted it was no there. But using "Modify" tool helped.

author: PGPG
author_rank: Frequent Contributor
timestamp: October 31, 2024, 11:36:19 am
content: Quote from: Kleinstein on October 31, 2024, 11:05:26 amYou are right. Oscillator may need some capacitance to meet the oscillation criteria.I believe (may be wrong) that these oscillators are always constructed that way that their own parasitic capacitance is enough for them to oscillate with any crystal (in specified frequency range) connected.For crystal itself the oscillation frequency for 0pF external capacitance can be found and is higher than for any higher than 0pF external capacitance.