Last active
April 13, 2024 23:18
-
-
Save nv1t/29949d5372e4baf2340c5a96d58641f7 to your computer and use it in GitHub Desktop.
read SD Card over SPI with Micropython. i run it with adafruit-ampy. maybe you need to place the sdcard module on the pico.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import machine | |
import os | |
import sdcard | |
import uos | |
import sys | |
import ubinascii | |
# Setup SPI bus | |
spi = machine.SPI(0, sck=machine.Pin(2), mosi=machine.Pin(3), miso=machine.Pin(4)) | |
# Setup SD card | |
sd = sdcard.SDCard(spi, machine.Pin(5)) # CS pin is GPIO 5 | |
# Function to read a block | |
def read_block(block_num, block_size=512): | |
block = bytearray(block_size) | |
sd.readblocks(block_num, block) | |
return block | |
# Main function to read and send data | |
def send_data_over_usb(): | |
block_count = sd.ioctl(4, 0) # Get the total number of blocks | |
for i in range(block_count): | |
data = read_block(i) | |
hex_data = ubinascii.hexlify(data).decode('utf-8') | |
print(hex_data) | |
# Main function that runs on startup | |
def main(): | |
# Place your main code logic here | |
# For example: | |
send_data_over_usb() | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment