Suggestions needed for an AVR board. - Page 1
Forum section: electronics / projects

author: ghani256
author_rank: Contributor
timestamp: July 26, 2017, 03:31:33 pm
content: I have just finished laying out a PCB for a AVR clock project that I am designing. This will be my first board built around a microcontroller and I want you guys to give your valuable suggestions so that I can learn from them.You can see the schematic as well as the board below. In designing the board I had to choose among a number of competing design considerations.First of all the crystal. Keeping design guidelines in mind, I placed a ground plane under the crystal. I tried to place the crystal as close to the AVR as possible, and I had to use short 90 degree traces on the loading caps and connect to the ground plane to avoid the stray inductance. I tried to route using only the top layer, but two jumpers were unavoidable. Still the ground plane is pretty much continuous.The decoupling capacitors were placed close to the supply pins. Also there is a plated via under the TQFP package of the AVR. I hope that will not cause any problems.Let me clear about the board a bit. JP_LCD is a jumper to which 7 wires of an 16x2 LCD connect ( as for the power pins, I will be supplying power to the LCD from another board therefore power pins are not needed here. Same goes for contrast control.) JP_SPST connects to four pushbuttons, which will also be on a seperate board. POW is for VCC and GND and BATT is for connecting a CR2032 cell to the RTC chip.So, take a look and give me some feedback. Thank you.

author: georges80
author_rank: Frequent Contributor
timestamp: July 26, 2017, 03:53:09 pm
content: Other than making things 'easier', why have an clock/calendar chip when you have a microcontroller already? i.e. why not write your own code to keep time/date (deal with leap years etc etc)??I prefer 10k for the reset pullup.Hope you have a decent common ground going from your uC board to your LCD board.The internal pullup resistors (which I assume you will configure for your switch inputs) are rather weak, especially with long wires to the switches and noise input - i.e. if any are used with pin change interrupt then you can get mis-triggers. All depends on your planned use.Via under the uC is not an issue.I'd shorten the ground via to caps trace length (to your bottom ground plane) just for good measure.cheers,george.

author: ghani256
author_rank: Contributor
timestamp: July 26, 2017, 04:13:03 pm
content: Hey thanks for the feedback.Rest assured, all the wires I will use will be small, on the order of 6-7cm, because I will mount everything in a pretty small case and connect them together.The main reason for using the RTC is in case of power loss, it will keep the current time and run off its backup battery.Here is a little updated version. I have shortened the traces between the caps and the vias which connect to ground. I have also modified the position of the crystal a bit. Take a look.In terms of overall design, would you say is this a good design or a bad one?

author: georges80
author_rank: Frequent Contributor
timestamp: July 26, 2017, 04:50:10 pm
content: You can battery back the uController too.... An opportunity to write some energy efficient code to throttle the clock speed (change divider), use standby modes that wake on timer interrupt etc etc, based on detecting if the main VCC input had gone out and are operating on battery backup. You could easily get to sub mA without trying. Good enough to deal with power outages etc. The cell could recharge when power returns etc.Layout will be fine - AVR are pretty robust/tolerant.I'd add one bulk cap to the VCC rail for good measure (something in the 10 - 20uF range is good enough, ceramic will do fine).cheers,george.

author: ghani256
author_rank: Contributor
timestamp: July 26, 2017, 05:04:12 pm
content: I will definitely add the capacitor you suggested.Apart from that, will this design work well, so should I consider it as final and send it out for fabrication? ( I will be using Seeed)It has been 3 days since I have been working on this board and I would like to get it done with

author: alm
author_rank: Super Contributor
timestamp: July 26, 2017, 05:11:24 pm
content: AVR042recommend a pull-up resistor of at least 10 kOhm to be able to use debugWire (hardware debugging on the AVR). That entire appnote might be a good read, since it is exactly about this topic. 22 pF sounds quite low, what is the load capacitance of your crystal? See page 14 of AVR042 on how to calculate the capacitors.C6 is far from the Vcc pin of IC2, but I am not sure if that matters for this IC. Ideally you would want it closer to the power pin. I do not love the routing of the power trace through the ISP header all the way around the board. I would connect pins 4 and 6 together with a short trace and a single decoupling cap. I would put a via near the pin 3 and 21, instead of running ground through C5.

author: georges80
author_rank: Frequent Contributor
timestamp: July 26, 2017, 05:14:54 pm
content: If all the pin assignments etc are correct, then should electrically function.I'd check the clearance between your uC pin 9/10 traces, they seem a bit tight - you could open that area up a lot, no need (or desire) to run traces too close together. Your board is pretty sparse - put a bit more space between tight traces.cheers,george.

author: ghani256
author_rank: Contributor
timestamp: July 26, 2017, 05:42:08 pm
content: QuoteI will be using a cheap USBasp programmer to program this so I do not think I need that.Also, I looked at the appnote an it says for standard high frequency crystals, the recommended capacitor value is 22-33pF , so I am just going with that.@alm I will take the rest of your suggestions into account.QuoteI will do just that. Sometimes you do not see the obvious.

author: alm
author_rank: Super Contributor
timestamp: July 26, 2017, 06:02:41 pm
content: Quote from: ghani256 on July 26, 2017, 05:42:08 pmCareful. The appnote is talking about ceramic resonators and crystals asresonators, and about external oscillators likethisasoscillators. An oscillator will produce a clock signal if you just apply DC power. A crystal works together with the oscillator inside the AVR to create the clock (see Figure 5-1 (1) in the appnote). Foroscillatorsthe suggested capacitor value is 22 pF - 33 pF.Forresonators, equation 2 on page 15 is the suggested way. CLis from the crystal datasheet, and CSdepends on the board layout. So for a crystal with CL= 18 pF, the capacitors should be something like 30 pF. For a crystal with CL= 33 pF, the capacitors should be about 60 pF.

author: janoc
author_rank: Super Contributor
timestamp: July 26, 2017, 08:20:27 pm
content: Quote from: alm on July 26, 2017, 06:02:41 pmPlease, do re-read the appnote. Your advice is somewhat incorrect.From the text, second paragraph of section 5.3 (emphasis mine):"When using the external crystal oscillator, crystals with a nominal frequency range starting from 400kHzcan be used. Forthe standard high frequency crystals, the recommended capacitor value range is in therange of22pF - 33pF"By "external crystal oscillator" they obviously referring to the internal oscillatorwithan external crystal, not a can oscillator like you have linked to from Digikey - for that one discussion about load capacitors wouldn't make any sense! The app note is not written in the best way, there are several such issues just on that one page.So the OP had it correct and 22pF is actually a very typical value used for AVRs (and not only for them) when a normal crystal is used. You have to also take into account the parasitic capacitance of the tracks/leads, so with a 22pF cap the actual capacitance seen by the circuit will be higher, probably closer to the 30pF. Fairly safe bet for most cheap crystals where there is no datasheet.Anyhow, for a one-off hobby circuit this is really not an issue - if the oscillator doesn't start reliably (never had that situation but could happen), it is trivial to swap the capacitors for another value.

author: alm
author_rank: Super Contributor
timestamp: July 26, 2017, 08:32:37 pm
content: You are right, my reading of the oscillator vs resonator was wrong. However, two (effectively) 30 pF caps inparallelseries is 15 pF. That is still low for a 8 MHz crystal, though it will likely work fine. The lowest CLfor an 8 MHz crystal I found on Digikey was 18 pF, and the highest 33 pF. So I maintain that the given values are low for an 8 MHz crystal.

author: janoc
author_rank: Super Contributor
timestamp: July 26, 2017, 08:40:27 pm
content: Quote from: alm on July 26, 2017, 08:32:37 pmHmm, I have always thought that two caps in parallel add up, so two 30pF caps in parallel is more like 60pF total ...

author: alm
author_rank: Super Contributor
timestamp: July 26, 2017, 09:02:39 pm
content: Clearly I suck at explaining app notes today. Just read the equation in AVR042, or one of thevariousotherappnotes on parallel resonant crystals and explain me how 22 pF to 30 pF makes more sense than 30 pF to 60 pF for an 8 MHz crystal with CLbetween 18 pF and 33 pF.

author: rstofer
author_rank: Super Contributor
timestamp: July 26, 2017, 09:09:05 pm
content: Looking at JP-LCD, I'm not fond of a trace hitting a pad at an angle when it isn't necessary. Look at the 3rd pin from the right. I can't convince myself that there is sufficient clearance between the trace and the 2d pin from the right.

author: NivagSwerdna
author_rank: Super Contributor
timestamp: July 26, 2017, 09:25:12 pm
content: You have plenty of space....... it looks a bit like an Ardunio Uno..... you can bring TX, RX and GND and RESET out to a pin header (which you need not populate)... but might be useful later if you ever decided to put the Arduino bootloader on it. (You could then reprogram via serial using an appropriate USB/TTL converter)... also put a place for an LED and Resistor to a spare line.... you can populate and blink.(https://www.mikrocontroller.net/attachment/325540/CH340G_UNO_ArduinoBoard_3.pdfis a nice design to borrow ideas from ... you could drop all the CH340G stuff but keep the C5,R2,D2 bit which is the DTR/RESET logic and have a board you could re-purpose at a later date)(Try to avoid using pins 19 and 22 as outputs.. I made that mistake twice!)I like the DS3231, you could also add a 3V Battery holder on the board for backup. (but it takes an annoying amount of real estate) (I buy DS3231 modules off ebay and use the parts required, ignoring the bits I don't need)https://edwardmallon.files.wordpress.com/2014/05/rtc-goodparts.jpg

author: NivagSwerdna
author_rank: Super Contributor
timestamp: July 26, 2017, 10:10:54 pm
content: Only my 2p worth...... if you put the headers along the edges then that gives you better routing options since they cut through on both sides and can be a PITA to route around.

author: janoc
author_rank: Super Contributor
timestamp: July 26, 2017, 10:50:35 pm
content: Quote from: alm on July 26, 2017, 09:02:39 pm22pF-30pF is actually ok for crystals with CLaround 18pF which are the most common case (totally nonscientific guesstimate from what Farnell stocks). Assuming 10pF stray capacitance (it is a through-hole board), that gives you the range of 16-20pF for the CL, covering the most crystals commonly sold.The rule of thumb formula is C = 2*CL- CsThe C is the value of each of the caps, not their combination.So for an 18pF crystal and 10pF stray capacitance, you get: C = 2*18pF-10pF = 36pF-10pF = 26pF. So the common 22pF cap is a pretty good match there.With your range of 30-60pF you would need crystals with CLfrom 20pF to 35pF. Those aren't really common.Adafruit has the calculation explained (she uses 2xCsbut that's a detail):https://blog.adafruit.com/2012/01/24/choosing-the-right-crystal-and-caps-for-your-design/In the previous reply, when you were talking about the parallel (now corrected to serial) combination, you were almost there - 15pF (from two 30pF caps) is right on the money for an 18pF crystal, because you need to add in the stray capacitance of the board/leads too (the Csvalue in the formula). Which is cca 5-10pF - exactly what is missing.

author: tooki
author_rank: Super Contributor
timestamp: July 27, 2017, 01:31:53 am
content: If you're going to use a RTC -- which, unlike georges80, I think is a very good idea -- then use the DS3231. It's far more accurate than the DS1302/1307. The 3231 has an accurate temperature compensated internal oscillator, whereas the crystal circuit for the 1302/1307 is finicky. (I've built a number of clock kits that use the 1302, and they are all over the place. The 3231-based clocks just work right.)

author: ghani256
author_rank: Contributor
timestamp: July 27, 2017, 05:52:17 am
content: It would be great if someone could clear up if I need to replace the 4.7k resistor with a 10k one. I will not be using any advanced debugger I will just be programming using a simple USBasp.QuoteYes I have space and I will clear that up. Thanks for pointing it out.QuoteHmmm this can be useful for debugging purposes. I have some pins free on PORTC. Guess I will use one for this purpose.@tooki: Well currently I have completely designed it to work around the DS1307, and it would not be wise to replace it right now. I will surely use the 3231 in future updates.

author: westfw
author_rank: Super Contributor
timestamp: July 27, 2017, 07:18:46 am
content: A couple of additional comments:An 8MHz AVR isn't all that critical. You don't need to be so worried about maintaining your unbroken ground plane, for instance. Single-sided versions of Arduino (16MHz with no ground plane at all) work fine, and the standard arduino has many traces on both sides.I think the 10pin programming header is losing out to the 6pin version these days. That's up to you, and depends on what device programmer you'll be using.I would make the VCC traces thicker, especially for the "long" runs not connecting directly to the chip.Go ahead and put a ground plane fill on the top layer as well. It's probably good for noise suppression, not to mention static avoidance (maybe? The idea is that a pokey finger will discharge into the nice robust power supply instead of a sensitive IO pin.)(combining 3 and 4, I tend to put a ground plane and thick VCC traces on the back for power dissipation, with the top-layer ground plane doubling as paths over the VCC traces, like the PCB here:https://farm1.staticflickr.com/685/32224166375_0ffcdf6889_z_d.jpg)Um. Power (VCC) for the LCD?

author: westfw
author_rank: Super Contributor
timestamp: July 27, 2017, 07:30:36 am
content: Make sure you spend some time looking at what the silkscreen layers are going to look like. One tends to get so involved with making the traces work that you forget that the drawing a text info is useful. Go ahead and make copies of important names on the back side of the PCB, label signals that don't have names (on both sides), add a copyright notice and aversion number...Look at the different layers with a gerber viewer (if you have trouble finding a working gerber viewer, the preview screens at OSHPark can be used instead. Their web interface is substantially brilliant and way beyond almost everyone else!)

author: ghani256
author_rank: Contributor
timestamp: July 27, 2017, 11:24:30 am
content: Thank you everyone for your valuable suggestions and feedback.This here is the final version of the board I will be sending to the fab house.

author: Fungus
author_rank: Super Contributor
timestamp: July 27, 2017, 11:54:50 am
content: If you're using a DS1307 RTC then why do you need an external crystal?The AVR chip will be perfectly happy using its internal clock.

author: ghani256
author_rank: Contributor
timestamp: July 27, 2017, 12:34:38 pm
content: Well the buttons am I using, they require debouncing. The debounce code I use requires pretty accurate timing therefore the use of the external crystal.Besides I did some research and in most scenarios its just advised to use an external crystal instead of the internal RC Oscillator.

author: Fungus
author_rank: Super Contributor
timestamp: July 27, 2017, 12:45:52 pm
content: Quote from: ghani256 on July 27, 2017, 12:34:38 pmIt's an 8MHz clock (more or less).I'd be interested to see some button debounce code needs to run at 8MHz and be accurate to 1%.Quote from: ghani256 on July 27, 2017, 12:34:38 pmCitation?

author: ghani256
author_rank: Contributor
timestamp: July 27, 2017, 01:05:38 pm
content: Well the Arduino board uses the external crystal so I though I would just go with it.Also it is communicating with the RTC using I2C. Is accurate timing not required for it?

author: alm
author_rank: Super Contributor
timestamp: July 27, 2017, 01:17:15 pm
content: Common reasons for using a crystal is either because you need a faster clock than the internal 8 MHz RC oscillator, for accurate timekeeping or for UART (asynchronous serial, like RS-232). The internal oscillator is not stable enough for reliable UART communication without tricks like oscillator calibration. The reason is the lack of clock signal (only RX and TX compared to CLK, MISO and MOSI for SPI), requiring the sender and receiver both to provide their own clock. If the sender and receiver do not agree on the clock rate within a few percent, they can get out of sync. This is not an issue with I2C (TWI) and SPI.Using an external crystal does not hurt, but it may not be necessary for this application.

author: ghani256
author_rank: Contributor
timestamp: July 27, 2017, 01:18:53 pm
content: It is alright. I mean I want to use it. It will just help me learn and also provide some groundwork if I want to use the crystal in one of my future projects.

author: Fungus
author_rank: Super Contributor
timestamp: July 27, 2017, 01:44:46 pm
content: Quote from: ghani256 on July 27, 2017, 01:05:38 pmArduino needs accurate timing for RS232.Quote from: ghani256 on July 27, 2017, 01:05:38 pmNope. I2C has a line with clock signal, timing isn't critical.

author: ghani256
author_rank: Contributor
timestamp: July 27, 2017, 02:15:36 pm
content: Well, since I spent so much time on the crystal layout and my initial plan was to use it, I think I will just go ahead with it. If it does not seem to work as intended or causes any problems, I will just program the fuses to use the internal oscillator.

author: Fungus
author_rank: Super Contributor
timestamp: July 27, 2017, 03:07:06 pm
content: Quote from: ghani256 on July 27, 2017, 02:15:36 pmI'm sure it will work. I'm just pointing out that it costs money and there's no good reason for it to be there.If you're just experimenting and only making a few then it's no problem.