StratoLab

Lesson 5: Writing Data to SD Card Module

Pre-requisites

Objectives

Results

Video Walk-through

In addition to the reading below, you can watch this video for guidance!

## Steps

### Hardware Configuration and Wiring

IMPORTANT Before wiring your Pico, UNPLUG IT FROM YOUR COMPUTER. If plugged in while wiring, you risk damaging the Pico or SDCard reader.

  1. Wire the HW-125 SDCard reader to the Raspberry Pi Pico. HW-125 Pins | Description | Pi Pico Pins ———– | ———– | ———— GND | (Voltage Common Collector): Provides power to the HW-125. Connect to the 5V pin on Pico | GND (38) VCC | (Ground): Connect to the ground pin on Pico | 5V (40) SCK | (Serial Clock): Accepts clock pulses from the Pico to synchronize data transmission | GP10 (14) MOSI | (Master Out Slave In): SPI input to microSD card module | GP11 (15) MISO | (Master In Slave Out): SPI output from the microSD card module | GP12 (16) CS | (Chip Select): Control pin used to select one (or set) of devices on the SPI bus | GP13 (17)

    sd-card-diagram

    Finished wire up: sd-card-diagram

  2. Format the micro SD card as FAT32. Use How to Format SD Card on Mac, Windows, Android and Camera as a guide.

:information_source: Partition larger SD card to 32GB
If you do not have a 32GB card you can partition a larger card down to 32GB and it will work as well. Be cautious in using Disk Formatter because you can accidentally reformat any attached storage device to your computer. Double check you have selected the SD card you want to format.

Install SD Card MicroPython Driver

Drivers are code modules for enabling certain functionality. One such driver allows us to read data from the SD Card module. This driver is called sdcard.py and is located in the Raspberry Pi Pico /drivers/src/ location. The following steps will result in saving this driver to the Raspberry Pi Pico so the driver can be used by our Python code.

  1. Download the driver called sdcard.py located in the Raspberry Pi Pico /drivers/src/ location.

  2. Connect your Raspberry Pi Pico to your computer using the USB cable.

  3. Open the Thonny IDE. Stop/Restart the backend to refresh the connection.

    stop-restart

    You should now see Raspberry Pi Pico displayed in the left-hand navigation of Thonny.If the “Files” window is not displaying add it from the View > Files menu.

    files-menu

  4. If one does not already exist, create a new directory in Thonny on the Raspberry Pi Pic called drivers.

    drivers-directory

  5. Using Thonny, select File then Open from the menu. Choose This Computer. Navigate to the location where you downloaded sdcard.py in a previous step. Select the file and click Open.

  6. Save the sdcard.py file to the Raspberry Pi Pico. This allows our code to use the driver to perform SD card actions in MicroPython when running on the Pi Pico.

    Click File then Save as…. Choose Raspberry Pi Pico. Double-click the drivers folder created in a previous step. Then save the sdcard.py file being sure to name it sdcard.py.

  7. If a file called __init__.py does not already exist in the /drivers folder, create a new file in Thonny called __init__.py.

    Click File then New. Then click File then Save as…. Choose Raspberry Pi Pico and save this empty file to the same drivers location as the previous step. Name the file __init__.py. This empty file is used by Python to indicate the drivers folder is to be used for Python modules.

    Your finished folders and files should look like this:
    files-menu

SD Card Reader/Writer Program

The steps in this section will use the previous hardware and driver sections to allow writing to, and reading from a CSV file. The code example for this lesson is located in Lesson 5: /src/main.py.

  1. Using Thonny, open the main.py file in Lesson 5: /src/main.py.

  2. Run the script.

    run-script

    Output will be generated to the console in Thonny describing the actions being taken. You will also see a new directory created on the SD card called /sd if using the example code. Within this folder is a called called data.csv. You may choose to download this file to your computer and open the file using a program such as Microsoft Excel to read the data in a more familiar program.

    output

Congratulations! You have successfully completed Lesson 5.


Want more?

If you have finished with the base lesson, check out the items below.

Update the code to do any/all of the following:

  1. Write your own custom output to the file and change the filename
  2. Use a loop to write the even numbers from 0 to 100 into the file 😵

Things to think about, validate, and/or try:

Challenge

Create a function to delete contents on the SD card

A successful implementation of this code will result in the following:

As you think through this code, also consider previous challenges in lessons that introduce concepts of Python Functions and Python Try Except blocks for exception handling.

Expand to see an example function to delete folder contents from the SD card You may choose to add this code to your `main.py` as a means to clear contents from the SD card. ```python from machine import Pin, SPI from drivers import sdcard import os def delete_files_from_sdcard_folder(folder): """Delete all files from SD Card folder""" print("Deleting files in %s" % folder) for file in os.listdir(folder): try: os.remove(folder + '/' + file) except Exception as e: pass print("Done.\n\n") if __name__ == "__main__": # Main entrypoint. Primary code functions start here. sd_dir = '/sd' # Directory created on SD card at root '/'. Expected format is '/'. Example: '/sd' # Intialize SPI peripheral spi = machine.SPI(1, baudrate=1000000, # 1 MHz polarity=0, phase=0, bits=8, firstbit=SPI.MSB, sck=machine.Pin(10), # Pico GPIO Pin 10 mosi=machine.Pin(11), # Pico GPIO Pin 11 miso=machine.Pin(12)) # Pico GPIO Pin 12 # Initialize the SD card to Pico GPIO Pin 13 on chip select (CS) pin sd = sdcard.SDCard(spi,Pin(13)) delete_files_from_sdcard_folder(sd_dir) # Uncomment this line to run a function to delete all the files # in the folder location passed in. ``` </details> ## Troubleshooting * `ERROR: No module named (drivers, sdcard, ...)` If you see this error it means Python is not able to locate a module to be imported. This can occur because the version of MicroPyhon you are using does not support the module you are trying to import. Specifically for this lesson it likely applies to the `drivers` step. Ensure the `drivers` folder and its contents, `sdcard.py` and `__init__.py`, are saved to the Raspberry Pi Pico device and _not_ your computer. Example error message: ```sh Traceback (most recent call last): File "", line 2, in ImportError: no module named 'drivers' ``` * Validate the SD card is 32GB or smaller and formatted to FAT32 * [How to Format SD Card on Mac, Windows, Android and Camera](https://www.cisdem.com/resource/how-to-format-sd-card.html) * [partitioning a larger SD card to 32GB](/telemetry/raspberry-pi-pico/python/lesson-5/assets/images/sdCardFormat.gif) ## Reference Material * [How to Format SD Card on Mac, Windows, Android and Camera](https://www.cisdem.com/resource/how-to-format-sd-card.html) * [Raspberry Pi Pico Pinout](https://datasheets.raspberrypi.com/pico/Pico-R3-A4-Pinout.pdf) * [Raspberry Pi Pico SDK](https://datasheets.raspberrypi.com/pico/raspberry-pi-pico-python-sdk.pdf) * [MicroPython releases for Pico](https://micropython.org/download/rp2-pico/) * [MicroPython libraries](https://docs.micropython.org/en/latest/library/index.html) ## Need help? Watch the walk-through [video](assets/videos/Lesson5.mp4?raw=true) for guidance!