Serial, i2c, LCD, keypad not working on custom PCB - ATMega2560

Hi

I have a custom PCB which has an ATMega2560 and FTDI32RL.
I have burned a bootloader to the ATMega2560 using Nick Gammon's bootloader and I am able to successfully upload sketches to the ATMega2560 from the Arduino IDE. The PC can also see the PCB via device manager / tty.

My issue is that when I run the same sketch on a genuine Arduino Mega with the same (replicated) wiring, the LCD, keypad, serial and i2c communications all work very well. When I run the sketch on my custom PCB, the serial and i2c should echo back what I send but are intermittent (sometimes works OK, sometimes the message received back is incorrect or sometimes missing altogether), the LCD does not work (only powers up with blocks on the top row and the keypad does not work (this may be linked to the serial). All +5v on the board comes from a single USB pin and all GND comes from a single USB pin (i.e. all the VCC are linked and all GND are linked).

My initial thoughts are there is some interference to cause the serial to be unreliable but I am unsure why the LCD would not work. I have checked all pins for continuity and they are all OK.

Connections:

Power -> ATMega2560
+5v -> VCC (0.1uf capacitor to GND)

FTDI -> ATMega2560
1 (TX) -> 2 (RX)
2 (DTR) -> 30 (RESET) via 0.1uF capacitor & pulled high by10k resistor
5 (RX)-> 3 (TX)
15 (D+) -> PC (USB)
16 (D-) -> PC (USB)
4, 20 -> +5v (0.1uf and 10uf capacitors to GND)
7, 18, 21, 25, 26 -> GND

LCD -> ATMega2560
1|GND
2|+5V
3|GND
4|6 (PE4) – RS
5|GND
6|8 (PE6) – EN/ES
7|13 (PH1) – DB0
8|12 (PH0) – DB1
9|15 (PH3) – DB2
10|14 (PH2) – DB3
11|17 (PH5) – DB4
12|16 (PH4) – DB5
13|19 (PB0) – DB6
14|18 (PH6) – DB7
15|+5V
16|GND

Keypad -> ATMega2560
1|24 (PB5)
2|26 (PB7)
3|28 (PG3)
4|25 (PD2)
5|36 (PL1)
6|38 (PL3)
7|40 (PL5)
8|42 (PL7)
9|46 (PD3)
10|27 (PH7)
11|29 (PG4)
12|35 (PL0)
13|37 (PL2)
14|39 (PL4)
15|41 (PL6)
16|47 (PD4)

If anyone could shed some light on this or advise things to try I would be greatly appreciated.

BS

Have you added pullups to the I2C lines SCL and SDA?

Yes - I am using 2.2k resistors and have tried 4.4k aswell.

Next suspects are power (spikes?) and clock.

I believe the power is stable, I monitored it over 15 minutes while sending commands and it v aries only 0.01 volts.
Can you elaborate on how I check the clock?

Thank you
BS

Also I tried setting the RX & TX pins:

DDRB = (0<<PE0); // Pin 2 (RX) INPUT
DDRB = (1<<PE1); // Pin 3 (TX) OUTPUT

It seems now (if it indeed changed) that if I send commands about 1-2 seconds apart, most of the time I get the correct response back but still occasionally the response is missing some characters. Another strange thing is, when I do receive part of the string, I always seem to receive the last character (they all end in a '%').
If I send the commands quickly, a lot of them are missed.

Update:
I ran this sketch:

Code:

int a = 0;

void setup() {
// put your setup code here, to run once:
DDRB = (0<<PE0); // Pin 2 (RX) INPUT
DDRB = (1<<PE1); // Pin 3 (TX) OUTPUT

Serial.begin(115200);
while (!Serial) ; // wait for serial port to connect. Needed for native USB
}

void loop() {
  // put your main code here, to run repeatedly:
delay (1000);
Serial.println(a);
a++;
}

The serial monitor received every number with no problems.

I changed the delay (1000); value from 1000 to 100 then 10 then 1 and it worked fine too.

Because of this I'm now thinking maybe my chips crystal or something isn't quite right and my code isn't actually being executed correctly?

The crystal I am using is the 16.00 mhz variant of this

and I have it wired like this:

image

Thanks
BS

You need a scope for finding problems with spikes and clock.

Unfortunately I don't have a scope.

I re-wired the crystal with 2 x 22pF capacitors and removed the resistors so it is now wired like this:

image

I re-uploaded the bootloader to check and then I uploaded the bootloader I am using (Nick Gammon's bootloader) to an Arduino Mega board and ran the same code and it works perfect.

The problem still persists..

I don't understand what works and what not?

On an Arduino Mega board, my sketch works perfectly. On my custom PCB, the LCD, keypad, and i2c don't work at all. The serial is intermittent:

It seems now (if it indeed changed) that if I send commands about 1-2 seconds apart, most of the time I get the correct response back but still occasionally the response is missing some characters. Another strange thing is, when I do receive part of the string, I always seem to receive the last character (they all end in a '%').
If I send the commands quickly, a lot of them are missed.

Thanks for your help.
BS

A producer of custom PCBs deserves all measuring instruments required for his job. Get a scope and learn to use it.

Here is an image of the oscilloscope display when I connect to the crystal (I wasn't sure if this is what you meant by checking the 'clock'?). It seems like a nice sine wave at 16.00MHz.

This is of the D+ and D- on the serial line (sending '1'):
D+:


D-:

This is of the USB power - I'm unsure if this is what you are after or what a 'clean' power source looks like) but it was constant like this:


Thanks BS

I meant searching for variations or drop outs of the digital clock signal which can be detected by overlaying a number of samples.

Thanks - I'm still learning. If the demo sketch above of sending an incremented number every 1000/100/10/1 ms worked fine, would that indicate the clock is OK?

Also does the power look normal to you?

Thanks
BS

Showing the typical behaviour is not very significant. Select a trigger beyond the noise on the Vcc.

I wasn't actually able to replicate the above image, even when playing with the trigger.
I ended up installing a ferrite bead on the line and the power looks like this:

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.