1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Raspberry Pi Pico で MicroPython を使い SDカードをアクセスする

1
Last updated at Posted at 2026-03-20

sdcard-test.png

自作ボードのSDカードスロットの動作確認の備忘録

  • 使用した MP Firmware
    MicroPython v1.25.0 on 2025-04-15; Raspberry Pi Pico with RP2040
     

  • 使用したSDカード

     

  • SD-Cardフォーマット

    FAT32 format
    # /dev/diskXXを確認 
    diskutil list
    diskutil eraseDisk FAT32 SDCARD MBRFormat /dev/disk4
    diskutil eject /dev/disk4
    

  • Pinout
    すべての信号線に 66Ω抵抗をはさんでいます。

    SD Card Slot Pico GPIO
    CLK GP2
    CMD GP3
    DAT0 GP4
    CD/DAT3 GP5
    VDD 3V3(OUT)
    VSS GND

  • 動作確認コード

    from machine import Pin, SPI
    import os, sdcard
    
    # SDCARD Slot
    sck_pin, mosi_pin, miso_pin, cs_pin = Pin(2), Pin(3), Pin(4), Pin(5)
    
    spi = SPI(0, baudrate=1000000, sck=sck_pin, mosi=mosi_pin, miso=miso_pin)
    sd = sdcard.SDCard(spi, cs_pin)
    vfs = os.VfsFat(sd)
    os.mount(vfs, '/sd')
    
    fp = open('/sd/test.txt', 'w')
    fp.write('test-file')
    fp.close()
    
    print(os.listdir('/sd'))
    
    f = open('/sd/test.txt', 'r')
    print(f.read())
    f.close()
    

    問題なくアクセスできました。

     

  • 性能検証
    次に、どこまでクロックを上げられるのか、検証します。

    検証コード
    from machine import Pin, SPI
    import os, time, sdcard
    
    # SDCARD Slot
    sck_pin, mosi_pin, miso_pin, cs_pin = Pin(2), Pin(3), Pin(4), Pin(5)
    
    spi = SPI(0, sck=sck_pin, mosi=mosi_pin, miso=miso_pin)
    sd = sdcard.SDCard(spi, cs_pin)
    spi.init(baudrate=80000000) 
    vfs = os.VfsFat(sd)
    os.mount(vfs, '/sd')
    #os.remove('/sd/test.bin')
    
    # 設定
    chunk_size = 32 * 1024  # 32KB (ブロックサイズ)
    total_mb = 10           # 10MB分
    test_file = '/sd/test.bin'
    
    # 0x00-0xFF の繰り返しデータを作成 (書き込み用)
    cyclic_pattern = bytearray(chunk_size)
    for i in range(chunk_size):
        cyclic_pattern[i] = i % 256
    
    # 読み込み用の固定バッファを事前に確保
    read_buffer = bytearray(chunk_size)
    num_writes = (total_mb * 1024 * 1024) // chunk_size
    
    def test_sd_speed():
        print(f"Target: {total_mb} MB ({num_writes} blocks of {chunk_size//1024} KB)")
    
        # --- 書き込み ---
        print("\n--- Write Test ---")
        start_ms = time.ticks_ms()
        with open(test_file, 'wb') as f:
            for i in range(num_writes):
                f.write(cyclic_pattern)
                if i % 20 == 0: print(".", end="")
                
        write_sec = time.ticks_diff(time.ticks_ms(), start_ms) / 1000
        print(f"\nWrite: {write_sec:.2f}s ({total_mb/write_sec:.2f} MB/s)")
    
        # --- 読み出し ---
        print(f"\n--- Read Test ---")
        start_ms = time.ticks_ms()
        
        with open(test_file, 'rb') as f:
            for i in range(num_writes):
                f.readinto(read_buffer)
                if i % 20 == 0: print(".", end="")
                    
        read_sec = time.ticks_diff(time.ticks_ms(), start_ms) / 1000
        print(f"\nRead: {read_sec:.2f}s ({total_mb/read_sec:.2f} MB/s)")
    
    try:
        test_sd_speed()
    except Exception as e:
        print("\nError:", e)
    

  • 結果

    baudrate=10000000
    Target: 10 MB (320 blocks of 32 KB)
    
    --- Write Test ---
    ................
    Write: 18.76s (0.53 MB/s)
    
    --- Read Test ---
    ................
    Read: 40.84s (0.24 MB/s)
    
    baudrate=20000000
    Target: 10 MB (320 blocks of 32 KB)
    
    --- Write Test ---
    ................
    Write: 14.43s (0.69 MB/s)
    
    --- Read Test ---
    ................
    Read: 36.35s (0.28 MB/s)
    
    baudrate=40000000
    Target: 10 MB (320 blocks of 32 KB)
    
    --- Write Test ---
    ................
    Write: 10.18s (0.98 MB/s)
    
    --- Read Test ---
    ................
    Read: 30.64s (0.33 MB/s)
    
    baudrate=80000000
    Target: 10 MB (320 blocks of 32 KB)
    
    --- Write Test ---
    ................
    Write: 10.17s (0.98 MB/s)
    
    --- Read Test ---
    ................
    Read: 30.40s (0.33 MB/s)
    
    • 書き込みより、読み込みの方が遅いとは 驚きです。 sdcard.pyの読み込みロジックの問題でしょう。
    • 40Mbpsと80Mbpsに差がないのは、Picoの限界でしょう。


       
  • Pico Pinout
    pico pinout
     

  • 参考にした記事

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?