라즈베리 파이 피코 마이크로 SD 카드

이 가이드에서는 Raspberry Pi Pico와 함께 마이크로 SD 카드를 사용하는 방법을 알아봅니다. 다음과 같은 세부 사항에 대해 논의할 것입니다:

라즈베리 파이 피코 마이크로 sd 카드

준비물

1×라즈베리 파이 피코 W 쿠팡 | 아마존
1×라즈베리 파이 피코 (또는) 쿠팡 | 아마존
1×마이크로 USB 케이블 아마존
1×마이크로 SD 카드 아마존
1×마이크로 SD 카드 모듈 쿠팡 | 아마존
1×점퍼케이블 쿠팡 | 아마존
1×(옵션) MicroSD to SD 메모리 카드 어댑터 아마존
1×(추천) 라즈베리 파이 피코용 스크루 터미널 확장 보드 아마존
공개: 이 포스팅 에 제공된 일부 링크는 아마존 제휴 링크입니다. 이 포스팅은 쿠팡 파트너스 활동의 일환으로, 이에 따른 일정액의 수수료를 제공받습니다.

마이크로 SD 카드 모듈 소개

마이크로 SD 카드 모듈은 Raspberry Pi Pico에 부착되며 마이크로 SD 카드를 내장하고 있습니다. 이 모듈은 Raspberry Pi Pico와 마이크로 SD 카드 사이의 연결 역할을 합니다.

핀아웃

마이크로 sd 카드 모듈 핀아웃

마이크로 SD 카드 모듈은 6개의 핀을 제공합니다.

  • VCC 핀을 Raspberry Pi Pico의 R4로 표시된 5V 핀에 연결합니다.
  • GND 핀을 Raspberry Pi Pico의 GND 핀에 연결합니다.
  • MISO 핀을 Raspberry Pi Pico의 MISO 핀에 연결합니다.
  • MOSI 핀을 Raspberry Pi Pico의 MOSI 핀에 연결합니다.
  • SCK 핀을 Raspberry Pi Pico의 SCK 핀에 연결합니다.
  • SS 핀을 Raspberry Pi Pico 코드에 정의된 SS 핀에 연결합니다.

준비

  • USB 3.0 SD 카드 리더기를 사용하여 컴퓨터에 Micro SD 카드를 삽입하세요.
  • Micro SD 카드가 FAT16 또는 FAT32로 포맷되어 있는지 확인하세요. 포맷 방법에 대한 지침은 온라인에서 확인할 수 있습니다.

선연결

라즈베리 파이 피코 마이크로 sd 카드 모듈 배선도

이 이미지는 Fritzing을 사용하여 만들어졌습니다. 이미지를 확대하려면 클릭하세요.

※ 주의:

만약 Ethernet 쉴드나 Micro SD 카드 슬롯이 있는 다른 쉴드를 가지고 있다면, 별도의 Micro SD 카드 모듈이 필요하지 않습니다. 단순히 Micro SD 카드를 쉴드의 슬롯에 꽂기만 하면 됩니다.

Raspberry Pi Pico - 마이크로 SD 카드에서 파일 열기 및 파일이 없으면 생성하는 방법

Raspberry Pi Pico 코드

자세한 사용 방법

다음의 지시 사항을 단계별로 따르십시오:

  • 컴퓨터에 Thonny IDE가 설치되어 있는지 확인합니다.
  • Raspberry Pi Pico에 MicroPython 펌웨어가 설치되어 있는지 확인합니다.
  • Raspberry Pico를 처음 사용하는 경우, 자세한 지침은 라즈베리 파이 피코 - 시작하기 튜토리얼을 참조하세요.
  • 제공된 다이어그램에 따라 부품들을 연결합니다.
  • USB 케이블을 사용하여 Raspberry Pi Pico를 컴퓨터에 연결합니다.
  • 컴퓨터에서 Thonny IDE를 실행합니다.
  • Thonny IDE에서 Tools Options로 이동하여 MicroPython (Raspberry Pi Pico) Interpreter을 선택합니다.
  • Interpreter 탭에서 드롭다운 메뉴에서 MicroPython (Raspberry Pi Pico)를 선택합니다.
  • 올바른 포트가 선택되어 있는지 확인합니다. Thonny IDE가 포트를 자동으로 감지하지만, 수동으로 선택해야 할 수도 있습니다 (예: Windows의 COM3 또는 Linux의 /dev/ttyACM0).
  • 위 코드를 복사하여 Thonny IDE의 편집기에 붙여넣습니다.
  • 다음과 같이 Raspberry Pi Pico에 스크립트를 저장합니다:
    • 저장 버튼을 클릭하거나 Ctrl+S 키를 사용합니다.
    • 저장 대화 상자에서 'This computer'와 'Raspberry Pi Pico' 두 영역이 보입니다. 'Raspberry Pi Pico'를 선택합니다.
    • 파일 이름을 main.py로 저장합니다.
  • 녹색 Run 버튼을 클릭하거나 F5 키를 눌러 스크립트를 실행합니다. 스크립트가 실행됩니다.
  • Thonny 하단의 Shell에서 메시지를 확인합니다.

스크립트 파일의 이름을 main.py로 지정하고 Raspberry Pi Pico의 루트 디렉터리에 저장하면, Pico의 전원이 켜지거나 리셋될 때마다 자동으로 실행됩니다. 이는 전원이 켜지자마자 바로 실행되어야 하는 독립형 애플리케이션에 유용합니다. 만약 스크립트 파일의 이름을 main.py가 아닌 다른 이름으로 지정하면, Thonnys의 Shell에서 수동으로 실행해야 합니다.

  • Micro SD 카드를 Micro SD 카드 모듈에 넣습니다.
  • 배선도를 사용하여 Micro SD 카드 모듈을 Raspberry Pi Pico에 연결합니다.
  • Thonny IDE에서 Serial Monitor를 엽니다.
  • 주어진 코드를 복사하여 Thonny IDE에 붙여넣습니다.
/* * 이 라즈베리 파이 피코 코드는 newbiely.kr 에서 개발되었습니다 * 이 라즈베리 파이 피코 코드는 어떠한 제한 없이 공개 사용을 위해 제공됩니다. * 상세한 지침 및 연결도에 대해서는 다음을 방문하세요: * https://newbiely.kr/tutorials/raspberry-pico/raspberry-pi-pico-micro-sd-card */ import machine import os import sdcard # Define SPI pins for the Micro SD Card module SPI_SCK_PIN = 2 # The Raspberry Pi Pico pin GP2 connected to SCK SPI_MOSI_PIN = 3 # The Raspberry Pi Pico pin GP3 connected to MOSI SPI_MISO_PIN = 4 # The Raspberry Pi Pico pin GP4 connected to MISO SPI_CS_PIN = 5 # The Raspberry Pi Pico pin GP5 connected to SS (CS) # Initialize SD card using SoftSPI and sdcard driver spi = machine.SoftSPI(baudrate=1000000, polarity=0, phase=0, sck=machine.Pin(SPI_SCK_PIN), mosi=machine.Pin(SPI_MOSI_PIN), miso=machine.Pin(SPI_MISO_PIN)) cs = machine.Pin(SPI_CS_PIN, machine.Pin.OUT) try: sd = sdcard.SDCard(spi, cs) os.mount(sd, "/sd") print("SD Card is ready") except Exception as e: print("SD CARD FAILED, OR NOT PRESENT!", e) raise SystemExit # Check if the file exists file_path = "/sd/pico.txt" if file_path.replace("/sd/", "") not in os.listdir("/sd"): print("pico.txt doesn't exist. Creating pico.txt file...") # Create a new file by opening it and immediately closing it f = open(file_path, "w") f.close() # Recheck if file is created or not if file_path.replace("/sd/", "") in os.listdir("/sd"): print("pico.txt exists on SD Card.") else: print("pico.txt doesn't exist on SD Card.") os.umount("/sd")
  • Thonny IDE에서 Upload 버튼을 클릭하여 코드를 Raspberry Pi Pico로 전송합니다.
  • 첫 업로드 후 Serial Monitor를 확인하여 결과를 확인합니다.
Shell x
>>> %Run -c $EDITOR_CONTENT
MPY: soft reboot SD Card is ready arduino.txt doesn't exist. Creating arduino.txt file... arduino.txt exists on SD Card.
MicroPython (Raspberry Pi Pico) • Board CDC @ COM29 ≡
  • 다음 시도에서 시리얼 모니터에 표시된 결과
Shell x
>>> %Run -c $EDITOR_CONTENT
MPY: soft reboot SD Card is ready arduino.txt exists on SD Card.
MicroPython (Raspberry Pi Pico) • Board CDC @ COM29 ≡

※ 주의:

첫 업로드 후 시리얼 모니터를 열면 출력이 없을 수도 있습니다.

  • 모듈에서 Micro SD 카드를 꺼냅니다.
  • 마이크로 탈착식 디스크 카드를 USB 카드 리더기에 삽입합니다.
  • USB 카드 리더기를 컴퓨터에 연결합니다.
  • 파일이 있는지 확인합니다.

라즈베리 파이 피코 - 마이크로 SD 카드의 파일에 데이터 쓰기/읽기 방법

이 코드는 다음과 같은 작업을 수행합니다:

  • 파일에 데이터를 저장합니다.
  • 파일을 열고 시리얼 모니터에 문자를 한 번에 하나씩 표시합니다.
/* * 이 라즈베리 파이 피코 코드는 newbiely.kr 에서 개발되었습니다 * 이 라즈베리 파이 피코 코드는 어떠한 제한 없이 공개 사용을 위해 제공됩니다. * 상세한 지침 및 연결도에 대해서는 다음을 방문하세요: * https://newbiely.kr/tutorials/raspberry-pico/raspberry-pi-pico-micro-sd-card */ import machine import os import sdcard # Define SPI pins for the Micro SD Card module SPI_SCK_PIN = 2 # The Raspberry Pi Pico pin GP2 connected to SCK SPI_MOSI_PIN = 3 # The Raspberry Pi Pico pin GP3 connected to MOSI SPI_MISO_PIN = 4 # The Raspberry Pi Pico pin GP4 connected to MISO SPI_CS_PIN = 5 # The Raspberry Pi Pico pin GP5 connected to SS (CS) # Initialize SD card using SoftSPI and sdcard driver spi = machine.SoftSPI(baudrate=1000000, polarity=0, phase=0, sck=machine.Pin(SPI_SCK_PIN), mosi=machine.Pin(SPI_MOSI_PIN), miso=machine.Pin(SPI_MISO_PIN)) cs = machine.Pin(SPI_CS_PIN, machine.Pin.OUT) try: sd = sdcard.SDCard(spi, cs) os.mount(sd, "/sd") print("SD CARD INITIALIZED.") except Exception as e: print("SD CARD FAILED, OR NOT PRESENT!", e) raise SystemExit file_path = "/sd/pico.txt" # Open file for writing (append mode) try: f = open(file_path, "a") f.write("Created by newbiely.com\n") # write a line to pico.txt f.write("Learn Raspberry Pi Pico and SD Card\n") # write another line to pico.txt f.close() except Exception as e: print("SD Card: Issue encountered while attempting to open the file pico.txt", e) # Open file for reading try: f = open(file_path, "r") while True: ch = f.read(1) # read characters one by one from Micro SD Card if not ch: break print(ch, end="") # print the character to the Shell f.close() except Exception as e: print("SD Card: Issue encountered while attempting to open the file pico.txt", e) os.umount("/sd")
  • 시리얼 모니터가 파일 안에 있는 내용을 보여주었습니다.
Shell x
>>> %Run -c $EDITOR_CONTENT
MPY: soft reboot Created by newbiely.com Learn Raspberry Pi Pico and SD Card
MicroPython (Raspberry Pi Pico) • Board CDC @ COM29 ≡

※ 주의:

Raspberry Pi Pico에서 코드를 실행하면 파일 끝에 데이터가 추가됩니다. Raspberry Pi Pico를 재시작하면 이 과정이 반복되어 파일에 동일한 텍스트가 반복됩니다. 시리얼 모니터에서 다음과 같은 추가 줄을 확인할 수 있습니다:

Shell x
>>> %Run -c $EDITOR_CONTENT
MPY: soft reboot newbiely.com에서 제작 Raspberry Pi Pico 및 SD 카드 학습 newbiely.com에서 제작 Raspberry Pi Pico 및 SD 카드 학습
MicroPython (Raspberry Pi Pico) • Board CDC @ COM29 ≡

모듈에서 마이크로 SD 카드를 빼내고 USB SD 카드 리더기를 사용하여 컴퓨터에서 파일을 확인할 수 있습니다.

Raspberry Pi Pico - 마이크로 SD 카드의 파일을 한 줄씩 읽는 방법

/* * 이 라즈베리 파이 피코 코드는 newbiely.kr 에서 개발되었습니다 * 이 라즈베리 파이 피코 코드는 어떠한 제한 없이 공개 사용을 위해 제공됩니다. * 상세한 지침 및 연결도에 대해서는 다음을 방문하세요: * https://newbiely.kr/tutorials/raspberry-pico/raspberry-pi-pico-micro-sd-card */ import machine import os import sdcard # Define SPI pins for the Micro SD Card module SPI_SCK_PIN = 2 # The Raspberry Pi Pico pin GP2 connected to SCK SPI_MOSI_PIN = 3 # The Raspberry Pi Pico pin GP3 connected to MOSI SPI_MISO_PIN = 4 # The Raspberry Pi Pico pin GP4 connected to MISO SPI_CS_PIN = 5 # The Raspberry Pi Pico pin GP5 connected to SS (CS) # Initialize SD card using SoftSPI and sdcard driver spi = machine.SoftSPI(baudrate=1000000, polarity=0, phase=0, sck=machine.Pin(SPI_SCK_PIN), mosi=machine.Pin(SPI_MOSI_PIN), miso=machine.Pin(SPI_MISO_PIN)) cs = machine.Pin(SPI_CS_PIN, machine.Pin.OUT) try: sd = sdcard.SDCard(spi, cs) os.mount(sd, "/sd") print("SD Card is ready") except Exception as e: print("SD CARD FAILED, OR NOT PRESENT!", e) raise SystemExit file_path = "/sd/pico.txt" # Open file for writing (append mode) try: f = open(file_path, "a") f.write("Created by newbiely.com\n") # write a line to pico.txt f.write("Learn Raspberry Pi Pico and SD Card\n") # write another line to pico.txt f.close() except Exception as e: print("SD Card: Issue encountered while attempting to open the file pico.txt", e) # Open file for reading line-by-line try: f = open(file_path, "r") line_count = 0 while True: line = f.readline() # read line-by-line from Micro SD Card if not line: break line_count += 1 print("Line {}: {}".format(line_count, line.strip())) # print the line to the Shell f.close() except Exception as e: print("SD Card: Issue encountered while attempting to open the file pico.txt", e)
  • 직렬 모니터에 표시된 결과.
Shell x
>>> %Run -c $EDITOR_CONTENT
MPY: soft reboot SD Card is ready Line 1: Created by newbiely.com Line 2: Learn Raspberry Pi Pico and SD Card
MicroPython (Raspberry Pi Pico) • Board CDC @ COM29 ≡

※ 주의:

파일에서 이전 내용을 제거하지 않으면, 시리얼 모니터에서 추가 줄이 표시될 수 있습니다.

Raspberry Pi Pico - 마이크로 SD 카드에서 파일 덮어쓰기 방법

일반적으로 정보는 파일 끝에 추가됩니다. 파일을 변경하려면 먼저 기존 파일을 제거한 다음 동일한 이름으로 새 파일을 만듭니다.

/* * 이 라즈베리 파이 피코 코드는 newbiely.kr 에서 개발되었습니다 * 이 라즈베리 파이 피코 코드는 어떠한 제한 없이 공개 사용을 위해 제공됩니다. * 상세한 지침 및 연결도에 대해서는 다음을 방문하세요: * https://newbiely.kr/tutorials/raspberry-pico/raspberry-pi-pico-micro-sd-card */ import machine import os import sdcard # Define SPI pins for the Micro SD Card module SPI_SCK_PIN = 2 # The Raspberry Pi Pico pin GP2 connected to SCK SPI_MOSI_PIN = 3 # The Raspberry Pi Pico pin GP3 connected to MOSI SPI_MISO_PIN = 4 # The Raspberry Pi Pico pin GP4 connected to MISO SPI_CS_PIN = 5 # The Raspberry Pi Pico pin GP5 connected to SS (CS) # Initialize SD card using SoftSPI and sdcard driver spi = machine.SoftSPI(baudrate=1000000, polarity=0, phase=0, sck=machine.Pin(SPI_SCK_PIN), mosi=machine.Pin(SPI_MOSI_PIN), miso=machine.Pin(SPI_MISO_PIN)) cs = machine.Pin(SPI_CS_PIN, machine.Pin.OUT) try: sd = sdcard.SDCard(spi, cs) os.mount(sd, "/sd") print("SD Card is ready") except Exception as e: print("SD CARD FAILED, OR NOT PRESENT!", e) raise SystemExit file_path = "/sd/pico.txt" # Delete the file if it exists (to overwrite) try: os.remove(file_path) except OSError: pass # Create new file by opening file for writing try: f = open(file_path, "w") f.write("Created by newbiely.com\n") # write a line to pico.txt f.write("Learn Raspberry Pi Pico and SD Card\n") # write another line to pico.txt f.close() except Exception as e: print("SD Card: Issue encountered while attempting to open the file pico.txt", e) # Open file for reading try: f = open(file_path, "r") while True: ch = f.read(1) # read characters one by one from Micro SD Card if not ch: break print(ch, end="") # print the character to the Shell f.close() except Exception as e: print("SD Card: Issue encountered while attempting to open the file pico.txt", e) os.umount("/sd")
  • 직렬 모니터의 결과
Shell x
>>> %Run -c $EDITOR_CONTENT
MPY: soft reboot SD Card is ready Created by newbiely.com Learn Raspberry Pi Pico and SD Card
MicroPython (Raspberry Pi Pico) • Board CDC @ COM29 ≡
  • Raspberry Pi Pico를 재시작합니다.
  • 시리얼 모니터에서 파일에 새로운 내용이 추가되었는지 확인합니다.

기기에서 마이크로 SD 카드를 꺼내 USB SD 카드 리더기를 사용하여 컴퓨터에서 그 안의 내용을 확인할 수도 있습니다.

동영상

비디오 제작은 시간이 많이 걸리는 작업입니다. 비디오 튜토리얼이 학습에 도움이 되었다면, YouTube 채널 을 구독하여 알려 주시기 바랍니다. 비디오에 대한 높은 수요가 있다면, 비디오를 만들기 위해 노력하겠습니다.