element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Arduino Projects
  • Products
  • Arduino
  • Arduino Projects
  • More
  • Cancel
Arduino Projects
Blog Adding more Storage to an Arduino: Using 8-pin SPI Flash Memory Chips
  • Blog
  • Documents
  • Events
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Arduino Projects to participate - click to join for free!
  • Share
  • More
  • Cancel
Group Actions
  • Group RSS
  • More
  • Cancel
Engagement
  • Author Author: shabaz
  • Date Created: 10 Jul 2024 4:02 AM Date Created
  • Views 3472 views
  • Likes 10 likes
  • Comments 3 comments
  • SST25VF080B
  • SPI Flash
  • spi
  • SST25VF
  • flash
Related
Recommended

Adding more Storage to an Arduino: Using 8-pin SPI Flash Memory Chips

shabaz
shabaz
10 Jul 2024

Table of Contents

  • Introduction
    • Simple Read Example
    • Erasing Flash
    • Simple Write Example
  • What’s the Purpose?
  • Connecting the SPI Flash
  • Arduino Flash Library
  • Simple Demo Code
  • Flash Monitor / Programmer
    • Menu access
    • Python access
  • Summary


Introduction

This blog post discusses how to make use of Serial Peripheral Interface (SPI) Flash memory with the Arduino, for simple byte-level access to data. I’m going to be using a custom Arduino library (there are other libraries that may be suitable, but mine is just a no-frills, simple-to-use option. It is more restrictive in that I’ve only tested with a single Flash chip, part code SST25VF080B ; it might not work if you use a different chip. The SST25VF080B chip stores 1Mbyte (i.e. memory locations 0x00000 to 0xFFFFF).

If you want to use a more feature-rich, or more general-purpose library that supports other Flash chips, that’s fine of course; the library described here is merely another option.

Simple Read Example

Lets assume you want to read 16 bytes into an array that you’ve defined:

char data[16];

Assuming the Flash memory is wired up to the Arduino, and the library is installed, then you can read the memory by using the following syntax.

uint32_t addr = 0x00000;
flash.flash_read(addr, data, 16);

The above example reads 16 bytes starting from address 0x00000, into the array data[16].

Erasing Flash

The Flash memory will need to be erased before writing. You can erase either the entire memory:

flash.flash_full_erase();

Or, a 4kbyte sized chunk (i.e. a length of 0x1000) can be erased:

flash.flash_4k_erase(addr);

Simple Write Example

Writing to Flash memory is pretty simple too:

flash.prepare_write(addr);
for (i=0; i<16; i++) {
    flash.byte_write(data[i]);
}
flash.write_complete();

What’s the Purpose?

Sometimes there’s a desire to store and use more storage memory space than exists in the Arduino. For instance, graphics files, or sound files can take up quite a bit of space. I intend to use this library to store star data.

One option is to use a microSD card. However, if you just need to store a few megabytes, then an 8-pin SPI Flash memory chip could work out to be cheaper and easier to use.

Note that this blog post does not consider formatting the memory chip into any filesystem format. The only access is raw bytes, accessible by selecting any address on the Flash chip. This is the simplest, most basic, way of using Flash memory.

The code in this blog post was only tested on an ESP32 board (Adafruit Qualia), and on an Arduino Uno R4 Minima modified for 3.3V logic levels. If you wish to use a 5V logic-level Arduino, then a different Flash memory chip will need to be selected, because the SST25VF080B chip used in this blog post only supports 3.3V logic levels.

Connecting the SPI Flash

Connect the Flash chip to the Arduino board as shown.

image

The pins connections between the Flash chip and the Arduino board are:

Flash Chip ESP32 Board Uno R4 Minima Description
1 (*CE) A0 (IO17) D6 Chip Select, can be any pin that works with DigitalOut
2 (SCK) SCK (IO5) D13 SPI Clock
3 (SI) MOSI (IO7) D11 SPI MOSI (COPI)
2 (SO) MISO (IO6) D12 SPI MISO (CIPO)

Here's my ugly prototyping on the back of a Uno R4 board:

image

And this is the prototyping on the underside of an ESP32 Adafruit Qualia board:

image

If you test this on a different Arduino board and modify the connections and make any code changes, then please mention it in the comments, to help others. Also, as mentioned earlier, the particular Flash chip that I used only supports 3.3V logic levels, so it won’t work with some Arduino boards. The Uno R4 Minima can be easily modified to work with 3.3V logic levels, and the ESP32 uses 3.3V levels by default.

Arduino Flash Library

Download the SST25VF Flash Arduino library from GitHub 

To install it, within the Arduino IDE, click on Sketch->Include Library->Add .ZIP File.

That’s it!

Simple Demo Code

Within the Arduino IDE, click on File->Examples->Flash_SST25VF_Library->Flash_SST25VF_SimpleDemo

When run, a menu appears in the Arduino Serial Monitor. It’s easy to use. You’ll be able to read and write 16 bytes to test the library. If the write test is successful, then 16 bytes (ASCII characters A-P) will be written at the start of the memory, and another 16 bytes (ASCII characters K-Z) will be written at the very end of the memory. That’s all the Simple Demo code does.

image

Flash Monitor / Programmer

By clicking on Examples->Flash_SST25VF_Library->Flash_SST25VF_Monitor_Programmer, a more useful piece of code is available. It is controlled in two ways; either from the menu (like the Simple Demo) or, alternatively, from Python code running on your PC.

Menu access

The menu is self-explanatory, it allows the user to read any part of the memory, by entering an address and number of bytes when requested.

image

As you can see, there are also some menu options to program the memory, but they are not worth running from the menu, because the expected input is raw binary data, which isn’t easy to enter from the Arduino Serial Monitor. Python can be used for that, which is discussed next. If you want to experiment without running the risk of overwriting data on the Flash chip, then use option 8 in the menu, to toggle the dry-run mode, which will make the code still outwardly behave as if it is writing to Flash, but won't actually do that.

Python access

If you want to program the Flash memory chip from a binary file on your PC, then the send_bin.py program can be used. It will communicate (via the USB Serial interface) to the Flash Monitor/Programmer code running on the Arduino, which will in turn program the Flash chip accordingly.

Type the following to transfer any binary file to the SPI memory:

pip install pyserial
python ./send_bin.py mybinaryfile.bin

If you immediately get an error regarding COM port, you’ll need to edit the Python code to reflect the correct serial port name for your system.

The code will take quite a long time (about 5 minutes) to program the chip.

image

To read the contents of the SPI Flash chip, you can use the following command:

python ./receive_bin.py

When run, the Flash contents will be transferred into a file called data.bin

image

Summary

For about $1-$2, an 8-pin SPI Flash memory chip can be attached to Arduino boards, and then the contents can be written or read with just a few commands in the Arduino code.

The Arduino Flash library which was described, also supports the transfer of larger binary files, using Python code running on the PC. This will be handy for bulk transfer of a lot of data (such as audio samples).

Thanks for reading!

  • Sign in to reply
  • shabaz
    shabaz 11 months ago

    I find it hard to mentally visualize memory locations, so I created a simple memory map that can be printed from the web browser. 

    image

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • shabaz
    shabaz 11 months ago in reply to embeddedguy

    1MB is luxury : )
    I still remember my first floppy disk purchase as a schoolkid.. splitting the cost of a pack of them with a friend so that we both could finally save files. : )

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • embeddedguy
    embeddedguy 11 months ago

    Cool stuff. your SPI Flash dangerously needs some space.!

    But useful to store some data such as WiFi AP, BLE and other connection related informations.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube