Hi,
I really am at my wits end with this.
How do I display data from an I2C sensor (Pressure) on an I2C (OLED) display that has a SSD1306?
Each device has its own address and if the reading and writing instructions were coded all should be easy enough but I am lost as to how it is done.
The cut down code pasted below is from a pressure sensor sketch and a supplier OLED programme. The OLED at address 0x51 prints characters to screen using Display8x16Str. I have been trying to print a variable D1, say, via DisplayIVANVAR so that it prints to screen on the second line.
I have seen the examples of 2 x Arduino master slave talking to each other but how do 2 I2C devices like these communicate over the 328?
Do I need to use the 128 / 256 Mega and the extra Ports to get the two I2C device talking to each other?
Best
// Wire Master Writer
#include <Wire.h>
#define OLED_ADDRESS 0x51
#define ADDRESS 0x76
#define RESET 0x03
#define DISP_8X16STR 0x10
#define SET_ADDRESS 0x21
#define DISP_IVANVAR 0x11
uint32_t D1 = 0;
uint32_t D2 = 0;
int64_t dT = 0;
int32_t TEMP = 0;
int64_t OFF = 0;
int64_t SENS = 0;
int32_t P = 0;
uint16_t C[7];
float Temperature;
float Pressure;
int DisplayIVANVAR(uint8_t page, uint8_t column, uint32_t D1)
{
Wire.beginTransmission(OLED_ADDRESS);
Wire.write(DISP_IVANVAR); //DEFINE VARIABLE to disay at bottome HERE
Wire.write(page);
Wire.write(column);
Wire.write(D1);
Wire.endTransmission();
}
void Display8x16Str(uint8_t page, uint8_t column, const char *str)
{
Wire.beginTransmission(OLED_ADDRESS);
Wire.write(DISP_8X16STR);
Wire.write(page);
Wire.write(column);
while(*str != '\0')
{
Wire.write(*str++);// that is why carries on writing not sto function
}
Wire.endTransmission();
}
void Reset()
{
Wire.beginTransmission(OLED_ADDRESS);
Wire.write(RESET);
Wire.endTransmission();
}
long getVal(int address, byte code)
{
unsigned long ret = 0;
Wire.beginTransmission(address);
Wire.write(code);
Wire.endTransmission();
delay(10);
// start read sequence
Wire.beginTransmission(address);
Wire.write((byte) 0x00);
Wire.endTransmission();
Wire.beginTransmission(address);
Wire.requestFrom(address, (int)3);
if (Wire.available() >= 3)
{
ret = Wire.read() * (unsigned long)65536 + Wire.read() * (unsigned long)256 + Wire.read();
}
else {
ret = -1;
}
Wire.endTransmission();
return ret;
}
void setup()
{
Wire.begin();
Serial.begin(9600);
Reset();
}
void loop(){
D1 = getVal(ADDRESS, 0x48); // Pressure raw
D2 = getVal(ADDRESS, 0x58);// Temperature raw
{
Display8x16Str(0,0, "Hello World!");
Serial.print(D1); //OK!!
//Here
DisplayIVANVAR(2,0, D1);
Display8x16Str(4,0, "Wellcom to use");
Display8x16Str(6,0, "Gem.Arduino");
delay(2000);
}}