Input Devices

1. Measure something by adding a sensor to a microcontroller board.
2. Read the sensor data

Phototransistor with SoftwareSerial

I finally understood SoftwareSerial and managed to get it working properly. I created a new iteration of the helloworld board with a phototransistor.

#include <SoftwareSerial.h>

#define rxPin 0
#define txPin 1
#define ledPin 7
#define photoPin 2
#define buttonPin 3

SoftwareSerial mySerial(rxPin, txPin);

boolean buttonState = HIGH;
int light = 0;

void setup() {
  mySerial.begin(4800);
  mySerial.println("Initiating...");
  pinMode(ledPin, OUTPUT);
  pinMode(buttonPin, INPUT);
  pinMode(photoPin, INPUT);
}

void loop() {
  if (buttonState != digitalRead(buttonPin)) {
    buttonState = digitalRead(buttonPin);
    if (buttonState == HIGH) {
      mySerial.println("BUTTON RELEASED");
    } else {
      mySerial.println("BUTTON PRESSED");
    }
    digitalWrite(ledPin, !buttonState);

    int light = analogRead(photoPin);
    mySerial.println( light );
  }
}

BeeShield™

In preparation for my revised final project I decided to record temperature and humidity measurements. Initially I prototyped my design with an Arduino and then later made my own board based off the Hello World board.

I2C Protocol

I also wanted to try and learn about the I2C protocol, so I configured two arduinos and ran code on them as Master-Slave and Slave-Master. I wanted to access the readings from a Temperature/Humidity sensor (DHT-22).

Sending multiple Floats was difficult over I2C but I found some simple methods that enable you to encapsulate them into a struct and then it reads the individual bytes and sends them down the Wire.

#include <Arduino.h>
#include <Wire.h>

template <typename T> int I2C_writeAnything (const T& value)
  {
    const byte * p = (const byte*) &value;
    unsigned int i;
    for (i = 0; i < sizeof value; i++)
          Wire.write(*p++);
    return i;
  }  // end of I2C_writeAnything

template <typename T> int I2C_readAnything(T& value)
  {
    byte * p = (byte*) &value;
    unsigned int i;
    for (i = 0; i < sizeof value; i++)
          *p++ = Wire.read();
    return i;
  }  // end of I2C_readAnything

Board Design Iterations

The BeeShield went through several iterations before arriving at a final design. The latest version is designed to work with the DS18B20 Digital Temperature Sensor and the DHT22 Temperature and Humidity sensor.

Final Board Design

All of these design and code files can be found on the final project page.

We quickly found that despite claims to the contrary, our DHT-22 was NOT compatible with 3.3V using the standard libraries. However, after some experiments we discovered that if the 3rd pin is connected to ground -the datasheet suggests leaving it floating- it is possible to get data readings from it on a 3.3V circuit, albeit very slowly (once every two seconds max).

Programming the Microcontroller

#include "DHT.h"
DHT dht(13, DHT22);
#include <Wire.h>
#include <RTClib.h>
RTC_Millis RTC;
#include <OneWire.h>
#include <DallasTemperature.h>
OneWire oneWire0(5);
DallasTemperature sensor0(&oneWire0);
OneWire oneWire1(9);
DallasTemperature sensor1(&oneWire1);
OneWire oneWire2(10);
DallasTemperature sensor2(&oneWire2);
#include <SD.h>
File myFile;
void setup() {
  Serial.begin(9600);

  while (!Serial);
  if (!SD.begin(11)) {
    Serial.println("SD initialization failed!");
    return;
  }
  Serial.println("SD initialization done.");

  sensor0.begin();
  sensor1.begin();
  sensor2.begin();
  RTC.begin(DateTime(__DATE__, __TIME__));
}
void loop () {
  float h = dht.readHumidity();
  float t = dht.readTemperature();
  DateTime now = RTC.now();
  String filename = "";
  String month = String(now.month());
  if (now.month() < 10) {
    month = "0" + month;
  }
  String day = String(now.day());
  if (now.day() < 10) {
    day = "0" + day;
  }
  String hour = String(now.hour());
  if (now.hour() < 10) {
    hour = "0" + hour;
  }
  filename += month;
  filename += day;
  filename += hour;
  filename += ".csv";
  char __dataFileName[filename.length() + 1];
  filename.toCharArray(__dataFileName, sizeof(__dataFileName));
  Serial.println("");
  Serial.println(__dataFileName);

  myFile = SD.open(__dataFileName, FILE_WRITE);
  if (myFile) {
    sensor0.requestTemperatures();
    sensor1.requestTemperatures();
    sensor2.requestTemperatures();
    Serial.println(sensor0.getTempCByIndex(0));
    Serial.println(sensor1.getTempCByIndex(0));
    Serial.println(sensor2.getTempCByIndex(0));
    Serial.println(t);
    Serial.println(h);
    myFile.print(sensor0.getTempCByIndex(0));
    myFile.print(",");
    myFile.print(sensor1.getTempCByIndex(0));
    myFile.print(",");
    myFile.print(sensor2.getTempCByIndex(0));
    myFile.print(",");
    myFile.print(t);
    myFile.print(",");
    myFile.print(h);
    myFile.print(",");
    myFile.println(now.get());
  } else {
    Serial.println("FAIL");
  }
  myFile.close();
  delay(120000);
};