{"id":22061,"date":"2018-01-14T21:33:45","date_gmt":"2018-01-14T14:33:45","guid":{"rendered":"http:\/\/tom.ji42.com\/?p=22061"},"modified":"2023-08-15T07:35:59","modified_gmt":"2023-08-15T00:35:59","slug":"using-arduino-with-an-i2c-eeprom","status":"publish","type":"post","link":"https:\/\/tom.tomwork.net\/?p=22061","title":{"rendered":"Using Arduino with an I2C EEPROM"},"content":{"rendered":"<p>I got my hands on an\u00a0<span class=\"wikiword\">AT24C256<\/span>\u00a0(256 kbit = 32 kbyte serial EEPROM). I found no library for it, so I created a small sketch with few functions to show how the i2c_eeprom_write_page and i2c_eeprom_read_byte functions work.<\/p>\n<p>Because this chip is\u00a0<span class=\"wikiword\">I2C<\/span>, it only uses the analog pins 4 &amp; 5 (SDA and SCL), and of course the power (5V) and GND.<\/p>\n<p>Connect as follows:<\/p>\n<p>Arduino analog pin 4 to EEPROM pin 5<br \/>\nArduino analog pin 5 to EEPROM pin 6<br \/>\nArduino 5V to EEPROM pin 8<br \/>\nArduino GND to EEPROM pin 1,2,3,4<\/p>\n<p><!--more--><\/p>\n<p>Be sure to leave pin 7 of the EEPROM open or tie it to GND otherwise the EEPROM will be write protected.<\/p>\n<p>Just a few quick functions for reading\/writing the EEPROM (not a library, yet). &#8216;deviceaddress&#8217; refers to the EEPROM\u00a0I2C\u00a0address, eg. 0x50.<\/p>\n<table border=\"0\" width=\"80%\" cellspacing=\"0\" cellpadding=\"5\" align=\"center\" bgcolor=\"#FFFFBB\">\n<tbody>\n<tr>\n<td valign=\"top\">\/*<br \/>\n* Use the I2C bus with EEPROM 24LC64<br \/>\n* Sketch: eeprom.ino<br \/>\n*<br \/>\n* Author: hkhijhe<br \/>\n* Date: 01\/10\/2010<br \/>\n*<br \/>\n*<br \/>\n*\/#include &lt;Wire.h&gt;<\/p>\n<p>void i2c_eeprom_write_byte( int deviceaddress, unsigned int eeaddress, byte data ) {<br \/>\nint rdata = data;<br \/>\nWire.beginTransmission(deviceaddress);<br \/>\nWire.write((int)(eeaddress &gt;&gt; 8)); \/\/ MSB<br \/>\nWire.write((int)(eeaddress &amp; 0xFF)); \/\/ LSB<br \/>\nWire.write(rdata);<br \/>\nWire.endTransmission();<br \/>\n}<\/p>\n<p>\/\/ WARNING: address is a page address, 6-bit end will wrap around<br \/>\n\/\/ also, data can be maximum of about 30 bytes, because the Wire library has a buffer of 32 bytes<br \/>\nvoid i2c_eeprom_write_page( int deviceaddress, unsigned int eeaddresspage, byte* data, byte length ) {<br \/>\nWire.beginTransmission(deviceaddress);<br \/>\nWire.write((int)(eeaddresspage &gt;&gt; 8)); \/\/ MSB<br \/>\nWire.write((int)(eeaddresspage &amp; 0xFF)); \/\/ LSB<br \/>\nbyte c;<br \/>\nfor ( c = 0; c &lt; length; c++)<br \/>\nWire.write(data[c]);<br \/>\nWire.endTransmission();<br \/>\n}<\/p>\n<p>byte i2c_eeprom_read_byte( int deviceaddress, unsigned int eeaddress ) {<br \/>\nbyte rdata = 0xFF;<br \/>\nWire.beginTransmission(deviceaddress);<br \/>\nWire.write((int)(eeaddress &gt;&gt; 8)); \/\/ MSB<br \/>\nWire.write((int)(eeaddress &amp; 0xFF)); \/\/ LSB<br \/>\nWire.endTransmission();<br \/>\nWire.requestFrom(deviceaddress,1);<br \/>\nif (Wire.available()) rdata = Wire.read();<br \/>\nreturn rdata;<br \/>\n}<\/p>\n<p>\/\/ maybe let&#8217;s not read more than 30 or 32 bytes at a time!<br \/>\nvoid i2c_eeprom_read_buffer( int deviceaddress, unsigned int eeaddress, byte *buffer, int length ) {<br \/>\nWire.beginTransmission(deviceaddress);<br \/>\nWire.write((int)(eeaddress &gt;&gt; 8)); \/\/ MSB<br \/>\nWire.write((int)(eeaddress &amp; 0xFF)); \/\/ LSB<br \/>\nWire.endTransmission();<br \/>\nWire.requestFrom(deviceaddress,length);<br \/>\nint c = 0;<br \/>\nfor ( c = 0; c &lt; length; c++ )<br \/>\nif (Wire.available()) buffer[c] = Wire.read();<br \/>\n}<\/p>\n<p>void setup()<br \/>\n{<br \/>\nchar somedata[] = &#8220;this is data from the eeprom&#8221;; \/\/ data to write<br \/>\nWire.begin(); \/\/ initialise the connection<br \/>\nSerial.begin(9600);<br \/>\ni2c_eeprom_write_page(0x50, 0, (byte *)somedata, sizeof(somedata)); \/\/ write to EEPROM<\/p>\n<p>delay(100); \/\/add a small delay<\/p>\n<p>Serial.println(&#8220;Memory written&#8221;);<br \/>\n}<\/p>\n<p>void loop()<br \/>\n{<br \/>\nint addr=0; \/\/first address<br \/>\nbyte b = i2c_eeprom_read_byte(0x50, 0); \/\/ access the first address from the memory<\/p>\n<p>while (b!=0)<br \/>\n{<br \/>\nSerial.print((char)b); \/\/print content to serial port<br \/>\naddr++; \/\/increase address<br \/>\nb = i2c_eeprom_read_byte(0x50, addr); \/\/access an address from the memory<br \/>\n}<br \/>\nSerial.println(&#8221; &#8220;);<br \/>\ndelay(2000);<br \/>\n}<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n","protected":false},"excerpt":{"rendered":"<p>I got my hands on an\u00a0AT24C256\u00a0(256 kbit = 32 kbyte serial EEPROM). I found no library for it, so I created a small sketch with few functions to show how the i2c_eeprom_write_page and i2c_eeprom_read_byte functions work. Because this chip is\u00a0I2C, it only uses the analog pins 4 &amp; 5 (SDA and SCL), and of course [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[13],"tags":[],"class_list":["post-22061","post","type-post","status-publish","format-standard","hentry","category-13"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p6cOVM-5JP","_links":{"self":[{"href":"https:\/\/tom.tomwork.net\/index.php?rest_route=\/wp\/v2\/posts\/22061","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/tom.tomwork.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/tom.tomwork.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/tom.tomwork.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/tom.tomwork.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=22061"}],"version-history":[{"count":4,"href":"https:\/\/tom.tomwork.net\/index.php?rest_route=\/wp\/v2\/posts\/22061\/revisions"}],"predecessor-version":[{"id":26504,"href":"https:\/\/tom.tomwork.net\/index.php?rest_route=\/wp\/v2\/posts\/22061\/revisions\/26504"}],"wp:attachment":[{"href":"https:\/\/tom.tomwork.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=22061"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/tom.tomwork.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=22061"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/tom.tomwork.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=22061"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}