I need to write and read the measurements received by a sensor on an SD card. The card reading would be to display the data on a TFT screen. Specifically, I am using the 1.8-inch ST7735 screen. To use the card with Arduino I am using an SD card reader module (ANGEEK brand). Both devices work with SPI protocol.
To verify that this would be possible I make a test code. In it, two functions (measure_V and measure_I) simulate the voltage and current measurements. So what I do is call those two functions to get 10 measurements and save all of them to SD. Then I read them and display them on the screen. I show them both on the serial monitor and on the TFT screen. In order to map the values of V_data and I_data I use two functions that give me the maximum value in each measure. However, nothing appears either on the serial monitor or on the screen. The two devices share the MISO, MOSI, and SCK pins. Could someone help me with this? It is the first time that I use the SPI protocol with more than one device.
#include <Adafruit_GFX.h>
#include <Adafruit_ST7735.h>
#include <SD.h>
#include <SPI.h>
#define TFT_CS 10
#define A0_pin 8
#define RST_pin 9
#define SD_CS 7 // CS pin of SD card connected to pin 7 of Arduino
Adafruit_ST7735 tft = Adafruit_ST7735(TFT_CS, A0_pin, RST_pin);
const uint8_t rotation=2; // Origin of coordinates top left
float V_data[10], I_data[10]; // Store voltage and current measurements
File V_points;
File I_points;
const uint16_t Display_Color_Green = 0x07E0; // Green color
int measure_V(int x){
int voltage;
voltage = x;
return voltage;
}
int measure_I(int x){
int current;
current = pow(x, 2);
return current;
}
int max_V(){
int maxVal = 0;
for (int i=0; i<sizeof(V_data)/sizeof(float); i++) {
if (maxVal < V_data[i]) {
maxVal= V_data[i];
}
}
return maxVal;
}
int max_I(){
int maxVal = 0;
for (int i=0; i<sizeof(I_data)/sizeof(float); i++) {
if (maxVal < I_data[i]) {
maxVal= I_data[i];
}
}
return maxVal;
}
void setup() {
Serial.begin(9600);
tft.initR(INITR_GREENTAB); // Initialize a ST7735R chip, green tab
tft.setSPISpeed(40000000);
tft.setRotation(rotation); // We set type of rotation
tft.fillScreen(ST7735_BLACK);
Serial.print("Initializing SD card...");
if (!SD.begin(SD_CS))
{
Serial.println("SD initialization failed!");
while (1) ;
}
Serial.println("SD initialization done.");
}
void loop() {
int static n = 0; // Measurement number (initial measurement = 0)
uint16_t pixelV, pixelI;
// We perform and save on the memory card 10 measurements
for(int i=0; i<=9; i++){
V_points.print(measure_V(i)); // New voltage value
I_points.print(measure_I(i)); // New current value
}
// We read the 10 measures of the card
while (V_points.available() && I_points.available()){
V_data[n] = V_points.parseFloat(); // Read voltage value from file
I_data[n] = I_points.parseFloat(); // Read current value from file
Serial.print("Voltage= ");
Serial.print(V_data[n]);
Serial.print(", ");
Serial.print("Current= ");
Serial.println(I_data[n]);
pixelV = map(V_data[n], 0, max_V(), 0, 128); // We map tension
pixelI = map(I_data[n], 0, max_I(), 0, 160); // We map current
tft.drawPixel(pixelV, pixelI, Display_Color_Green);
n++;
}
}
-
Check the specific details of your sensors to check that they properly put the MISO pin into high-impedance when SS is pulled high. See my answer to a similar question for a possible workaround.sempaiscuba– sempaiscuba2022年05月10日 20:46:32 +00:00Commented May 10, 2022 at 20:46
-
it could be this problem: arduino.stackexchange.com/questions/36912/…Juraj– Juraj ♦2022年05月11日 09:23:57 +00:00Commented May 11, 2022 at 9:23
-
Thanks, I will take a look at what you have gone through and see if it fixes it.Libegria– Libegria2022年05月11日 18:12:40 +00:00Commented May 11, 2022 at 18:12
-
You appear to be neither opening nor closing the files that you are attempting to write or read. See this for an example of using an SD card: arduino.cc/en/Tutorial/LibraryExamples/ReadWrite6v6gt– 6v6gt2022年05月13日 13:03:02 +00:00Commented May 13, 2022 at 13:03