crystal problem

I have designed and had manufactured (and soldered) a standalone atmega328P-PU to just do the functions I need it to do (4 analogue inputs, 6 digital outputs and a port connected ready for i2c communications in future for an LCD readout).
All of the parts have been sourced by the manufacture house as per my specifications.

The problem I'm having is with the 16mhz, 2 pin crystal (this one)

First batch I received of the completed board, the manufacturer seemed to use a different brand crystal. The code ran fine, but would instantly freeze if I touched the crystal with my finger. Wouldn't restart the code if I lifted my finger. Only when removing power, then powering back up would it be ok again (no reset button).

Second batch it seems they have used the crystal linked above. Again the code freezes when the crystal is touched, but 2 out of the 10 wouldn't even run any of the code at all.
On a hunch, I desoldered the new crystal and soldered in a spare I had lying around and the code runs fine (still freezing upon touch).

I can live with the freezing since it's living in an enclosure, but I'd really like to not have 20% failure rates of my boards if I order in larger quantities (these will be going out to paying customers).

I have a 22p cap on each leg of the crystal. From what I've read, the value doesn't need to be perfect. But the data sheet of the crystal states load capacitance of 18pF. Should they be 18pF instead, would this help?

Or am I maybe just dealing with defective crystals from factory?

More info if it helps: I have 0.1uF cap to ground from pins VCC, AVCC, AREF. As well as 10uF caps to ground from the input voltage, and the 5v output from the regulator.

Show us a photo and schematics please.
The 22pF instead of 18pF is okay.
All GND pins need to be connected to GND.
The power should be applied to VCC and AVCC.
The crystal and both 22pF should be very close the ATmega. Together with the 100nF decoupling capacitor it should run just fine.

The cheapest crystals without brand are bad, they might stop working below 0 degrees Celsius. Dropping a crystal on a concrete floor might break it.

I think the problem is most likely one of these:
1 ) Perhaps the crystal is a few centimeters away from the ATmega chip.
2 ) Or somehow the decoupling of VCC is not okay.
3 ) Maybe the voltage regulator is oscillating.
4 ) Is your reset is floating ?

But it might also be a wrong fuse setting of the ATmega. Do you use a standard Arduino bootloader ?

moggle_spears:
I have a 22p cap on each leg of the crystal. From what I've read, the value doesn't need to be perfect. But the data sheet of the crystal states load capacitance of 18pF. Should they be 18pF instead, would this help?

Most likely, the board layout or the board substrate (material) are to blame. If you take a look at commercially designed boards, you will note that the crystal is usually surrounded by a ground-plane, or copper pour. This adds stability. Also, inexpensive board materials may inhibit high-frequency operation.

The crystal technical reference will specify the most appropriate load capacitor values. The PDF for the Citizen crystal specifies 18pF, but that is the maximum which includes the PC board runs, too.

Not all crystals do well with surface-mount soldering techniques. The heat of a reflow oven can skew the crystal and it may not recover from the process. You may need to just hand-solder the crystals. You may also need to ground the crystal case is the PC board layout does not provide ample ground pours. In the old days of Ham Radio RF home-brew, tin-can crystals were usually soldered by hand a a wire was also soldered to the crystal can to ground.

Check out X3 near the center bottom of this pix and note that the two crystals have a 3rd wire ground.
X-Tal Can Ground

Ray

I've been using these SMD crystals, smaller footprint than H49/S also. Can see one here, soldered on the pads of a SMD H49/S part (ordered them by accident for this build, now I have an eagle footprint so I can add them to boards for a better fit).

Your first step is to solder a wire to the case from the adjacent ground, performing the soldering as quickly as possible. This will definitely prevent the sensitivity to touch. If it then starts correctly on power-up, you have won, if not, you lose. :grinning:

Note that the specified crystal loading capacity is the total capacitance across the crystal. but this is divided into the two capacitances on each pin in series, with each pin capacitance being the parallel value of the capacitor, the MCU pin intrinsic capacitance and the PCB. So if each pin capacitance totals 36 pF, then you have 18 pF across the crystal.

A common finding by experimenters here using the MCU in a breadboard, is that it works with no capacitors but will not work when they are added, this phenomenon due to the capacitance of the breadboard channels. :astonished:

Thanks for all your replies so far.

Attached is a photo of my board. The crystal and caps are quite close to the chip. Not close enough?

Also attached is a photo of what I did grounding the crystal case. This solves the problem of touching the crystal, but if I touch one of the legs (the solder point underneath), it freezes.

Arduino can ship it's uno boards without any of these problems. Someone has said it uses a resonator, would this be a better option for me? More reliable? I'm not timing anything in the code, just reading analogue inputs continuously.

If uno uses a resonator, why does it have a crystal clearly on it's board?

you might try changing the fuse for full swing instead of the foolish arduino default. i study crystal behavior professionally and as a hobby without ever seeing one that don't start up when you remove your hand. at least not one manufactured last 50 years.

also worth mentioning unless somewhat more precise frequency is desired i dont bother with any caps at all. in that case makes more sense to use the $1 ebay rtc modules anyway.

How did you connect the 22pF and the crystal ?
Did you connect both GNDs ?
We need to know/see more, that is not enough.

moggle_spears:
Also attached is a photo of what I did grounding the crystal case. This solves the problem of touching the crystal, but if I touch one of the legs (the solder point underneath), it freezes.

Here's a hint: You aren't supposed to touch them! :astonished:

actually most crystals will stop oscillating if input pin is loaded. what never happens, at least with oscillator configured properly, is failure to start when load is removed. which as i mentioned seems to be the case with op.

Paul__B:
Here's a hint: You aren't supposed to touch them! :astonished:

I absolutely agree! And I'm more than happy to not touch them, and it'll live in it's enclosure just fine. I just included the info if it helped someone figure out the failure to start problem I've seen in a few of the boards.

Koepel:
How did you connect the 22pF and the crystal ?
Did you connect both GNDs ?
We need to know/see more, that is not enough.

See attached screenshot. Does this help? There's more to the project, but this is the atmega chip and power supply.

Aref should not be connected to 5V.
How will you program the part with Reset tied to 5V?
Ground planes would have been better than ground trace meandering around.
Change low fuse byte setting to have Full swing crystal oscillator, suggested above, could help.
Leave upper 4 bits alone, make lower 4 bits of low fuse byte = 0111.
Change settings in boards.txt and reburn bootloader to change the fuses.

Crystal on the Uno is for the Atmega16U2.

Cool thanks.

So aref should be left not attached to anything? Not sure where I read to connect it to 5v. Should it still have a cap?

I program the chip on an arduino, then pop it out, and put it into my board, so I don't need the reset on mine.

I haven't learnt about ground planes or low fuse byte settings, I'll do some research.

Thanks for the info!

moggle_spears:
So aref should be left not attached to anything? Not sure where I read to connect it to 5v. Should it still have a cap?

From the datasheet...

The voltage reference may be externally decoupled at the AREF pin by a capacitor for better noise performance.

The internal voltage reference may thus be decoupled by an external capacitor at the AREF pin to improve noise immunity.

...and the reference voltage can be made more immune to noise by connecting a capacitor between the
AREF pin and ground.

This is why the connection to 5V is a bad idea...

If the user has a fixed voltage source connected to the AREF pin, the user may not use the other reference voltage options in the application, as they will be shorted to the external voltage.

With AREF connected to 5 V, if you try to enable the 1.1 V internal reference you will fry the processor.

moggle_spears:
So aref should be left not attached to anything? Not sure where I read to connect it to 5v. Should it still have a cap?

Certainly is not attached on the Arduino, which would be your guide. Some poor "instructable" perhaps? Of course it should have a capacitor if you are going to use Analog.

moggle_spears:
I program the chip on an Arduino, then pop it out, and put it into my board, so I don't need the reset on mine.

Using a ZIF socket?

Better design is to have the ICSP pinout on your board and use the Arduino as ISP. That does mean the reset should have a resistor. If you have no provision on your board for serial programming, then you do not need the bootloader and downloading using the ICSP removes it anyway.

moggle_spears:
I haven't learnt about ground planes or low fuse byte settings, I'll do some research.

If using a double sided board, all unused parts of the board should be "flood fill" with "thru"s connecting it to ground. That's the ground plane.