Overclocking the Arduino. UNO & MEGA.

Windows 10 Pro. Panasonic CF-C1 laptop. Arduino 1.8.2. Arduino 1.0.5-r2.

Having the need for a faster processor in order to speed up my touchscreen TFT response, I wondered if it was possible to make the Arduino Mega run with a faster crystal.

Short answer, Yes ! But first …….

A faster UNO.
Imagine my surprise when I discovered that overclocking the Arduino is quite popular. Since I have a spare UNO, and it has a ‘thro hole’ crystal, I decided to experiment with that. This type of Arduino build is the easiest to modify as will become apparent.
I removed the crystal and fitted a 3 pin socket with the centre pin removed. This gives the correct spacing for a HC-49 size crystal can. Experimenting with a couple of odd crystals that I have, I discovered, with some surprise, that the Uno will work OK, up to 28.224MHz. ( this frequency produces meaningful baud rates with integer division) This is the highest frequency fundamental crystal that I have. The crystal needs to be a fundamental type, as the Arduino internal oscillator will not function correctly with an overtone crystal, such as a 27MHz RC type. Which are 9MHz, 3rd harmonic. It will more than likely oscillate at 1/3rd of the Marked frequency. Also, if your UNO has a ceramic resonator ( much more difficult, but not impossible to work with ) you will need to add a ceramic capacitor of 22pF, from each crystal leg to ground. The can type crystal will have these on the Arduino board already.
Arduino IDE Serial monitor is compromised due to the incorrect clock frequency in relation to the baud rate. This can be resolved by editing the ‘boards.txt’ file in the Arduino\hardware\arduino path. Find the entry for UNO. You need to change the build.f_CPU= 16000000L to whatever value your crystal is, 282240000L in my case. Alternatively, create a complete new entry with a unique name, such as ‘Uno_28MHz’. Just cut, paste and edit the existing Uno entry. Rename every parameter including the name entry. The new ‘board’ will appear in the Arduino IDE under the TOOLS menu drop down, after restarting the IDE.
Sketch transfer still needs to be at the original 16MHz, ( hence the socket ) using the original 16MHz UNO board selection and correct COM port. I found this to work without any problems, other than the inconvenience of changing the crystal too often.
Ultimately the sketch can be developed at 16MHz, and the crystal and board selection finally changed when debugging is complete. Then the sketch can be uploaded with the new setup.
The 28.224 MHz crystal experiment would not work in the MEGA !

External Oscillator.
An external ‘canned’ oscillator can also be used as the clock source, but the Arduino ‘fuses’ will need to be programmed in order to tell the processor which clock source to use. The Arduino CANNOT modify its own fuses via the IDE. This will need to be done using an ISP device and a program such as AVRDUDE.EXE. This is not the friendliest utility and a quick search of the internet produced a GUI version called AVRDUDESS, which I find so much easier to use, rather than the command line version.
Download it here ….
http://blog.zakkemble.co.uk/avrdudess-a-gui-for-avrdude/
Install the software and place a copy of libusb0.dll into the same folder. This dll file is buried inside the Arduino installation.
On my laptop this is at C:\Program Files (x86)\Arduino\hardware\tools\avr\bin\

The MEGA.
I had already carefully removed the ceramic resonator from a MEGA clone and fitted a 28MHz crystal and capacitors, I later replaced the crystal with a 22MHz oscillator module. The output of the module connects to Xtal1 on the Atmega 2560 chip. This is the solder pad nearest the Mega I/O terminal strip, and furthest from the chip itself. It is powered from the 5v and GND connections on the Mega that are conveniently nearby.
Not having an ISP programmer I constructed a simple device using an Arduino Pro Mini, running the ArduinoISP sketch that is found in the Arduino EXAMPLES folder. The processor needs to be at least a 328. A UNO or NANO with 328p processor would be suitable of course.
See my previous post for details of a NANO version. The diagram shows the recommended circuit and the connections from the NANO to the Mega, as an example. The LED wiring was the most complicated part of that project.
After uploading the ArduinoISP sketch in the normal way, a 10uF at 10v or more capacitor needs to be fitted between RESET and GND, this prevents the processor from being reset when used as a programmer for the UNO or Mega, or any other device you choose.

Fuses.
The processor fuses that we are interested in are labeled lfuse and hfuse. Meaning low fuse and high fuse. The lfuse contains the bits that need to be changed in order to select the external oscillator as the clock source. The default value appears to be 0xFF. The lower 4 bits and bits 6 and 7, need to be programmed to 0. (The logic is inverted. See the datasheets for more info ) The new value is 0xA0. 1010 0000 in binary. The hfuse is 0xD8 by default and needs no changes.

Caution
Do not change any of the other values, it is possible to easily ‘brick, your device. Only a HV programmer can fix this unwanted situation. Beyond the scope of this article.

AVRDUDESS.
Connect up the devices and connect the programming board to the PC USB port. Check the COM port number of the ISP in the Arduino ISP TOOLS>PORT.
Using Windows explorer, navigate to the AVRDUDESS install folder and launch the application. In the Programmer drop down, select Arduino as the programming interface. Select the correct COM port from its drop down. Type in the baud rate, 19200 is the default for the ISP. At the top righthand side select the MEGA2560 in the 2 device boxes. This is sufficient information for avrdudess to read your device settings. Click on READ fuses. The program responds in the DOS window, and updates the fuse readings in the GUI. Clicking on CONFIG will allow you to see the individual fuse settings in bit patterns. They can be modified in this screen.
The default hfuse bit pattern is 110110001 = 0xD8
The NEW lfuse bit pattern is 10100000 = 0xC0
Exit the screen. Then write to the device by clicking WRITE in the fuses section.
Avrdudess reports the changes. Clicking on SAVE will remember your settings.

A NEW board.
Create a new MEGA entry in the boards.txt file with a unique name, such as Mega_22. Copy one of the existing entries and edit it. Change the lfuse and hfuse values. Change the build.f_CPU frequency as detailed previously. Save the changes. On restarting the IDE the new board appears in the TOOLS/BOARD drop down. Select it. Select the COM port of the ISP device. The Mega can now be programmed from the Arduino IDE, using the ISP programmer. The crystal does not control the baud rate of the ISP which is fixed at 19200 baud. Connect your newly programmed board to the USB port, and select the correct device and port inside the Arduino IDE TOOLS menu. The IDE serial monitor should work correctly due to the new crystal information in the boards.txt file. You will always need to use the ISP device for programming unless you modify the bootloader. I have not yet managed to do this with the MEGA, but the UNO was a different story………..

UNO Optiboot Bootloader.
In order to be able to program the faster UNO directly from the IDE, without a programmer, the boot loader files will need to be edited to reflect the new oscillator frequency. Then the MAKEFILE will have to be compiled with MAKE to produce a new boot loader hex file. This is straightforward as long as you get all the details correct. It can stretch your patience a little !!

MAKE is not shipped with the latest versions of Arduino. You will need an earlier version such as Arduino-1.0.5-r2, which is still available for download. Unzip this to a separate folder e.g. C:\ArduinoOld. Navigate to the \bootloaders directory and open the \optiboot sub-directory. Open “makefile” in a text editor. Similar to the boards.txt file, make a new entry in this makefile reflecting the changes to CPU speed. Give it a unique name as before, such as FastUNO. You will need a unique name for the bootloader that will be produced, such as FastUNO328.hex. Edit the entry and save the changes. If you have problems here see the footnote on permissions.

Open the Windows command line as Administrator. Failure to do this will probably result in the ‘access denied’ error message. Navigate to the directory which holds the edited makefile. The DOS command is CD ‘path’. Start with CD C:\ this gets you to the root. You can find the Arduino directory in Windows Explorer and copy the path from the Windows address bar, then CTRL v to paste it into the DOS window at the root prompt, usually C:\ >
The Windows batch file, “omake.bat” MUST be in the same folder as the “makefile” that you edited previously.
The MAKE command line entry MUST use the unique name you chose earlier. Such as “omake FastUNO” Any failures are probably due to the wrong path or privileges. I had to edit the omake.bat file to point directly to the make.exe location, which resides at C:\ArduinoOld\hardware\tools\avr\utils\make . Alternatively if you are sufficiently familiar with Windows environment variables, you can create a new path entry. Beyond my scope, but take a look here…. https://www.computerhope.com/issues/ch000549.htm
At the command prompt, try OMAKE -v. This will force MAKE to supply its version number, and confirm it is available, similarly avr-gcc –version will report the version number of the compiler.
Any error messages will be due to incorrect path or privileges. On successful completion of the OMAKE FASTUNO command, you should find the following 2 files in the bootloaders\optiboot folder.
FastUNO328.hex
FastUNO328.lst.
Copy these to the \optiboot folder in your current Arduino installation. Launch AVRDUDESS again, Avrdudess can now be used to install FastUNO.hex as the new boot loader, by searching your local drive from within the avrdudess FLASH search bar.
This process has no affect on your current Arduino installation. Although I experienced some problems with the paths in omake.bat and in makefile, due to the Arduino file structure changes,  and the whole process is a bit long-winded, it all worked properly once the details were correct. Optiboot will NOT work on the MEGA due to the different addressing requirements.

Permissions in Arduino.
The Arduino installation protects its files by making them accessible only to special security privileges. Ensure you are logged in as Administrator. Open an Arduino ‘H’ file in Windows Notepad for example. After editing the file, you cannot save the changes because the file is write protected, even tho you are logged in as Admin. To get around this, open Windows Explorer and navigate to the Arduino installation. Right click on the Arduino folder, go to Properties, and select Security. In the GROUP box, select Users(computer)\Users. Then click EDIT. This pops up another GROUP box, click Users etc. then in the Permissions box tick the top 2 checkboxes, Full Control and Modify. Click Apply. Windows will scroll thro the Arduino directory entry applying the permissions. Click OK on any open pop-ups. You can now edit Arduino files with any suitable text editor and save your changes. I find Notepad ++ to be very useful.

Conclusion.

You should now be able to create faster Arduino’s  for the price of a crystal or oscillator module. I’m an electronics hardware guy, not a programmer, so forgive the long and winding explanation. It has taken me at least a month of research to get this far. I really hope there are no errors and someone else finds this helpful.

I searched the forums as usual, only to find misleading info, typos and outright mis-information. One reply to a query on the Mega was a suggestion to “read the datasheet” all 435 pages of tables and equations. Another reply, and I quote ” you can’t do it like that. “, very constructive. One “Instructable” that I found had 3 incorrect steps, which I discovered the hard way.  How can anyone learn from that ?

Will ZL4SAE

Leave a comment