Skip to main content
Chemistry LibreTexts

2: Writing to a CSV file (DHT22 data)

  • Page ID
    434772
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)

    \( \newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\)

    ( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\)

    \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)

    \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\)

    \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)

    \( \newcommand{\Span}{\mathrm{span}}\)

    \( \newcommand{\id}{\mathrm{id}}\)

    \( \newcommand{\Span}{\mathrm{span}}\)

    \( \newcommand{\kernel}{\mathrm{null}\,}\)

    \( \newcommand{\range}{\mathrm{range}\,}\)

    \( \newcommand{\RealPart}{\mathrm{Re}}\)

    \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)

    \( \newcommand{\Argument}{\mathrm{Arg}}\)

    \( \newcommand{\norm}[1]{\| #1 \|}\)

    \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)

    \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    \( \newcommand{\vectorA}[1]{\vec{#1}}      % arrow\)

    \( \newcommand{\vectorAt}[1]{\vec{\text{#1}}}      % arrow\)

    \( \newcommand{\vectorB}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \( \newcommand{\vectorC}[1]{\textbf{#1}} \)

    \( \newcommand{\vectorD}[1]{\overrightarrow{#1}} \)

    \( \newcommand{\vectorDt}[1]{\overrightarrow{\text{#1}}} \)

    \( \newcommand{\vectE}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash{\mathbf {#1}}}} \)

    \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)

    In this activity you will read temperature and humidity data from a DHT22 sensor and post them to a csv file along with the time of the reading.  If you are not familiar with the DHT22 you will need to check the following two links, where we go over how it works, and how to set it up to collect data.

    Checking DHT22 Setup 

    Run the following code in Thonny  

    #!/usr/bin/python3
    
    #Import the modules
    import time
    import board
    import adafruit_dht
    
    #Declare the sensor and the GPIO pin it is connected to
    dhtDevice = adafruit_dht.DHT22(board.D4, use_pulseio=False)
    
    while True:
        try:
            # Print the values to the serial port
            tempC = dhtDevice.temperature
            humidity = dhtDevice.humidity
            print(f"Temp: {tempC} C    Humidity: {humidity}% ")
            time.sleep(10.0)  #pause for 10 seconds
        except RuntimeError:
            pass
    

    or alternatively, type the following code into the command line (you will need to use the path to the file on your computer, and be sure the program is executable and has a shebang that identifies the interpreter

    /home/pi/Programs//DHT22/DHT22_1.py 
    

    What this code is doing is printing the temperature and time every 2 seconds

    clipboard_e2931f08b46910652b75acf4d3c93f52a.pngFigure \(\PageIndex{1}\): Copy and Paste Caption here. (Copyright; author via source)

    Now that we know the sensor is reading data correctly we need to put the data into a file so that it can be used.

    Writing to CSV File

    CSV (comma separated variable) are one of the most common types of data files, and are the kinds of files Excel and Google Docs work with. These are not files that are read by python in the sense that they are a data type that python works with.  But you can generate these (write) with the python inFile.write command, and in a latter exercise we will see how to read a CSV file by converting its content into a datatype that Python can work with, like a list.

    Before running the following code save it in your folder for this exercise and then look at the contents of that folder (you can do this with the "ls" command, or using the desktop file manager).  Then run this program and look again at the content of the folder.  You should see a new file appear called "DHTtemperatures.csv".  Right click on this and choose "text editor" (note, if you installed LibreOffice and click on the file it will open in LibreCalc, which is an open source program like Excel.

    import datetime
    from time import sleep
    import board
    import adafruit_dht
    
    def getValues():
        dhtDevice = adafruit_dht.DHT22(board.D4, use_pulseio=False)
        for i in range(0,5):
            try:
                humidity = dhtDevice.humidity
                temperature = dhtDevice.temperature
            except RuntimeError as error:
                print(error.args[0])
                sleep(2.0)
                continue
            except Exception as error:
                dhtDevice.exit()
                raise error
        if humidity is not None and temperature is not None:
            return (humidity, temperature)
                    
    inFile= open("DHTtemperatures.csv","w")
    inFile.write("time,humidity,temperature\n")
    
    for x in range(0,10):
        humidity,temperature = getValues()
        now = datetime.datetime.now().strftime("%H:%M:%S")
        inFile.write('%s,%s,%s\n' %(now,humidity,temperature))
        sleep(2)
        #note the DHT22 does not read faster than this
        print(x)
    
    inFile.close()
    

    Questions

    Exercise \(\PageIndex{1}\)

    What is the role of the for loop in the function getValues()?

    Answer

    This is assigning humidity and temperature values to the variables humidity and temperature.  If the value returned is not valid it retries 5 times. This is important in working with sensor readings as you need checks to be sure the data you are transferring is real data.

     

    Exercise \(\PageIndex{2}\)

    What does the following line of code do?

    inFile=open(DHTtemperatures.csv","w") do?  If the file already exists, what happens to the data?

    Answer

    This creates the file DHTtemperatures.csv and clears out all data if there was data in the file.

     

    Exercise \(\PageIndex{3}\)

    What does the following line of code do?

    inFile.write("time,humidity,temperature\n")

    Answer

    It inserts into the CSV file the headings of time, humidity and temperature.  The \n generates a new line.

     

    Exercise \(\PageIndex{4}\)

    Where in the code are the variables assigned values, what is the variable for time and why is its assignment executed after the variables humidity and temperature are assigned values?

    Answer

    They are assigned in the for loop. The variables humidity and temperature are assigned values by calling the function getValues(). The variable for time is now and it is called after the function getValues() has returned a value.  If you look at the function getValues() you see a loop with up to 5 iterations, depending on if a valid value is returned.  So the time delay could be 0,2,4,6,8 seconds.  By getting the time after the value is returned we are certain the time is correct, and so our plot will be correct, although the data values may not be evenly spaced.  If 5 bad values are returned you have a serious problem and may need to check your wiring, 

     

    Exercise \(\PageIndex{5}\)

    Change the range of the loop from 10 to 5 iterations and run the code.  Before looking at the file, how many values to you expect to be generated?

    Answer

    You will see 5 data values plus the title.  You are writing a new file ('w'), not overwriting the original data

     

    Exercise \(\PageIndex{6}\)

    Change the "w" of the infile= open( ) statement to an "a" and change the 5 in the range of the loop back to 10.  Run the code. What happens, how many lines of data do you see, and what does "a" stand for?

    Answer

    "a" stands for append, and you are now adding data to the file, and will have 15 rows of data. Note, since you ran the program twice you generated the title twice.  

     

    temporary stuff

        Spod1Backup = open("openspod1backup.csv","a")
        #Spod1Backup.write("\n")
        Spod1Backup.write(str(values)[1:-1])
        Spod1Backup.write("\n")
        Spod1Backup.close()
    

     

     


    2: Writing to a CSV file (DHT22 data) is shared under a not declared license and was authored, remixed, and/or curated by LibreTexts.

    • Was this article helpful?