🌐
Arduino Forum
forum.arduino.cc › projects › programming
PROGMEM and using array with pgm_read_byte_near - Programming - Arduino Forum
August 17, 2018 - I am designing some code to light up some LED's with an ATTINY, and because of limited RAM, I am using the FLASH to store the patterns. I am currently testing this on a regular Arduino UNO: #include #include uint8_t const sineWave[] PROGMEM = { 16,16,17,17,18,18,18,19,19,20,20,20,21,21,21,22,22,22,23,23,24,24,24,25,25,25,26,26,26,26,27,27,27,28,28,28,28,29,29,29,29,30,30,30,30,30,30,31,31,31,31,31,31,31,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,3...
🌐
FC2
garretlab.web.fc2.com › top › arduino › internal structure of arduino software › avr-gcc › pgmspace.h › pgm_read_byte_near()
pgm_read_byte_near() - garretlab - FC2
July 31, 2025 - A page to record my playing with Arduino · pgm_read_byte_near() The pgm_read_byte_near() is a macro that reads a byte of data stored in a specified address(PROGMEM area). The pgm_read_byte_near() is defined in hardware/tools/avr/avr/include/avr/pgmspace.h as below.
Discussions

PROGMEM and using array with pgm_read_byte_near
I am designing some code to light up some LED's with an ATTINY, and because of limited RAM, I am using the FLASH to store the patterns. I am currently testing this on a regular Arduino UNO: #include #includ… More on forum.arduino.cc
🌐 forum.arduino.cc
0
0
August 17, 2018
pgm_read_byte() Question
Arduino noob here, struggling to understand how a couple of things work in a sketch I'm trying to hack. const uint8_t *framePos = frameList; x = pgm_read_byte(framePos++) I read this as: define constant pointer framePos to frameList array in RAM set x to byte read from memory at address held ... More on forum.arduino.cc
🌐 forum.arduino.cc
0
0
May 20, 2016
pgm_read_byte() does not work well without delay()
I have the following code: · But swapping Serial.println(data, HEX); and delay(250); then works. Why More on github.com
🌐 github.com
44
April 15, 2017
serial - Send a 2d PROGMEM array over SoftwareSerial - Arduino Stack Exchange
Then I learned about pgm_read_byte_near() and pgm_read_word(). More on arduino.stackexchange.com
🌐 arduino.stackexchange.com
🌐
Arduino Forum
forum.arduino.cc › projects › programming
PROGMEM and arrays - Programming - Arduino Forum
February 18, 2018 - Hi, I'm in process of lowering memory footprint of little program of mine, main culprit being that I have need of quite a few arrays. I thought I could put arrays into progmem, and then reading them with pgm_read_byte_near() as needed. Example of array: int shiftTimeMap[14][12] PROGMEM { {999, 0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100 }, //shift pressure % //----------------------------------------------------------------------- { -20, 1100, 1100, 1100, 1100, 1...
🌐
Nongnu
nongnu.org › avr-libc › user-manual › group__avr__pgmspace.html
avr-libc: <avr/pgmspace.h>: Program Space Utilities
The address is a byte address. The address is in the program space. Read a float from the program space with a 16-bit (near) address.
🌐
FC2
garretlab.web.fc2.com › top › arduino › internal structure of arduino software › avr-gcc › pgmspace.h › pgm_read_byte()
pgm_read_byte() - garretlab - FC2
July 31, 2025 - The pgm_read_byte() is a macro that read a word of data stored in a specified address(PROGMEM area).
🌐
WoodUino.ca
woodsgood.ca › projects › 2015 › 02 › 06 › accessing-arduino-program-memory
Accessing Arduino Program Memory – WoodUino.ca
February 24, 2015 - The pgm_read_* and pgm_read_*_near macros make it simple to access individual elements of an array, no matter whether it is an array of bytes/characters, strings, integers, longs or floats.
🌐
GitHub
github.com › arduino › ArduinoCore-sam › blob › master › cores › arduino › avr › pgmspace.h
ArduinoCore-sam/cores/arduino/avr/pgmspace.h at master · arduino/ArduinoCore-sam
#define pgm_read_byte_near(addr) pgm_read_byte(addr) #define pgm_read_word_near(addr) pgm_read_word(addr) #define pgm_read_dword_near(addr) pgm_read_dword(addr) #define pgm_read_float_near(addr) pgm_read_float(addr) #define pgm_read_ptr_near(addr) pgm_read_ptr(addr) ·
Author   arduino
🌐
TutorialsPoint
tutorialspoint.com › how-to-use-progmem-in-arduino-to-store-large-immutable-data
How to use PROGMEM in Arduino to store large immutable data?
July 24, 2021 - pgm_read_byte_near(address) − This function returns the value of one byte located at address.
🌐
Arduino Forum
forum.arduino.cc › projects › programming
PROGMEM and using array with pgm_read_byte_near - #5 by DKWatson - Programming - Arduino Forum
August 17, 2018 - First, you can use pgm_read_byte_near or simply pgm_read_byte. The pgm_read functions want an address. When you issued your statements that 'worked' (i.e. Serial.println(pgm_read_byte_near(waveTable[1])); ), you were passing the base address of waveTable[1], ignoring the fact that waveTable ...
Find elsewhere
🌐
Phipps Electronics
phippselectronics.com › home › blog › using the flash program memory of arduino to pre-store data
Using the Flash Program Memory of Arduino to Pre-store Data - Phipps Electronics
November 29, 2023 - It simply tells the program to ... automatically stored in RAM space. Here are sample variable declarations using PROGMEM · To access this byte stored in the program memory space, use the pgm_read_byte_near() function....
🌐
Arduino Forum
forum.arduino.cc › projects › programming
pgm_read_byte() Question - Programming - Arduino Forum
May 20, 2016 - Arduino noob here, struggling to understand how a couple of things work in a sketch I'm trying to hack. const uint8_t *framePos = frameList; x = pgm_read_byte(framePos++) I read this as: define constant pointer framePos to frameList array in RAM set x to byte read from memory at address held in pointer.
🌐
GitHub
github.com › esp8266 › Arduino › issues › 3140
pgm_read_byte() does not work well without delay() · Issue #3140 · esp8266/Arduino
April 15, 2017 - PROGMEM const uint8_t table[2][3] = { {0x7B,0x60,0x5D}, {0x7D,0x48,0x3E} }; void setup() { Serial.begin(115200); } void loop() { uint8_t data = pgm_read_byte(&table[1][1]); delayMicroseconds(0); Serial.println(data, HEX); delay(250); }
Author   milkpirate
🌐
GitHub
github.com › redbear › STM32-Arduino › blob › master › arduino › cores › RedBear_Duo › firmware › wiring › inc › avr › pgmspace.h
STM32-Arduino/arduino/cores/RedBear_Duo/firmware/wiring/inc/avr/pgmspace.h at master · redbear/STM32-Arduino
#ifdef pgm_read_byte_near · #undef pgm_read_byte_near · #endif · #define pgm_read_byte_near(addr) pgm_read_byte(addr) · #ifdef pgm_read_word_near · #undef pgm_read_word_near · #endif · #define pgm_read_word_near(addr) pgm_read_word(addr) · #define pgm_read_dword_near(addr) pgm_read_dword(addr) #define pgm_read_float_near(addr) pgm_read_float(addr) #define pgm_read_ptr_near(addr) pgm_read_ptr(addr) ·
Author   redbear
🌐
FC2
garretlab.web.fc2.com › top › arduino › internal structure of arduino software › avr-gcc › pgmspace.h › pgm_read_byte()
pgm_read_byte()
July 31, 2025 - The pgm_read_byte() is a macro that read a word of data stored in a specified address(PROGMEM area).
🌐
GitHub
github.com › espressif › arduino-esp32 › blob › master › cores › esp32 › pgmspace.h
arduino-esp32/cores/esp32/pgmspace.h at master · espressif/arduino-esp32
#define pgm_read_byte_near(addr) pgm_read_byte(addr) #define pgm_read_word_near(addr) pgm_read_word(addr) #define pgm_read_dword_near(addr) pgm_read_dword(addr) #define pgm_read_float_near(addr) pgm_read_float(addr) #define pgm_read_ptr_near(addr) pgm_read_ptr(addr) #define pgm_read_byte_far(addr) pgm_read_byte(addr) #define pgm_read_word_far(addr) pgm_read_word(addr) #define pgm_read_dword_far(addr) pgm_read_dword(addr) #define pgm_read_float_far(addr) pgm_read_float(addr) #define pgm_read_ptr_far(addr) pgm_read_ptr(addr) ·
Author   espressif
🌐
Arduino
docs.arduino.cc › language-reference › en › variables › utilities › PROGMEM
PROGMEM | Arduino Documentation
May 20, 2024 - Arduino programming language can be divided in three main parts: functions, values (variables and constants), and structure · For controlling the Arduino board and performing computations
Top answer
1 of 4
7

I am afraid there is no good solution to this problem. One option I do like is to use the __flash qualifier instead of PROGMEM:

const uint8_t ram_array[] = { 1, 2, 3, 4 };
__flash const uint8_t flash_array[] = { 5, 6, 7, 8 };

void function_reading_ram(const uint8_t *array)
{
    uint8_t secondElement = array[1];
    // ...
}

void function_reading_flash(__flash const uint8_t *array)
{
    uint8_t secondElement = array[1];
    // ...
}

int main(void)
{
    function_reading_ram(ram_array);      // OK
    function_reading_flash(flash_array);  // OK
    function_reading_ram(flash_array);    // Warning
    function_reading_flash(ram_array);    // Warning
}

Note that, with __flash, you don't need pgm_read_byte_near() or anything similar. You just use the array like you would use any regular array, and the compiler is smart enough to generate the code required to access the Flash memory.

The warnings generated by gcc are:

warning: conversion from address space ‘__flash’ to address space ‘generic’ [-Waddr-space-convert]
     function_reading_ram(flash_array);
     ^
warning: conversion from address space ‘generic’ to address space ‘__flash’ [-Waddr-space-convert]
     function_reading_flash(ram_array);
     ^

Given how nice this solution is, you may wonder why I wrote “there is no good solution”. Well, this comes with two caveats, one small and one huge. The small caveat is that you need to explicitly pass the option -Waddr-space-convert to the compiler if you want it to generate the warnings. It is not implied even with -Wall -Wextra. The huge caveat is that this only works in plain C. If you try to use this trick in C++ you get:

error: ‘__flash’ does not name a type

This is an implementation of the “named address spaces” extension to the standard C. Since nothing like this is standardized in C++, the authors of gcc assumed it would not be useful to users of C++. :-( You can mix C and C++ sources in Arduino, but you can't call class methods from C.

2 of 4
5

I will expand on KIIV's comment here.

This is a known issue to the people who wrote the Arduino framework. Essentially, the pointers to PROGMEM and RAM are both data pointers (think of a pointer like an array index, but for the entire RAM/Flash itself rather than one chunk of it that you manage) of the same size and type. They cannot be told apart since pointer types are only determined by the size of the data, not where the data is.

So, they used a clever solution: Make the pointer into a different pointer that isn't compatible with the regular RAM pointer. This is done by casting to the __FlashStringHelper class (and then casting back when it must be read). The FSH class doesn't actually have any methods or do anything; it's simply used to define a custom pointer type that is different from the normal const *.

You can directly go read one of the better guides I've seen on it here (Note that it's for the ESP8266, but this part works the same), or I can try to explain it. I suggest reading it anyway, since it has a lot of other info that's useful but not relevant to this question. Do note that they have a few examples near the end which don't de-allocate memory when they are done using it, though, so copying them directly could cause a memory leak (see my string processing example to see the proper method).

First, you need to cast the array to the FSH class:

const byte MyProgmemArray[] PROGMEM = {4,4,4,4,4,4,4,4}; //Fixes issue #82 (add randomness); see https://xkcd.com/221/ for details :D
__FlashStringHelper * MyArray = (__FlashStringHelper *) MyProgmemArray;

I am unaware if there is any method (and I did look) to declare these in-place, i.e. to make the original array declaration into a __FlashStringHelper * directly. So you still need, as the programmer/user, to know which strings are in flash when you add them, and add the conversion line. This means that what you wanted (fully automatic RAM/Flash detection) isn't quite possible, but this is as close as I think it gets. A further warning is that the original array still exists, meaning that it could accidentally be used instead (will still cause the original mismatch issue) and the new definition takes up slightly more memory for the additional pointer (if the compiler does not optimize it out?).

Oddly, it works fine if your want to store a string:

__FlashStringHelper * MyString = PSTR("yaddayaddayadda...");

Strings can be inlined, but I think it's because of both the PSTR macro and because string definitions might be treated differently than an array of values (even though they are physically the same thing). Perhaps you could experiment with however PSTR works (it's actually a macro that does something, and isn't just a keyword) and see if you can implement this in your own code after all.


Now, you still need to get the data OUT of the array in that function you mentioned. To do that, we actually use two versions of that function. This is called overloading (and I think it's C++ only, but this is Arduino, which is by definition C++, so it's safe).

The RAM function you declare as normal:

void MyFunction(const uint8_t *MY_ARRAY){
  byte index_two = MY_ARRAY[1];
  ...
}

The PROGMEM function you declare as follows (assuming that pgm_read_byte_near IS the right function. Also note that it gets more complicated if you need multi-byte values, like int or something...):

void MyFunction(__FlashStringHelper *MY_PROGMEM_ARRAY){
  const byte *MY_ARRAY = (const byte *)MY_PROGMEM_ARRAY; //convert back to normal
  byte index_two = pgm_read_byte_near(MY_ARRAY+1); //still have to read it as flash, of course
  ...
}

As a bonus bit of info that is unrelated to the question as asked, but that may interest you/be relevant later:

There are a few more notes if you use this for string data (or pretend it's a string for this part, which works if your data contains no zeros and ends in one). By the latter, I refer to the fact that strings in C (NOT the C++ std::string or Arduino String, though, so keep this in mind) are basically char arrays with a 0 tacked on the end. All string-processing functions run an infinite loop until they read a 0 at their current index, which allows them to work with strings of any length but still know when they are done. As a result, when reading it, you can treat your data like a string, with three caveats:

  1. It can never contain a 0, except for the very last item, which won't be read.
  2. When defining it, you'd need to still use {} instead of "" since most values don't have printable ASCII characters. (And even so, you'd have to write " " for 32, for instance.)
  3. You can't actually print the alleged string, since the data won't actually contain printable characters. You'll get any mix of numbers, letters, punctuation, and fun things like backspace and newline all jumbled together. You would need to read each character and convert it (via, say, String num = <your value here>; or itoa() etc).

If you want to print it using an existing Arduino function, like Serial.print, and it is actually a string (and not data pretending to be a string), it will natively support __FlashStringHelper *, so you don't need the conversion step (this lets you do Serial.print(F("No static RAM used here!")); and similar).

There are two ways to read the data if it is for your own string-processing function, however. The first, the simple one, is not memory efficient and creates a full copy of the data or string in RAM while it's being used. As a result, it is not capable of processing strings that are too large for RAM and may have issues if it's called while a lot of RAM is already in use or it is called deep down a set of nested functions. It is very convenient, however:

//convert from __FlashStringHelper * as above, but let's assume it's a char* here
int size = strlen_P(MY_ARRAY);
if (size!=0){
  char * data = new char[size];
  if (data!=NULL){
    strcpy_P(data,MY_ARRAY);
    //do stuff with the string
    delete data;
  }
}

The second method is more annoying to actually do, and requires more control over how the data is processed, but you can also read out the data byte-by-byte as shown in my first decoding example above using pgm_read_byte_near. This lets you work with data that is too large to fit in RAM and keeps a constant stack size too. The downside is that it requires that you are actually able to stream the data byte-by-byte in all places that it's then used (or that you re-read it when it needs to be accessed out-of-order).

🌐
Arduino Forum
forum.arduino.cc › other hardware › storage
Direct addressing to the Arduino Flash Memory - Storage - Arduino Forum
July 25, 2022 - Is there a way to use pgm_read_byte_near(n) or pgm_read_byte_far(n) when n, in this particular case, is a physical address of type 0x00AFFFF (ie as a pointer to a variable, but without defining any variables? Namely, pgm_read_byte_near(0x00AFFFF). I'm loading an array of data into the flash ...