Hello,
I have a data logger system which is running well and logging to my SD card and displaying the live data on the screen. Odd thing, since adding the SD card the screen more or less flashes during and in sync with an SD card write.
Ive put a video on YouTube to try to show rather than explain. I2s arduino screen flash with spi sd write - YouTube
The flashing isnt always regular but it more or less is and follows the once per sec SD card write. If i remove the SD card or disconnect it or disable in the programme, this flashing stops.
In the video, its connected up to an Uno and is very spread about - ive since stripped down and wired into a Nano and pushed everything together with shorter wires and generally made it very much neater.
Thinking this was perhaps an EMI issue, i twisted together wires going to the SD card into their own bundle - no effect.
Have fitted capacitors near the power feeds to the SD card, the level shifter, the LCD - again, no effect.
I also beefed up the power supply adding an external supply. The 5v rails supplied by USB was sagging to about 4.8V, now back to just over 5.0V and again no effect.
Im wondering if there is a programming thing going on somewhere, or if the SPI & I2C conflict with eachother somewhere?
The relevant parts of the programming are as follows:
/*
* SD card attached to SPI bus as follows:
** MOSI - pin 11
** MISO - pin 12
** CLK - pin 13
** CS - pin 4
*/
#include <Wire.h>
#include "RTClib.h"
#include <FreqMeasure.h>
#include <LiquidCrystal_I2C.h>
#include <SD.h>
#define lenght 20.0
double percent=100.0;
unsigned char b;
unsigned int peace;
RTC_DS1307 RTC;
const int chipSelect = 10;
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
void setup() {
delay(100);
Serial.begin(57600);
Wire.begin();
RTC.begin();
lcd.begin(20, 4);
FreqMeasure.begin();
lcd.createChar(0, p1);
lcd.createChar(1, p2);
lcd.createChar(2, p3);
lcd.createChar(3, p4);
lcd.createChar(4, p5);
pinMode(ledred, OUTPUT);
pinMode(ledgrn, OUTPUT);
pinMode(ScrnBtn, INPUT);
pinMode(FOutPIN, OUTPUT);
digitalWrite(ledred, LOW);
digitalWrite(ledgrn, HIGH);
Serial.print("Initializing SD card...");
// make sure that the default chip select pin is set to
// output, even if you don't use it:
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Initializing SD card...");
pinMode(10, OUTPUT);
// 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:
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("SD card failed,");
lcd.setCursor(0, 1);
lcd.print("or not present");
return;
}
Serial.println("card initialized.");
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("SD card initialized.");
if (! RTC.isrunning()) {
Serial.println("RTC is NOT running!");
// following line sets the RTC to the date & time this sketch was compiled
//RTC.adjust(DateTime(__DATE__, __TIME__));
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("RTC is NOT running!");
}
delay(1000);
}
DateTime now = RTC.now();
if (millis() >= (lastSec + 1000))
{
// Serial.println("Date & Time,Frequency,Sign,DeltaFreq,TempADC,TempUsed,TAdjust,THyst,Delta-,Delta+");
Serial.print(now.year(), DEC);
Serial.print('/');
Serial.print(now.month(), DEC);
Serial.print('/');
Serial.print(now.day(), DEC);
Serial.print(' ');
Serial.print(now.hour(), DEC);
Serial.print(':');
Serial.print(now.minute(), DEC);
Serial.print(':');
Serial.print(now.second(), DEC);
Serial.print(",");
Serial.print(frequency,3);
Serial.print(",");
Serial.print(sign);
Serial.print(",");
Serial.print(DeltaFreq);
Serial.print(",");
Serial.print(TempMeasure);
Serial.print(",");
Serial.print(temperature);
Serial.print(",");
Serial.print(TAdjust);
Serial.print(",");
Serial.print(THyst);
Serial.print(",");
Serial.print(TempSetUse - THyst);
Serial.print(",");
Serial.println(TempSetUse + THyst);
lastSec = millis();
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//SD data log
File dataFile = SD.open("datalog.txt", FILE_WRITE);
// if the file is available, write to it:
if (dataFile) {
// dataFile.println("Date & Time,Frequency,Sign,DeltaFreq,TempADC,TempUsed,TAdjust,THyst,Delta-,Delta+");
dataFile.print(now.year(), DEC);
dataFile.print('/');
dataFile.print(now.month(), DEC);
dataFile.print('/');
dataFile.print(now.day(), DEC);
dataFile.print(' ');
dataFile.print(now.hour(), DEC);
dataFile.print(':');
dataFile.print(now.minute(), DEC);
dataFile.print(':');
dataFile.print(now.second(), DEC);
dataFile.print(",");
dataFile.print(frequency,3);
dataFile.print(",");
dataFile.print(sign);
dataFile.print(",");
dataFile.print(DeltaFreq);
dataFile.print(",");
dataFile.print(TempMeasure);
dataFile.print(",");
dataFile.print(temperature);
dataFile.print(",");
dataFile.print(TAdjust);
dataFile.print(",");
dataFile.print(THyst);
dataFile.print(",");
dataFile.print(TempSetUse - THyst);
dataFile.print(",");
dataFile.println(TempSetUse + THyst);
dataFile.close();
}
// if the file isn't open, pop up an error:
else {
Serial.println("error opening datalog.txt");
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("error opening datalog.txt");
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//LCD data output
ScrnBtnState = digitalRead(ScrnBtn);
if (ScrnBtnState != ScrnBtnLastState)
{
if (ScrnBtnState == HIGH)
{
ScrnBtnCount++;
}
ScrnBtnLastState = ScrnBtnState;
}
if (ScrnBtnCount > ScrnBtnMax)
{
ScrnBtnCount = 0;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// if (ScrnBtnCount == 0)
if (buttonPinValue >= 0 && buttonPinValue <= 100)
{
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Freq: Temp:");
lcd.setCursor(0, 1);
lcd.print(frequency, 3); //Display to 4DP
lcd.print("Hz ");
lcd.print(temperature, 1); //Display to 1DP
lcd.print((char)223);
lcd.print("C ");
lcd.setCursor(0,2);
lcd.print((frequency - FreqSet),4);
double percent = DeltaFreq/0.3*100.0;
lcd.setCursor(0,3);
//lcd.rightToLeft();
double a=lenght/100*percent;
// drawing black rectangles on LCD
if (a>=1) {
for (int i=1;i<a;i++) {
lcd.write(4);
b=i;
}
a=a-b;
}
peace=a*5;
// drawing charater's colums
switch (peace) {
case 0:
break;
case 1:
lcd.print((char)0);
break;
case 2:
lcd.write(1);
break;
case 3:
lcd.write(2);
break;
case 4:
lcd.write(3);
break;
}
}
If there are any clues, i would appreciate pointers in the right direction.