Can't get section(".noinit") working on Nano

When I connect a terminal server to a Uno, Nano or NodeMCU (to monitor or change settings), this connection resets the board and I lose counter values I've been collecting for weeks. I know I can disable the reset by hardware mods but I don't want to do that.

I don't want to use use EEPROM because the NodeMCU flash memory has a lifetime of ~10,000 cycles - I'll blow through that in about a week.

So I've been trying to retain the values of just a few key variables during the reset process using:

section(".noinit")

The following code works well on the Uno but not on the others (the variable gets reinitialized on reset).

Can anyone help point me in the right direction to fix this?

Thanks

Andy

int savedFlag attribute ((section(".noinit")));
void setup() {
Serial.begin(9600);
Serial.print("Old: ");
Serial.println(savedFlag);
savedFlag = 1234;
Serial.print("New: ");
Serial.println(savedFlag);
}
void loop() {
}

What leads you to believe .noinit works with NodeMCU?

I don't want to use use EEPROM because the NodeMCU flash memory has a lifetime of ~10,000 cycles - I'll blow through that in about a week.

An external EEPROM would have a different limit, and be just as easy to use.

If you are changing settings 10000 times a week, you need to take your ADHD meds.

Thank you both for your input.

My Nano had the old bootloader installed. Once I updated it, the noinit worked!

So, Coding Badly, I believe the Nano works because it now does :slight_smile:

The reason why I don't want to use EEPROM is that I have running counters on a beer brewing system to log max and min temperatures, current weights, bubble rates etc. It's only a few bytes per record and I only need one record but it needs frequent updating. SD would be over kill to store just 8 bytes repetitively.

Keeping these values from reinitializing in RAM during soft reset caused by serial terminal connection is a good way to go. I also have Li Ion battery backup in case of power failure (which I need for other functions anyway).

It was not an obvious answer to this question but perseverance paid off and so now I'm good to go.

Thanks again

The way I do that is with a SPDT slide switch and a 100nF ceramic capacitor, with the switch in one position, the cap is connected between reset pin and GND, keeps the Nano from resetting on serial connect, the other switch position disconnects cap from reset pin and connects it to 5V, keeping it charged, the Nano can now be programmed.
cap_sw.pngcap_n_sw.png

Wombat37:
So, Coding Badly, I believe the Nano works because it now does :slight_smile:

Reread my question.

I just did, and it was just as helpful as it was the first time I read it.

I apologize for not treating your response more seriously.

To answer your question directly, the section(".noinit") attribute is discussed in many articles and books and even in this forum for the Arduino Uno. As the Nano is sort of a smaller version of the Uno and uses the same processor and compiler, I thought it would be worth trying to see if it would work. This was the reason behind my original question. If you had doubts about it working, then that would have been a useful response. However, I did get it working by myself and I suppose I could be accused of wasting other peoples' time. However, there may be others in future who want to do the same thing and here is one possible solution for them.

Moving on...

".noinit" will prevent the C startup code IN YOUR SKETCH or program from zeroing out variables in that section, but it does not (can not!) prevent other code (like the bootloader) from using the same area of memory where those variables exist, and "accidentally" wiping them out.
So on the Nano with the Old Bootloader, the Old Bootloader corrupted your .noinit variables, but Optiboot happens to leave them along (by luck, more than anything else.)

The NodeMCU does "lots of stuff" in ROM, having to do with its wireless hardware, before running your code, and I'm not surprised that you're having trouble preserving memory across boots. I don't have any suggestions for making it work, beyond what's already been mentioned.

Wombat37:
To answer your question directly, ... Arduino Uno ... Nano

You failed. Neither of those boards (processor) are mentioned in my question.

PaulS:
An external EEPROM would have a different limit, and be just as easy to use.

like 5PCS/LOT 24LC512I/P 24LC512 512K I2C Serial EEPROM|Integrated Circuits| - AliExpress

5 for $1.84, delivered, 256k each, run from about 2.5 to 6.5v, and 1,000,000 cycles.

Either should store more than enough data . . .

Or 64k at 10/$1.60: 10pcs/lot 24LC64 24LC64 I/SN SOP8 24LC64 I/P DIP 8|Integrated Circuits| - AliExpress

FRAM based external memories have unlimited write cycles.

An SD card is usually easier because you can pop out the card and read it directly in your PC instead of having to query the Arduino.

For the SD, though, you have to build something that will handle the insertion and removal of the card.

Everything I want to do seems to get some type of wireless connection to monitor, so the query can be automated.

Or 64k at 10/$1.60: https://www.aliexpress.com/item/10pcs-lot-24LC64-

Careful - eeprom is usually numbered in bits, so a 24lc64 is “only” 8k bytes.
If you need less than that, there are numerous tiny packages with up to 512bytes (4K) for less than $0.20, from digikey...

Good point, which I had missed . . .

But there's $9 shipping on that 16c part!

10 for 70c delivered for a 74C02! drops down to under a nickel if you want a hundred . . . also much les for SOT (this is for DIP)