See this thread
where you will see sprintf() on Arduino does not do floats.
You need to "look into dtostrf();".
HTh
a7
See this thread
where you will see sprintf() on Arduino does not do floats.
You need to "look into dtostrf();".
HTh
a7
Thank you, my problem is to get the values on the sd card, can you explain what exactly i need to edit?
Please read the thread and look into dtostrf().
And when you experiment, serial print your bufer character array to see what really gets stored on the card.
a7
I have read the thread. I see the lines that I need to change, but I still don't understand how to do it.
/*
Program to demonstrate Data Logging/Visualisation using Arduino
###Connection with SD card module###
Vcc->5V
Gnd->Gnd
MISO->pin 12
MOSI->pin 11
SCK-> pin 13
CS-> pin 4
###Connection with DS3231###
Vcc->5V
Gns->Gnd
SCL->pin A5
SDA-> pin A4
###Connection with DT11###
Vcc->5V
Gnd->Gnd
Out-> pin 7
*/
#include <RTClib.h> //Library for RTC module (Download from Link in article)
#include <Wire.h>
#include <SPI.h> //Library for SPI communication (Pre-Loaded into Arduino)
#include <SD.h> //Library for SD card (Pre-Loaded into Arduino)
#include <dht.h> //Library for dht11 Temperature and Humidity sensor (Download from Link in article)
#define DHT11_PIN 7 //Sensor output pin is connected to pin 7
dht DHT; //Sensor object named as DHT
RTC_DS3231 rtc;
const int chipSelect = 4; //SD card CS pin connected to pin 4 of Arduino
void setup() {
Serial.begin(9600);
Initialize_SDcard();
rtc.begin();
}
void loop() {
Write_SDcard();
delay(5000); //Wait for 5 seconds before writing the next data
}
void Write_SDcard() {
// open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.
int chk = DHT.read11(DHT11_PIN);
float t = DHT.temperature;
float h = DHT.humidity;
Serial.println(t, 1);
Serial.println(h, 1);
DateTime now = rtc.now();
File dataFile = SD.open("LoggerCD.txt", FILE_WRITE);
// if the file is available, write to it:
if (dataFile) {
char bufer[50];
sprintf(bufer, "%02u.%02u.%02u,%02u:%02u:%02u,%0u.%0u,%0u.%0u\r\n",
now.year(),
now.month(),
now.day(),
now.hour(),
now.minute(),
now.second(),
uint16_t(floor(t)), uint16_t(t * 10) % 10,
uint16_t(floor(h)), uint16_t(h * 10) % 10
);
dataFile.print(bufer);
dataFile.close(); //Close the file
}
else
Serial.println("OOPS!! SD card writing failed");
}
void Initialize_SDcard() {
// see if the card is present and can be initialized:
if (!SD.begin(chipSelect)) {
Serial.println("Card failed, or not present");
// don't do anything more:
return;
}
// 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 dataFile = SD.open("LoggerCD.txt", FILE_WRITE);
// if the file is available, write to it:
if (dataFile) {
dataFile.println("Date,Time,Temperature,Humidity"); //Write the first row of the excel file
dataFile.close();
Thank you, that is it!
Do you know why the decimal in the values always matches the value?
sorry, it must be
uint16_t(floor(t)), uint16_t(t * 10) % 10,
uint16_t(floor(h)), uint16_t(h * 10) % 10
Yes, now it works as I wanted.
I would like to connect two more sensors. As I understand it, I just need to use the required lines with another variable. Can you give me a tip on how to get started? So you write #define DHT11_PIN 8 and how should I then proceed with the object?
Wouldn't it be possible to simply name the sensor differently and put it on a different pin?
It looks like it - try it and see
The solution may be in this thread:
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.