Help programing I2C with Arduino  - Page 1
Forum section: electronics / beginners

author: DTIVKITT
author_rank: Newbie
timestamp: July 09, 2021, 02:04:01 am
content: Hi everyone. I need to program an 24LC512 I2C EEPROM with the entire chip programed with 0x09. To do this I have an Arduino Uno, but all the sketches I could find only program 1 byte at time. Any help would be appreciated.

author: ledtester
author_rank: Super Contributor
timestamp: July 09, 2021, 02:28:14 am
content: You just keep on sending data -- up to a total of 128 data bytes -- using Wire.send() or whatever sends a byte over I2C. After sending 128 data bytes you have to "endTransmission" and start a new I2C transmission.If you post your sketch we can tell you how to modify it.

author: DTIVKITT
author_rank: Newbie
timestamp: July 09, 2021, 12:32:49 pm
content: Great, thanks. Here is the code I used. To test its working, after uploaded the 1st time, I commented the "writeAddress(address, val)" and reuploaded. The result was as expected "The returned value is 9"Code:#include "Wire.h"#define EEPROM_I2C_ADDRESS 0x50void setup(){Wire.begin();Serial.begin(9600);int address = 0;byte val = 0x09;writeAddress(address, val);byte readVal = readAddress(address);Serial.print("The returned value is ");Serial.println(readVal);}void loop(){}void writeAddress(int address, byte val){Wire.beginTransmission(EEPROM_I2C_ADDRESS);Wire.write((int)(address >> 8)); // MSBWire.write((int)(address & 0xFF)); // LSBWire.write(val);Wire.endTransmission();delay(5);}byte readAddress(int address){byte rData = 0xFF;Wire.beginTransmission(EEPROM_I2C_ADDRESS);Wire.write((int)(address >> 8)); // MSBWire.write((int)(address & 0xFF)); // LSBWire.endTransmission();Wire.requestFrom(EEPROM_I2C_ADDRESS, 1);rData = Wire.read();return rData;}

author: ledtester
author_rank: Super Contributor
timestamp: July 09, 2021, 01:56:16 pm
content: Have a look at the routine i2c_eeprom_write_page on this page:https://playground.arduino.cc/Code/I2CEEPROM/

author: DTIVKITT
author_rank: Newbie
timestamp: July 09, 2021, 05:51:37 pm
content: I think I should change this part right?Code:char somedata[] = "this is data from the eeprom"; // data to writeTried changing to 0x09, 09, 9, 011, always get an error when compiling, without the "". Tried also with the "", then it compiles and I can upload, but that don't seem right to me, looks like this way its going to the EEPROM as a "text" and not a "code". Correct me if I'm wrong.As you can see, I don't understand much about programing and stuff.

author: mon2
author_rank: Frequent Contributor
timestamp: July 09, 2021, 06:15:31 pm
content: 1) Post your code & error log that you are seeing.2) The "" (2 of double quotes) defines a string type. These are text values that will be stored in an array format.Take for example, "0" = 0x30 = 30 (hex) = 48 (decimal). All are the same. So if you save "0" to the eeprom, you can expect that a hex value of 30 will be stored inside the EEprom device.If you have for example, "01" -> this will save 30 31 (each are in hex format) inside the EEprom."0123" = 30 31 32 33 (hex format).

author: macboy
author_rank: Super Contributor
timestamp: July 09, 2021, 06:19:18 pm
content: Quote from: DTIVKITT on July 09, 2021, 05:51:37 pmThis is how you might change it to use numeric data instead of a string.Code:uint8_t somedata[] = { 0, 1, 0x2, 0xFF }; // data to writeThat is an array with four 8-bit values: 0, 1, 2, and 255. The curly braces and commas are how you specify an array.Technically, a "char" type in C++ (which is what Arduino IDE uses, if you didn't know) is a signed type, and has range of -128 to +127. To use numeric values in the range 0 to 255 (0x00 to 0xFF), it is better to use either "unsigned char" or "uint8_t". The "uint8_t" is defined in more recent variations of C++, but with older versions it is usually defined in some library header as a typedef to one of the standard types (probably unsigned char).

author: DTIVKITT
author_rank: Newbie
timestamp: July 10, 2021, 09:53:29 pm
content: Thanks everyone. I think I managed to write the 0x09 (or Horizontal Tab, according to the tables on the internet). After changing this partCode:char somedata[] = "this is data from the eeprom"; // data to writeto thisCode:uint8_t somedata[] = {0x09,0x09,0x09,0x09}; // data to writeand commenting all the "Serial.println" on the code, to make sure nothing else goes to the chip, I uploaded and opened the Serial Monitor and got a lot of blank texts, then copied all of it and pasted on a hex editor, and got a lot of 09. So I think it worked.Just one little thing. How can I make sure the entire chip is full with 09 ?