H there,
First of all, thanls so much for this great tutorial. Some weeks ago i developed my own SD Card shield and it runs perfectly.
However, now i have a problem: sometimes it does not write data into the SD.
Let me explain. I made a datalogger based on a RTC ds1337 and a SD card wired such as showed in this topic. No apparenty it is not the problem.
I have the arduino sleeping most of the time, and ones per hour the rtc get up the arduino and measure by different senors, and save the data into a SD.
From time to time i remove the SD card from its slot and check the new data. I observed that when i do it (remove and put the SD card into the slot again, it does not save the data). BUT if i remove the slot, but if i remove the power to arduino just immediately after to put the SD card into the slot, OR push the reset button (at the end it is the same)... the next data are saved. :-/
So, i am a little bit lost in this situation. It is not a big problem, because to remove the power and connecting it again is enough, or pushing the reset button but it is not ideal for my project..., right?
I use the filelogger library.
The code is prettly long, so i put here just only the essential lines..
#include <FileLogger.h> // Librería para almacenamiento de dtos en microSD
#include <DallasTemperature.h> // Librería para el sensor de temperatura DS18B20
#include <OneWire.h>
#include <Wire.h>
#include <DS1337.h> // Librería para el RTC DS1307
#include <Sensirion.h> // Librería para el sensor de temperatura y humedad SH15
#include <avr/power.h>
#include <avr/sleep.h> // Librería para modo dormir del microcontrolado
// Definición de los pin analógicos
#define luz 0 // Luminosidad LDR
#define suelo 1 // Humedad del suelo
#define lluvia 2 // Intensidad de la lluvia
#define bateria 3 // Voltaje de entrada
// Definición de los pin digitales
#define alarma 2 // Alarmas del RTC DS1337
#define viento 3 // Anemómetro QRD1114
#define tempsuelo 4 // Temperatura del suelo DS18B20
#define temphumdat 5 // Temperatura/humedad/Punto de rocío SH15
#define temphumcl 6 // Temperatura-humedad SH15
#define vida 7 // Led de vida
#define power 8 // Alimentación de los sensores
#define temp 9 // Temperatura al sol DS18B20
// Activación de librerías
DS1337 RTC = DS1337(); // Configuración librería RTC DS1337
OneWire oneWire(temp); // Configuración librería termómetro DS18B20
DallasTemperature sensors(&oneWire);
OneWire oneWire2(tempsuelo); // Configuración librería termómetro DS18B20
DallasTemperature sensors2(&oneWire2);
DeviceAddress tempDeviceAddress; // Define la dirección de cada sensor de temperatura
Sensirion SH15 = Sensirion(temphumdat, temphumcl);
// Definición de constantes
const float pi = 3.14159265; // Numero PI
const float volt = 0.0108480556; // Resolución voltaje de entrada
const int periodo2 = 10000; // Periodo de medida del anemómetro (ms)
const int radio = 65; // Radio de anemómetro (mm)
const int temp_precision = 12; // Resolución de los sensores de temperarura (bits)
const int sensibilidad = 30; // Sensibilidad del disdrometro
const float area=0.001654; // Area de medida del sensor disdrómetro (m^2)
// Definición de variables
long contador = 1; // Contador de datos almacenados
unsigned int year = 0000; // Año
int month = 00; // Mes
int day = 00; // Día
int hora = 00; // Hora
int minuto = 00; // Minuto
int segundo = 00; // Segundos
float batt = 0; // Voltage de entrada
float innerVcc; // Voltaje interno
float innertemp; // Temperarura interna
float ambtemp = 0; // Temperatura ambiental SH15 (ºC)
float humedad = 0; // Humedad ambiental SH15 (%)
float TRocio = 0; // Punto de rocio (ºC)
unsigned long BWCounter = 0; // Pulsos Blanco/Negro del anemómetro
float velviento = 0; // Velocidad del viento (m/s)
long lux = 0; // Luminosidad (LUX)
float tempext = 0; // Temperatura (ºC)
float suelohum = 0; // Humedad del suelo (%)
float TSAr = 0; // Temperatura del suelo arriba (ºC)
float TSAb = 0; // Temperatura del suelo abajo (ºC)
unsigned long intensidad = 0; // Intensidad de lluvia (gotas/horas/m2)
void setup(){
// Inicializacion RTC
RTC.start();
// Configuración de la alarma
RTC.enable_interrupt();
RTC.setSeconds(55);
RTC.setMinutes(59);
RTC.setAlarmRepeat(EVERY_HOUR);
RTC.writeAlarm();
pinMode(alarma, INPUT); // Receptor de alarmas
digitalWrite(alarma, HIGH);
// Configuración de pins
pinMode(power, OUTPUT); // Alimentacion de sensores
pinMode(vida, OUTPUT); // Led de vida
// Inicializacion del puerto serie
Serial.begin(9600); // Inicia comunicaciones
// Inicialización de sensores
sensors.begin(); // Inicia el sensor de temperatura DS18B20
sensors2.begin();
// Mostrar cabecera por puerto serie (*)
SplashScreen();
}
void loop(){
digitalWrite(power, HIGH);
delay(5000);
digitalWrite(vida, HIGH); // Enciende el led mientras realiza las lecturas
tiempo(); // Toma la hora
delay(20);
readVcc(); // Lee el voltaje interior
delay(20);
midebateria(); // Mide el voltaje de entrada
delay(20);
readTemp(); // Mide la temperatura interna
delay(20);
midegotas(); // Mide la intensidad de lluvia (disdrómetro)
delay(20);
temperatura(); // Mide la temperatura exterior (DS18B20)
delay(20);
sueloH(); // Mide la humedad del suelo
delay(20);
suelotemp(); // Mide la temperatura del suelo
delay (20);
temphumroc(); // Lee temperatura y humedad (SH15) y calcula el punto de rocío
delay (20);
luminosidad(); // Mide la luminosidad (LDR)
delay (20);
velocidadviento(); // Mide la velocidad del viento (QRD1114)
mostrar(); // Muestra los datos a través del puerto serial
grabar(); // Graba los datos en formato ascii en un soporte microSD
delay(5000);
contador = contador + 1; // Actualiza el contador de medidas
digitalWrite(vida, LOW); // Apaga el led al acabar el proceso
digitalWrite(power, LOW);
//attachInterrupt(0, Despertar, FALLING);
delay(500);
Dormir();
}
void Despertar(){
}
void Dormir(){
attachInterrupt(0, Despertar, FALLING);
set_sleep_mode(SLEEP_MODE_PWR_DOWN);
sleep_enable();
RTC.enable_interrupt();
sleep_mode();
// -->FASE DE BAJO CONSUMO DEL DISPOSITIVO<--
sleep_disable();
RTC.disable_interrupt();
detachInterrupt(0);
}
// Lee el Reloj de Tiempo Real (DS1337)
void tiempo(){
// Lee el RTC
RTC.readTime();
// Memoriza la fecha actual
day = RTC.getDays();
month = RTC.getMonths();
year = RTC.getYears();
//Memoriza la hora actual
hora = RTC.getHours();
minuto = RTC.getMinutes();
segundo = RTC.getSeconds();
return;
}
// Lee el voltaje de entrada
float midebateria(){
batt = (analogRead(bateria))*volt;
return batt;
}
Code follows with other functions to measure the different sensors. I think that they are not essential here...
[Sorry, comments are in Spanish]
So,any idea about what could be the idea and how could i solve it by software or hardware?
Thanks!!