Skip to content

Commit

Permalink
Updated examples
Browse files Browse the repository at this point in the history
  • Loading branch information
tigoe committed Feb 27, 2016
1 parent e41ecee commit 20e2ce4
Show file tree
Hide file tree
Showing 8 changed files with 410 additions and 52 deletions.
@@ -1,48 +1,45 @@
/*
SD card Temp & Humidity datalogger
This example shows how to log data from
a DHT11 temperature and humidity sensor
to an SD card using the SD library.
It also uses the Adafruit DHT sensor library from
https://github.com/adafruit/DHT-sensor-library
The circuit:
* SD card attached to SPI bus as follows:
This example shows how to log data from
a DHT11 temperature and humidity sensor
to an SD card using the SD library.
It also uses the Adafruit DHT sensor library from
https://github.com/adafruit/DHT-sensor-library
The circuit:
SD card attached to SPI bus as follows:
** MOSI - pin 11
** MISO - pin 12
** CLK - pin 13
** CS - pin 4
* DHT11 sensor connected as follows:
DHT11 sensor connected as follows:
** Ground connected to 5
** Voltage connected to 8
** data out connected to 7
** 10K resistor connected from 7 to +5V
created 9 Feb 2012
by Tom Igoe
This example code is in the public domain, subject to the licenses
of the libraries used.
*/
created 9 Feb 2012
by Tom Igoe
This example code is in the public domain, subject to the licenses
of the libraries used.
*/

#include <SD.h>
#include "DHT.h"

#define DHTPIN 7 // what pin the sensor is connected to
#define DHTPIN 5 // what pin the sensor is connected to
#define DHTTYPE DHT11 // Which type of DHT sensor you're using:

#define DHT_GND 5 // ground pin of the sensor
#define DHT_VCC 8 // voltage pin of the sensor

#define TEMPERATURE 1 // for the DHT sensor
#define HUMIDITY 0 // for the DHT sensor

// initialize the sensor:
DHT dht(DHTPIN, DHTTYPE);
const int interval = 10*1000; // the interval between reads, in ms
const int interval = 10 * 1000; // the interval between reads, in ms
long lastReadTime = 0; // the last time you read the sensor, in ms


Expand All @@ -55,19 +52,19 @@ const int chipSelect = 4;
void setup() {
Serial.begin(9600);
if (startSDCard() == true) {
startSensor();
startSensor();
}
}

void loop()
{
// Get the current time in ms:
// Get the current time in ms:
long currentTime = millis();

if (currentTime > lastReadTime + interval) {
float humidity = readSensor (HUMIDITY);
float temperature = readSensor(TEMPERATURE);

// open the file:
File dataFile = SD.open("datalog.csv", FILE_WRITE);

Expand All @@ -77,16 +74,17 @@ void loop()
dataFile.print("\t");
dataFile.println(temperature);
dataFile.close();
// print to the serial port too:
Serial.print(humidity);
}
// print to the serial port too:
Serial.print(humidity);
Serial.print("\t");
Serial.println(temperature);
lastReadTime = millis();
}
lastReadTime = millis();
// }
// if the file isn't open, pop up an error:
else {
Serial.println("error opening datalog.csv");
}
// else {
// Serial.println("error opening datalog.csv");
// }
}
}

Expand All @@ -103,7 +101,7 @@ boolean startSDCard() {
Serial.println("Card failed, or not present");
// don't do anything more:
result = false;
}
}
else {
Serial.println("card initialized.");
File dataFile = SD.open("datalog.csv", FILE_WRITE);
Expand All @@ -113,19 +111,14 @@ boolean startSDCard() {
dataFile.close();
result = true;
}
}
}
return result;
}


void startSensor() {
// set up pins to power and read sensor:
pinMode(DHT_VCC, OUTPUT);
pinMode(DHT_GND, OUTPUT);
digitalWrite(DHT_VCC, HIGH);
digitalWrite(DHT_GND, LOW);
// start sensor:
dht.begin();
dht.begin();
}

// get the sensor readings and concatenate them in a String:
Expand All @@ -135,18 +128,18 @@ float readSensor( int thisValue) {

if (thisValue == TEMPERATURE) {
result = dht.readTemperature();
}
}
else if (thisValue == HUMIDITY) {
// read sensor:
result = dht.readHumidity();
result = dht.readHumidity();
}

// make sure you have good readings. If the reading
// is not a number (NaN) then return an error:
if (isnan(result)) {
// an impossible result for either reading
// an impossible result for either reading
// so it'll work as an error:
result = -273.0;
result = -273.0;
}
return result;
}
Expand Up @@ -14,7 +14,7 @@
#include <SPI.h>
#include <SD.h>

const int batteryPin = A7; // battery volotage is on pin A7
const int batteryPin = A7; // battery voltage is on pin A7
const int chipSelect = 4; // SPI chip select for SD card
const int cardDetect = 7; // pin that detects whether the card is there
const int writeLed = 8; // LED indicator for writing to card
Expand Down Expand Up @@ -79,7 +79,7 @@ void loop() {
logFile.println(sensorVoltage); // print sensor voltage & newline to the log
logFile.close(); // close the file

// for degugging only:
// for debugging only:
Serial.print(batteryVoltage);
Serial.print(",");
Serial.println(sensorVoltage);
Expand Down
48 changes: 48 additions & 0 deletions Columbia_Brown_Center_2016/DHT11/DHT11.ino
@@ -0,0 +1,48 @@
/*
This example shows how to read data from
a DHT11 temperature and humidity sensor.
The circuit:
DHT11 sensor connected as follows:
** Ground connected to ground
** Voltage connected to Vcc
** data out connected to 5
** 10K resistor connected from 5 to +5V
created 9 Feb 2012
modified 25 Feb 2016
by Tom Igoe
*/

#include <SD.h>
#include "DHT.h"

#define DHTPIN 5 // what pin the sensor is connected to
#define DHTTYPE DHT11 // Which type of DHT sensor you're using:

// initialize the sensor:
DHT dht(DHTPIN, DHTTYPE);
const int interval = 10 * 1000; // the interval between reads, in ms
long lastReadTime = 0; // the last time you read the sensor, in ms

void setup() {
Serial.begin(9600);
dht.begin();
}

void loop() {
// Get the current time in ms:
long currentTime = millis();
// if the read delay interval has passed, read the sensor:
if (currentTime > lastReadTime + interval) {
float humidity = dht.readTemperature();
float temperature = dht.readHumidity();

// print to the serial port:
Serial.print(humidity);
Serial.print(",");
Serial.println(temperature);
lastReadTime = millis();
}
}
108 changes: 108 additions & 0 deletions Columbia_Brown_Center_2016/TempHumidityLogger/TempHumidityLogger.ino
@@ -0,0 +1,108 @@
/*
Temperature and humidity logger
This sketch reads the temperature and humidity from a DHT11 sensor
on an Adalogger and saves their values to a comma-separated values (CSV) file.
Written and tested on an Adalogger M0 board
.
created 25 Feb 2016
by Tom Igoe
*/


#include <SD.h>
#include "DHT.h"

#define DHTPIN 5 // what pin the sensor is connected to
#define DHTTYPE DHT11 // Which type of DHT sensor you're using:

// initialize the sensor:
DHT dht(DHTPIN, DHTTYPE);

const int chipSelect = 4; // SPI chip select for SD card
const int cardDetect = 7; // pin that detects whether the card is there
const int writeLed = 8; // LED indicator for writing to card
const int errorLed = 13; // LED indicator for error
long lastWriteTime = 0; // timestamp for last write attempt
long interval = 10000; // time between readings
char fileName[] = "datalog.csv"; // filename to save on SD card

void setup() {
Serial.begin(9600); // initialize serial communication

// initialize LED and cardDetect pins:
pinMode(writeLed, OUTPUT);
pinMode(errorLed, OUTPUT);
pinMode(cardDetect, INPUT_PULLUP);

// Stay in this loop until the card is inserted:
while (digitalRead(cardDetect) == LOW) {
Serial.println("Waiting for card...");
digitalWrite(errorLed, HIGH);
delay(750);
}

// check if the card initialized successfully:
if (startSDCard() == true) {
Serial.println("card initialized.");
delay(100);
} else {
Serial.println("Card failed");
}
// open the log file:
File logFile = SD.open(fileName, FILE_WRITE);
// write header columns to file:
if (logFile) {
logFile.println("Temperature,Humidity");
logFile.close();
}

// initialize the sensor:
dht.begin();
}

void loop() {
// if the cart's not there, don't do anything more:
if (digitalRead(cardDetect) == LOW) {
digitalWrite(errorLed, HIGH);
return;
}
// turn of the error LED:
digitalWrite(errorLed, LOW);

// read sensors every 10 seconds
if (millis() - lastWriteTime >= interval) {
File logFile = SD.open(fileName, FILE_WRITE); // open the log file
if (logFile) { // if you can write to the log file,
digitalWrite(writeLed, HIGH); // turn on the write LED
// read sensor:
float humidity = dht.readTemperature();
float temperature = dht.readHumidity();

// print to the log file:
logFile.print(humidity);
logFile.print(",");
logFile.println(temperature);
logFile.close(); // close the file

// for debugging only:
Serial.print(humidity);
Serial.print(",");
Serial.println(temperature);

// update the last attempted save time:
lastWriteTime = millis();
}
digitalWrite(writeLed, LOW); // turn off the write LED
}
}

boolean startSDCard() {
// check to see that the SD card is responding:
if (!SD.begin(chipSelect)) { // if not,
digitalWrite(errorLed, HIGH); // turn on error LED
return false;
}
return true;
}
23 changes: 19 additions & 4 deletions feather-m0-adalogger.html
Expand Up @@ -36,12 +36,27 @@ <h1>

In this workshop you'll be working with the Adafruit Feather M0 Adalogger microcontroller board. This board uses a Cortex ARM M0+, which is the same processor as the Arduino Zero. That means it's Arduino-compatible, and a very capable processor for datalogging. The <a href="https://learn.adafruit.com/adafruit-feather-m0-adalogger?view=all">Adafruit Guide to the Feather M0 Adalogger</a> is a thorough introduction. This page is a quick-reference only.

<h3>Setting up the board</h3>
<h3>Functions of the Board</h3>

<p>
The Feather M0 Adalogger can read various sensors using its digital and analog inputs and its asynchronous serial and I2C communications pins. It can also read and write from an attached SD card. It can communicate with a personal computer via USB as well. It has a Lithium-Poly (LiPo) battery charger built-in so you can charge a battery while you're programming, then unplug and run off the battery independently. There's also a built-in real-time clock that can keep accurate time and two built-in LEDs that you can use to indicate what's going on.
</p>
<p>
Below is the pin diagram for the board. Many of the pins have multiple functions. For the purposes of this workshop, the important pins are as follows:
</p>

<ul>
<li><strong>Digital I/O pins.</strong> You can attach digital inputs (switches and pushbuttons) and digital outputs (LEDs, etc) to these pins to control them using the digitalRead() and digitalWrite() commands</li>
<li><strong>Analog input pins</strong> You can attach sensors that produce a changing analog voltage to these pins and read them using the analogRead() command. Potentiometers, light sensors, distance sensors, and force sensors are good examples of this kind of sensor.</li>
<li><strong>SPI pins</strong> The pins labeled MISO, MOSI, and SCK are for synchronous serial communication using the SPI protocol. They control the SD card. You shouldn't use them for other inputs or outputs</li>
<li><strong>I2C pins</strong> The pins labeled SDA and SCL are for synchronous serial communications using the I2C protocol. These are used for communicating with sensors like the LIS3DH accelerometer and others.</li>
</ul>
<a href="images/adafruit_products_p2796.png"><img src="images/adafruit_products_p2796.png" alt="Feather M0 Adalogger pin diagram" width="640"/></a>
<em>Click the image to enlarge. For more on the pin functions, see <a href="https://learn.adafruit.com/adafruit-feather-m0-adalogger?view=all#pinouts">this link.</a></em>

To set up the board, you need to download the Arduino IDE version 1.6.7 and install the boards definition as shown on the <a href="brown-workshop.html#getting-started">intro page for the workshop</a>.
<h3>Setting up the board</h3>

<a href="images/adafruit_products_p2796.png"><img src="images/adafruit_products_p2796.png" alt="Feather M0 Adalogger pin diagram" width="800"/></a>
<em>Click the image to enlarge</em>
<p>To set up the board, you need to download the Arduino IDE version 1.6.7 and install the boards definition as shown on the <a href="brown-workshop.html#getting-started">intro page for the workshop</a>. Once you've got the necessary software installed, plug the board into a soldereless breadboard as shown below, then plug it into your computer's USB port. </p>


</section>
Expand Down
Binary file added images/56B457F03AD2654E.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 20e2ce4

Please sign in to comment.