I'm new to Arduino and I'm using an Arduino UNO to read and write current & volt using max471 and reading temperature using three sensors of DS18B20. My code works to read and write on the SD card, but after recording the data, it stops. I tried modifying the code and changing the SD card and SD card reader, but without results.
I leave my code here to see if you can help me with this.
Thanks!
float A;
float AA;
////////////////////////
const int vPIN = A0; //Arduino Analog Pin
float vOUT = 0.0;
float vIN = 0.0;
float R1 = 10000; //Resistor 1 value in ohms (10000 ohms = 10Kohms)
float R2 = 1000; //Resistor 2 value in ohms (1000 ohms = 1Kohms)
int value = 0;
/////////////////////////
const int vPINz = A2; //Arduino Analog Pin
float vOUTz = 0.0;
float vINz = 0.0;
float R1z = 9860; //Resistor 1 value in ohms (10000 ohms = 10Kohms)
float R2z = 990; //Resistor 2 value in ohms (1000 ohms = 1Kohms)
int valuez = 0;
///////////////////////////////
#include <OneWire.h>
#include <DallasTemperature.h>
#define ONE_WIRE_BUS 2
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
int deviceCount = 0;
float tempC;
/////////////////////////
#include <SD.h> //for the SD card module
#include <SPI.h> //for the SD card module
int chipSelect = 4; //chipSelect pin for the SD card Reader
File mySensorData; //Data object you will write your sesnor data to
void setup () {
Serial.begin (9600);
pinMode (A0, INPUT);
pinMode (A1, INPUT);
pinMode (A2,INPUT);
pinMode (A3,INPUT);
sensors.begin();
Serial.print("Locating devices...");
Serial.print("Found ");
deviceCount = sensors.getDeviceCount();
Serial.print(deviceCount, DEC);
Serial.println(" devices.");
Serial.println("");
///////////////////////////////////
Serial.print("Temp1");
Serial.print("\t");
Serial.print("Temp2");
Serial.print("\t");
Serial.print("Temp3");
Serial.print("\t");
Serial.print("Volt1");
Serial.print("\t");
Serial.print("Curr1");
Serial.print("\t");
Serial.print("Volt2");
Serial.print("\t");
Serial.println("Curr2");
////////////////////////////////////
pinMode(10, OUTPUT); //Must declare 10 an output and reserve it
SD.begin(4); //Initialize the SD card reader
}
void loop () {
mySensorData = SD.open("Data.txt", FILE_WRITE);
if (mySensorData) {
sensors.requestTemperatures();
for (int i = 0; i < deviceCount; i++)
{
tempC = sensors.getTempCByIndex(i);
Serial.print(tempC);
Serial.print("\t");
}
///////////////////////////
vOUT = (value * 5.0) / 1023.0;
vIN = vOUT / ( R2 / (R1 + R2) );
value = analogRead(vPIN);
Serial.print(vIN*0.24922);
Serial.print("\t");
A= analogRead (A1)/183.3; //-------calibrate
Serial.print (A*0.791366,3);
Serial.print ("\t");
/////////////////////////////
vOUTz = (valuez * 5.0) / 1023.0;
vINz = vOUTz / ( R2z / (R1z + R2z) );
valuez = analogRead(vPINz);
Serial.print(vINz*0.257);
Serial.print("\t");
AA =analogRead (A3)/183.3;
Serial.print (AA*0.761,3);
Serial.println ("");
////////////////////
for (int i = 0; i < deviceCount; i++)
{
tempC = sensors.getTempCByIndex(i);
mySensorData.print(tempC);
mySensorData.print("\t");
}
mySensorData.print(vIN*0.24922);
mySensorData.print("\t");
mySensorData.print(A*0.791366,3);
mySensorData.print("\t");
mySensorData.print(vINz*0.257);
mySensorData.print("\t");
mySensorData.print(AA*0.761,3);
mySensorData.println("");
mySensorData.close();
}
delay(1000);
}
The data file should be opened only once in setup(), and closed when you are finished collecting data.
Opening the file, writing one line of data and closing it again in loop() drastically increases the power draw, the SD card error rate and greatly slows down data collection.
To prevent data loss when the power is cut, issue a mySensorData.flush(); command every 15 minutes, or hour, etc.
Can you explain how to use a circuit with push button to start and close SD card manually I want to know the code that activates the push buttons for start and stop the SD card
Then there is something wrong with the wiring, the Arduino or the power supply. The Arduino Uno is 5V and the SD card is 3.3V, so logic level shifters are required for the connections between them. If connected without them, the setup might work for a little bit, but one or both devices will eventually be destroyed.
Post a pic of a hand drawn wiring diagram, with pins and connections clearly labeled, and links to the modules.
I checked the SD card and it's written that it operates with 5V not 3.3 V, and I checked my connections but I think the problem is because of the buffer
Some manufacturers exaggerate their claims. Many SD modules have an onboard 5V to 3.3V regulator, so they "operate with 5V", but have no level shifters. Post a link to the product page for the module.
Cross-posting is against the Arduino forum rules. The reason is that duplicate posts can waste the time of the people trying to help. Someone might spend a lot of time investigating and writing a detailed answer on one topic, without knowing that someone else already did the same in the other topic.
Repeated cross-posting can result in a suspension from the forum.
In the future, please only create one topic for each distinct subject matter. This is basic forum etiquette, as explained in the "How to get the best out of this forum" guide. It contains a lot of other useful information. Please read it.
In the IDE, A by itself, turns a color - like it's a keyword.
There's a variable 'A', though. Is that consequential (beyond bad form, a single-letter variable)?
There are several instances of writing to the SD the result of an operation, in parentheses, i.e. --
mySensorData.print(AA*0.761,3);