RPi Pico B.Tech/Diploma Intermediate

Data Logger with SD Card & Raspberry Pi Pico MicroPython

Making a DHT11 Humidity Temperature Sensor Data logger with Micro SD Card Module & Raspberry Pi Pico using MicroPython Code

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

About this project

Overview
In this project, we will build a
data logger
using
Micro SD Card
&
Raspberry Pi Pico
. We will use a
DHT11 Sensor
to read the
Humidity
and
Temperature
data. The programming will be done in
Thonny IDE
using
MicroPython Code
. The temperature humidity data read will be stored in SD Card. Hence this can be useful in
data logging
In previous project, we learned Interfacing of
SD Card Module with Raspberry Pi Pico
Board. We will just modify the previous code and also add reading of
DHT11 Sensor data
. Thus DHT11 Data will be saved in SD Card over a period of time. It keeps track of the
temperature
and
humidity
levels in our surroundings. It can
record environmental data
over time, helping us monitor our surroundings.
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

DHT11 Sensor

Amazon
AliExpress
SunFounder

Jumper Wires

Amazon
AliExpress
SunFounder
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
The SD Card has been successfully formatted. You can now utilize it with the
SD Card Module
for your project applications.
Circuit: Data Logger with SD Card & Raspberry Pi Pico
Let us take a look at the connection diagram of
Data Logger
with SD Card & Raspberry Pi Pico.
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)
For the DHT11 Part, connect the VCC, GND & Output to the 3.3V, GND & GP5 of Raspberry Pi Pico. You may check the
DHT11 Raspberry Pi Pico
interfacing guide.
You may use a breadboard for assembly and troubleshooting.
MicroPython Code
The code for
Data Logger
with SD Card &
Raspberry Pi Pico
has been written in MicroPython. Hence you need a MicroPython Compiler like Thonny IDE.
For this project, we need
dht11 MicroPython Library
and also the
SD Card MicroPython Library
. Hence it is necessary to first save these two libraries in the Raspberry Pi Pico Board.
dht.py
Copy the following code in the Thonny Editor and save it with name
dht.py
in Raspberry Pi Pico.

import
array
import
micropython
import
utime
from
machine
import
Pin
from
micropython
import
const
class
InvalidChecksum
(
Exception
)
:
pass
class
InvalidPulseCount
(
Exception
)
:
pass
MAX_UNCHANGED
=
const
(

)
MIN_INTERVAL_US
=
const
(
200000
)
HIGH_LEVEL
=
const
(

)
EXPECTED_PULSES
=
const
(

)
class
DHT11
:
_temperature
:
int
_humidity
:
int
def
__init__
(
self
pin
)
:
self
_pin
=
pin
self
_last_measure
=
utime
ticks_us
(
)
self
_temperature
=

self
_humidity
=

def
measure
(
self
)
:
current_ticks
=
utime
ticks_us
(
)
if
utime
ticks_diff
(
current_ticks
self
_last_measure
)
<
MIN_INTERVAL_US
and
(
self
_temperature
>

or
self
_humidity
>

)
:
# Less than a second since last read, which is too soon according
# to the datasheet
return
self
_send_init_signal
(
)
pulses
=
self
_capture_pulses
(
)
buffer
=
self
_convert_pulses_to_buffer
(
pulses
)
self
_verify_checksum
(
buffer
)
self
_humidity
=
buffer
[

]
+
buffer
[

]
/

self
_temperature
=
buffer
[

]
+
buffer
[

]
/

self
_last_measure
=
utime
ticks_us
(
)
@
property
def
humidity
(
self
)
:
self
measure
(
)
return
self
_humidity
@
property
def
temperature
(
self
)
:
self
measure
(
)
return
self
_temperature
def
_send_init_signal
(
self
)
:
self
_pin
init
(
Pin
OUT
Pin
PULL_DOWN
)
self
_pin
value
(

)
utime
sleep_ms
(

)
self
_pin
value
(

)
utime
sleep_ms
(

)
@
micropython
native
def
_capture_pulses
(
self
)
:
pin
=
self
_pin
pin
init
(
Pin
IN
Pin
PULL_UP
)
val
=

idx
=

transitions
=
bytearray
(
EXPECTED_PULSES
)
unchanged
=

timestamp
=
utime
ticks_us
(
)
while
unchanged
<
MAX_UNCHANGED
:
if
val
!=
pin
value
(
)
:
if
idx
>=
EXPECTED_PULSES
:
raise
InvalidPulseCount
(
"Got more than {} pulses"
format
(
EXPECTED_PULSES
)
)
now
=
utime
ticks_us
(
)
transitions
[
idx
]
=
now
timestamp
timestamp
=
now
idx
+=

val
=

val
unchanged
=

else
:
unchanged
+=

pin
init
(
Pin
OUT
Pin
PULL_DOWN
)
if
idx
!=
EXPECTED_PULSES
:
raise
InvalidPulseCount
(
"Expected {} but got {} pulses"
format
(
EXPECTED_PULSES
idx
)
)
return
transitions
[

:
]
def
_convert_pulses_to_buffer
(
self
pulses
)
:
""
"Convert a list of 80 pulses into a 5 byte buffer
The resulting 5 bytes in the buffer will be:
0: Integral relative humidity data
1: Decimal relative humidity data
2: Integral temperature data
3: Decimal temperature data
4: Checksum
"
""
# Convert the pulses to 40 bits
binary
=

for
idx
in
range
(

len
(
pulses
)

)
:
binary
=
binary
<<

int
(
pulses
[
idx
]
>
HIGH_LEVEL
)
# Split into 5 bytes
buffer
=
array
array
(
"B"
)
for
shift
in
range
(

)
:
buffer
append
(
binary
>>
shift *

&
0xFF
)
return
buffer
def
_verify_checksum
(
self
buffer
)
:
# Calculate checksum
checksum
=

for
buf
in
buffer
[

:

]
:
checksum
+=
buf
if
checksum
&
0xFF
!=
buffer
[

]
:
raise
InvalidChecksum
(
)
sdcard.py
Now copy the following code to Thonny editor and save it with name
sdcard.py

""
"
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_TIMEO

Technologies used

ESP8266Raspberry PiBLEDHT11PythonSPIMicroPython

Hardware components

  • ESP8266
  • Raspberry Pi
  • DHT11
  • SD Card

Software & tools

  • Thonny
  • 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.