Store data in flash (program) memory instead of SRAM. There’s a description of the various types of memory available on an Arduino board.
The PROGMEM keyword is a variable modifier, it should be used only with the datatypes defined in pgmspace.h. It tells the compiler «put this information into flash memory», instead of into SRAM, where it would normally go.
PROGMEM is part of the pgmspace.h library. It is included automatically in modern versions of the IDE. However, if you are using an IDE version below 1.0 (2011), you’ll first need to include the library at the top of your sketch, like this:
Using PROGMEM is also a two-step procedure. After getting the data into Flash memory, it requires special methods (functions), also defined in the pgmspace.h library, to read the data from program memory back into SRAM, so we can do something useful with it.
const dataType variableName PROGMEM =
Note that because PROGMEM is a variable modifier, there is no hard and fast rule about where it should go, so the Arduino compiler accepts all of the definitions below, which are also synonymous. However, experiments have indicated that, in various versions of Arduino (having to do with GCC version), PROGMEM may work in one location and not in another. The «string table» example below has been tested to work with Arduino 13. Earlier versions of the IDE may work better if PROGMEM is included after the variable name.
const dataType variableName PROGMEM = <>; // use this form
const PROGMEM dataType variableName = <>; // or this one
const dataType PROGMEM variableName = <>; // not this one
dataType : Allowed data types: any variable type.
variableName : the name for your array of data.
The following code fragments illustrate how to read and write unsigned chars (bytes) and ints (2 bytes) to PROGMEM.
Arrays of strings
It is often convenient when working with large amounts of text, such as a project with an LCD, to setup an array of strings. Because strings themselves are arrays, this is actually an example of a two-dimensional array.
These tend to be large structures so putting them into program memory is often desirable. The code below illustrates the idea.
Notes and Warnings
Please note that variables must be either globally defined, OR defined with the static keyword, in order to work with PROGMEM.
The following code will NOT work when inside a function:
The following code WILL work, even if locally defined within a function:
The notes on this page are for all boards except the Due, which has a different architecture
There are three pools of memory in the microcontroller used on avr-based Arduino boards :
Flash memory (program space), is where the Arduino sketch is stored.
SRAM (static random access memory) is where the sketch creates and manipulates variables when it runs.
EEPROM is memory space that programmers can use to store long-term information.
Flash memory and EEPROM memory are non-volatile (the information persists after the power is turned off). SRAM is volatile and will be lost when the power is cycled.
The ATmega328P chip found on the Uno has the following amounts of memory:
The ATmega2560 in the Mega2560 has larger memory space :
Notice that there’s not much SRAM available in the Uno. It’s easy to use it all up by having lots of strings in your program. For example, a declaration like:
char message = «I support the Cape Wind project.»;
puts 33 bytes into SRAM (each character takes a byte, plus the ‘\0’ terminator). This might not seem like a lot, but it doesn’t take long to get to 2048, especially if you have a large amount of text to send to a display, or a large lookup table, for example.
If you run out of SRAM, your program may fail in unexpected ways; it will appear to upload successfully, but not run, or run strangely. To check if this is happening, you can try commenting out or shortening the strings or other data structures in your sketch (without changing the code). If it then runs successfully, you’re probably running out of SRAM. There are a few things you can do to address this problem:
If your sketch talks to a program running on a (desktop/laptop) computer, you can try shifting data or calculations to the computer, reducing the load on the Arduino.
If you have lookup tables or other large arrays, use the smallest data type necessary to store the values you need; for example, an int takes up two bytes, while a byte uses only one (but can store a smaller range of values).
If you don’t need to modify the strings or data while your sketch is running, you can store them in flash (program) memory instead of SRAM; to do this, use the PROGMEM keyword.
How to expand program memory on an arduino?
Sometimes I run out of space of the program size for the embedded C program that I want to put on an arduino. So what chip do I need to expand the program memory of the arduino so that I can use bigger C programs with my arduino?
5 Answers 5
An ATmega2560 is the chip to add for more memory when you run out of program memory on an Uno. Referring to the Memory webpage at arduino.cc, you’ll see that it has Flash=256KB, SRAM=8KB, EEPROM=4KB, where the ATmega328 chip on the Uno has Flash=32KB, SRAM=2KB, EEPROM=1KB.
When you run out of program memory on an Uno, you have about three choices that are practical: make the program smaller; remove the bootloader (to free up one or two KB); use a different board with a more-capable processor. The CPU on an Uno board doesn’t have a way to access program bytes from places other than the on-chip 32KB flash. The CPU on a Mega2560 board starts out with 8 times as much flash, so it can hold bigger programs than an Uno can.
If you remove the bootloader, you could instead program using a USBASP board, as described in a “Program AVR chip using a USBASP with 10 pin cable” article at learningaboutelectronics.com.
Another way (other than MCU with more memory) is not using Arduino framework and its libraries. Such level of abstraction is expensive in both ways — memory usage and speed. But that’s much harder way. If you need some library, you have to port it (if you haven’t found native one) or at least provide required functions from Arduino.
For example in Arduino IDE 1.6.11 empty sketch is using up 444 Bytes of flash. Using one pinMode and digitalWrite(13, ! digitalRead(13)); + delay(500); means 964 Bytes. Of course, these are most likely one-timers. If you use six more pins and toggle it, it uses much less program memory than using first one (1192B). But it still grows really fast.
In pure C empty program is about 134 Bytes long. Same functionality (toggling one pin every 500ms) takes 158 Bytes (and it’s way faster).
But for hobby project i would go for ATMega2560 or ATMega644/1284 instead.
And you can use direct access to hardware too. Toggle pin sketch with using registers directly and _delay_ms from avr libraries uses 468 Bytes instead of 964B. With delay from Arduino core libraries it’s 602B.
Lasers, CNC, Digital Fabrication, Design and Related
Getting More Program Space on an Arduino Nano
I recently compiled Grbl with the COREXY features enabled. To my surprise, the upload to my Nano failed because it did not have enough program space. I was surprised because the Nano and Arduino UNO use the same ATmega328p microcontroller. A little researched showed that they use different bootloaders.
Normally you a special programmer to program microcontrollers like Arduinos. You plug the programmer into the ICSP (In Circuit Serial Programming) pins on the microcontroller.
Bootloaders eliminate the need for this by putting the programmer right on the microcontroller. When you start (boot) the microcontroller, the bootloader runs first. It looks for someone trying to load a program. If it does not see that it switches to running the last uploaded program in another part of memory. The only penalty to this is that the bootloader needs to use a small amount of memory and it delays the start of your program a little. The total amount of memory available to the users is the flash memory size (32k for the …328P chip) minus the bootloader. The Nano and UNO use different bootloaders. The Nano uses a 2k bootloader and the UNO uses a 0.5k bootloader.
To get more memory of the Nano we have two options. We can use an ICSP programmer to use all of the memory. This removes the bootloader. The other option is to use a programmer to put the smaller UNO bootloader on the Nano.
Changing the Bootloader
You will need a programmer. you can get any of a dozen types of dedicated programmers, but you can also use another Arduino with a special sketch loaded on it.
How to use an UNO to Program a Nano
Use the Arduino IDE to upload the Arduino ISP (In-System Programmer) sketch to the UNO.
You will need 6 jumper wires. It is helpful to have a few colors to make it easy to verify the connections. They should have male pins on the UNO side and typically female sockets on the Nano side. These ribbon style jumpers are nice because you can peel off what you need and they have a lot of colors.
|Arduino Uno (the programmer)||Nano I/O (the target)||Nano ICSP (the target)|
Or using the ICSP Header
Install the Bootloader.
Be sure UNO is selected in the boards menu. This will make sure the UNO bootloader is used.
Change the programmer to Arduino ISP in the programmer menu. Remember to return it to current setting, typically AVR ISP, after you are done installing the bootloader.
Now select the Burn Bootloader menu. This will now install the UNO bootloader onto the Nano using the Arduino UNO as a programmer.
Using the Nano.
The Nano should now have the smaller UNO bootloader. When programming from now on, be sure to select UNO in the boards menu. The IDE will no longer be able program it as a Nano. You might want to mark the Nano so you remember this.
How does the USB cable reboot the microcontroller?
In order to program the microcontroller via USB it needs to restart (boot). It does this using a special trick. Arduinos show up as a serial port (COM port). The Arduino only uses the Tx and Rx signals to transmit data. The USB adapter supports other RS232 signals. The DTR (Data Terminal Ready) signal is used to reset the Arduino. The DTR signal can be controlled by the program uploading the firmware.
Not shown in the schematic is a 1K pull up resistor on the reset pin near the ATmega328p. To reset the microcontroller you pull the reset pin to ground. The uploader program pulls the DTR signal low. The cap passes this change through, causing a reset, but the 1k pull up returns the reset back to the high (run) state. Remember: Capacitors pass changes (AC), but block a steady voltage (DC).
Why do they use different bootloaders?
I do not know the answer, but it probably due to the fact that they use different USB chips.
If you want to be notified of future blog posts, please subscribe.
Arduino For Beginners
Learn about Arduino and the Arduino UNO and how you can integrate this board into your makerspace and coding program. Make interactive makerspace projects while learning to code and problem solve.
More and more makerspaces around the world are looking to add coding and electronics to their maker education programs. One of the best ways to do this is by integrating an Arduino board into makerspace projects and lessons.
We’ve found that a lot of maker educators haven’t taken the plunge into coding or Arduino because they think programming is scary. Because of this, we wanted to make sure this tutorial was written for the absolute beginner with no experience whatsoever.
This tutorial is a high level view of all the parts and pieces of the Arduino ecosystem. In future posts, we will take you step by step in creating your first simple Arduino project.
What Is Arduino?
Arduino is an open source programmable circuit board that can be integrated into a wide variety of makerspace projects both simple and complex. This board contains a microcontroller which is able to be programmed to sense and control objects in the physical world. By responding to sensors and inputs, the Arduino is able to interact with a large array of outputs such as LEDs, motors and displays. Because of it’s flexibility and low cost, Arduino has become a very popular choice for makers and makerspaces looking to create interactive hardware projects.
Arduino was introduced back in 2005 in Italy by Massimo Banzi as a way for non-engineers to have access to a low cost, simple tool for creating hardware projects. Since the board is open-source , it is released under a Creative Commons license which allows anyone to produce their own board. If you search the web, you will find there are hundreds of Arduino compatible clones and variations available but the only official boards have Arduino in it’s name.
In the next section, we’re going to discuss a few of the Arduino boards available and how they differ from each other.
Types of Arduino Boards
Arduino is a great platform for prototyping projects and inventions but can be confusing when having to choose the right board. If you’re brand new to this, you might have always thought that there was just one “Arduino” board and that’s it. In reality, there are many variations of the official Arduino boards and then there are hundreds more from competitors who offer clones. But don’t worry, we’re going to show you which one to start with later on in this tutorial.
Below are a few examples of the different types of Arduino boards out there. The boards with the name Arduino on them are the official boards but there are also a lot of really great clones on the market as well. One of the best reasons to buy a clone is the fact they are generally less expensive than their official counterpart. Adafruit and Sparkfun for example, sell variations of the Arduino boards which cost less but still have the same quality of the originals. One word of caution, be careful when buying boards from companies you don’t know.
Image credit – Sparkfun.com
Another factor to consider when choosing a board is the type of project you are looking to do. For example, if you want to create a wearable electronic project, you might want to consider the LilyPad board from Sparkfun. The LilyPad is designed to be easily sewn into e-textiles and wearable projects. If your project has a small form factor, you might want to use the Arduino Pro Mini which has a very small footprint compared to other boards. Check out Sparkfun’s Arduino Comparison Guide for a breakdown and comparison of the top boards out there.
Next, we’re going to focus on our favorite Arduino board which we recommend beginners start with.
One of the most popular Arduino boards out there is the Arduino Uno. While it was not actually the first board to be released, it remains to be the most actively used and most widely documented on the market. Because of its extreme popularity, the Arduino Uno has a ton of project tutorials and forums around the web that can help you get started or out of a jam. We’re big fans of the Uno because of it’s great features and ease of use.
Here are the components that make up an Arduino board and what each of their functions are.
- Reset Button – This will restart any code that is loaded to the Arduino board
- AREF – Stands for “Analog Reference” and is used to set an external reference voltage
- Ground Pin – There are a few ground pins on the Arduino and they all work the same
- Digital Input/Output – Pins 0-13 can be used for digital input or output
- PWM – The pins marked with the (
) symbol can simulate analog output
Arduino Power Supply
The Arduino Uno needs a power source in order for it to operate and can be powered in a variety of ways. You can do what most people do and connect the board directly to your computer via a USB cable. If you want your project to be mobile, consider using a 9V battery pack to give it juice. The last method would be to use a 9V AC power supply.
Another very important item when working with Arduino is a solderless breadboard. This device allows you to prototype your Arduino project without having to permanently solder the circuit together. Using a breadboard allows you to create temporary prototypes and experiment with different circuit designs. Inside the holes (tie points) of the plastic housing, are metal clips which are connected to each other by strips of conductive material.
On a side note, the breadboard is not powered on its own and needs power brought to it from the Arduino board using jumper wires. These wires are also used to form the circuit by connecting resistors, switches and other components together.
Here is a visual of what a completed Arduino circuit looks like when connected to a breadboard.
How To Program Arduino
Once the circuit has been created on the breadboard, you’ll need to upload the program (known as a sketch) to the Arduino. The sketch is a set of instructions that tells the board what functions it needs to perform. An Arduino board can only hold and perform one sketch at a time. The software used to create Arduino sketches is called the IDE which stands for Integrated Development Environment. The software is free to download and can be found at https://www.arduino.cc/en/Main/Software
Every Arduino sketch has two main parts to the program:
void setup() – Sets things up that have to be done once and then don’t happen again.
void loop() – Contains the instructions that get repeated over and over until the board is turned off.
How Everything Works Together
The following 15 second video gives you a quick idea of how a breadboard, Arduino, jumper wires and the sketch work together to perform a function. In this example, we use a momentary push button switch to blink an LED. In a future post, we’ll dig deeper into the creation of circuits and programs for a few beginner projects.
You may be wondering what an Arduino board can do besides blink an LED. Below are some example projects which help to showcase how truly amazing this board is and the capabilities of it. If you’re looking for more project ideas, check out sites such as Instructables or Make Magazine which are loaded with helpful tutorials.
Next we will help to highlight some of the most common tools you will need when working with Arduino projects.
If you want to add a very specific functionality to your Arduino, you will need to use a shield. Arduino shields plug into the top of the Arduino board and can add capabilities such as WiFi, Bluetooth, GPS and much more. There are literally hundreds of shields to choose from and here are a few examples.
- WiFi Shield
- LCD Shield
- GPS Logger Shield
- MP3 Music Maker Shield
- Ethernet Shield
- Motor/Stepper/Servo Shield
GPS Shield Plugged into Arduino Uno – Sparkfun.com
If you want your Arduino to sense the world around it, you will need to add a sensor. There are a wide range of sensors to choose from and they each have a specific purpose. Below you will find some of the commonly used sensors in projects.
- Distance Ranging Sensor
- PIR Motion Sensor
- Light Sensor
- Degree of Flex Sensor
- Pressure Sensor
- Proximity Sensor
- Acceleration Sensor
- Sound Detecting Sensor
- RGB and Gesture Sensor
- Humidity and Temperature Sensor
Examples of Arduino Sensors
Below are some of our favorite places we tend to go when we need makerspace materials or electronic components.
Are you ready to create your first Arduino project? Our next post called Simple Arduino Projects For Beginners will walk you step by step from setup to completion. These simple Arduino projects are a great way to get your feet wet and learn about the board and coding language.