Hi.
I'm trying to make a datalogger with a temperature and humidity sensor, an oled display and an sd card reader
I have a DHT22 sensor
a 0.96 "oled display (I2C)
Sd card reader (ICSP)
everything works well
but it does not work all at once.
I cant read sensor ant write in sd , and i cant read and show on screen.
but I can not do it all together.
this code readsensor and write in sd card
// Este lee el sensor y guarda los datos en la tarjeta sd
#include <SPI.h>
#include <SD.h>
//librerias del sensor
#include <DHT.h>
#include <DHT_U.h>
#include <Wire.h>
// librerias display
#include <Adafruit_SSD1306.h>
#include <splash.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SPITFT.h>
#include <Adafruit_SPITFT_Macros.h>
#include <gfxfont.h>
#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321
// defines display
#define SCREEN_WIDTH 128 // OLED display ancho, en pixels
#define SCREEN_HEIGHT 64 // OLED display altura
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
//Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
File dataFile;
//File dataFile;
const int DHTPin = 12; // pin digital del sensor
DHT dht(DHTPin, DHTTYPE);
void setup() {
Serial.begin(9600);
Serial.println("DHTxx test!");
if (!SD.begin(4)) {
Serial.println("Falta tarjeta");
// don't do anything more:
while (1);
}
Serial.println("Iniciado correctamente.");
dht.begin();
}
void loop() {
// Pausa entre lecturas
delay(2000);
float h = dht.readHumidity();
float t = dht.readTemperature();
if (isnan(h) || isnan(t)) {
Serial.println("Error leyendo el sensor!");
return;
}
delay(200); // Pause for 2 seconds
// Comienza guardar datos en sd
File dataFile = SD.open("datalog.txt", FILE_WRITE);
// abre el archivo y guarda datos
if (dataFile) {
dataFile.print("Temp = ");
dataFile.print(t);
dataFile.print(" - Humed = ");
dataFile.print(h);
dataFile.println();
dataFile.close();
// print to the serial port too:
Serial.print("Temp : ");
Serial.print(t);
Serial.print(" Humed : ");
Serial.print(h);
Serial.println();
}
// mensaje de error si no se puede abrir el archivo
else {
Serial.println("error abriendo datalog.txt");
}
}
This one reads the sensor and shows the data on the screen
// Esta version muestra temperatura y humedad
// En el display y en monitor serie
#include <SD.h> //Libreria del lector de tarjeta
File logFile;
#include <SPI.h> //Libreria para bus SPI
#include "DHT.h" //libreria sensor temp/Humedad DHT
#include <DHT_U.h>
#include <Adafruit_GFX.h> //librerias display
#include <Adafruit_SPITFT.h>
#include <Adafruit_SPITFT_Macros.h>
#include <gfxfont.h>
#include <Adafruit_SSD1306.h>
#include <splash.h>
#include <Wire.h>
#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321
#define SCREEN_WIDTH 128 // OLED display ancho, en pixels
#define SCREEN_HEIGHT 64 // OLED display altura
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
const int DHTPin = 13; // pin digital del sensor
DHT dht(DHTPin, DHTTYPE);
//RTC_DS3231 rtc;
void setup() {
Serial.begin(9600);
delay(2000);
Serial.println("DHTxx test!");
dht.begin();
Serial.begin(9600);
Serial.print("Iniciando SD ...");
if (!SD.begin(4)) {
Serial.println("No se pudo inicializar");
return;
}
Serial.println("inicializacion exitosa");
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3D for 128x64
Serial.println(F("SSD1306 allocation failed"));
for (;;); // Don't proceed, loop forever
}
display.display();
delay(2000); // pausa
// limpiamos el buffer
display.clearDisplay();
display.clearDisplay();
display.setTextSize(2); // tama�o fuente
display.setTextColor(WHITE); // color
display.setCursor(0, 4); // Start at top-left corner
display.println(F("Termometro V1"));
display.display();
delay(2000); // Pause for 2 seconds
}
void loop() {
// pausa entre lecturas
delay(2000);
// Reading temperature or humidity takes about 250 milliseconds!
float h = dht.readHumidity();
float t = dht.readTemperature();
if (isnan(h) || isnan(t)) {
Serial.println("No se puede leer el sensor!");
return;
}
Serial.print("Humedad: ");
Serial.print(h);
Serial.print(" %\t");
Serial.print("Temperatura: ");
Serial.print(t);
Serial.print(" *C ");
Serial.println("");
// muestra datos en pantalla
display.clearDisplay();
display.display();
display.setTextSize(2); // tama�o del texto
display.setTextColor(WHITE); // color
display.setCursor(0, 0);
display.print("T: ");
display.print(t);
display.println();
display.println();
display.print("H: ");
display.print(h);
display.println();
display.display();
delay(200);
}
I tried other pins, in case there was some incompatibility, but the program stops when you try to open the file.
Food all by the USB of the arduino plate.
Can it be a feeding problem?
What do I do wrong?