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

insert image description here

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, in accordance with the concept of true knowledge (must be hands-on), for the purpose of learning and communication, here I am going to try and do more experiments one by one. Whether it is successful or not, it will be recorded ——Small progress or unsolvable problems, I hope to be able to throw bricks and spark jade.

[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

Experimental environment of the Micro SD card reading and writing module
1. Hardware list required for the 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
Several 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

Program 7: Arduino data logger with SD card and DHT11 humidity and temperature sensor
(1) Experimental wiring diagram

insert image description here
(2) Arduino reference 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 出错");
}

(3) The return status of the experimental serial port

insert image description here
(3) Experimental scene diagram

insert image description here
Program 8: Play audio files 2.wav and 3.wav
(1) Experimental wiring diagram (here CS is connected to D10)

insert image description here

(2) 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");
    }
  }
}

(3) The return status of the experimental serial port

insert image description here
(4) Experimental scene diagram

insert image description here

insert image description here

Guess you like

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