[Diao Ye learns programming] Arduino hands-on (52)---MicroSD card reading and writing module 5

The reference to 37 sensors and modules has been widely circulated on the Internet. In fact, there must be more than 37 sensor modules compatible with Arduino. In view of the fact that I have accumulated some sensor and actuator modules on hand, according to the concept of true knowledge (must be hands-on), for the purpose of learning and communication, I am going to try and do more experiments one by one here, and I will record them whether I succeed or not.

[Arduino] 168 kinds of sensor module series experiments (data code + simulation programming + graphics programming)
Experiment 52: SPI interface Micro SD card module TF card reader (with level conversion chip)

insert image description here
Hardware list required for Micro SD card reading and writing module experiment
1G and 4GTF card X2
8Ω 0.5W small speaker X1
Micro SD card reader X2
Arduino Uno development board X1
Micro SD card reading and writing module X2
DHT11 temperature and humidity module X1
DuPont lines (9 pieces are prepared)
LED light-emitting diode (blue) X1
Proto Shield prototype expansion board (with mini breadboard) X1

insert image description here

Software platform required for sensor module experiments
Code programming Arduino IDE (version 1.8.19)
simulation programming Linkboy (version V4.6.3)
graphics programming Mind+ (version V1.7.0 RC2.0)
and learning while editing (online platform https://ide.codepku.com/?type=Arduino)

Arduino – SD library
The built-in SD library enables Arduino to read and write SD cards. It is developed based on William Greiman's SdFat library. The SD library supports FAT16 and FAT32 file system modes on standard SD cards and SDHC cards.

The parameters in the functions in the SD library can contain paths separated by forward slashes /, such as "directory / filename.txt". Since the working directory is always the root of the SD card, its name points to the same file whether or not it contains a slash (for example, "/file.txt" is equivalent to "file.txt"). Starting from version 1.0, SD library supports opening multiple files.

The communication between the development board and the SD card uses SPI, note:
the SS pin must be connected to select the SD card. On UNO, SS is pin D10 by default, and on MEGA2560, SS is pin 53 by default, or you can also specify an SS pin in the SD.begin() function and connect to it. Please note that after you customize the SS pin, please set the pin to output mode, otherwise the SD library will not work properly.

Experimental Wiring Diagram
Pin VCC ===> 5V Arduino
Pin GND ===> GND Arduino
Pin MOSI ===> Pin 11 Arduino
Pin MISO ===> Pin 12 Arduino
Pin SCK ===> Pin 13 Arduino
Pin CS ===> Pin 10 Arduino

insert image description here

Arduino creates and deletes files on SD card
(1) Arduino reference open source code

/*
  【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
  程序六:Arduino在SD卡上创建和删除文件
  实验接线:
  Micro SD      UNO
  CS-------------10
  MOSI(DI)------11
  MISO(DO)------12
  CLK------------13
*/

#include <SPI.h>
#include <SD.h>
 
// 创建File实例化对象
File myFile;
 
void setup(){
    
    
    // 初始化硬件串口并设置波特率为9600
    Serial.begin(9600);
    while (!Serial)
    {
    
    
        ; //等待串口连接,仅使用本地 USB 端口
    }
 
    Serial.print("正在初始化 SD 卡...");
 
 
    // 检测是否初始化完成
    if (!SD.begin())
    {
    
    
        Serial.println("初始化失败!");
        return;
    }
    Serial.println("初始化完成。");
 
    // 查看是否存在"example.txt"文件
    if (SD.exists("example.txt"))
    {
    
    
        Serial.println("example.txt exists.");
    }
    else
    {
    
    
        Serial.println("example.txt 不存在");
    }
 
    // 打开一个新文件,然后立即将其关闭(创建文件)
    Serial.println("创建 example.txt...");
    myFile = SD.open("example.txt", FILE_WRITE);
    myFile.close();
 
    // 检查该文件是否新建成功
    if (SD.exists("example.txt"))
    {
    
    
        Serial.println("有example.txt文件了");
    }
    else
    {
    
    
        Serial.println("example.txt 不存在");
    }
 
    //删除该文件
    Serial.println("移除 example.txt...");
    SD.remove("example.txt");
 
    if (SD.exists("example.txt"))
    {
    
    
        Serial.println("example.txt exists.");
    }
    else
    {
    
    
        Serial.println("example.txt 不存在。");
    }
}
 
void loop(){
    
    
}

(2) The return status of the experimental serial port

insert image description here

Arduino data logger with SD card and DHT11 humidity and temperature sensor
Experimental wiring diagram

insert image description here
Arduino experiment open source code

/*
  【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
  程序七:带有SD卡和DHT11湿度和温度传感器的Arduino数据记录器
  实验接线:DHT11接D4
  Micro SD      UNO
  CS-------------10
  MOSI(DI)------11
  MISO(DO)------12
  CLK------------13
*/

#include <SPI.h>    // 包含 SPI 库(SD 卡需要)
#include <SD.h>     // 包含 SD 库
#include <DHT.h>    // 包含 DHT 传感器库

File dataFile;

#define DHTPIN 4         // DHT11 数据引脚连接到 Arduino 引脚 4
#define DHTTYPE DHT11    // 使用 DHT11 传感器
DHT dht(DHTPIN, DHTTYPE); // 初始化 DHT 库

void setup() {
    
    
  // 打开串行通信并等待端口打开
  Serial.begin(9600);
  while (!Serial)
    ; // 等待串口连接。仅本机 USB 端口需要
  Serial.print("正在初始化 SD 卡...");
  if (!SD.begin())
  {
    
    
    Serial.println("初始化失败!");
    while (1);
  }
  Serial.println("初始化完成。");
  delay(2000);
  dht.begin();
}

uint16_t line = 1;

void loop() {
    
    
  delay(1000);
  // 读取湿度
  byte RH = dht.readHumidity();
  //以摄氏度读取温度
  byte Temp = dht.readTemperature();
  //打开文件DHT11Log.txt
  dataFile = SD.open("DHT11Log.txt", FILE_WRITE);

  // 如果文件打开正常,写入它:
  if (dataFile)
  {
    
    
    Serial.print(line);
    Serial.print(": 温度 = ");
    Serial.print(Temp);
    Serial.print("°C, 湿度 = ");
    Serial.print(RH);
    Serial.println("%");
    // 将数据写入 SD 卡文件 (DHT11Log.txt)
    dataFile.print(line++);
    dataFile.print(": 温度 = ");
    dataFile.print(Temp);
    dataFile.print("°C, 湿度 = ");
    dataFile.print(RH);
    dataFile.println("%");
    dataFile.close();

  }
  // 如果文件没有打开,打印错误
  else
    Serial.println("打开 DHT11Log.txt 出错");
}

Experimental serial port return status
insert image description here
Arduino experimental scene diagram

insert image description here

Wiring diagram for playing audio files 2.wav and 3.wav
experiments (here CS is connected to D10)

insert image description here
Arduino Reference Open Source Code

/*
  【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
  程序八:播放音频文件2.wav和3.wav
  实验接线:喇叭接D9
  Micro SD      UNO
  CS-------------10
  MOSI(DI)------11
  MISO(DO)------12
  CLK------------13
*/

#include <SD.h>        //包含SD模块库
#include <TMRpcm.h>    //包含扬声器控制库

#define SD_ChipSelectPin 10   //将pin10定义为CS pin

TMRpcm tmrpcm;                //为扬声器库创建一个对象

void setup() {
    
    
  //定义扬声器引脚。扬声器库正在使用 pin9。
  tmrpcm.speakerPin = 9;

  // 设置串口
  Serial.begin(9600);
  Serial.println("准备就绪");
  

  //查看卡是否存在并且可以初始化。
  if (!SD.begin(SD_ChipSelectPin)) {
    
    
    Serial.println("SD 失败");
    return;               //如果没有就不要再做
  }

  //每次arduino上电或重置时都会播放声音文件“2”
  Serial.println("播放声音文件2.wav");
  Serial.println(" ");
  tmrpcm.play("2.wav");
}

void loop() {
    
    
  if (Serial.available()) {
    
    
    //通过串口监视器发送字母a开始播放
    if (Serial.read() == 'a') {
    
    
      // 设置音量。数字的范围可以是 0-7。
      // 此时设置音量为5。
      tmrpcm.setVolume(5);    // 将音量设置为 5。

      // 播放文件 3。文件 3 是 Red Light 的音频。
      Serial.println("字母a");
      Serial.println("播放声音文件3.wav");
      tmrpcm.play("3.wav");
    }
  }
}

Experimental serial port return

insert image description here
Arduino experiment scene diagram

insert image description here

Guess you like

Origin blog.csdn.net/weixin_41659040/article/details/131875725