For an index to all my stories click this text.
I wrote two stories on using an SD card on the Raspberry Pi Pico (and Pico W). The first story was about connecting the SD card. And the second story described how to use the SD card in MicroPython. You can read those stories here, and I urge you to do so.
http://lucstechblog.blogspot.com/2025/01/pico-sdcard-part-1-hardware.html
http://lucstechblog.blogspot.com/2025/01/pico-sd-card-part-2-software.html
After the previous story I received a mail from one of my readers. His question was simple. He wanted to know if I have a method to make a pleasant presentation from the stored data. Charts like line- or bar- charts would be perfect.
Well if you attached the SD card to a Pico W you could produce graphs on a webpage. I described this in a story on this webpage. You can read that here: http://lucstechblog.blogspot.com/2019/11/esp-webpage-with-linechart.html
That story discussed a program in C++ but the web-page building and the Javascript code can easily be converted to MicroPython.
There is however a quick and easy method.
CSV files.
In the wifi and network world JSON is a standard for transferring files between computer systems and even between programming languages.
For other communications between computer systems there already was a standard and that is still widely used, It is called CSV files.
CSV means comma separated values. So you build a file with lines of data in which the data is separated by comma's.
An example would be a file in which the average temperatures in a certain time span are captured.
For the sake of this story I made such a file. Here is how it looks.
20:05,"15,00"
20:10,"14,94"
20:15,"14,88"
20:20,"14,81"
20:25,"14,94"
20:30,"15,00"
20:35,"15,06"
20:40,"15,06"
The first column shows the time. I measured the temperatures in my man-cave from 20:05 hour to 20:40 hour.
The second column shows the temperatures in degrees Celsius. And yes it was cold !!
The temperatures in the second column are set between quotation marks. If that was not the case 14,94 would get considered as two values 14 and 94 because of the comma in between. The way this file is build "14,94" is seen as a string and not as separate figures.
The program that captures the temperatures.
The breadboard setup for this program is the same as the breadboard setup in the first story about attching an SD acrd. Read that story here: http://lucstechblog.blogspot.com/2025/01/pico-sdcard-part-1-hardware.html
I just added a Dallas DS18B20 to the Pico (GPIO17).
If you do not know how to add a Dallas DS18B20 thermometer chip you can look at the story about the talking thermometer: http://lucstechblog.blogspot.com/2024/11/pico-audio-part-4-talking-thermometer.html
Or better yet: buy one of my books about the Pico (listed at the bottom of this story) which describe many more sensors...........
The program starts a timer (tim1) that would start a function every 5 minutes (period=1000*60*5). The function captures the temperature and saves the values to the SD card. The program is written in MicroPython and you can find it at the bottom of this page.
There is a special statement in the program for formatting the time.
currenttime =(str("%2.2d" %(hour)) + ":" + str("%2.2d" %(minutes)))
%2.2d converts the string into 2 numbers each with 2 figures. The d defines the numbers as integers.
temperature = ("%2.2f" %(sensor.read_temp(roms[0])))
In this line the temperature is read and formatted in a (f) floating point number with 2 numbers in front and 2 numbers behind the comma.
I will not go over the details because The program is not what this story is about. You can replace the temperature readings with any sensor readings you need/want. You can also alter the timers period from 5 minutes to any interval you like/need. The program can be found at the end of this story.
You can stop the program anytime you like. The program writes the data once every 5 minutes to the SD card and then closes the tempvalues.csv file. You can only stop a program that writes to an SD card if the file is closed. Otherwise you might loose data or even corrupt the SD card.
In this case the file is closed direct after time and temperature is written to it. So each 5 minutes the file is opened for less then a second and then closed again. So no problem in stopping the program whenever you like.
Here you can see the program in action.
And here you can see that the program made a new file in my SD directory (called storage) called tempvalues.csv
Stop the program by clicking CTRL-C in Thonny or clicking on it's stop icon. Now you can transfer the program to your PC.
At the top of Thonny's directory structure choose a directory on your PC where you want to store the file. Then click with the right mouse button on the file and choose Download to.......
Office
A CSV file can be opened by any Office program. I am using OpenOffice. OpenOffice is free and compatible with the other Office packages like Microsoft Office etc. I even use it to write my books on and send the files to the print company.
The only thing you now have to do is to click on thye tempvalues files on your PC and Office will open automatically.
And there it is.
with your mouse select the rows and colums like the picture shows.
From the drop-down menu choose diagram. My Openoffice is in Dutch (like I am) so your office version might look different but the functionality will be the same.
Tell the program to use the first column as the labels. An you are done.
You can change the color of the colums and their appearance.
In OpenOffice you can change the appearance of the graph by clicking on it with the right mousebutton.
There are lots of options in the program so play with these.
I chose to alter the diagram in a line-diagram.
And there we are. A line diagram with a nice capture.
That is it for now. Enough material to play with and display your data in a fancy way.
I am sure there are other programs that can do the same and maybe even in a graphically nicer way. If you know of any drop me a line. It might be interesting to others too.
Till next time,
have fun
Luc Volders
import os
import sdcard
from machine import Timer, SPI, Pin
import time
import machine
import onewire
import ds18x20
# ===================================
# first initialise the Dallas DS18B20
# ===================================
dallaspin = machine.Pin(17)
sensor = ds18x20.DS18X20(onewire.OneWire(dallaspin))
roms = sensor.scan()
print('A ds18x20 is present')
# =================================
# Initialise the RTC and local time
# =================================
rtc = machine.RTC()
year = 2023
month = 2
date = 17
hour = 20
minutes = 00
rtc.datetime((year, month, date, 0, hour, minutes, 00, 0))
print(time.localtime())
# ===============
# Start the timer
# ===============
tim1=Timer()
# ======================
# Initialise the SD card
# ======================
spi = SPI(1,sck=Pin(14), mosi=Pin(15), miso=Pin(12))
cs = Pin(13)
sd = sdcard.SDCard(spi, cs)
os.mount(sd, '/sd')
# ===============================================
# Function that gets the time and temperature and
# writes it to SD
# ===============================================
def function1(noval):
# get the time
hour = time.localtime()[3]
minutes = time.localtime()[4]
currenttime =(str("%2.2d" %(hour)) + ":" + str("%2.2d" %(minutes)))
print(currenttime)
print(type(currenttime))
# get the temperature
sensor.convert_temp()
time.sleep(1)
temperature = ("%2.2f" %(sensor.read_temp(roms[0])))
temperature = temperature.replace(".",",")
print(temperature)
print(type(temperature))
# write to sd
file = open('/sd/tempvalues.csv', 'a')
# for Europe when decimal ,
file.write(currenttime + "," + '"' + temperature + '"' + "\n")
# for US with decimal .
# file.write(currenttime + "," + temperature + "\n")
file.close()
tim1.init(period=1000*60*5, mode=Timer.PERIODIC, callback=function1)