Skip to content

Instantly share code, notes, and snippets.

@nv1t
Last active April 13, 2024 23:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nv1t/29949d5372e4baf2340c5a96d58641f7 to your computer and use it in GitHub Desktop.
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.
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