I am have difficulty understanding where these strange characters are coming from in the Serial Monitor. I am using an array of uint8_t and then coverting it to an array of type byte, using a function intArrToByteArr(). The array of type byte, is then stored into an AT24C32. The data is then read from the AT24C32 and displayed using the Serial Monitor. I am running it on an Arduino UNO R3, and the AT24C32 is part of a DS3231 RTC module connected to the I2C bus. It is also wired to the 5v and ground pins with the CS connected to pin 10 of the UNO. At the moment I am just learning how to read and write to the EEPROM, the RTC functionality is all ok and used in a different sketch. This is the sketch I am using:
#include <Wire.h>
#define address 0x57
byte data;
int out{0};
uint8_t protime[2][2] {{10, 20}, {12, 30}};
byte arr[2][2];
void setup(){
//Function Prototyping
void intArrToByteArr();
Wire.begin();
Serial.begin(9600);
delay(1000);
Serial.println("Accessing Array of int");
for (int i = 0; i < 2; i++){
for (int j = 0; j < 2; j++){
Serial.println(protime[i][j]);
}
}
intArrToByteArr();
Serial.println("Accessing Array of byte");
for (int i = 0; i < 2; i++){
for (int j = 0; j < 2; j++){
Serial.println(arr[i][j], BIN);
}
}
Serial.println("Store to EEPROM");
//Write to EEPROM
Wire.beginTransmission(address);
Wire.write(0x04);
Wire.write(0x04);
for (int i{0}; i < 2; i++){
for (int j{0}; j < 2; j++){
Wire.write(arr[i][j]);
delay(10);
}
}
Wire.endTransmission();
delay(10);
Serial.println("Stored");
Serial.println("Read from EEPROM");
//Read from EEPROM
Wire.beginTransmission(address);
Wire.write(0x04);
Wire.write(0x04);
Wire.endTransmission();
delay(10);
Wire.requestFrom(address, 4);
delay(10);
for (int i{0}; i < 2; i++){
for (int j{0}; j < 2; j++){
data = Wire.read();
Serial.write(data);
Serial.println(data, BIN);
}
}
delay(10);
Serial.println("Complete");
}
void loop(){
}
void intArrToByteArr(){
byte a1l;
byte a2l;
byte a3l;
byte a4l;
a1l = lowByte(protime[0][0]);
a2l = lowByte(protime[0][1]);
a3l = lowByte(protime[1][0]);
a4l = lowByte(protime[1][1]);
arr[0][0] = a1l;
arr[0][1] = a2l;
arr[1][0] = a3l;
arr[1][1] = a4l;
}
The Serial Monitor outputs:
Accessing Array of int
10
20
12
30
Accessing Array of byte
1010
10100
1100
11110
Store to eprom
Stored
Read from eprom
1010
10100
1100
11110
Complete
The symbols in the output from the EEPROM will not paste here so I have taken a snippet:
I noticed that the first outputted byte, didn't have a symbol next to it, so I changed the integer from 10 to 20 and it ended up the same as the second byte, with a square before it. This suggest that the interger used has an impact, but I am well within the 0-255 range with the integers I have used.
I have tried changing the baud rate on both the sketch and Serial Monitor, and Pins 0 and 1 are unused.
Any insight you can offer would be greatly appreciated.
The Serial monitor interprets the raw bytes as ascii characters, and 10,20,12,30 are not printable characters.
In order to see the numerical value, you can use Serial.print(value, DEC).
for (int i{0}; i < 2; i++){
for (int j{0}; j < 2; j++){
data = Wire.read();
Serial.write(data); //not printable as a character
Serial.print('\t');
Serial.print(data,DEC);
Serial.print('\t');
Serial.println(data, BIN);
}
}
Thanks for the response cattledog, really appreciated. So I get a full understanding of your response, I have a couple of questions. As I have converted from an eight bit integer to a byte I assumed that the information going into the EEPROM would be the same as the information coming out. So byte in and byte out would be the same. What I think your response suggests is that after reading the bytes coming out of the EEPROM there is some hidden processing of the information which assumes a value of type char? For instance something in the Wire or Serial libraries? The second question is if I use the byte coming out of the EEPROM to populate a variable of type uint8_t would the variable be a true representation of the 8 bit integer that went in to the EEPROM or would this suspected hidden functionality cause a type mismatch I.e. expecting type uint8_t but the code is trying to assign it a value of type char? Thank you in advance.
First, you need to realize that uint8_t and byte are the same type as defined in Arduino.h which defines much of the Arduino specific syntax.
typedef uint8_t byte;
Second is that all data on computers is stored and processed in binary. How the binary gets displayed for humans to can vary and the Serial monitor is not as full featured as some other terminal programs.
Sometimes a binary byte value can represent a character. For example the value 123 is byte B01111011. You can interpret this single byte as either the number 123 or the ascii character '{'.
after reading the bytes coming out of the EEPROM there is some hidden processing of the information which assumes a value of type char?
The serial monitor is set up to interpret it as ascii.
Sometimes number can be represented by a character string "123" which will be the three bytes (0x31,0x32,0x33).
The second question is if I use the byte coming out of the EEPROM to populate a variable of type uint8_t would the variable be a true representation of the 8 bit integer that went in to the EEPROM
Yes, as you said, byte in byte out, both 8 bit binary. Wire library does not change anything.