Raspberry Pi Pico File System & SD Card Reader |
Written by Mike James & Harry Fairhead | |||||||
Monday, 29 May 2023 | |||||||
Page 3 of 3
The SD Card driver isn't currently a default module in MicroPython. The simplest thing to do is to go to: and copy and paste the Python code into a file called sdcard.py. Upload this to the Pico and you should be able to include it in your program. To make use of it you first have to define the SPI interface: spi = machine.SPI(1, and a GPIO line to use as CS: cs = machine.Pin(9, machine.Pin.OUT) You then use these to create an instance of sdCard: sd = sdcard.SDCard(spi, cs) The instance is a block device to which you can install a file system and then mount. For example, assuming that the SD card isn't formatted, you can format it to a FAT file system using: os.VfsFat.mkfs(sd) Notice that this will delete any data on the SD card and it takes few minutes to complete. Once you have a formatted card you can mount it: os.mount(sd,"/sd") The folder used as the mount point will be created if it doesn't exist. Now that the SD card is mounted we can read and write it using the standard file operations. A complete test program that erases the SD card, writes some data and reads it back is: import machine f=open("/sd/Hello.txt", "w") f=open("/sd/Hello.txt", "r") If you don't want to format the card first comment out the line os.VfsFat.mkfs(sd). If you find you have an SD card that doesn't work with this program try a lower clock speed. You can try increasing the clock speed to see if the SD card reader will cope. Some SD cards will fail to work at any speed as they don't implement the SPI bus in the correct way. As the SD card is FAT formatted it can be read in any machine that has an SD card slot. Programming the Raspberry Pi Pico/W In MicroPython Second EditionBy Harry Fairhead & Mike JamesBuy from Amazon. Contents
Also of interest: <ASIN:B0BR8LWYMZ> <ASIN:B0BL1HS3QD> To be informed about new articles on I Programmer, sign up for our weekly newsletter, subscribe to the RSS feed and follow us on Twitter, Facebook or Linkedin.
Comments
or email your comment to: comments@i-programmer.info |
|||||||
Last Updated ( Thursday, 08 June 2023 ) |