I'm looking to design a PCB with the ATmega328 and USB type B, to easily upload new code to it. I'm struggling to understand how the USB type B is wired to the ATmega328's pins and what's all going on there electronically. I understand the Arduino has an analog to serial converter, and that you can theoretically upload new code to an already soldered ATmega328 by wiring it from an arduino, basically using it as a converter, but I'd rather just incorporate the USB type B into the design and upload code that way.
The USB is wired to a USB to serial converter chip and that is wired to the ATmega328 chip physical pins 2 and 3.
Look at an Uno or Nano schematic to see how it is wired.
Actually the AT328 has an analog to digital converter allowing it to convert an analog signal to something the code can understand.
Not everything can easily connect to a USB port. To make a board out of the AT328 you would need to add a USB - Tx/Rx converter. FTDI makes one and Silicon labs makes one.
Given you're new to µP board design and I assume PCB design I suggest you start on something more basic. Also think about how you would solder the AT328 to the PCB (SMD pins are really small).
To make it easy to upload code, you should just purchase a board that has that function built in.
That's what I do usually as well. In fact, I generally implement two ways of in-system programming when using a 328P(/B):
- 6-pin ISP header (for use with an USB-ASP programmer or Arduino-as-ISP)
- USB over CH340G (although I'll probably migrate to CP2102 or something else; same principle though)
There's no (feasible) way you can hook up a USB connector directly to a 328 and have a working Serial connection. You need a USB-Serial converter chip.
Yeah, those are the somewhat more expensive ones; i.e. they cost more than an Atmega328P.
CH340G is the Chinese budget option.
The real question is, can a novice be able to solder all this together?
When I build a stand alone 328 board I attach a 5 pin header that connects to reset through a 0.1uF cap, RX pin, TX pin, Vcc and ground. Then to program it with the IDE using serial I connect a USB to serial adapter and program through the serial port. That way you do not have to include the USB to serial adapter on the PCB.
Connect adapter DTR to reset, adapter RX to TX, adapter TX to RX, ground to ground and Vcc to 5V or 3.3V whichever is appropriate.
CP2102 from SilLabs is a QFN package, which is tricky to solder. FTDI offers different products, giving the choice between regular SMD (albeit narrow pitch; looks like .5mm) and QFN. CH340x chips have a remarkably wide pitch (1mm or so, perhaps even 1.27; check datasheet) and are easy to hand solder.
@groundFungus' solution is of course both functional and flexible.
How long had you been soldering when you first soldered a 1 mm pitch device?
I think the OP wants to include the USB-Serial adapter to make it easier to upload.
OK. Just offering an alternative to soldering another chip on their PCB.
Thank you all for your wonderful tips and advice. Does this look good, as far as all the connections? Yes, I am still a beginner, so the tips are appreciated!
I'm new to soldering as well, but I think youtube and practice will be my best friend here
I would be concerned about the lack of decoupling capacitors. A 0.1uF on each Vcc, AVcc and Vref to ground on the 328, Vcc to ground on the CH340.
I agree with @groundFungus about the capacitors. You should also know the physical layout of the parts is critical, especially for the crystals.
Speaking of the crystals, you show X1 and X2 as having internal capacitors. Many crystals require external capacitors. I would add the locations for the external capacitor and not populate them if there are unneeded.
You didn't show output terminals for the processor you need to allow access to them in your testing.
Optional but I would add an LED to verify power.
Also optional but newer processors and many devices have gone to 3.3V If you stay with a 5V processor you will need to do level shifting for any 3.3V devices you use.
But you cannot run the processor at 16MHz at 3.3V.
Correct, the op would have to change the crystal to 8Mhz. Although a number of folks in this forum have reported running 16Mhz at 3.3v with no issues.
One of those "works fine until it doesn't" deals. Engineering wisdom days not to depend on things working outside of specifications.
Thanks for all your suggestions! Looks like I was good on the main wiring and routing but the decoupling capacitors and standalone crystals with external capacitors are good ideas for best practice. I'd rather worry about why my code isn't working, not why my board suddenly decided to stop working.
The schematic shows only one cap for Vcc and AVcc. There should be a cap for each one. Decoupling caps need to be as close, physically, as possible to the IC. See here.
Even if I am not using the Analog to Digital converter I still decouple the Aref pin to ground with its own 0.1uF cap.
I've made a number of board for personal use. I've found unless you have a size constraint you should add anything you can think of. It is frustrating to look at a PCB and have to hack it to do something you hadn't considered, so you think.... if only I had an extra pad on "this" pin or provisions for....
For instance, the USB connector shield, don't leave it at the device pin. Bring it to a pad you could easily ground it if you find that is needed.
And for the AREF, and as @groundFungus mentioned, add a capacitor, however you might consider adding two capacitors, you could use the pad for the 2nd Capacitor (leaving the cap off) to connect and external AVREF.
You should be careful of the USB connector you choose. Many USB's are fragile because they don't have enough pad area to be mechanically durable. You might even consider a through hole USB.