SD Card, Circuit Python via Thonny

 Finally got Pi Pico to run simple SD card program with Thonny and Circuit Python using adafruit library.



# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries PB1 try

# SPDX-License-Identifier: MIT

#Worked !! Sat Jun 25 15:20:38 NZST 2022


import os

import busio

import digitalio

import board

import storage

import adafruit_sdcard


# The SD_CS pin is the chip select line.

#

#     The Adalogger Featherwing with ESP8266 Feather, the SD CS pin is on board.D15

#     The Adalogger Featherwing with Atmel M0 Feather, it's on board.D10

#     The Adafruit Feather M0 Adalogger use board.SD_CS

#     For the breakout boards use any pin that is not taken by SPI


#SD_CS = board.SD_CS  # setup for M0 Adalogger; change as needed

#block below from DoneBot workshop pb Some capitalisation changes

MOSI = board.GP11

MISO = board.GP12

#clk = board.GP10

SCK = board.GP10

cs = board.GP15


# Connect to the card and mount the filesystem.

spi = busio.SPI(board.GP10, board.GP11, board.GP12)

#cs = digitalio.DigitalInOut(SD_CS)


#cs = board.GP15

#sdcard = adafruit_sdcard.SDCard(spi, board.GP15)

#cs = digitalio.DigitalInOut(board.SD_CS)

cs = digitalio.DigitalInOut(board.GP15)

sdcard = adafruit_sdcard.SDCard(spi, cs)

vfs = storage.VfsFat(sdcard)

storage.mount(vfs, "/sd")


# Use the filesystem as normal! Our files are under /sd


# This helper function will print the contents of the SD

def print_directory(path, tabs=0):

    for file in os.listdir(path):

        stats = os.stat(path + "/" + file)

        filesize = stats[6]

        isdir = stats[0] & 0x4000


        if filesize < 1000:

            sizestr = str(filesize) + " bytes"

        elif filesize < 1000000:

            sizestr = "%0.1f KB" % (filesize / 1000)

        else:

            sizestr = "%0.1f MB" % (filesize / 1000000)


        prettyprintname = ""

         for _ in range(tabs):

            prettyprintname += "   "

        prettyprintname += file

        if isdir:

            prettyprintname += "/"

        print("{0:<40} Size: {1:>10}".format(prettyprintname, sizestr))


        # recursively print directory contents

        if isdir:

            print_directory(path + "/" + file, tabs + 1)



print("Files on filesystemmm:")

print("====================")

print_directory("/sd")

&&&&&&&&&&&&&&&&&&&&Below = serial from above Prog &&&&&&&&&&&&&&&&

>>> %Run -c $EDITOR_CONTENT

Files on filesystemmm:

====================

System Volume Information/               Size:    0 bytes

   WPSettings.dat                        Size:   12 bytes

   IndexerVolumeGuid                     Size:   76 bytes

TEST.TXT                                 Size:  246 bytes

pico.txt                                 Size:    0 bytes

code.py                                  Size:  414 bytes

>>> 


Next, add routines that write and read text files.


Comments

Popular posts from this blog

Spud sensor update 3 August 2022

Real time LDR readings sent to receiver Wemos Mini