Friday, June 14, 2013

Volt Meter & Temperature Monitor - Part 2

I was unhappy with the previous version of this project, which used a TMP36 analog temperature sensor. The output moved around alot, up to 5 degrees, and wouldn't stabilize. I replaced it with my favorite, the DS18B20 digital sensor. Now I have rock solid temperature readings, rock solid voltage measurement, and everyone is happy.

Since my waterproof DS18B20 is already wired, I wanted to keep the leads together, so I wrote a LOW to D2 to make it the Gnd, and a HIGH to D6 to make it +5vdc to power the DS18B20. A 4.7k resistor goes between data (D3) and +5vdc (D6).

This version measure 0-29vac. To measure DC, remove bridge rectifier and capacitor.


See the changes below:



#include "OneWire.h"
#include "DallasTemperature.h"
#include "LiquidCrystal.h"
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);

// Data wire is plugged into pin 3 on the Arduino
#define ONE_WIRE_BUS 3

// Setup a oneWire instance to communicate with any OneWire devices
OneWire oneWire(ONE_WIRE_BUS);

// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);

DeviceAddress insideThermometer = { 0x28, 0x27, 0xB0, 0xBD, 0x04, 0x00, 0x00, 0xF0 };

int tempNegPin = 2; // Gnd for DS18B20
int tempPosPin = 6;  // +5vdc for DS18B20

int voltPin = 0;     // voltage divider (middle terminal) connected to analog pin 0
                       // outside leads to 0-29vac
int val = 0;           // variable to store the value read
int volt = 0;           // variable to store the voltage calculated


void setup(void)
{
  // Used two unused digital ports as +5v and Gnd for the DS18B20
  pinMode(tempPosPin, OUTPUT);      // sets the digital pin as output
  pinMode(tempNegPin, OUTPUT);      // sets the digital pin as output
  digitalWrite(tempPosPin, HIGH);       // sets digital pin to +5vdc
  digitalWrite(tempNegPin, LOW);     // sets digital pin to Gnd

  lcd.begin(20, 4);            // setup LCD

  // Start up the library
  sensors.begin();
  // set the resolution to 10 bit (good enough?)
  sensors.setResolution(insideThermometer, 10);

}

void printTemperature(DeviceAddress deviceAddress)
{
  float tempC = sensors.getTempC(deviceAddress);
  if (tempC == -127.00) {
    lcd.setCursor(0, 2);
    lcd.print("Error getting");
    lcd.setCursor(0, 3);
    lcd.print("temperature");

  } else {
    lcd.setCursor(0, 2);
    lcd.print("Temp ");
    lcd.print((char)223);
    lcd.print("C: ");
    lcd.print(tempC);
    lcd.setCursor(0, 3);
    lcd.print("Temp ");
    lcd.print((char)223);
    lcd.print("F: ");
    lcd.print(DallasTemperature::toFahrenheit(tempC));

  }
}

void loop(void)
{
  delay(500);

   val = analogRead(voltPin);    // read the input pin

   volt = map(val, 0, 1023, 0, 29); // map 29v range

   lcd.setCursor(0, 0);
   lcd.print("VAC  ");
   if (volt<10) lcd.print=(" ");
   lcd.print(volt);

  sensors.requestTemperatures();

  printTemperature(insideThermometer);

}