ATMega328P Crashing

I've created a custom PCB using the ATMega328P. I'm running it on a CR2 battery (3V, 850mAh) with an 8MHz crystal (essentially a Pro Mini - something I've done many times before without any issues). I successfully uploaded the bootloader and managed to program the board fine.

However, on this design I have two load switches (TPS22860) which I'm controlling from pins D5 and D6. This is to switch power from the battery to a GPS module and a BME688 sensor.

The issue is, whenever I try to set either D5 or D6 HIGH, the ATMega will restart. In the code below which I put together to test it, it restarts at the end of the main loop.

int pwr = 5;
int pwrA = 6;

void setup() 
{
  Serial.begin(9600);
  pinMode(pwr, OUTPUT);
  pinMode(pwrA, OUTPUT);
  Serial.println("SETUP FLAG");
}

void loop() 
{
  Serial.println("FLAG 1");
  digitalWrite(pwr, HIGH);
  digitalWrite(pwrA, HIGH);
  Serial.println("FLAG 2");
  delay(1000);
  Serial.println("FLAG 3");
  digitalWrite(pwr, LOW);
  digitalWrite(pwrA, LOW);
  Serial.println("FLAG 4");
  delay(1000);
}

Serial monitor output below.

SETUP FLAG
FLAG 1
FLAG 2
FLAG 3
FLAG 4
�SETUP FLAG
FLAG 1
FLAG 2
FLAG 3
FLAG 4
�SETUP FLAG
FLAG 1
FLAG 2
FLAG 3
FLAG 4

I wasn't sure whether the ATMega was just restarting when it reached the end of the main loop , so I toggled D5 and D6 a few extra times within the main loop, but it still restarted after Flag 4.

Whenever I comment out the "digitalWrite(pwr, HIGH)" and "digitalWrite(pwrA, HIGH);", the ATMega stops restarting and it loops fine, even when I manually pull D5 and D6 HIGH.

The strange thing is, the device is fine when I run the more complex code below for the GPS module. It allows me to toggle D6 fine.

#include <TinyGPS++.h>
#include <SoftwareSerial.h>
#define rxGPS 3
#define txGPS 2

const int pwr = 6;
long lat, lon;
SoftwareSerial gpsSerial(rxGPS, txGPS);
TinyGPSPlus gps;
 
void setup()
{
  Serial.begin(9600); // connect serial
  gpsSerial.begin(9600); // connect gps sensor
  pinMode(pwr, OUTPUT);
  Serial.println("SETUP FLAG");
}
 
void loop()
{
  digitalWrite(pwr, HIGH);
  while (gpsSerial.available())     // check for gps data
  {
    if (gps.encode(gpsSerial.read()))   // encode gps data
    {
      Serial.print("SATS: ");
      Serial.println(gps.satellites.value());
      Serial.print("LAT: ");
      Serial.println(gps.location.lat(), 6);
      Serial.print("LONG: ");
      Serial.println(gps.location.lng(), 6);
      Serial.print("ALT: ");
      Serial.println(gps.altitude.meters());
      Serial.print("SPEED: ");
      Serial.println(gps.speed.mps());
 
      Serial.print("Date: ");
      Serial.print(gps.date.day()); Serial.print("/");
      Serial.print(gps.date.month()); Serial.print("/");
      Serial.println(gps.date.year());
 
      Serial.print("Hour: ");
      Serial.print(gps.time.hour()); Serial.print(":");
      Serial.print(gps.time.minute()); Serial.print(":");
      Serial.println(gps.time.second());
      Serial.println("---------------------------");
      //delay(4000);
    }
  }
  Serial.println("Waiting for data...");
}



I've checked for shorts and the hardware seems fine - I've used this design many times before. The issue just seems to be the ATMega cannot assert those pins HIGH in certain cases without it restarting the device. I thought it may be some issue with the UART comms to the PC, but everything is fine with the GPS sketch. I then thought it may be some memory corruption? But I'm not sure how I'd investigate that further.

Any advice or suggestions would be appreciated!

In your test code, put "Serial.flush();" after each print to Serial, that will wait for the serial buffer to empty before continuing on to the next line of code and give you a better idea of where the problem occurs.

Thanks. I've just tried that and it now restarts after Flag 1, so as soon as D5 and D6 are set HIGH.

Can you put a few hundred uF on the VBat bus to hold it up during the switching. I see caps but no clue as to the value.

This was it! Thanks! The schematic was meant to have a 0.1uF and a 10uF capacitor, but they'd both been set to 0.1uF by accident. I've just swapped one out and it seems to have fixed it.

1 Like

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