0

I am using an Arduino Due board to read three temperatures from three separate smt160-30 digital sensors but the program is behaving very randomly. the code I am using is one I got from arduino official site and changed the structure to get three temp readings and display them on a 16x2 lcd. The problem is when I use one sensor the output is reasonable but when I use all three the temperature showing are all off by 4 degrees. also I tried removing the line lcd.print(temperature1, 1) and the output temperatures changed. Is this odd behavior because of SRAM issues ? if so how can I find out about it and fix it ?

#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

unsigned long now;
unsigned long lastSample;
unsigned int sampleSize;

unsigned int highCount1;
unsigned int highCount2;
unsigned int highCount3;

unsigned long lastOutput;

float temperature1;
float temperature2;
float temperature3;
float hcf1, hcf2, hcf3;
float ssf;

int sensorPin1 = 23;
int sensorPin2 = 24;
int sensorPin3 = 26;

void setup(){

    pinMode(sensorPin1, INPUT);
    pinMode(sensorPin2, INPUT);
    pinMode(sensorPin3, INPUT);

    digitalWrite(sensorPin1, LOW);
    digitalWrite(sensorPin2, LOW);
    digitalWrite(sensorPin3, LOW);

    lastSample = 0;
    sampleSize = 0;

    highCount1 = 0;
    highCount2 = 0;
    highCount3 = 0;

    temperature1 = 0;
    temperature2 = 0;
    temperature3 = 0;

    lastOutput = 0;

    delay(2000);
    lcd.setCursor(0, 0);

}

void loop() {
    now = micros();
    if (now - lastOutput > 3000000) {
        lcd.clear();

        hcf1 = highCount1;
        hcf2 = highCount2;
        hcf3 = highCount3;

        ssf = sampleSize;

        temperature1 = ((hcf1 / ssf) -0.32)/0.0047;
        temperature2 = ((hcf2 / ssf) -0.32)/0.0047;
        temperature3 = ((hcf3 / ssf) -0.32)/0.0047;

        lastOutput = now;
        sampleSize = 0;

        highCount1 = 0;
        highCount2 = 0;
        highCount3 = 0;


        lcd.print(temperature1, 1);
        lcd.print(" ");

        lcd.print(temperature2, 1);
        lcd.print(" ");

        lcd.print(temperature3, 1);

    }
    if (now - lastSample > 50) {
        sampleSize++;
        highCount1 = highCount1 + digitalRead(sensorPin1);
        highCount2 = highCount2 + digitalRead(sensorPin2);
        highCount3 = highCount3 + digitalRead(sensorPin3);
        lastSample = now;
    } else {
        delayMicroseconds(10);
    }
}

Thank you!

1

Did you read about this smt160 sensor ? It relies on the calculation of duty cycle of the square wave output from the sensor, to calculate the temparature.

temperature = (dutyCycle) -0.32)/0.0047

The square wave output has to be sampled at 8KHz or at 125us for no loss of information. This block samples that data.

if (now - lastSample > 50) {

sampleSize++;

highCount1 = highCount1 + digitalRead(sensorPin1);

highCount2 = highCount2 + digitalRead(sensorPin2);

highCount3 = highCount3 + digitalRead(sensorPin3);

lastSample = now; }

else { delayMicroseconds(10); }

when you increased the number of code lines , information loss also increased, cz more number of instructions have to be executed now , till reaching the corresponding digital reads.

  • So if I put it in a for loop to guarantee the needed sample it should work right ? is there better way to do this ? thank you for responding – Meysam Afghani Nov 12 '17 at 14:11
  • I don't think so. This is already running in loop under void loop(). So It may still have the same problem. – Mitu Raj Nov 12 '17 at 14:14
  • One solution is to shift the start of sampling of three sensors. Divide the time frame into 3 parts. First time frame only for sampling and displaying the temp of sensor 1, second one only for second sensor, first sensor would be idle. Like that. But the temperature update interval will increase. – Mitu Raj Nov 12 '17 at 14:20
  • I don't understand how to divide it into different time frames. Is it possible to create a function for getting temperature and call it three times for each sensor ? if so how do I write the function where is the exit point of it ? – Meysam Afghani Nov 12 '17 at 14:34
  • You have to code some logic using if conditions and a variable to keep track of time passed. Based on the value of that variable , call functions for processing sensor 1, 2 or 3. – Mitu Raj Nov 12 '17 at 14:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.