Solar Eclipse Data Logger
If you're like me you're getting pretty excited about the 2017 total solar eclipse that will take place on August 21st. I plan on being in the path of totality not only to witness the phenomenon in person, but also to film the event with my camera. I was reading about total solar eclipses and learned something interesting. We all know that when the moon passes in front of the sun it will get very dark almost immediately, but what I didn't know was that there is also sudden drop in temperature. The reason for this sudden drop in temperature is how localized the moon's shadow is. Instead of slowly transitioning from day to night over several hours, the light from the sun is entirely blocked in a matter of minutes! According to Space.com, "During the total solar eclipse on Dec. 9, 1834, the Gettysburg Republican Banner reported that in some places, the eclipse caused the temperature to drop by as much as 28 degrees Fahrenheit.
The change in temperature during a total eclipse varies based on location and time of year. For the eclipse next week, meteorologists expect a drop in temperature between 5 and 10 degrees. Having learned this, I thought it would be fun to build a simple environment data logging device that will capture ambient light lelvels and temperature in real time and record them to an SD card. My goal is to have this device collect data for the entire duration of the solar eclipse.
Here are the components I used to build this project
- Arduino Nano ($2.34)
- SD card module ($0.54)
- DS1307 Real time clock ($0.52)
- DHT11 temperature and humidity sensor ($0.71)
- Photo resistor ($0.07)
- 400 tie point breadboard ($1.11)
In total these items cost about $5.29 USD when purchased on eBay.
I started out by plugging the Arduino Nano into the breadboard. The first task I wanted to accomplish was being able to write a text file to the SD card. The SD card communicates with the Arduino using the Serial Peripheral Interface or SPI bus. That means in addition to connecting power and ground, the four SPI pins on the SD card need to connect to the four SPI pins on the Arduino. Chip select connects to pin 10, MOSI gets connected to pin 11, MISO goes to pin 12, and clock connects to pin 13. Writing to the SD card is actually pretty simple. If you need help there are several examples included with the SD library. To write something to an SD card you need to include these two libraries, define which pin the chip select will be, which is pin 10 in our case, and finally declare a FILE type variable which we'll call dataFile. In the setup function you need to call SD.begin() with the chip select pin as the single argument. Now in the main loop function we can open up our file by writing this line of code. This opens a file called data.txt and puts it in Write mode. Now we can write whatever we want to that file. For now I'm going to write the words Time, temperature, humidity, and light level. Once we're done writing to the file, we need to make sure to close it by calling dataFile.close(). And that's all there is to it! We can compile this code and run it on the Arduino. We can check to see if our code works by taking the SD card out of the module and popping it into the computer. As expected the program wrote our four words over and over in the file. This is perfect, lets move onto the next part.
The DS1307 is a real time clock which allows us to set and keep precise time. These devices are pretty awesome, because they'll tell you the current time from what year it is all the way down to the second. There is a coin cell battery on board which means that even when we disconnect the power it will still be able to keep time. The DS1307 communicates to the Arduino using the I2C bus. The I2C bus is quite a bit simpler than the SPI bus. In addition to power and ground we only need to connect two signals to the Arduino, Data and Clock. The Data pin connects to Analog pin 4, and the Clock pin connects to Analog pin 5. I had to download a library for this device but the Arduino library manager makes this process really easy. The library has a couple of examples which we'll need to run to setup this device. I loaded the SetTime example and ran it on the Arduino. This clever code grabs the compile time from your computer and sets the real time clock for you. The coin cell battery needs to already be installed when this step is performed. Next I ran the ReadTime example just to make sure the time was set correctly. For this solar eclipse data logger I want to be able to create a time stamp for each sensor reading I take. That way I can plot it out and we can see what time each event happened. The real time clock will also be useful for giving unique names to the files on the SD card. I can simply name each file whatever the current time is. To use the DS1307 we include these three libraries, and declare a time element variable called RealTimeClock. In the main loop function we call RTC.read() with RealTimeClock as the argument. Now we can retrieve the current time by accessing each attribute. For example RealTimeClock.Hour RealTimeClock.minute and RealTimeClock.second. First I want to replace the word time with the actual time. The RTC.read() function returns integers, so I'll need to format them as strings before I can write them to the SD card file. I'll compile my code, run it on the Arduino, and check the text file on the SD card to make sure everything it working.
Next I'll move on to the temperature and humidity sensor. For the solar eclipse we're only interested in the temperature, but since the DHT11 is capable of reporting the humidity we might as well use that too. Again we'll connect power and ground to the DHT11 as well as the Data pin to pin 2 on the Arduino. Using the DHT11 is also pretty straight forward. I'll add the library to my sketch, as well as these two constant definitions. I'll need to instantiate the device then call dht.begin(). Now in the main loop we can replace the words temperature and humidity with dht.readTemperature() and dht.readHumidity respectively. Again, I have to format these floating point numbers as strings in order to write them to the SD card.
Light level readings
Finally we get to the light sensor. As I mentioned earlier I chose to use a simple photo resistor to detect light levels. I created a voltage divider with the photo resistor and a regular 1k resistor. This is accomplished by connecting the two resistors in series, and placing them between 5V and GND. I'll use the analog to digital converter or ADC on the Arduino to read the voltage between the two resistors. Since the Arduino has a 10 bit ADC, this value will vary from 0 (totally dark) to 1023 or very bright. Reading an analog voltage on the Arduino is trivial. Connect the output from the photo resistor to Analog pin 0. In the code we can define a constant to represent analog pin 0. Now we can replace the words light level with the function analogRead(LIGHTPIN); As we did before we'll need to format this value as a string.
I want to make clear that this is the bare minimum code needed to get these devices working. In the real world there is a lot of error checking, formatting, and debugging practices that should be implemented.
Time to see this thing in action! I will aim my heat gun at the DHT11 to raise the temperature briefly, then use my hand to cover the photo resistor. When we open up the file from the SD card we should see these events with their time stamp. Sweet it works!