How to wire a mega328p-AU

Im trying to create a pcb for a project, and to utilise the Mega328p AU (SMD version) to which I can't find too many useful schematics online for my requirements.

I have a few clone nanos which I would take the parts from and re apply to my pcb.

I would like to talk to the Microcontroller via a USB port (Mini or micro depending on which is suitable) as I have micro and mini smd usb ports but most nanos come with a mini usb and I was wondering what's stopping me from using a micro USB port.

This will go to a CH340C chip, which as far as I can tell allows the mega329p to talk to the computer via a USB port, as the online Arduino pdf used an FT232RL and I can't seem to compare their wiring. I have some SMD HC-49S 16MHz crystals which are required and ceramic capacitors.

I would also like to use the reset button which is on all boards and the Leds, but I won't need the external headers.

Can anyone help me by pointing me in the right direction for each requirement or offer any help of their own?

Sorry for such a tedious post but any help is appreciated - Harvey :slight_smile:

Nothing stops you :wink:

Not sure where I got it from, but it might be of use; a link to the original pdf was posted here on the forum a while ago.

nano_ch340_schematics-rev1.pdf (851.9 KB)

Feel free to omit them.

You should probably be careful to make it possible to connect to the ISP programming pins of the chip relatively easily, so that it can be programmed with the bootloader (SMT chips are normally programmed after they are mounted to the PCB.)

It sounds like your best bet would be to simply mount a Nano on your PCB as you are gaining little or nothing, apart from experience, by effectively building your own

If you can consider using a Nano Every then it has castellated pads on the edges which are designed expressly to make this easy and has no components on the back of the board so that it can lay flat on your PCB

@sterretje Thank you, the pdf is really detailed and actually easy to understand, however on the crystals they seem to be pulled to ground but form the middle of the component, does that mean it doesn't matter which side to pull to ground (Or to pin 8 in the diagram).

Also, I didn't mention before but id like the board to be battery powered and only have the micro USB port for data transfer and programming, is it s simple as not wiring the power pins form the usb and replacing the ground and 5V with the output of my battery? (After a 5v regulator)

@westfw if I remove the chip from a preassembled nano clone, would I need to change and boatload the chip again, if so I can always expose the required pins as pads or through holes to wire up and program the chip.

@UKHeliBob I'm simply trying to learn to wire and solder smd microprocessors, and using a nano is cheap and effective (especially as i'm likely to mess up), as I've used a teensy with this method, id like to try soldering this cheaper chip first, plus its a lot easier than a teensy processor.

Thank you - Harvey :slight_smile:

I just wanted to check if my wiring is correct, I have the micro USB port, connected to the CH340C which is connected to the Processor. A 16MHz quartz Crystal is connected to the chip. The board will be battery powered with a transistor to give out 5V at 100mA which is connected to the AVCC and all VCC chips on the Atmega328p-AU. All ground pins are pulled down and power and indicator LEDS also. I am also using my own 3.3v 1A transistor.

One thing i'm not sure about is if the TX and RX pins are the correct way around, whether TX goes to RX as I've found this to be the issue when using external component before (Radios).

Thank you for any help - Harvey :slight_smile:

edit: Also do SCL and SDA need to be pulled up to 3.3v via a 10k resistor?

Here's my schematic of the chip and power, The sensors and storage derives have been left out as the wiring has worked on previous projects -

Does SCL and SDA need to be pulled upto 3.3v, I have 4.7k resistors pulling my BMP280 and MPU9250 sensors unto 3.3v. Do I need to or is there anything else I need to pull up/down to 0V, 3.3V or 5V (Separately) ?

-Harvey

Your I2C bus is OK as it is now, as far as that is visible.
You don't pull anything up / down in addition to what you've already done, that's ok so far. Only: which MPU9250? I don't see an MPU2950 - your schematic seems incomplete. Something seems to be cut off below the BMP280, so it is unfortunately not possible to see whether something is still missing somewhere. So please post the complete circuit diagram, not just a section of the screen.

@Deltaflyer
Here's my full schematic, I realised afterwards about asking about pulling up so it wasn't included in the first picture :slight_smile:

if I do pull up SDA and SCL will it still work properly :slight_smile:
-Harvey

Is VCC 5V or 3.3V ? Either way, there are many problems with the schematic.
The schematic says "LM1117S-5.0", but that could be a mistake.

The ATmega328P can output 40mA with a pin. If you make a digital output HIGH, then it is a strong 5V signal. When you connected that to a 3.3V device, then it might get damaged.
A I2C signal from a 3.3V device is also not enough for a 5V ATmega328P.
That means your interface between the ATmega328P and the SD card, the MPU-9250 and the BMP280 are not okay.

The "RX" and "TX" signals are shortcutted/clipped by the leds.

There is no diode from /RESET to 5V. It could damage the code or even the bootloader when the /RESET pin gets a high voltage via a pulse at the DTR and the ATmega turns into High Voltage programming mode.

Is C13 a decoupling capacitor of 22pF ? Can you make that 100nF ?

I'm not sure about the diode between the 78L05 and the 5V. It is safer, but your 5V will be 4.5V, and if you accidentally reverse the battery polarity, then you still damage the board and the battery.

Only one set of pullup resistors of 4k7 (2k2 to 10k) at the ATmega chip is required. You have 10k with the MPU-9250 and 10k with the BMP280. That is okay.

What kind of buzzer is it ? A piezo disc should have a small resistor to reduce the peak currents (the piezo element itself acts as a capacitor) and a buzzer can be inductive.

@Koepel
Thank you for helping :slight_smile:
The "LM1117s-5.0" is 3.3v which I've now changed its just what the profile was called.

So does that mean the ATmega works on 5v logic level, so do I need to have a 3.3v transistor at each sensor (Similar to how it is on my breakout board for the sensors) or can I just have connect the SDA and SCL output from the Atmega to a 3.3v transistor and connect my sensors to the output (At 3.3v) or will that not really work.

As for the interface with my SD card the wiring worked for my teensy 4.0 at 3.3v, what do I need to change for the ATmega to be able to interface properly.

I wanted to set up the Leds to blink like on a normal Nano when they are in use, I followed the diagram @sterretje posted which shows the same LED layout, what improvements do you suggest.

For the. reset button, should I add a diode after the 10k pull-up resistor, with the conventional flow going towards the 5V (Triangle points towards the 5V). What can I do to protect against reverse polarity, its unlikely it will happen but just incase.

I can change C13 to 100nF, it was just in the schematic from before, I have 100nF everywhere else so I guest would be a better option.

Instead of having the diode straight after the transistor, I wanted to place it inline with the power inout to the ATmega (Pin AVCC) but would it not give the same result?

On the MPU and BMP I followed schematics which used the 10k Resistors which also change the I2C address, if I changed these to 4.7k would there be any difference? (Thats what I used on my teensy project)
BMP 280 schematic :

Here is the buzzer i've used, the place I bought it from doesn't specify the type of buzzer it is, is it worth adding a resistor just as insurance, and if so what value should I add?

@Koepel thank you again for your time and help

-Harvey :slight_smile:

A "transistor" is this : https://en.wikipedia.org/wiki/Transistor
A "voltage regulator" is this : https://en.wikipedia.org/wiki/Voltage_regulator

If you want to interface with the SD card and sensors without level shifters, then you need to run the ATmega328P at 3.3V and 8MHz.
A single voltage controller of 3.3V for the whole board is the most simple solution. But your ATmega should have a 8MHz crystal instead of 16MHz.

This is with a HC4050 for level shifting: https://www.adafruit.com/product/254
This is only for 3.3V : https://www.adafruit.com/product/4682

Use the resistors R20 and R8 for the leds only. If you want to override the RX and TX from the CH340, then you need another pair of 1k resistors. I don't know if the CH340 has 3.3V signals or 5V signals for RX and TX.

The diode should be parallel to the 10k at the /RESET. See the schematic of the Arduino Uno. It is D2. That is not to prevent reverse polarity, that is to prevent High Voltage programming which can corrupt the bootloader.

About the diode after the voltage regulator: If you power the ATmega with 3.3V, then it is okay. The diode protects the circuit when powering it with 5V and VCC will still be 3.3V.

If you have 10k pullup resistors in two places for SDA and SCL, then they are a combined pullup of 5k. There is no need to change one or two of them to 4k7.
You can remove all the pullup resistors for SDA and SCL and have just one pair of 4k7.

The pullup or pulldown resistor for the I2C address selection is only there to allow to change it. For most sensors, you may hardwire the address selection pin to GND or VCC.

Here is some information about the buzzer: https://www.switchelectronics.co.uk/5v-electromagnetic-active-buzzer.
It is 30mA, 4kHz, electromagnetic.
A output pin of the ATmega is maximum 40mA, but a maximum of 20mA is advised (less voltage drop, less heat, less loss).
Since 30mA is smaller than 40mA, it is allowed according to the datasheet. But a transistor (with diode to the base and a flyback diode over the buzzer) might also be a good choice.
Maybe someone else can say something about this.

@Koepel
Yeah sorry I get the two terms mixed up usually, I've only use voltage regulators in my projects.

So I want to power the ATmega chip at 5V so I can use 16MHz but does that mean that the SDA and SCL lines need to work at 5V or 3.3V. To which I'd pull them up to. As for the addresses do you mean to basically pull down the SDO chip straight to ground with no resistor in place? (Less components the better:) )

The CH340C has a 3v3 output which I think is a better option to use rather than the LM1117-3.3 as it is what is actually used on the Nano I just wanted to be safe with current draw but thinking about it its obviously going to be fine :slight_smile: .
I've never used a level shifter before so I wouldn't know where to start.

Ive also realised I had the LEDs for the TX and RX stupidly wrong.
Here's what i've changed it to :
Screenshot 2021-10-27 at 13.32.04

With the diode, i'm powering the board from a 7.4V Lipo, is it a better idea to add the diode at the solder pads on pin 1 as hopefully it wouldn't drop below 5v then.

For the reset pin is this ok?

Thank you again for helping -Harvey

Did you check the links to the Adafruit SD modules. They show the difference. If you want to run the ATmega at 5V, then you need a level shifter for the SD card. You will also need level shifters for the SDA and SCL for the sensors.

Did you check the link to the schematic of the Uno and where D2 is ?

@Koepel
I have correctly wired up the diode for the reset pin.
Screenshot 2021-10-27 at 23.00.05

I looked at the level shifters form the Adafruit website and looked at the schematics and data sheet of the BSS138 Level shifter but the labels for the outputs were a bit confusing, is this correctly wired?

The nets are labelled purely for demonstrating the connection, if correct would I need to create a new net for connecting the BMP and MPU I2C busses to the 3.3v output of the level shifter. As I communicate to the SD with SPI do I need to level shift and have all of the MISO, MOSI and CS pins at the 3.3v output of the level shifter? Is my flashchip going to also require the same thing?

-Harvey

The diode at the reset pin is correct. Is that a symbol for a Schottky diode ? I can be a normal diode.

The BSS138 level shifter is correct.
Then you will have a 5V I2C bus and a 3.3V I2C bus. That are two different things in the schematic.

The SPI interface to/from the SD memory card also needs level shifters. A SD memory card works at 3.3V. You can check the schematic from Adafruit : https://learn.adafruit.com/assets/35635. As you can see, they connect the DO directy from the SD memory card to a input of the Arduino. That is not perfect, but it will work.

@Koepel
The schematic Adafruit supplied is a bit confusing, I've tried to search for a schematic of the HC4050 and they don't seem straight forward either.

On another Arduino post I found this image, its a different level shifter but shows straight forward the input and output from each side (Side being SD and Microcontroller)
0b61cc4aa8cd62d0a98027ff3040e04b49ea3d0d_2_683x500

Screenshot 2021-10-28 at 12.49.33
Here is the HC4050, so do I assume that 3A and 3Y are separate sides of the level shifter (Like the previous photo), and would A or Y be the Sd or microcontroller ?

Also another stupid question, but do I need a level shifter for each SDA and SCL line or can they be connected to the same level shifter?

Screenshot 2021-10-28 at 12.56.41
Is this wrong?

-Harvey

The HC4050 has inputs that can be higher than its VCC.
So if the HC4050 is powered with 3.3V, and the input signals of 5V (data-to-sd-card, clock, chip-select) are going into the 'A' inputs, then the 'Y' outputs are 3.3V digital signals.
Have you read a datasheet of the HC4050 ?

It is not allowed to shortcut SDA to SCL. You need a level shifter for SDA and another level shifter for SCL. You can find that in almost every schematic of Adafruit modules for I2C.

I'm afraid that this can go on for a long time. All this voltage levels shifting is not needed when the microcontroller runs at 3.3V.

@Koepel Thank you, again :slight_smile:
I realise it requires a lot of components now to level shift each data line, however I need the performance from the chip, so its a pain but it don't mind a bit more soldering :slight_smile:

I've wired up the HC4050 for my SD and Flash and one for both my BMP and MPU

Here is how I've wired up each HC4050
Screenshot 2021-10-28 at 17.53.45
My BMP 280 wiring


MPU

Here is my flash chip and SD with each input from the chip at 5V and 3.3v for each sensor.

So I will have 3 HC4050s in total but I hope they're all wired correctly

thank you again for helping and putting up with my stupid questions - Harvey