Using a 4mhz crystal - timining/coding? issues?

I came across someone who wired up a 4mhz crystal to an atmega328p .... because of the fact he was limited to 3v (from a single cell aaa energizer through a step up to deliver3v), he claimed that's the most it would operate at 4mhz, but if this was true what are the steps required if you wish to change the crystal but still use Arduino's IDE? what if anything needs to be modified?

(because... I just seem to remember someone saying that swapping an 18mhz for a 20mhz crystal meant changing things in config files, would the 4mhz not be a similar deal?)

I haven't tried it, but I believe you need to edit a few things including:
preferences.txt: build.f_cpu=4000000L
makefile: F_CPU = 4000000
the bootloader

There's some info here:

It may also affect the timer based functions.

@Hillridge is close. You really don't need to modify preferences.txt and, unless you are using a makefile to build, you don't need to modify the makefile.

You will either need to build and install a 4 MHz bootloader or adjust the baud rate (115200/4).

You will have to create an entry in boards.txt.

I believe everything in the core will work correctly with the exception of the analog-to-digital converter code. You will have to modify the code in init to set the prescaler to an appropriate value.

Cheers, thanks for clearing that up :slight_smile: very useful....

Did it work? can you show us the files used?
Thanks a lot I am interested in having a 4mhz botloader for an 328p-pu thanks a lot!!

Some of the library functions like delay(), delayMicroseconds(), and SoftwareSerial() won't work correctly at any speed other than 8, 16, or 20 Mhz. A few additional functions only work correctly at 8 and 16 MHz.

You can run at 16 MHz down to 3.78V and at 8 MHz down to 2.4V. You only need to slow to 4 MHz if you want to run at 1.8V.

BTW, "conventional wisdom" is that you save more power by running at higher clock speed for less time.
That is, your cpu should spend most of it's time in various sleep modes whose power consumption is very low and not dependent on clock speed, and wake up and do stuff as necessary, as quickly as possible, and then go back to sleep again. Apparently running for 1/4 the time at 16MHz is better than running at 30% power (or whatever) at 4MHz...

cjdelphi:
I came across someone who wired up a 4mhz crystal to an atmega328p .... because of the fact he was limited to 3v (from a single cell aaa energizer through a step up to deliver3v), he claimed that's the most it would operate at 4mhz, but if this was true what are the steps required if you wish to change the crystal but still use Arduino's IDE? what if anything needs to be modified?

At 3V you can run at 11 MHz.

For 8 MHz you only need 2.4V.

I agree with the above posters. You are better off staying at 8 MHz (using the internal oscillator) and sleeping. In any case the step-up gadget will consume a lot more current than the processor. Try using 2 x AAA cells (or 3), and skip the step-up converter.

Also you can run at various low frequencies by not changing the crystal (or using a crystal at all) but just by changing the clock prescaler.

That way you can upload sketches using the normal IDE, but then the code slows the frequency down (assuming this is desirable, which is debatable). See reply #7 in the above thread.