atmega328p weird situation

Hi all,
I have an atmega328p placed into a 28 pin socket on a pcb I've had desgined and the other components placed and soldered by machine.
When my board is powered up, it's lighting up the LEDs as it should but as soon as I physically touch the top of the 16mhz crystal, the code stops running and freezes. The LEDs that were lit up stay lit, but nothing continues to happen as it should.
Any thoughts on possible reasons why this would happen?
I have a 5v regulator with 10uF capacitor on input to gnd and a 10uF capacitor on output and gnd.
I have a 22p capacitor on each leg of the crystal to gnd.
That's assuming my PCB manufacturer has the values of capacitors correct. Could incorrect capacitor values do this?

Much likely when you put your finger on it you are injecting lot of noise in the oscillator.To solve the crash you could set the internal whatchdog timer to make a reset if it stops running ...

Do you have the two caps on the xtal? Usually the problem can be fixed by soldering a ground lead on the xtal housing.

// Per.

What's xtal? Oh crystal? Sorry.

Ok so if I have my board in an enclosure, there shouldn't be any issues? How can I stop this from happening in future versions of this board. Because arduino boards don't freeze when you touch the crystal

I didn't hear the 0.1uf decoupling capacitors on that list... You do have them, right? If not, that would explain the problem. You must have a 0.1uf decoupling capacitor each on both Vcc pins and AVcc (to ground, in all three cases) as close to the chip as possible.

Damn, no I don’t have 0.1uF caps. So as well as the 2 x 10uF?

And of course, you did make sure the crystal is directly adjacent to the MCU, with traces no more than 15 mm between?

Crystal right next to chip

Damn, no I don't have 0.1uF caps. So as well as the 2 x 10uF?

Yup - every chip needs decoupling caps, 0.1uf between Vcc and Gnd as close to the pins as possible, for every Vcc pin, and AVcc and gnd. And ARef and gnd.

Ok thanks. Will these boards have issues then if I use them?
Maybe I could retrofit some caps

OK. It is somewhat improbable that it is the loading effect of your hand capacitance that affects the crystal operation and in any case it would start itself again as soon as you removed your hand. (Does it?)

More likely you are causing ESD and crashing the MCU from the impulse as static discharges. The solution is as advised, to ground the crystal case. There are various notions as to whether it is appropriate to solder to the crystal case though this is extremely common in commercial designs. At the very least, you could solder a wire strap over the crystal, grounded to one of the points where the loading capacitors mount.

Thanks Paul,
It doesn't restart when I remove my finger.
So why doesn't an arduino board crash when I touch it's crystal?

Many or most Arduinox use a ceramic resonator rather than a crystal. You may be putting your finger on the crystal for the 16U2 by mistake as it is more obvious than the resonator for the ATmega328.

If the resonator has three terminals as it includes its own loading capacitors, then its case is almost certainly grounded already.

It doesn't restart when I remove my finger.

Then you are almost certainly crashing it by static discharge. Will probably not happen if you are holding a ground terminal before you touch it.

Maybe I’m using the wrong term. It’s the silver thing in the attached picture with 16.000 written on it. My board is the green one behind the arduino. The component looks exactly the same. Both 2 pins. But I don’t have 0.1uF capacitors

Yes, that its the crystal for the 16U2, the chip below it in the picture, which is the USB interface. If you were to crash it, you lose USB connectivity, but the sketch in the 328 continues to run.

The ceramic resonator for the ATmega328 is the tiny little silver rectangle to the left of your photo, immediately below the ATmega328.
And if you look, it has three terminals - it contains the loading capacitors.

Note the UNO schematic which shows the resonator and its two capacitors as a CSTCE16MOV53-50, not specifying the capacitors as separate items. A quick test however reveals that its metal shell is in fact not grounded (or connected in any way).

Your board looks nice - if rather sparse. :grinning:

Ok thanks. Will these boards have issues then if I use them?
Maybe I could retrofit some caps

I would expect the board to crash, glitch, or reset itself the moment you attempted to change the state of any IO pin that's connected to a load (for example, lighting an LED), if it even managed to successfully finish the programming process (often without caps, it will fail in programming process). You must have decoupling caps.

With that board layout, you should have no problem sticking some caps on the back of the board. 0.1uf ceramic caps - you will likely use more of those than any other single component in your designs.

Hi all again,
I've just soldered 3 x 0.1uF caps as suggested between vcc -> gnd, aref -> gnd and avcc -> gnd.
Still crashes when I touch the top of the crystal.
So maybe I'll try grounding the crystal case.
Interestingly, I've had one of my boards powered up running the LED outputs for almost a week now, running 24hrs a day. It's working perfectly. I bet if I touched the crystal though it'd crash...

I guess my next question would be how do I design my next board so I don't have this problem? I can't ground the crystal in the design software since it doesn't have a pin connected to its case...
I come back to the arduino(uno), can I copy something it has that it doesn't crash like mine does?

Ok I grounded the crystal case and I can touch it, but if I touch one of the solder points of a leg of the crystal, it crashes.
Only one of the legs mind you.
The other one I can touch and it doesn't crash. Anyone have more ideas for a solution?

Don't touch the crystal (pins).

Wow! That was simple!

Go back and read my reply #10.