I'm a beginner and I'm working on a project at university. I needed an ultrasonic sensor to read data (distance) and add it to the SD card. My hardware is all fine but for some reason my code doesn't work. At first the SD card worked fine but the distance that was read was 0. Then i switched the code around and now the ultrasonic reads the distance but it doesn't open a file on the sd card. I don't see the error. Can someone help? is there some kind of logic error?
#include <SD.h>
const int trigPin = 9; // Set your trigPin number
const int echoPin = 10; // Set your echoPin number
File dataFile; // File object to write data to microSD card
long duration;
int distance;
void setup() {
// Initialize the microSD card
if (SD.begin(4)) { // Set your microSD card's CS pin number
Serial.println("SD card is ready.");
} else {
Serial.println("SD card initialization failed.");
return;
}
// Create a new file on the microSD card
dataFile = SD.open("data.txt", FILE_WRITE);
// Check if the file opened successfully
if (dataFile) {
dataFile.println("Distance Data:");
dataFile.close();
Serial.println("Data file created.");
} else {
Serial.println("Error opening data file.");
}
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
Serial.begin(9600);
}
void loop() {
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
noInterrupts();
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
interrupts();
distance = duration * 0.034 / 2;
Serial.print("Distance: ");
Serial.println(distance);
// Append the distance data to the microSD card
dataFile = SD.open("data.txt", FILE_WRITE);
if (dataFile) {
dataFile.print("Distance: ");
dataFile.println(distance);
dataFile.close();
} else {
Serial.println("Error writing to data file.");
}
delay(500); // Adjust the delay based on your application requirements
}
I moved your topic to an appropriate forum category @sandysbob.
In the future, please take some time to pick the forum category that best suits the subject of your topic. There is an "About the _____ category" topic at the top of each category that explains its purpose.
Try another pin for the echoPin.
Is it on a Arduino Uno or Nano ?
The SPI bus uses pins 11,12,13 and pin 10 is default the CS signal. If you use another pin with SD.begin(4) then that uses pin 4.
However, pin 10 must still be an OUTPUT pin, or else the SPI bus is not working.
Maybe you still have to use pinMode to set pin 10 as OUTPUT.
It is just a weird thing of the ATmega328P microcontroller that is used on the Uno and Nano. But this is the weirdest one, there are not many other weird things.
code for the ultrasonic works seprately. With all the same wires too. When I add the SD card code it stops working. The SD card writes the information printed so there probably isn't an issue with it.
I'm using an arduino uno. I'll try setting pin 10 to output but i'm not sure that's the problem, because the code for the ultrasonic works separately. The SD card code (the last bit that writes on the sad card) is where I think the main issue is because when I write a serial.print (opened) after that line it never prints anything. I think that last loop is the problem. I’ve tried just keeping the loop open after set up but it doesn’t work.
When You add the SD card, the ultrasonic stops working. Have You verified that the SD writings are actually there?
I've heard aboat collisions between libraries and system resorces, like the timer for pulseln, but I can't tell for sure that this is the reason.
So the SD card only writes when the distance being printed is 0. When I changed the code around, it would print out actual distances but it would stop writing to the SD care. Not sure why though. The last part of the code I had been referring to is this
// Append the distance data to the microSD card
dataFile = SD.open("data.txt", FILE_WRITE);
if (dataFile) {
dataFile.print("Distance: ");
dataFile.println(distance);
dataFile.close();
} else {
Serial.println("Error writing to data file.");
}
delay(500); // Adjust the delay based on your application requirements
}