Go Down

Topic: 'How to use Atmel Dataflash New Library That Works!' (Read 10789 times) previous topic - next topic


I have re-written the playground dataflash library so it works. Atmel AT45DB161D seems to work ok, but I'm no expert and haven't tested every permutation.
You can easily add your own 'block erase' etc, just keep clear of the sector protection etc programming codes or you could brick the flash.
Well, I mean I probably would.
There are a few op-codes not yet implemented, eg block erase, which is very quick.
I suggest you copy the ccp source block of code for a similar function to your new one. Only change the relevant op-code , then create a new function name and create a prototype in dataflash.h and use the txt.file to add it as a keyword high-lighted in orange. I find Keywords are very useful, because spelling mistakes get spotted by their 'plain' text colour.
The change to 512 byte pages from default 528 bytes is 'once-only'. Just a reminder if you choose to do that.


I was interrupted by an official at the frontdoor halfway through last posting and forget to tidy up the example.ino file before attaching.

The //comments re:buffer 2 not working are not relevant any longer of course.
Perhaps a moderator can substitute this updated Atmel dataflash library and example on the Arduino Playground?


Since this is the only code in the public domain which will allow arduino to use atmel dataflash with full functionality, I have added a splash line at the top of the cpp and dataflash.h files with the date 2015.

To moderator - the current dataflash library in the arduino playground does not work properly.

There are numerous unresolved posts which would have been easily answered and fixed by those generous individuals who give their time to assist others with their projects. All it would now need is a reference to this updated 2015 version.

I have done nothing more than go through the files line by line checking what the actual code does.
I have found obvious and unexplained bits of code which meant it would never have worked with Arduino.
I have fixed those parts and now it does.

The old version needs replacing with this one, then problems with dataflash can be directed to it and avoid the frustration I felt when assuming if it was on the playground and described, it must be correct code. It was not.


thanks for the library!

I am trying to use it with a AT45DB041E which has a buffer size of 256/264 instead of the 512/526 byte for the chip you tested the library on.
I tried the example, but it is not working.
Do you know what I should change to make it work?


I am not an expert.
However, one thing you will need to change in the example sketch is the number of bytes written to the buffer to match your memory size.
Also the same change needed when reading back those test values.

I hope you can now find and check out those lines of the relevant loop code which fill the buffer and then read back.

In dataflash.cpp it has code which is supposed to check for the memory size - by reading the DataFlash chip status register.
However, like many other things in that (originally) Atmel library, it is flakey.

These devices are now almost vintage, you may have better luck on trying for help on one of the AVR forum.

Sorry I can't give you the answer off the top of my head, but it took weeks to unravel that dataflash library code, but also really I was only wanting to get it specifically to use the AT45DB161D chip.


Firstly I must register my interest in Dataflash - I work for Adesto, the company that has taken over from Atmel and is manufacturing the Dataflash parts now.

The AT45DB161D parts are now obsolete and you will struggle to get hold of them but we have a replacement called the AT45DB161F family. These parts with the F suffix are in full production are available in densities from 2Mbit to 64Mbit and have an applications note to help you move from the D parts to the F parts in our FAQ section.

We are always looking for C code examples to help our customers work with these parts and can help you help us by supplying samples in return for code examples


Hello guys,

I hope this thread is still alive so I will try to ask my question.

I'm using AT45DB161D flash memories together with this library.

I also know how to connect one single DataFlash memory with the Arduino:
dataflash  arduino
pin 1 -- pin 11
pin 2 -- pin 13
pin 3 -- pin  8
pin 4 -- pin 10
pin 5 -- pin  7
pin 6 -- V (3.3v to 3.6v)
pin 7 -- ground
pin 8 -- pin 12

I wonder if is it feasible to use multiple dataflash pins with this library? I thought maybe it is possible for all dataflash chips to share the same SPI pins (MISO, MOSI, SCK) and just to have different chip selects (CS). Am I on a right way?

Thank you for your time and effort.



 According to this page   http://playground.arduino.cc/Code/Dataflash   for connecting dataflash and arduino

dataflash  arduino
pin 1 -- pin 11
pin 2 -- pin 13
pin 3 -- pin  8
pin 4 -- pin 10
pin 5 -- pin  7
pin 6 -- V (3.3v to 3.6v)
pin 7 -- ground
pin 8 -- pin 12

but dataflash I am using is W25Q80 ( file:///home/aayush/Documents/datasheet.pdf ). vcc is on pin 8 and  ground on pin4 of the dataflash. please help.


aayush8895, The W25Q80 is serial SPI flash, but it's not Dataflash.  I wouldn't expect this library to work with it.  Dataflash is a specific type of flash that was made to work with Atmel chips. 

However, it looks like you still may be in luck.  Someone has written a library for the Winbond flash you have here: https://forum.arduino.cc/index.php?topic=324009.0


Hello I'm trying to use this library with a 1Mb AT45DB011D chip and a standard Uno.  Should this library work with this chip?   In the example sketch I changed the page size to 256 and removed the buffer#2 sections.  However when I print the read values they are apparently random numbers except for the first value. 

Could you help me make this work?  I included my sketch below and the serial output.
Code: [Select]
#include <dataflash.h>

int buffer_counter = 0;
int page_counter = 0;
int lastpage = 0;  //last page written to
int pages = 8;  //total pages that will be used

Dataflash dflash;

void setup()
  dflash.init(); //initialize the memory (pins are defined in dataflash.cpp
  Serial.println("setup complete");

void loop()
  int j = 0;
  int i = 0;

  if (lastpage < pages)
    for (buffer_counter = 0; buffer_counter < 256 ; buffer_counter++)
      dflash.Buffer_Write_Byte(1, buffer_counter, 33);  //  write to buffer 1, 1 byte at a time
    dflash.Buffer_To_Page(1, lastpage);  //  write buffer 1 to the memory on page: lastpage
    for (int i = 0; i <= pages; i++)
      dflash.Page_To_Buffer(i, 1);  //  copy page i to the buffer
      for (int j = 0; j < 256; j++) //  j depends on the amount of data on the page
        Serial.print("page = ");
        Serial.print("loc = ");
        Serial.print("value = ");
        Serial.println(dflash.Buffer_Read_Byte(1, j));  //print the buffer data to the serial port

Code: [Select]
setup complete
page = 0 loc = 0 value = 33
page = 0 loc = 1 value = 191
page = 0 loc = 2 value = 245
page = 0 loc = 3 value = 255
page = 0 loc = 4 value = 118
page = 0 loc = 5 value = 159
page = 0 loc = 6 value = 239
page = 0 loc = 7 value = 247
page = 0 loc = 8 value = 127
page = 0 loc = 9 value = 221
page = 0 loc = 10 value = 58

After 1 page all values = 255.

Interactive addressable led displays for holidays; interactive robots for fun.


FYI I checked the Opcodes for:


for the AT45DB011D these are all the same as the Opcodes in dataflash.h.

Interactive addressable led displays for holidays; interactive robots for fun.


Jun 15, 2019, 01:27 pm Last Edit: Jun 15, 2019, 01:55 pm by Hiddenvision
Just wanted to say THANKS.
I used this thread to start my little project to read/write the at45db321e in a Roberts 93i Radio that had a corrupt firmware. (PC Wizard) on screen.

Roberts were little help wanting me to send the radio for servicing.
It is a radio in fairly poor visible condition so no doubt they would have blamed the condition for the fault and wanted to charge mega bucks to repair.

But I had a working 93i so it was just a matter of copying the contents, editing the MAC and other identifiers to be correct and flashing a new chip.

They are tricky little chips that nothing seems available to read or write off the shelf, so had to make my own.

You need to be careful as they do have some security features that can prevent erase per sector and also some one time fuses that can totally lock a sector from ever being erased ever again.
Oh I remember SS mentioned those, but I did not brick many flash chips on purpose.!
Ahh, still cheaper than even the cost of postage for the radio so no real loss.

I can say this set of files work 100% and a great starting point.
I added some extra Serial code, some other Erase and Size change tools (512 to 528) and wrapped up a small vb6 prog to Read, compare and write the device @ 115200.
Not the fastest but quicker than doing it by hand.!

It maybe possible that there is some newer stuff out there but all my searching found nothing.

Thumbs up to @simplystupid and also mthomas and Atmel for providing the library.

Go Up