Thursday, August 4, 2011

Arduino + Ethernet Shield + Temperature 2

After monitoring the thermometer for a couple of hours, it turned out the temperature sensor is a bit off. About 2.5 degrees.
At least compared to me being cold and a good old HG thermometer.

Now this meant we need some improvement in our little thermometer or better, let's find a way to calibrate it.

So looking around in the tool box I quickly discovered a potentiometer and some more cables and thought, we can wire this to another pin and scale it to +/- 5 degree celsius and apply this correction.

This is how it roughly looks now:

And the new code to apply the correction settings is here

#include <SPI.h>
#include <Ethernet.h>

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192,168,1, 2 };

Server server(80);

float coreVoltage = 5.0;
float correctionScale = 10; //in degree celsius +/- half scale

void setup()
// start the Ethernet connection and the server:
Ethernet.begin(mac, ip);

void loop()
// listen for incoming clients
Client client = server.available();
if (client) {
// an http request ends with a blank line
boolean currentLineIsBlank = true;
while (client.connected()) {
if (client.available()) {
char c =;
// if you've gotten to the end of the line (received a newline
// character) and the line is blank, the http request has ended,
// so you can send a reply
if (c == '\n' && currentLineIsBlank) {
// send a standard http response header
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: application/json");

// output the value of each analog input pin

//read twice to improve precission
float voltageSensor = analogRead(0);
voltageSensor = analogRead(0) * coreVoltage;

//get temperature of the potentio meter
float voltagePotentioMeter = analogRead(1);
voltagePotentioMeter = analogRead(1)*coreVoltage/1000;

float debugVotlage = voltagePotentioMeter;

//scale it to +/- 1/2 coreVoltage
voltagePotentioMeter = voltagePotentioMeter - (coreVoltage/2);

//calculate the correction value in celsius, based on our scale
float correctionValue = voltagePotentioMeter * correctionScale / coreVoltage ;

voltageSensor /= 1024.0;

//temperature calculation
float temperatureC = (voltageSensor - 0.5) * 100 + correctionValue; //converting from 10 mv per degree wit 500 mV offset
float temperatureF = (temperatureC * 9.0 / 5.0) + 32.0;

client.print("sensor {");









if (c == '\n') {
// you're starting a new line
currentLineIsBlank = true;
else if (c != '\r') {
// you've gotten a character on the current line
currentLineIsBlank = false;
// give the web browser time to receive the data
// close the connection:

Also since we want to keep track of the correction values, we updated the expected json string a bit to reflect the changes in our implementation.

sensor {temperature_voltage:0.75,temperature_celsius:21.98,temperature_fahrenheit:71.56,temperature_correction:-3.22,voltage_potentiometer:0.89}

I have to say, I'm really start to enjoy playing with this little thing. The next step should be have the transmission of several sensors done wirelessly over XBees and to send them to one central location...

No comments:

Post a Comment