极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 29810|回复: 10

廉价SD卡模块Arduino无法初始化解决办法

[复制链接]
发表于 2015-2-27 22:33:09 | 显示全部楼层 |阅读模式
试了很多次,无法初始化SD卡,开始以为电源不够,也采用单独供电试过。

后来翻看资料增加电平转换OK。



上图中使用SD和Arduino连接部分即可








[pre lang="arduino" line="1" file="seekyong"]/*
SD card datalogger
This example shows how to log data from three analog sensors
to an SD card using the SD library.
       
The circuit:
* SD card attached to SPI bus as follows:
** UNO:  MOSI - pin 11, MISO - pin 12, CLK - pin 13, CS - pin 4 (CS pin can be changed)
and pin #10 (SS) must be an output
** Mega:  MOSI - pin 51, MISO - pin 50, CLK - pin 52, CS - pin 4 (CS pin can be changed)
and pin #52 (SS) must be an output
** Leonardo: Connect to hardware SPI via the ICSP header
Pin 4 used here for consistency with other Arduino examples

created  24 Nov 2010
modified 9 Apr 2012 by Tom Igoe

This example code is in the public domain.
*/
#include <SPI.h>
#include <SD.h>

// On the Ethernet Shield, CS is pin 4. Note that even if it's not
// used as the CS pin, the hardware CS pin (10 on most Arduino boards,
// 53 on the Mega) must be left as an output or the SD library
// functions will not work.
const int chipSelect = 53;

File dataFile;
void setup()
{
// Open serial communications and wait for port to open:
Serial.begin(9600);
  while (!Serial) {
   ; // wait for serial port to connect. Needed for Leonardo only
}

Serial.print("Initializing SD card...");
// make sure that the default chip select pin is set to
// output, even if you don't use it:
pinMode(SS, 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:
   while (1) ;
}
Serial.println("card initialized.");

// Open up the file we're going to log to!
dataFile = SD.open("datalog.txt", FILE_WRITE);
if (! dataFile) {
   Serial.println("error opening datalog.txt");
   // Wait forever since we cant write data
   while (1) ;
}
}

void loop()
{
// make a string for assembling the data to log:
String dataString = "";

// read three sensors and append to the string:
for (int analogPin = 0; analogPin < 3; analogPin++) {
   int sensor = analogRead(analogPin);
   dataString += String(sensor);
   if (analogPin < 2) {
     dataString += ",";
   }
}

dataFile.println(dataString);

// print to the serial port too:
Serial.println(dataString);

// The following line will 'save' the file to the SD card after every
// line of data - this will use more power and slow down how much data
// you can read but it's safer!
// If you want to speed up the system, remove the call to flush() and it
// will save the file only every 512 bytes - every time a sector on the
// SD card is filled with data.
dataFile.flush();

// Take 1 measurement every 500 milliseconds
delay(500);
}[/code]

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x
回复

使用道具 举报

 楼主| 发表于 2015-2-27 22:40:41 | 显示全部楼层


上面这个模块在国外很出名啊,老外觉得灰常便宜还从香港包邮

稳压管采用3.3V的

去掉电容测试,也能初始化

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x
回复 支持 反对

使用道具 举报

 楼主| 发表于 2015-2-27 22:41:22 | 显示全部楼层
电容、电阻去掉后SD卡还是能初始化
回复 支持 反对

使用道具 举报

 楼主| 发表于 2015-2-27 22:49:10 | 显示全部楼层
MD毛线,去掉电容、电阻,稳压管直接连接,现在居然也行了。。。

接稳压管之前用IDE自带例子,不是发的这个,难道程序问题?!
回复 支持 反对

使用道具 举报

发表于 2015-2-28 06:31:01 | 显示全部楼层
面包板有时插得不好,接触不好或电阻大,就有问题。原来我调IIC时也时,明明电路对,就是不读,后来全重插一遍,就又好了。
回复 支持 反对

使用道具 举报

发表于 2015-2-28 09:32:27 | 显示全部楼层
增加电平转换是什么意思?

前几天我也在搞这个,一个SD卡死活不认,另外一个没问题

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x
回复 支持 反对

使用道具 举报

发表于 2015-2-28 10:42:20 | 显示全部楼层
本帖最后由 wwwusr 于 2015-2-28 10:43 编辑

http://hackerspace-ffm.de/wiki/index.php?title=SimpleSDAudio
http://www.geek-workshop.com/thread-2611-1-1.html
这个例子里有SD卡的连接方法,应该是SD卡的电压是3.3V,如果你的arduino是5V版本的,那么,与SD卡之间的电压 通信,都要有电平转换。
不然就会通信不上,卡烫手,最后卡挂掉。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x
回复 支持 反对

使用道具 举报

发表于 2015-2-28 12:35:20 | 显示全部楼层
直接3.3v供电,稳定着呢
回复 支持 反对

使用道具 举报

发表于 2015-2-28 16:51:04 | 显示全部楼层
5v系统电平转换,是必须的。
回复 支持 反对

使用道具 举报

发表于 2016-8-4 14:34:05 | 显示全部楼层
zoologist 发表于 2015-2-28 09:32
增加电平转换是什么意思?

前几天我也在搞这个,一个SD卡死活不认,另外一个没问题

针对这个模块,如果是51单片机,需将5Vcmos点平转成3.3V TTL电平
回复 支持 反对

使用道具 举报

发表于 2016-8-4 14:46:22 | 显示全部楼层
dataFile.flush();
没在SD library看到这个函数,是什么意思?
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|联系我们|极客工坊

GMT+8, 2026-6-14 16:32 , Processed in 0.036581 second(s), 24 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表