-->
Page 1 of 1

NodeMCU multiple i2c dont working.

PostPosted: Fri Apr 19, 2019 3:01 pm
by Barış Güzel
Hello guys;
I'm new in this module. I'm working a project. I try so many ways but i can't do anything. Can u help me?
The project is Data Logger. I have sd card module,rtc ds 1307, mcp9808(i2c temp) and 16x2 lcd.
I want to do run together. I can do in arduino but i can't do in nodemcu.

The problem is how can i run i2c modules together. Sometimes one module working and other modules not working.

My code;
/*
SD card read/write
This example shows how to read and write data to and from an SD card file
The circuit:
SD card attached to SPI bus as follows:
** MOSI - pin 11
** MISO - pin 12
** CLK - pin 13
** CS - pin 10 (for Adafruit Feather 32u4)
*/

#include <SPI.h>
#include <SD.h>
#include <Wire.h>
#include "RTClib.h"
#include "Adafruit_MCP9808.h"
#include <LiquidCrystal_PCF8574.h>

RTC_DS1307 RTC; // define the Real Time Clock object
Adafruit_MCP9808 tempsensor = Adafruit_MCP9808(); // define the MCP9808 object
LiquidCrystal_PCF8574 lcd(0x27); // set the LCD address to 0x27 for a 16 chars and 2 line display


const int chipSelect = 10;
File logfile;
int show;

void setup() {
int error;
// Open serial communications and wait for port to open:
Serial.begin(115200);
Serial.println("LCD...");




while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
Serial.println("Dose: check for LCD");

// See http://playground.arduino.cc/Main/I2cScanner
Wire.begin(D3, D4);
Wire.beginTransmission(0x27);
error = Wire.endTransmission();
Serial.print("Error: ");
Serial.print(error);

if (error == 0) {
Serial.println(": LCD found.");

} else {
Serial.println(": LCD not found.");
} // if

lcd.begin(16, 2); // initialize the lcd
show = 0;
Serial.print("SD Kart Yükleniyor... ");

// see if the card is present and can be initialized:
if (!SD.begin(chipSelect)) {
Serial.println("Kart Hatası");
// don't do anything more:
return;
}
Serial.println("Kart Yüklendi.");

// Open the file to write the .csv legends.
logfile = SD.open("data.txt", FILE_WRITE);
logfile.println("Tarih/Saat, Sıcaklık (°C,°F)");
logfile.close();
Wire.begin(D1, D2);
if (!tempsensor.begin(0x18)) {
Serial.println("Sıcaklık Sensörü Algılanamadı");
while (1);
}


// connect to RTC
Wire.begin(D3, D4);
if (!RTC.begin()) {
logfile.println("Zaman Sensörü Algılanamadı");
}

RTC.adjust(DateTime(F(__DATE__), F(__TIME__)));
}

void loop() {

if (show == 0)
{
lcd.setBacklight(255);
lcd.home(); lcd.clear();


// fetch the time
DateTime now = RTC.now();
// get the temperature
float c = tempsensor.readTempC();
float f = tempsensor.readTempF();
// set up buffer for date and time
char dateBuffer[100];

// Open the file. Note that only one file can be open at a time,
// so you have to close this one before opening another.
File logfile = SD.open("data.txt", FILE_WRITE);

if (logfile) {
logfile.print("Tarih/Saat:");
sprintf(dateBuffer, "%02d-%02d-%02d %02d:%02d:%02d, ", now.day(), now.month(), now.year(), now.hour(), now.minute(), now.second());
logfile.print(dateBuffer);
Serial.print("Tarih:");
Serial.print(dateBuffer);
Serial.print("\n");

Serial.print("Sıcaklık:");
logfile.print(c, 3); logfile.print("°C\t ve ");
logfile.print(f, 3); logfile.println("°F.");
logfile.close();


Serial.print(c, 3); Serial.print("°C\t ve ");
Serial.print(f, 3); Serial.println("°F.");

lcd.setCursor(10, 0);
lcd.print(c,1);
lcd.print((char)223); // degrees character
lcd.print("C");
lcd.setCursor(10, 1);
lcd.print(f,1);
lcd.print((char)223); // degrees character
lcd.print("F");
lcd.setCursor(0, 0);
lcd.print(now.day(), DEC);
lcd.print('/');
lcd.print(now.month(), DEC);
lcd.print('/');
lcd.print(now.year(), DEC);
lcd.setCursor(0, 1);
if (now.hour() < 10) lcd.print("0"); // add zero
lcd.print(now.hour(), DEC); // Print hour
lcd.print(":");
if (now.minute() < 10) lcd.print("0"); // add zero
lcd.print(now.minute(), DEC); // Print minute
lcd.print(":");
if (now.second() < 10) lcd.print("0"); // add zero
lcd.print(now.second(), DEC); // Print second




}
else {
Serial.println("Data Açma Hatası");
}

delay(1000);
} }