Muestra las diferencias entre dos versiones de la página.
| Próxima revisión | Revisión previa | ||
| proyectos:datalogger [2024/10/30 08:04] – creado Félix Sánchez-Tembleque | proyectos:datalogger [2024/10/30 18:55] (actual) – [Montaje] Félix Sánchez-Tembleque | ||
|---|---|---|---|
| Línea 1: | Línea 1: | ||
| - | Registrador de datos | + | ====== |
| - | En esta entrada se recoge la creación de un pequeño registrador de datos utilizando un reloj de tiempo real (RTC) y una tarjeta microSD. Para este proyecto en concreto he usado una placa Adafriut Feather 32u4 basic proto con una shield que incluye las dos cosas, el reloj y el zócalo para tarjetas microSD. | + | ===== Montaje ===== |
| - | El montaje del hardware es sencillo, sólo requiere soldar los pines hembra y macho para apilar la placa y la shield. En este caso además en la basic proto se han soldado una tira de pines hembra correspondientes a las seis entrada analógicas y al pin de masa (GMD) que trae la Feather 32u4. Pero podrían valer muchas otras otras combinaciones, | + | En esta entrada se recoge la creación de un pequeño registrador de datos -o datalogger- utilizando un reloj de tiempo real (RTC) y una tarjeta microSD. Para este proyecto en concreto he usado una placa Adafruit Feather 32u4 basic proto con una shield -Adalogger Featherwing- que incluye las dos cosas, el reloj y el zócalo para tarjetas microSD. Para funcionar de manera autónoma necesita una pila de botón para mantener la hora. |
| + | |||
| + | El montaje del hardware es sencillo, sólo requiere soldar los pines hembra y macho para apilar la placa y la shield. En este caso además en la shield | ||
| + | |||
| + | {{: | ||
| El reloj integrado tiene una pequeña deriva de unos 0,4s al día. Si necesitas más precisión Adafruit tiene otro, el [[https:// | El reloj integrado tiene una pequeña deriva de unos 0,4s al día. Si necesitas más precisión Adafruit tiene otro, el [[https:// | ||
| + | |||
| + | |||
| + | ===== Programación ===== | ||
| Una vez soldado sólo necesitas las librerías de Adafruit para el RTC y el código. Aquí tienes un ejemplo: | Una vez soldado sólo necesitas las librerías de Adafruit para el RTC y el código. Aquí tienes un ejemplo: | ||
| + | |||
| + | < | ||
| + | #include " | ||
| + | #include < | ||
| + | #include < | ||
| + | // Feather 32u4 basic proto | ||
| + | #define LED_RED LED_BUILTIN | ||
| + | #define SD_CS 10 | ||
| + | |||
| + | RTC_PCF8523 rtc; | ||
| + | File logfile; | ||
| + | |||
| + | unsigned long timestamp=millis(); | ||
| + | |||
| + | void setup() { | ||
| + | // put your setup code here, to run once: | ||
| + | while (!Serial){; | ||
| + | Serial.begin(115200); | ||
| + | |||
| + | pinMode(LED_RED, | ||
| + | |||
| + | // initialize Real Time Clock (RTC) | ||
| + | |||
| + | if (! rtc.begin()) { | ||
| + | Serial.println(" | ||
| + | Serial.flush(); | ||
| + | while (1) delay(10); | ||
| + | } | ||
| + | | ||
| + | if (! rtc.initialized() || rtc.lostPower()) { | ||
| + | Serial.println(" | ||
| + | | ||
| + | // When time needs to be set on a new device, or after a power loss, the | ||
| + | // following line sets the RTC to the date & time this sketch was compiled | ||
| + | rtc.adjust(DateTime(F(__DATE__), | ||
| + | | ||
| + | // This line sets the RTC with an explicit date & time, for example to set | ||
| + | // January 21, 2014 at 3am you would call: | ||
| + | // rtc.adjust(DateTime(2014, | ||
| + | |||
| + | } | ||
| + | |||
| + | // see if the card is present and can be initialized: | ||
| + | if (!SD.begin(SD_CS)) { | ||
| + | Serial.println(" | ||
| + | error(2); | ||
| + | } | ||
| + | Serial.println(" | ||
| + | |||
| + | // naming the log.csv file to save in the SD card | ||
| + | |||
| + | DateTime now = rtc.now(); | ||
| + | char filename[12]; | ||
| + | sprintf(filename, | ||
| + | | ||
| + | Serial.println(filename); | ||
| + | |||
| + | logfile = SD.open(filename, | ||
| + | if( ! logfile ) { | ||
| + | Serial.print(" | ||
| + | Serial.println(filename); | ||
| + | error(3); | ||
| + | } | ||
| + | Serial.print(" | ||
| + | Serial.println(filename); | ||
| + | Serial.println(" | ||
| + | |||
| + | } | ||
| + | |||
| + | void loop() { | ||
| + | // put your main code here, to run repeatedly: | ||
| + | |||
| + | // ---------------------------------- | ||
| + | // IMPORTANT! | ||
| + | // ---------------------------------- | ||
| + | | ||
| + | if (millis()-timestamp> | ||
| + | { | ||
| + | timestamp=millis(); | ||
| + | if (timestamp> | ||
| + | |||
| + | DateTime now = rtc.now(); | ||
| + | | ||
| + | int U=analogRead(A0); | ||
| + | int V=analogRead(A1); | ||
| + | int W=analogRead(A2); | ||
| + | int S=analogRead(A3); | ||
| + | |||
| + | // creates string to log onto SD card | ||
| + | |||
| + | char logline[40]; | ||
| + | sprintf(logline, | ||
| + | Serial.println(logline); | ||
| + | logfile.println(logline); | ||
| + | // save the output! | ||
| + | logfile.flush(); | ||
| + | } | ||
| + | } | ||
| + | |||
| + | // blink out an error code | ||
| + | void error(uint8_t errnum) { | ||
| + | while(1) { | ||
| + | uint8_t i; | ||
| + | for (i=0; i< | ||
| + | digitalWrite(LED_RED, | ||
| + | delay(100); | ||
| + | digitalWrite(LED_RED, | ||
| + | delay(100); | ||
| + | yield(); | ||
| + | } | ||
| + | for (i=errnum; i<10; i++) { | ||
| + | delay(200); | ||
| + | yield(); | ||
| + | } | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | El ejemplo registra sólo cuatro canales y toma una lectura cada segundo, pero es sencillo modificarlo para variar esto. La lectura no es estrictamente simultánea, | ||
| + | |||
| + | ===== Equipo ===== | ||
| + | |||
| + | * [[: | ||
| + | |||
| + | ===== Enlaces ===== | ||
| + | |||
| + | * [[https:// | ||
| + | |||