Hack a Rain Gauge - Project Stage 4

Home Basic Guides Projects Blog

BOARD AND SHIELD

Here is the board, shield and power pack combined with a 9v DC power pack.  The enclosure is a standard kitchen box for sandwich.  These are cheap and pretty water tight.

ALL CONNECTED

Nearly there.  Now we can see the Arduino and SD card shield connected in to the rain gauge.

SHIELD CLOSE-Up

There is a close up showing all the connection we need.  The yellow wire is from the Reed switch to Pin 9 and the Black to ground.  The two small red jumpers work the LEDs on the shield.

Stage 2 Prototype

Now we are happy that we can track the movement of the tipping bucket the next stage is to record the data in an appropriate way.  I am going to use the Adafruit SD card shield.  I really like it.  It is simple to use, comes with an excellent getting started tutorial and has never let me down.
If you want to know all about setting up the shield - follow the link below to the Adafruit website

https://learn.adafruit.com/adafruit-data-logger-shield

A – Set up the Adafruit shield

B– Solder the wires to the shield for the Reed switch and for the LED on the shield

C – Install the Arduino and shield in a project box (which needs to be water proof!).  Feed the wires to the Reed switch through drilled blank grommets to ensure that the enclosure remains water proof.  In this example I have used a 6mm drill bit and 6.2mm blank grommets that I have subsequently drill and hole through that is slightly smaller than the diameter of the Standard 22 AWG hook-up wire.  I like to use terminal blocks to connect the wires.  This allows me to disconnect everything without having to cut the wires and re-solder them.  

D – Up load the code and test the unit.
The code is given below and a basic code walk-through is included as comments.  Don’t forget to download the SD, Wire and RTCLib libraries if you don’t already have them..
To download the code from Github Click here

https://github.com/flowrc/Website-downloads/tree/master

Hopefully if you are following this you will end up with the set-up is shown in the adjacent photos.  (You will see a couple of additional wires.  We’ll

/*

Arduino sketch to record the switching of the reed switch in the rain gauge

The sketch work by counting the number of times the reed switch has changed position

The sketch has been extended to include code to log the analogue reading from the rain detector

Version XB - code has been reduce to remove the rain sensor functionality

*/

#include <SD.h>          

#include <Wire.h>        //Remember no semi-colon after these commands

#include "RTClib.h"

const int REED = 9;     //The reed switch outputs to digital pin 9

int val = 0;            //Current value of reed switch

int old_val = 0;        //Old value of reed switch

int REEDCOUNT = 0;      //The intial count is zero


const int chipSelect = 10;

File logfile;

#define SYNC_INTERVAL 60000

uint32_t syncTime = 0; // time of last sync()


const int redLEDpin = 3;

const int greenLEDpin = 2;


RTC_DS1307 RTC; // define the Real Time Clock object


//Program Set-up//////////////////////////////////////////////////////


void setup(void){

 Serial.begin(9600);

 pinMode (REED, INPUT_PULLUP);  //Activate the internal pull-up resistor

 pinMode(greenLEDpin, OUTPUT);

 pinMode(redLEDpin, OUTPUT);


//Setting up the file on the SD Card///////////////////////////////


 Serial.println("Initializing SD card...");

 pinMode(10, OUTPUT);  // make sure that the default chip select pin is set to output


 // see if the card is present and can be initialized:

 if (!SD.begin(chipSelect)) {

   Serial.println("Card failed, or not present");

 }

 Serial.println("card initialized.");


 // create a new file

 char filename[] = "LOGGER00.CSV";      //The file names must be in the 8.3 format

 for (uint8_t i = 0; i < 100; i++) {

   filename[6] = i/10 + '0';

   filename[7] = i%10 + '0';

   if (! SD.exists(filename)) {

     // only open a new file if it doesn't exist

     logfile = SD.open(filename, FILE_WRITE);

      break;  // leave the loop!

   }

 }


 if (! logfile) {

   Serial.println("could not create file");

 }


 Serial.print("Logging to: ");

 Serial.println(filename);


 //Connect to RTC

 Wire.begin();

 if (!RTC.begin()) {

   logfile.println("RTC failed");

 }

 Serial.println("channel, Reading/count, date, time");

 logfile.println("channel, Reading/count, date, time");  


//The Program Loop//////////////////////////////////////////////


void loop(){

 DateTime now;    // and record date/time of change

 val = digitalRead(REED);  //Low means that the Reed switch is open

 if ((val == LOW) && (old_val == HIGH)){

   delay(10);    // Delay put in to deal with any "bouncing" in the switch.

   REEDCOUNT = REEDCOUNT + 1;

   now = RTC.now();

   old_val = val;

   digitalWrite(redLEDpin, HIGH);

   char buf1[50];

   Serial.print("Reedcount");

   Serial.print(", ");

   Serial.print(REEDCOUNT);

   Serial.print(", ");

   sprintf(buf1, "%02d/%02d/%02d, %02d:%02d:%02d", now.day(), now.month(), now.year(), now.hour(), now.minute(), now.second());

   Serial.println(buf1);


   logfile.print("Reedcount");

   logfile.print(", ");

   logfile.print(REEDCOUNT);

   logfile.print(", ");

   logfile.println(buf1);

   digitalWrite(redLEDpin, LOW);

 }


 else {

   old_val = val;

 }


 if ((millis() - syncTime) < SYNC_INTERVAL) return;

 digitalWrite(greenLEDpin, HIGH);

 delay(10);

 logfile.flush();

 digitalWrite(greenLEDpin, LOW);

 syncTime = millis();

}


Next Step

© Flow RC June 2014

Disclaimer:  Flow RC cannot accept any liability for any mishaps that you may have if you attempt to copy these projects.  Please read our terms and conditions.


Please contact us at: feedback@flowrc.co.uk