Gadget Renesas

Linking Ideas and Electronics

アイテム

GR-PEACH Special Page

Working out the logger with SD Card

Overview:

With GR-PEACH, save the data in CSV form in SD Card and display graph.in excel file. This includes applied usage such as reading of real-time-clock (RTC) or the value on analog.

Preparations

Get ready GR-PEACH, a USB cable (microB type), a micro SD card, illuminance sensor (NJL7502L)
and 10kΩ resistance. You can try halfway even you don't have illuminance sensor.

login login


login login login

 

Working out the clock


Execute below sample program to have green LED flicker every 1 second.


#include <Arduino.h>
#include <RTC.h>

RTC rtc;
static int year, mon, day, hour, min, sec, week;
 
void setup() {
    pinMode(PIN_LED_GREEN, OUTPUT);
    digitalWrite(PIN_LED_GREEN, LOW); // LED OFF
    Serial.begin(9600);
     
    year = 2016;
    mon  = 7;
    day  = 23;
    hour = 13;
    min  = 20;
    sec  = 50;
    week = RTC_WEEK_SATURDAY;
     
    rtc.begin();
    rtc.setDateTime(year, mon, day, hour, min, sec, week);
 
}
 
void loop() {
    static int sec_old = sec;
    rtc.getDateTime(year, mon, day, hour, min, sec, week);
	
    if((sec - sec_old) % 60){
    	digitalWrite(PIN_LED_GREEN, HIGH); // blink LED
    	delay(5);
    	digitalWrite(PIN_LED_GREEN, LOW);
        Serial.print(year); Serial.print("/");
        Serial.print(mon);  Serial.print("/");
        Serial.print(day);  Serial.print(" ");
        Serial.print(hour); Serial.print(":");
        Serial.print(min);  Serial.print(":");
        Serial.print(sec);
        Serial.println();
    	sec_old = sec;
    }
}
                                


Serial monitor such as Teraterm or Coolterm shows time every one second. This is used for time stamp on data storage in SD Card or time –axis as data logger.

login

 


Working out the graph after data storage in SD Card.

Next, sample program to save the data in SD Card. Insert Micro SD into socket back of GR-PEACH to execute sketch. When writing in SD Card completed successfully, green LED flicker. When you fail to access to SD Card, red LED flash, please make sure if micro SD is inserted correctly.




#include <Arduino.h>
#include <RTC.h>
#include <SD.h>

RTC rtc;
static int year, mon, day, hour, min, sec, week;
void sd_timestamp(uint16_t* date, uint16_t* time);


void setup() {
	pinMode(PIN_LED_GREEN, OUTPUT);
	pinMode(PIN_LED_RED, OUTPUT);
	digitalWrite(PIN_LED_GREEN, LOW);
	digitalWrite(PIN_LED_RED, LOW);
	
	Serial.begin(9600);
	if (!SD.begin()) {
		digitalWrite(PIN_LED_RED, HIGH); // error
		while(1);
	}
	
	SdFile::dateTimeCallback( &sd_timestamp );
	
	year = 2016;
	mon  = 7;
	day  = 23;
	hour = 13;
	min  = 20;
	sec  = 50;
	week = RTC_WEEK_SATURDAY;
	
	rtc.begin();
	rtc.setDateTime(year, mon, day, hour, min, sec, week);

}

void loop() {
	static int deg = 0;
	File file = SD.open("data.csv", FILE_WRITE);
	if (file) {
		digitalWrite(PIN_LED_GREEN, HIGH);
		rtc.getDateTime(year, mon, day, hour, min, sec, week);
		// date
		file.print(year);	file.print("/");
		file.print(mon);	file.print("/");
		file.print(day);
		file.print(",");
		// time
		file.print(hour);	file.print(":");
		file.print(min);	file.print(":");
		file.print(sec);
		file.print(",");
		// data
		file.print(sin(deg * PI / 180.0));
		file.print(",");
		file.print(cos(deg * PI / 180.0));
		file.println();
		file.close();
		deg+=5;
		digitalWrite(PIN_LED_GREEN, LOW);
	}
	
	delay(500);
}

void sd_timestamp(uint16_t* date, uint16_t* time)
{
	*date = FAT_DATE(year, mon, day);
	*time = FAT_TIME(hour, min, sec);
}
                                


Remove the SD Card in appropriate timing, check the status with PC if the file is created in SD Card.

login



Open the DATA.CSV file with a spreadsheet program such as excel.

login



You can see the wave form of Sin and Cos in the graph.

login

 


Working out the logger on illuminance sensor

Finally, we introduce the sample program for data storage in SD Card, measuring with illuminance sensor. The different from the program above mentioned is reading the analog value of A0 pin with analogRead (A0) and save in SD Card. Distinguishing from the above program, once writing in SD Card completed successfully, blue LED flicker. Let's connect the illuminance sensor and resistance to execute the sample program.

login




#include <Arduino.h>
#include <RTC.h>
#include <SD.h>

RTC rtc;
static int year, mon, day, hour, min, sec, week;
void sd_timestamp(uint16_t* date, uint16_t* time);
 
 
void setup() {
    pinMode(PIN_LED_BLUE, OUTPUT);
    pinMode(PIN_LED_RED, OUTPUT);
    digitalWrite(PIN_LED_BLUE, LOW);
    digitalWrite(PIN_LED_RED, LOW);
     
    Serial.begin(9600);
    if (!SD.begin()) {
        digitalWrite(PIN_LED_RED, HIGH); // error
        while(1);
    }
     
    SdFile::dateTimeCallback( &sd_timestamp );
     
    year = 2016;
    mon  = 7;
    day  = 28;
    hour = 23;
    min  = 55;
    sec  = 50;
    week = RTC_WEEK_THURSDAY;
     
    rtc.begin();
    rtc.setDateTime(year, mon, day, hour, min, sec, week);
 
}
 
void loop() {
    File file = SD.open("data.csv", FILE_WRITE);
    if (file) {
        digitalWrite(PIN_LED_BLUE, HIGH);
        rtc.getDateTime(year, mon, day, hour, min, sec, week);
        // date
        file.print(year);   file.print("/");
        file.print(mon);    file.print("/");
        file.print(day);
        file.print(",");
        // time
        file.print(hour);   file.print(":");
        file.print(min);    file.print(":");
        file.print(sec);
        file.print(",");
        // data
        file.print(analogRead(A0));
        file.println();
        file.close();
        digitalWrite(PIN_LED_BLUE, LOW);
    }
     
    delay(500);
}
 
void sd_timestamp(uint16_t* date, uint16_t* time)
{
    *date = FAT_DATE(year, mon, day);
    *time = FAT_TIME(hour, min, sec);
}
                                


According to variation of the illuminance, you can see the numerical value change in the graph. For example, low value shows the low illuminance. The unit of the value of this sample program is not LUX. To know the value of illuminance, calculation is required, considering the spec of the illuminance sensor and changing the resistance level depend on the surrounding environment. Let's try.

login

 


share