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.
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)
Finished wire up:
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.
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.
Download the driver called sdcard.py
located in the Raspberry Pi Pico /drivers/src/ location.
Connect your Raspberry Pi Pico to your computer using the USB cable.
Open the Thonny IDE. Stop/Restart the backend to refresh the connection.
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.
If one does not already exist, create a new directory in Thonny on the Raspberry Pi Pic called drivers
.
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.
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
.
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:
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.
Using Thonny, open the main.py
file in Lesson 5: /src/main.py.
Run the 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.
Congratulations! You have successfully completed Lesson 5.
If you have finished with the base lesson, check out the items below.
Update the code to do any/all of the following:
Things to think about, validate, and/or try:
Create a function to delete contents on the SD card
A successful implementation of this code will result in the following:
delete_files_from_sdcard_folder
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.