Hi … I have a OLED 128×64 Graphic Display I2C Mini Module connected to a MCP3428 4-Channel 4-20mA 16-Bit Current Receiver with IoT Interface.
When I run the following code, it finds an I2C device at address 0x3C
/*
- i2c_port_address_scanner
- Scans ports D0 to D7 on an ESP8266 and searches for I2C device. based on the original code
- available on Arduino.cc and later improved by user Krodal and Nick Gammon (www.gammon.com.au/forum/?id=10896)
- D8 throws exceptions thus it has been left out
*/
#include <Wire.h>
void setup() {
Serial.begin(115200);
while (!Serial); // Leonardo: wait for serial monitor
Serial.println("\n\nI2C Scanner to scan for devices on each port pair D0 to D7");
scanPorts();
}
uint8_t portArray[] = {16, 5, 4, 0, 2, 14, 12, 13};
//String portMap[] = {“D0”, “D1”, “D2”, “D3”, “D4”, “D5”, “D6”, “D7”}; //for Wemos
String portMap[] = {“GPIO16”, “GPIO5”, “GPIO4”, “GPIO0”, “GPIO2”, “GPIO14”, “GPIO12”, “GPIO13”};
void scanPorts() {
for (uint8_t i = 0; i < sizeof(portArray); i++) {
for (uint8_t j = 0; j < sizeof(portArray); j++) {
if (i != j){
Serial.print("Scanning (SDA : SCL) - " + portMap[i] + " : " + portMap[j] + " - ");
Wire.begin(portArray[i], portArray[j]);
check_if_exist_I2C();
}
}
}
}
void check_if_exist_I2C() {
byte error, address;
int nDevices;
nDevices = 0;
for (address = 1; address < 127; address++ ) {
// The i2c_scanner uses the return value of
// the Write.endTransmisstion to see if
// a device did acknowledge to the address.
Wire.beginTransmission(address);
error = Wire.endTransmission();
if (error == 0){
Serial.print("I2C device found at address 0x");
if (address < 16)
Serial.print("0");
Serial.print(address, HEX);
Serial.println(" !");
nDevices++;
} else if (error == 4) {
Serial.print("Unknow error at address 0x");
if (address < 16)
Serial.print("0");
Serial.println(address, HEX);
}
} //for loop
if (nDevices == 0)
Serial.println(“No I2C devices found”);
else
Serial.println("**********************************\n");
//delay(1000); // wait 1 seconds for next scan, did not find it necessary
}
void loop() {
}
But when I run this other code, it doesn’t find any I2C device
/*********
Rui Santos
Complete project details at https://randomnerdtutorials.com
*********/
#include <Wire.h>
void setup() {
Wire.begin();
Serial.begin(115200);
Serial.println("\nI2C Scanner");
}
void loop() {
byte error, address;
int nDevices;
Serial.println(“Scanning…”);
nDevices = 0;
for(address = 1; address < 127; address++ ) {
Wire.beginTransmission(address);
error = Wire.endTransmission();
if (error == 0) {
Serial.print(“I2C device found at address 0x”);
if (address<16) {
Serial.print(“0”);
}
Serial.println(address,HEX);
nDevices++;
}
else if (error==4) {
Serial.print(“Unknow error at address 0x”);
if (address<16) {
Serial.print(“0”);
}
Serial.println(address,HEX);
}
}
if (nDevices == 0) {
Serial.println(“No I2C devices found\n”);
}
else {
Serial.println(“done\n”);
}
delay(5000);
}
How do I communicate with the display?
What code do I use?
*With this example code I do not get readings on the display either … I have two displays and two I2C mini module … none works
#include <Wire.h>
#include <Adafruit_SSD1306.h>
#include <Adafruit_GFX.h>
#define Addr 0x78 // AMS5812 Diff Pressure Sensor I2C address is 0x78(120)
#define OLED_ADDR 0x3C // OLED display I2C address (0x3C)
Adafruit_SSD1306 display(-1);
void setup() {
Wire.begin(); // Initialise I2C communication as MASTER
Serial.begin(115200); // Initialise serial communication, set baud rate = 9600
delay(600); // delay after initializing text
// initialize and clear display
display.begin(SSD1306_SWITCHCAPVCC, OLED_ADDR);
display.clearDisplay();
display.display();
display.setTextSize(1); // display a line of text
display.setTextColor(WHITE);
display.setCursor(15, 20);
display.print(“Initializing…”);
display.display(); // update display with all of the above graphics
}
void loop()
{
unsigned int data[4];
display.clearDisplay();
delay(600); //delay to start below
Wire.requestFrom(Addr, 4); // Request 4 bytes of data
// Read 4 bytes of data: pressure msb, pressure lsb, temp msb, temp lsb
if (Wire.available() == 4)
{
data[0] = Wire.read();
data[1] = Wire.read();
data[2] = Wire.read();
data[3] = Wire.read();
}
float pressure = ((data[0] & 0xFF) * 256 + (data[1] & 0xFF)); // Convert the data
float temp = ((data[2] & 0xFF) * 256 + (data[3] & 0xFF));
pressure = ((pressure - 3277.0) / ((26214.0) / 30.0)) - 15.0;
float cTemp = ((temp - 3277.0) / ((26214.0) / 110.0)) - 25.0;
float fTemp = (cTemp * 1.8 ) + 32;
float pressurembar = pressure * 68.9476;
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(15, 05);
display.print("Pressure: ");
display.setCursor(15, 15);
display.print(pressurembar);
display.setCursor(15, 25);
display.println(“mbar”);
display.setCursor(15, 35);
display.print(pressure);
display.display(); // update display with all of the above graphics
delay(500);
display.clearDisplay();
// Output data to serial monitor
Serial.print(data[0]);
Serial.print(" “);
Serial.print(data[1]);
Serial.print(” “);
Serial.print(data[2]);
Serial.print(” “);
Serial.print(data[3]);
Serial.print(” ");
Serial.print(“Pressure : “);
Serial.print(pressure);
Serial.println(” PSI”);
}