Raspberry Pi Pico SD Card

Overview

Things to do:

Network - WiFi


Code: Connect to the network

SD Card Read Write



Micro SD Card Interface

Driver:
The MicroPython “sdcard.py”  driver:  https://github.com/micropython/micropython-lib/blob/master/micropython/drivers/storage/sdcard/sdcard.py

Get this and then put it into a directory called lib

Hardware.


https://www.amazon.ca/dp/B0989SM146

A note here, the SD card is a 3.3V device.  Many SD card adapter modules that you can buy are 5V and they have a voltage converter on board.  There is no need for that here as the Pi Pico is 3.3V

Connections for the SPI Bus


NOTE:  The SD Card Adapter I used is a 3.3 Adapter and connects to the 3.3V out
Pin 36

SPI Bus - Serial Peripheral Interface

SCK - Clock
MOSI - Master out Slave in
MISO - Master in Slave out
CS - chip select
The Pico is Master


Getting the SD card adapter working is actually pretty easy.  Include the driver and plug 4 signal pins and power and ground to the correct places. 

Network Time Protocol


By Benjamin D. Esham (bdesham) - This vector image was created with Inkscape by v ., Public Domain, https://commons.wikimedia.org/w/index.php?curid=2815097

pool.ntp.org

deid@32gig:~/Desktop$ host pool.ntp.org
pool.ntp.org has address 54.39.196.172
pool.ntp.org has address 172.97.221.33
pool.ntp.org has address 23.133.168.246
pool.ntp.org has address 198.50.127.72
pool.ntp.org mail is handled by 0 .


NTP_DELTA = 2208988800 

Convert from a epoch of 1900-01-01 to 1970-01-01 to keep micropython happy.

How do I know?:

import utime
utime.gmtime(0)[0]

From Wikipedia

The 64-bit binary fixed-point timestamps used by NTP consist of a 32-bit part for seconds and a 32-bit part for fractional second, giving a time scale that rolls over every 232 seconds (136 years) and a theoretical resolution of 2−32 seconds (233 picoseconds). NTP uses an epoch of January 1, 1900. Therefore, the first rollover occurs on February 7, 2036.[35][36]


Code: Set the Pi Pico time.

Note if you are using Thonny it will, by default, set the time on the Pico to local time.  To get the time from an NTP server and leave it in UTC you will have to turn this off in the Thonny config.  Why?  Because in the end this will run not connected to Thonny.

Read the Temperature DS18B11


Code get the time and temperature and format it for output

Timer Interrupt

Because waiting for a web server request is a blocking call, the writing of the time and temperature to the SD card is implemented using a timer interrupt.  A bonus, the timing is more accurate than it would be using time.sleep calls to delay.


Code Write to SD Card

Web Server


Web Page served  from the Pi Pico

Code: Loop Listening for and handling a connection

Code: Display the web page

Send the normal HTML Response

Code: Helper functions

Charts


Chart google sheets


Chart javascript
Code: JavaScript  to graph the last 24+ hours


References


Pi Pico Pinout


Code: All the python code

Thanks to: