Pro mini doesn't work properly (after reboot)

I have a pro mini (5v m328p) connected to a 74HC595-controlled 4-digit 7-segment display. When I upload the sketch via USBASP, everything works perfectly even after a few times of resets. But when I unplug USBASP and power it up (whether connect to computer or just VCC&GND), things get messed up except Serial. Then I need to upload again with a programmer to make it work once. Upload via Serial can't even make it work. Same sketch worked perfectly on an Arduino Uno whatsoever. What can I do? Thanks a lot.

45gfg9: What can I do? Thanks a lot.

Post sketch, circuit diagram, and other information as requested in the sticky. So far I don't have a clue of what's going on.

When you upload a sketch using the USBasp, it erases the bootloader. After doing that, you always need to do a Tools > Burn Bootloader before you can go back to uploading over serial again.

wvmarle:
Post sketch, circuit diagram, and other information as requested in the sticky. So far I don’t have a clue of what’s going on.

Sketch:

#include <EEPROMex.h> // EEPROMEx
#include <RTClib.h>   // RTClib by NeiroN

#define POWER_PIN 5

#define LIGHT_PIN A4
#define LIGHT_THRESHOLD 600

#define BUTTON_PIN 11

#define SDI 2
#define LCK 3
#define CLK 4

const byte NUM[] { 0xFC, 0x60, 0xDA, 0xF2, 0x66, 0xB6, 0xBE, 0xE0, 0xFE, 0xF6 };

RTC_Millis rtc;

DateTime compile = DateTime(__DATE__, __TIME__) + TimeDelta(60);
DateTime eeprom;
DateTime target(2019, 6, 28);

void setup() {
  delay(2000);
  Serial.begin(9600);

  pinMode(BUTTON_PIN, INPUT_PULLUP);
  pinMode(POWER_PIN, OUTPUT);
  pinMode(9, OUTPUT);
  pinMode(13, OUTPUT);
  digitalWrite(9, LOW);
  digitalWrite(13, LOW);

  Serial.print("Last compile: ");
  Serial.println(dt2string(compile));

  Serial.print("Reading EEPROM: ");
  Serial.print(EEPROM.readBlock(0, eeprom));
  Serial.print(" bytes read. ");
  Serial.println(dt2string(eeprom));

  rtc.begin((eeprom.year() != 255 && eeprom > compile) ? eeprom : compile);

  digitalWrite(POWER_PIN, HIGH);
  show((target - rtc.now()).days() + 1);
}

void loop() {
  if ((rtc.now() - eeprom).totalseconds() >= 1800) {
    eeprom = rtc.now();
    Serial.print("Updating EEPROM: ");
    Serial.print(EEPROM.updateBlock(0, eeprom));
    Serial.print(" byte(s) updated. ");
    Serial.println(dt2string(eeprom));
    digitalWrite(POWER_PIN, HIGH);
    show((target - eeprom).days() + 1);
  }

  digitalWrite(POWER_PIN, (!digitalRead(BUTTON_PIN) || analogRead(LIGHT_PIN) < LIGHT_THRESHOLD));

  delay(50);
}

// utility

void show(int num) {
  String str = String(num);

  digitalWrite(LCK, LOW);
  for (int i = 0; i < 4; i++) {
    shiftOut(SDI, CLK, LSBFIRST, 0);
  }
  digitalWrite(LCK, HIGH);

  do {
    str = " " + str;
  } while (str.length() < 4);

  digitalWrite(LCK, LOW);
  for (int i = str.length() - 1; i >= 0; i--) {
    shiftOut(SDI, CLK, LSBFIRST, NUM[str[i] - 48]);
  }
  digitalWrite(LCK, HIGH);
}

String dt2string(DateTime dt) {
  return String(dt.year()) + "/" + String(dt.month()) + "/" + String(dt.day()) + " " +
         String(dt.hour()) + ":" + String(dt.minute()) + ":" + String(dt.second());
}

Diagram:


In this case the button is never pushed down [ digitalRead(BUTTON_PIN) always returns 1]

What I expected: display some readable numbers (140, 139, …), when it’s dark the display will go off
What actually happened: some randomly unreadable, characters? when it’s dark the display goes dim (the voltage on pin5 is 2.08v instead of LOW)

pert: When you upload a sketch using the USBasp, it erases the bootloader. After doing that, you always need to do a Tools > Burn Bootloader before you can go back to uploading over serial again.

No I understand that The situation is much more complicate Please see #3

Why are the button and the LDR connected between two pins, instead of a pin and GND?

Did you get the display to work using a basic example sketch (just counting numbers or so)? Same for the button and the LDR on their own?

Where is the 74HC595 in your diagram? One register will only run one display.

Current draw?

I'm missing the most important item: your alternative power supply, how it is connected :-]

wvmarle: Why are the button and the LDR connected between two pins, instead of a pin and GND?

Did you get the display to work using a basic example sketch (just counting numbers or so)? Same for the button and the LDR on their own?

Well I've run out of GND pins so I have to digitalWrite two (9, 13) as GND :| Yes, it does count numbers when on a simple sketch,

tasmod: Where is the 74HC595 in your diagram? One register will only run one display.

Current draw?

Um, the display is driven by 4 74HC595 chips... the seller is on holiday these days (lunar new year) so they'll back to work on 2.14... I hope they have the diagram of the module. So it's 4 1-digit display, each is controlled by a 74HC595 chip. There are 6 pins I can connect, which are pins above and SDO. The usage is to shiftOut a byte each time.

DrDiettrich:
I’m missing the most important item: your alternative power supply, how it is connected :-]

through RAW(9v)/VCC(5v) and GND pin

You can connect many things to one GND pin (or to any pin for that matter - just because it's a single pin doesn't mean you're limited to connecting a single wire to it).

45gfg9: through RAW(9v)/VCC(5v) and GND pin

9V is quite a high voltage for the on-board voltage regulator, even more for a 3.3V Mini. Have a separate power supply (regulator...) to power the display.

Are you powering the display off a digital output instead of Vcc? I'm be amazed if that worked at all.

wvmarle: You can connect many things to one GND pin (or to any pin for that matter - just because it's a single pin doesn't mean you're limited to connecting a single wire to it).

Yes, just because, the project is really compact and I can't fit many wires..

david_nc: Are you powering the display off a digital output instead of Vcc? I'm be amazed if that worked at all.

Yes, that works, but only once. When pin5 goes HIGH it works as VCC and the display is on. When pin5 goes LOW it works as GND, and the display shuts off (normal case).

DrDiettrich: 9V is quite a high voltage for the on-board voltage regulator, even more for a 3.3V Mini. Have a separate power supply (regulator...) to power the display.

Um, I hope so, but the project is kinda compact (#13)..My board runs at 5v. 9v (on RAW) will drive it, and I guess it works well?

Are you powering the display off a digital output instead of Vcc????? I'm be amazed if that worked at all.

45gfg9: Yes, that works, but only once. When pin5 goes HIGH it works as VCC and the display is on. When pin5 goes LOW it works as GND, and the display shuts off (normal case).

I can believe the "but only once" part - the current necessary for that type of display is far in excess of what a digital output can supply, and also exceeds the capacity of the on-board voltage regulator on the pro mini.

The normal way of turning that type of display "off" is to leave the power connected, and turn off all the individual LED segments.

45gfg9: Yes, just because, the project is really compact and I can't fit many wires..

So you add extra wires to other pins? Doesn't make sense, that's just poor design and generally a very bad idea (though it will work fine in this case). Ground wires can go from one to the other and the next.

Same for powering your display from an output pin. Poor design, and a terrible idea (that display needs much more power than your pin can safely supply). This limited current supply ability from the pin is probably what did keep your regulator from overheating... but it may also be the reason your display is misbehaving.

Why supply it with 9V in the first place? Don't you have a 5V supply (in the form of an old mobile phone charger or so) at hand?

Your uncommon ideas are interesting but of no practical use :-(

Try to understand what current is, what it does to a component. An Arduino pin can source 20mA continuously, more current will damage the chip sooner or later.

Uh, thanks everyone! I did some improves and it looks fine now! :slight_smile:

Basically I changed my wiring to this: (a mess, but that’s what I can do :expressionless: )

Sketch:

#include <EEPROMex.h>
#include <RTClib.h>

#define LIGHT_PIN A4
#define LIGHT_THRESHOLD_MIN 490
#define LIGHT_THRESHOLD_MAX 510

#define BUTTON_PIN 10

#define SDI 2
#define LCK 3
#define CLK 4

const byte NUM[] { 0xFC, 0x60, 0xDA, 0xF2, 0x66, 0xB6, 0xBE, 0xE0, 0xFE, 0xF6 };
bool state;
int days;

RTC_Millis rtc;

DateTime upload = DateTime(__DATE__, __TIME__) + TimeDelta(60);
DateTime eeprom;
DateTime target(2019, 6, 28);

void setup() {
  delay(2000);
  Serial.begin(9600);

  pinMode(BUTTON_PIN, INPUT_PULLUP);

  Serial.print("Last upload: ");
  Serial.println(dt2string(upload));

  Serial.print("Reading EEPROM: ");
  Serial.print(EEPROM.readBlock(0, eeprom));
  Serial.print(" bytes read. ");
  Serial.println(dt2string(eeprom));

  rtc.begin((eeprom.year() != 255 && eeprom > upload) ? eeprom : upload);

  days = (target - rtc.now()).days() + 1;
}

void loop() {
  if ((rtc.now() - eeprom).totalseconds() >= 1800) {
    eeprom = rtc.now();

    Serial.print("Updating EEPROM: ");
    Serial.print(EEPROM.updateBlock(0, eeprom));
    Serial.print(" byte(s) updated. ");
    Serial.println(dt2string(eeprom));

    days = (target - eeprom).days() + 1;
  }

  if (state != (!digitalRead(BUTTON_PIN) || brightEnough())) {
    state = (!digitalRead(BUTTON_PIN) || brightEnough());
    state ? show(days) : show();
  }

  delay(50);
}

void show() {
  digitalWrite(LCK, LOW);
  for (int i = 0; i < 4; i++) {
    shiftOut(SDI, CLK, LSBFIRST, 0);
    Serial.println("shiftOut(SDI, CLK, LSBFIRST, 0);");
  }
  digitalWrite(LCK, HIGH);
}

void show(int num) {
  String str = String(num);
  show();

  do {
    str = " " + str;
  } while (str.length() < 4);

  digitalWrite(LCK, LOW);
  for (int i = str.length() - 1; i >= 0; i--) {
    shiftOut(SDI, CLK, LSBFIRST, (isSpace(str[i]) ? 0 : NUM[str[i] - 48]));
    Serial.println("shiftOut(SDI, CLK, LSBFIRST, " + String((isSpace(str[i]) ? 0 : NUM[str[i] - 48])) + ");");
  }
  digitalWrite(LCK, HIGH);
}

String dt2string(DateTime dt) {
  return String(dt.year()) + "/" + String(dt.month()) + "/" + String(dt.day()) + " " +
         String(dt.hour()) + ":" + String(dt.minute()) + ":" + String(dt.second());
}

bool brightEnough() {
  return analogRead(LIGHT_PIN) < (state ? LIGHT_THRESHOLD_MAX : LIGHT_THRESHOLD_MIN);
}

Yes, I did add something. But the purpose is the same…

The only thing that concerns me is the power issue. 9v is sure quite high.

wvmarle:
Why supply it with 9V in the first place? Don’t you have a 5V supply (in the form of an old mobile phone charger or so) at hand?

Because, um, it have to run on battery. The most convenient (and small) battery I can find that provides >5v is a 9F22 9v battery.
What I am considering now is to add an L7805 to decrease voltage (9v → 5v). I have to wait for the sellers to get back to work, and shipping may take a few days.
Thanks again!

Edit: I found an AMS1117-5.0 module, which costs CN¥1.29 ($0.19). Hope that’ll work.