RPi Pico B.Tech/Diploma Beginner

Micro SD Card Module with Raspberry Pi Pico & MicroPython

Interfacing SPI Micro SD Card Module with Raspberry Pi Pico using MicroPython Code & library: Read-write text data

  • Suitable forB.Tech/Diploma RPi Pico students
  • Build time1-2 weeks
  • LevelBeginner
  • Available atAjvika Labs, Kakinada

About this project

Overview
In this beginner’s guide, we’re going to play with a
Micro SD Card Module
and a microcontroller called
Raspberry Pi Pico
. With a language called
MicroPython
, we’ll teach our Pico to talk to the
SD Card Module
. This module is like a helper that lets Pico save and read data from a small SD card, just like the one you might use in your camera.
What’s cool is we’ll make a small device that can keep a
record
of things, like
temperature
or
humidity
, every second. This is called a
Data Logger
. This way, your Pico can store all kinds of data, like words, sounds, or pictures on the SD card. With this
project
, you can do more fun and useful things with your
Raspberry Pi Pico
Bill of Materials
For this tutorial, we recommend to buy the following list of components directly from given links.
S.N.
Components Name
Quantity
Purchase Links

Raspberry Pi Pico

Amazon
AliExpress
SunFounder

SD Card Module

Amazon
AliExpress
SunFounder

SD Card 4-16 GB

Amazon
AliExpress
SunFounder

USB Card Reader

Amazon
AliExpress
SunFounder

Jumper Wires

Amazon
AliExpress
SunFounder
SD Card Module/Adapter
The
SD card module
is especially useful for projects that require data logging. There are actually two ways to interface with micro SD cards –
SPI mode
and
SDIO mode
. Hobbyists like us prefer SPI Mode for interfacing as it’s easy compared to SDO mode which is very complex.
The
operating voltage
of micro SD Cards is
3.3 V
. Therefore, we cannot SD Card directly with 5V logic. But the module has an onboard
ultra-low dropout regulator
that converts voltages from
3.3V – 6V
down to
~3.3V
. There is also a L
ogic Level converter
IC
74LVC125A
on the module which converts the interface logic from 3.3V-5V to 3.3V.
Micro SD Card Module Pinout
There are total of six pins (
GND, VCC, MISO, MOSI, SCK, CS
) on a SD-Card Adapter.
1. GND:
Ground Pin
2. VCC:
+5V power supply
3. MISO:
SPI output
4. MOSI:
SPI input
5. SCK:
Accepts clock pulses for Data Synchronization
6. CS:
Chip select signal pin to enable/disable line
Preparing the micro SD card
Before you insert the
micro SD card
into the module, you must properly format the card. You should format the SD card as FAT16 or FAT32.
To format the SD card, insert it into your computer. Go to My Computer and right-click on the SD card. A new window pops up. Select FAT32 as a Formatting option. Then press Start to initialize the formatting process
Now the SD Card is properly formatted and you can use the SD Card with SD Card Module to use in project applications.
Interfacing Micro SD Card Module with Raspberry Pi Pico
Now let us learn
interfacing of SD Card Module with Raspberry Pi Pico
to read & write data. Since the SD Card Module works on
SPI Communication protocol
, thus we need to connect it to SPI Pin of Raspberry Pi Pico Board.
Connect the SD Card Module to Raspberry Pi Pico as per the above
Circuit Diagram
. For Raspberry Pi Pico board, the SPI pins we can use are
2 (SCK)
4 (MISO)
3 (MOSI)
and
1 (CS)
The connection between Pi Pico Board & SD Card Module is as follows.
SD Card Pin
Raspberry Pi Pico
GND
GND
VCC
3.3V
MISO
GP4
MOSI
GP3
SCK
GP2
CS
GP1
SD Card MicroPython Library
For this project, we will require
sdcard.py library
. Here is a
Micropython Library
for interfacing Micro SD Card Module with a Raspberry Pi Pico.

""
"
MicroPython driver for SD cards using SPI bus.
Origial: https://github.com/micropython/micropython/tree/master/drivers/sdcard
RAW: https://raw.githubusercontent.com/micropython/micropython/master/drivers/sdcard/sdcard.py
Requires an SPI bus and a CS pin. Provides readblocks and writeblocks
methods so the device can be mounted as a filesystem.
Example usage on pyboard:
import pyb, sdcard, os
sd = sdcard.SDCard(pyb.SPI(1), pyb.Pin.board.X5)
pyb.mount(sd, '/sd2')
os.listdir('/')
Example usage on ESP8266:
import machine, sdcard, os
sd = sdcard.SDCard(machine.SPI(1), machine.Pin(15))
os.mount(sd, '/sd')
os.listdir('/')
"
""
from
micropython
import
const
import
time
_CMD_TIMEOUT
=
const
(

)
_R1_IDLE_STATE
=
const
(

<<

)
# R1_ERASE_RESET = const(1 << 1)
_R1_ILLEGAL_COMMAND
=
const
(

<<

)
# R1_COM_CRC_ERROR = const(1 << 3)
# R1_ERASE_SEQUENCE_ERROR = const(1 << 4)
# R1_ADDRESS_ERROR = const(1 << 5)
# R1_PARAMETER_ERROR = const(1 << 6)
_TOKEN_CMD25
=
const
(
0xFC
)
_TOKEN_STOP_TRAN
=
const
(
0xFD
)
_TOKEN_DATA
=
const
(
0xFE
)
class
SDCard
:
def
__init__
(
self
spi
cs
baudrate
=
1320000
)
:
self
spi
=
spi
self
cs
=
cs
self
cmdbuf
=
bytearray
(

)
self
dummybuf
=
bytearray
(

)
self
tokenbuf
=
bytearray
(

)
for
i
in
range
(

)
:
self
dummybuf
[
i
]
=
0xFF
self
dummybuf_memoryview
=
memoryview
(
self
dummybuf
)
# initialise the card
self
init_card
(
baudrate
)
def
init_spi
(
self
baudrate
)
:
try
:
master
=
self
spi
MASTER
except
AttributeError
:
# on ESP8266
self
spi
init
(
baudrate
=
baudrate
phase
=

polarity
=

)
else
:
# on pyboard
self
spi
init
(
master
baudrate
=
baudrate
phase
=

polarity
=

)
def
init_card
(
self
baudrate
)
:
# init CS pin
self
cs
init
(
self
cs
OUT
value
=

)
# init SPI bus; use low data rate for initialisation
self
init_spi
(
100000
)
# clock card at least 100 cycles with cs high
for
i
in
range
(

)
:
self
spi
write
(
b
"\xff"
)
# CMD0: init card; should return _R1_IDLE_STATE (allow 5 attempts)
for
_
in
range
(

)
:
if
self
cmd
(

0x95
)
==
_R1_IDLE_STATE
:
break
else
:
raise
OSError
(
"no SD card"
)
# CMD8: determine card version
r
=
self
cmd
(

0x01AA
0x87

)
if
r
==
_R1_IDLE_STATE
:
self
init_card_v2
(
)
elif
r
==
(
_R1_IDLE_STATE
_R1_ILLEGAL_COMMAND
)
:
self
init_card_v1
(
)
else
:
raise
OSError
(
"couldn't determine SD card version"
)
# get the number of sectors
# CMD9: response R2 (R1 byte + 16-byte block read)
if
self
cmd
(

False
)
!=

:
raise
OSError
(
"no response from SD card"
)
csd
=
bytearray
(

)
self
readinto
(
csd
)
if
csd
[

]
&
0xC0
==
0x40
:
# CSD version 2.0
self
sectors
=
(
(
csd
[

]
<<

csd
[

]
)
+

)
*
1024
elif
csd
[

]
&
0xC0
==
0x00
:
# CSD version 1.0 (old, <=2GB)
c_size
=
(
csd
[

]
&
0b11
)
<<

csd
[

]
<<

csd
[

]
>>

c_size_mult
=
(
csd
[

]
&
0b11
)
<<

csd
[

]
>>

read_bl_len
=
csd
[

]
&
0b1111
capacity
=
(
c_size
+

)
*
(

*
*
(
c_size_mult
+

)
)
*
(

*
*
read_bl_len
)
self
sectors
=
capacity
// 512
else
:
raise
OSError
(
"SD card CSD format not supported"
)
# print('sectors', self.sectors)
# CMD16: set block length to 512 bytes
if
self
cmd
(

)
!=

:
raise
OSError
(
"can't set 512 block size"
)
# set to high data rate now that it's initialised
self
init_spi
(
baudrate
)
def
init_card_v1
(
self
)
:
for
i
in
range
(
_CMD_TIMEOUT
)
:
self
cmd
(

)
if
self
cmd
(

Technologies used

ESP8266Raspberry PiBLEPythonSPIMicroPython

Hardware components

  • ESP8266
  • Raspberry Pi
  • SD Card

Software & tools

  • MicroPython

What you get

  • Working project / hardware kit, tested before delivery
  • Complete source code with explanation
  • Project demo and guidance for your viva
  • Project report, PPT and IEEE paper support (on request)
Delivery & Payment
Delivery takes a minimum of two weeks from the date your inquiry is confirmed. Payment is in two parts: • 50% advance to start the project • Remaining 50% before delivery Note: • Documentation and PPT are not included by default • If you need documentation/PPT, it can be arranged for extra time and cost We'll confirm the exact timeline and cost with you after reviewing your requirements.