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
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?
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
I re-wired the crystal with 2 x 22pF capacitors and removed the resistors so it is now wired like this:
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.
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.
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.
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?
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: