![]() |
![]() |
#51 |
Lurking Renovator
Join Date: Dec 2014
Location: USA
Posts: 13
Thanks: 1
Thanked 0 Times in 0 Posts
|
![]() Just stumbled across this thread and I have a question.
What is the purpose of dividing by 2: "Ssensor = analogRead(SsensorPin) / 2;" "Tsensor = analogRead(TsensorPin) / 2;" I am using LM34s and anticipating degF readings; I am getting strange results. |
![]() |
![]() |
![]() |
#52 |
Administrator
Join Date: Aug 2008
Location: Germantown, WI
Posts: 5,525
Thanks: 1,162
Thanked 374 Times in 305 Posts
|
![]() Hello nluck. Welcome to the site.
![]() The LM34 will output 10mV per degree F (the LM35 does the same, but in degrees C). Each 'step' (aka the number returned by the analog read function) on the arduino is roughly 5mV. So, 2 steps = 1 degree. Thus, I divide steps by 2 and your variables Ssensor and Tsensor are now in degrees.
__________________
Current project - To view links or images in signatures your post count must be 0 or greater. You currently have 0 posts. To view links or images in signatures your post count must be 0 or greater. You currently have 0 posts. & To view links or images in signatures your post count must be 0 or greater. You currently have 0 posts. |
![]() |
![]() |
![]() |
#53 |
Administrator
Join Date: Aug 2008
Location: Germantown, WI
Posts: 5,525
Thanks: 1,162
Thanked 374 Times in 305 Posts
|
![]() Haha, I totally forgot to update this thread. The new software smoothing worked very well. It still wasn't completely flawless, but it was much better. I'll have to grab that code when I'm home and post it up.
__________________
Current project - To view links or images in signatures your post count must be 0 or greater. You currently have 0 posts. To view links or images in signatures your post count must be 0 or greater. You currently have 0 posts. & To view links or images in signatures your post count must be 0 or greater. You currently have 0 posts. |
![]() |
![]() |
![]() |
#54 |
Administrator
Join Date: Aug 2008
Location: Germantown, WI
Posts: 5,525
Thanks: 1,162
Thanked 374 Times in 305 Posts
|
![]() Aha, found it after all.
Code:
/* Thermal Differential Controller The program monitors two temperature sensors and activates a relay when one sensor is warmer than the other. */ // Pins int SsensorPin = 4; // the pin to read solar panel temperature int TsensorPin = 5; // the pin to read water tank temperature int RelayPin = 2; // the pin to control the pump relay int ledPin = 13; // led pin to verify relay on condition // Variables int Ssensor = 0; // holds the value from SsensorPin int SensorOnDiff = 0; // holds the Ssensor value plus on differential (OnDiff) int SensorOffDiff = 0; // holds the Ssensor value plus off differential (OffDiff) int Tsensor = 0; // holds the value from TsensorPin int SensorReadings = 10; // number of sensor readings to take int Counter = 0; // sensor readings loop counter int OnDiff = 4; // temp diff that must exist to turn pump on (dec C) int OffDiff = 2; // temp diff that must exist to turn pump on (deg C) void setup() { pinMode(SsensorPin, INPUT); pinMode(TsensorPin, INPUT); pinMode(RelayPin, OUTPUT); pinMode(ledPin, OUTPUT); Serial.begin(9600); digitalWrite(RelayPin, LOW); // turn pump off } void loop() { // read sensor inputs, convert to degrees, and assign to variables Counter = 0; while(Counter < SensorReadings) { Ssensor += analogRead(SsensorPin) / 2; Tsensor += analogRead(TsensorPin) / 2; Counter++; digitalWrite(ledPin, HIGH); Delay(250); digitalWrite(ledPin, LOW); Delay(250); } // calculate average temp from sensor inputs Ssensor = Ssensor / SensorReadings; Tsensor = Tsensor / SensorReadings; // add temperature difference to Ssensor value SensorOnDiff = Ssensor - OnDiff; SensorOffDiff = Ssensor - OffDiff; // if heat source is warmer than heat sink sensor, enable relay if (SensorOnDiff > Tsensor) { digitalWrite(RelayPin, HIGH); digitalWrite(ledPin, HIGH); delay(30000); } // if heat source sensor is cooler than heat sink sensor, disable relay if (SensorOffDiff < Tsensor) { digitalWrite(RelayPin, LOW); digitalWrite(ledPin, LOW); delay(1000); } }
__________________
Current project - To view links or images in signatures your post count must be 0 or greater. You currently have 0 posts. To view links or images in signatures your post count must be 0 or greater. You currently have 0 posts. & To view links or images in signatures your post count must be 0 or greater. You currently have 0 posts. |
![]() |
![]() |
![]() |
#55 |
Lurking Renovator
Join Date: Dec 2014
Location: USA
Posts: 13
Thanks: 1
Thanked 0 Times in 0 Posts
|
![]() Hmm, thanks for the update. I am fairly new to C++ and trying to figure out some of the commands. At the moment, my pump relay (RelayPin) remains ON. Only made a few minor changes to your code (at least I think minor):
// Pins int SsensorPin = A0; // the pin to read solar panel temperature int TsensorPin = A1; // the pin to read water tank temperature int RelayPin = 2; // the pin to control the pump relay int ledPin = 13; // led pin to verify relay on condition // Variables int Ssensor = 0; // holds the value from SsensorPin int SensorOnDiff = 0; // holds the Ssensor value plus on differential (OnDiff) int SensorOffDiff = 0; // holds the Ssensor value plus off differential (OffDiff) int Tsensor = 0; // holds the value from TsensorPin int SensorReadings = 10; // number of sensor readings to take int Counter = 0; // sensor readings loop counter int OnDiff = 4; // temp diff that must exist to turn pump on (dec F) int OffDiff = 2; // temp diff that must exist to turn pump on (deg F) void setup() { pinMode(SsensorPin, INPUT); pinMode(TsensorPin, INPUT); pinMode(RelayPin, OUTPUT); pinMode(ledPin, OUTPUT); Serial.begin(9600); digitalWrite(RelayPin, LOW); // turn pump off } void loop() { // read sensor inputs, convert to degrees, and assign to variables Counter = 0; while(Counter < SensorReadings) { Ssensor = analogRead(SsensorPin) / 2; Tsensor = analogRead(TsensorPin) / 2; Counter++; digitalWrite(ledPin, HIGH); delay(250); digitalWrite(ledPin, LOW); delay(250); Serial.print(Ssensor); Serial.print(", "); Serial.print(Tsensor); Serial.print("\n"); delay(250); } // calculate average temp from sensor inputs Ssensor = Ssensor / SensorReadings; Tsensor = Tsensor / SensorReadings; // add temperature difference to Ssensor value SensorOnDiff = Ssensor - OnDiff; SensorOffDiff = Ssensor - OffDiff; // if heat source is warmer than heat sink sensor, enable relay if (SensorOnDiff > Tsensor) { digitalWrite(RelayPin, HIGH); digitalWrite(ledPin, HIGH); delay(3000); } // if heat source sensor is cooler than heat sink sensor, disable relay if (SensorOffDiff < Tsensor) { digitalWrite(RelayPin, LOW); digitalWrite(ledPin, LOW); delay(1000); } } Any help is appreciated. Thanks. |
![]() |
![]() |
![]() |
#56 | |
Administrator
Join Date: Aug 2008
Location: Germantown, WI
Posts: 5,525
Thanks: 1,162
Thanked 374 Times in 305 Posts
|
![]() Well, I see one issue:
Quote:
It should be: // read sensor inputs, convert to degrees, and assign to variables Counter = 0; while(Counter < SensorReadings) { Ssensor += analogRead(SsensorPin) / 2; Add the += here Tsensor += analogRead(TsensorPin) / 2; Add the += here Counter++; digitalWrite(ledPin, HIGH); delay(250); digitalWrite(ledPin, LOW); delay(250); Serial.print(Ssensor); Serial.print(", "); Serial.print(Tsensor); Serial.print("\n"); delay(250); } // calculate average temp from sensor inputs Ssensor = Ssensor / SensorReadings; Tsensor = Tsensor / SensorReadings; The while loop adds all of the readings together and then afterwards, the readings get divided by the number of readings taken (SensorReadings). In your code, you're not adding the readings together. Your code is just using the last reading taken, and the dividing it by the readings taken. So, if you take 3 readings and read 30, 40, 50. Your code will use 50 degrees and divide by 3 (giving 17) instead of adding 30 + 40 + 50 and dividing by 3 (giving 40). If fixing that doesn't help, I'd move the serial output after the line: Ssensor = Ssensor / SensorReadings; Tsensor = Tsensor / SensorReadings; This will show you the values that are actually being compared vs the values that are being read by the temperature sensors.
__________________
Current project - To view links or images in signatures your post count must be 0 or greater. You currently have 0 posts. To view links or images in signatures your post count must be 0 or greater. You currently have 0 posts. & To view links or images in signatures your post count must be 0 or greater. You currently have 0 posts. |
|
![]() |
![]() |
![]() |
#57 |
Lurking Renovator
Join Date: Dec 2014
Location: USA
Posts: 13
Thanks: 1
Thanked 0 Times in 0 Posts
|
![]() Almost everything works well, except the relayPin starts HIGH. The temp readings are uniformly higher by about 12-15 degF than they should be. Any suggested corrections? Thanks for the help. My code follows:
// Pins int SsensorPin = 4; // the pin to read solar panel temperature int TsensorPin = 5; // the pin to read water tank temperature int RelayPin = 2; // the pin to control the pump relay int ledPin = 13; // led pin to verify relay on condition // Variables int Ssensor = 0; // holds the value from SsensorPin int SensorOnDiff = 0; // holds the Ssensor value plus on differential (OnDiff) int SensorOffDiff = 0; // holds the Ssensor value plus off differential (OffDiff) int Tsensor = 0; // holds the value from TsensorPin int SensorReadings = 10; // number of sensor readings to take int Counter = 0; // sensor readings loop counter int OnDiff = 4; // temp diff that must exist to turn pump on (dec F) int OffDiff = 2; // temp diff that must exist to turn pump off (deg F) void setup() { pinMode(SsensorPin, INPUT); pinMode(TsensorPin, INPUT); pinMode(RelayPin, OUTPUT); pinMode(ledPin, OUTPUT); Serial.begin(9600); digitalWrite(RelayPin, LOW); // turn pump off } void loop() { // read sensor inputs, convert to degrees, and assign to variables Counter = 0; while(Counter < SensorReadings) { Ssensor += analogRead(SsensorPin) / 2; Tsensor += analogRead(TsensorPin) / 2; Counter++; digitalWrite(ledPin, LOW); delay(250); digitalWrite(ledPin, LOW); delay(250); } // calculate average temp from sensor inputs Ssensor = Ssensor / SensorReadings; Tsensor = Tsensor / SensorReadings; // add temperature difference to Ssensor value SensorOnDiff = Ssensor - OnDiff; SensorOffDiff = Ssensor - OffDiff; Serial.print("Solar temp= "); Serial.println(Ssensor); Serial.print("Tank temp= "); Serial.println(Tsensor); // if heat source is warmer than heat sink sensor, enable relay if (SensorOnDiff > Tsensor) { digitalWrite(RelayPin, HIGH); digitalWrite(ledPin, HIGH); delay(3000); } // if heat source sensor is cooler than heat sink sensor, disable relay if (SensorOffDiff < Tsensor) { digitalWrite(RelayPin, LOW); digitalWrite(ledPin, LOW); delay(500); } } |
![]() |
![]() |
![]() |
#58 |
Lurking Renovator
Join Date: Dec 2014
Location: USA
Posts: 13
Thanks: 1
Thanked 0 Times in 0 Posts
|
![]() Got the output working, now to adjust the temp readings.
|
![]() |
![]() |
![]() |
#59 |
Administrator
Join Date: Aug 2008
Location: Germantown, WI
Posts: 5,525
Thanks: 1,162
Thanked 374 Times in 305 Posts
|
![]() It doesn't really matter if both sensors are off as long as they're off the same amount and its consistent. You're only measuring the differential temperature, so they only need to be accurate to each other. If this bugs you, you can do some sort of manual calibration to dial them in closer.
When I tested mine, I put a short lead on them, put them right next to each other for a half hour and read the serial output. They were probably within a degree C of each other and that was close enough for my application.
__________________
Current project - To view links or images in signatures your post count must be 0 or greater. You currently have 0 posts. To view links or images in signatures your post count must be 0 or greater. You currently have 0 posts. & To view links or images in signatures your post count must be 0 or greater. You currently have 0 posts. |
![]() |
![]() |
![]() |
#60 |
Lurking Renovator
Join Date: Dec 2014
Location: USA
Posts: 13
Thanks: 1
Thanked 0 Times in 0 Posts
|
![]() Yes, I agree with the differential being the important item. Just prefer an accurate representation of the actual temp readings as it seems important to me. I will try to tweak them. Thanks for the reply.
|
![]() |
![]() |
![]() |
Tags |
arduino, controller, differential, thermal |
Thread Tools | |
Display Modes | |
|
|