OLED(I2C)和Micro SD卡模块无法在Arduino中一起使用 [英] OLED (I2C) and Micro SD card module not working together in Arduino

查看:360
本文介绍了OLED(I2C)和Micro SD卡模块无法在Arduino中一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将一些数据写入SD卡,然后将其读回串行监视器,并显示给OLED.

I am trying to write some data to SD card and read it back to serial monitor as well as display it to the OLED.

SD卡和OLED可以单独工作,但是当组合使用时,它们似乎相互干扰.我已经使用了Arduino SD和Adafruit OLED库.

Both the SD card and OLED work separately but they seem to be interfering with each other when combined. I have used Arduino SD and Adafruit OLED libraries.

从Arduino Uno到Micro SD卡模块的连接:

Connections from Arduino Uno to Micro SD card module:

5V to SD VCC
GND TO SD GND
PIN 10 TO SD Chip Select
PIN 11 TO SD MOSI
PIN 12 TO SD MISO
PIN 13 TO SD SCK

与OLED的连接:

3.3V to OLED VCC
GND TO OLED GND
A4 TO OLED SDA
A5 TO OLED SCK

这是代码:

#include <SPI.h>
#include <SD.h>

File myFile;

#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels

// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
#define OLED_RESET     -1 
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  while(!Serial) {
    ;
  }

  if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { 
    Serial.println(F("SSD1306 allocation failed"));
    for(;;); // CODE GETS STUCK HERE. DISPLAY NEVER INITIALISES
  }     
  display.clearDisplay();           
  display.setTextSize(1);             
  display.setTextColor(WHITE);       
  display.setCursor(29,29);
  display.print("INITIALISING");
  display.display();
  delay(5000);

  if (!SD.begin(10)) {
    Serial.println("initialization failed!");
    while (1);
  }
  Serial.println("initialization done.");

  myFile = SD.open("test.txt", FILE_WRITE);

  if (myFile) {
    Serial.print("Writing to test.txt...");
    myFile.println("testing 1, 2, 3.");
    // close the file:
    myFile.close();
    Serial.println("done.");
  } else {
    // if the file didn't open, print an error:
    Serial.println("error opening test.txt");
  }

  display.clearDisplay();

  myFile = SD.open("test.txt");
  if (myFile) {
    Serial.println("test.txt:");
    while (myFile.available()) {
      Serial.write(myFile.read());
      display.setCursor(0,0);
      display.print(myFile.read());
      display.display();
      delay(5000);
    }
    // close the file:
    myFile.close();
  } else {
    // if the file didn't open, print an error:
    Serial.println("error opening test.txt");
  }
}

void loop() {
  // put your main code here, to run repeatedly:

}

如上所述,代码卡在了OLED初始化上.如果我替换这些行:

Code gets stuck at OLED initialization as mentiontioned above. If I replace these lines:

if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { 
    Serial.println(F("SSD1306 allocation failed"));
    for(;;); // CODE GETS STUCK HERE. DISPLAY NEVER INITIALISES
    } 

对此:

  display.begin(SSD1306_SWITCHCAPVCC, 0x3C); 

我已经在OLED上运行了I2C扫描器代码,因此地址"0x3C"被设置为是正确的.

I have run I2C scanner code on OLED so the address "0x3C" is correct.

OLED仍然无法正常工作,并且SD卡已初始化,但是Arduino将错误的数据写入SD卡上的TXT文件,如下所示:

The OLED still doesn't work and SD card initialises but Arduino is writing wrong data to TXT file on SD card like this:

teóting 1,à2, ó® 

代替:

testing 1, 2, 3.

我还尝试过将U8G2库的草图与SD卡一起使用,以防Arduino内存不足,但仍然无法正常工作.我也将SD芯片选择更改为Arduino数字引脚4,但结果仍然相同.

I have also tried using U8G2 library's sketches with SD card in case Arduino was running out of RAM but it still doesn't work. I have also changed SD chip select to Arduino digital pin 4 but still same results.

在更多的浏览和实验中,我发现SD的MISO或MOSI PIN可能会干扰OLED的SDA/SCL引脚.也许接线需要改变.

On browsing and experimenting more,I found MISO OR MOSI PIN of SD maybe interfering with SDA/SCL pins of OLED. Maybe wiring needs to change.

有任何建议吗?

推荐答案

(我遇到了同样的问题)只是将评论的建议作为答案发布,全部归功于 @datafiddler 来发现这一点:

(I had the same issue) Just posting the suggestions of the comments as an answer, all credit to @gre_gor and @datafiddler for discovering this:

根据我的测试,如果先在setup()中初始化SD库,则SD库可能会继续工作.解决方案是使用 U8G2 OLED驱动程序,这样可以节省更多的内存.该驱动程序也在官方的Arduino库指令中,因此您可以直接从IDE安装和使用它.

From my testing, the SD library might continue to work if it is initialized first in setup(). The solution is to use the U8G2 OLED driver, which is much more memory economic. The driver is also in the official Arduino library directive, so you can install and use it directly from the IDE.

这篇关于OLED(I2C)和Micro SD卡模块无法在Arduino中一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆