pulseIn() alyway shows 0

Hi,

i am sure it’s because i am still beginner so i hesitate to ask stupid questions here. But After maybe 5 hours reading and trying. I hope someone can push me in the right direction.

A 22Ohm resistor is connected between D7 and GND. This resistor is glued to a Dallas temperature sensor. So when D7 is HIGH the resistor heats up the temperature sensor till 1 degrees hotter than a second temp-sensor. That all works fine already.
But i need to measure how long the resistor was ON during the heating process. Which always results in 0 :frowning:

Here is my code:

#define MY_DEBUG
#include <SPI.h>
#include <OneWire.h>
#include <DallasTemperature.h>

// Data wire is plugged into port 2 on the Arduino 
#define ONE_WIRE_BUS 10
#define TEMPERATURE_PRECISION 10 

int ledPin = 7; 
unsigned long duration;
 
// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs) 
OneWire oneWire(ONE_WIRE_BUS); 

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

DeviceAddress  temp1 = { 0x28, 0xFF, 0xD0, 0xAB, 0x62, 0x16, 0x03, 0x65 };
DeviceAddress  temp2 = { 0x28, 0xFF, 0xB0, 0x6C, 0x63, 0x16, 0x03, 0x60 };

void setup(void) 
{

    pinMode(ledPin, OUTPUT);      // sets the digital pin as output
    
  // start serial port
  Serial.begin(115200);

  // report parasite power requirements
  Serial.print("Parasite power is: "); 
  if (sensors.isParasitePowerMode()) Serial.println("ON");
  else Serial.println("OFF");

  // set the resolution to 9 bit
  sensors.setResolution(temp1, TEMPERATURE_PRECISION);
  sensors.setResolution(temp2, TEMPERATURE_PRECISION);

}

void loop()
{  
    // call sensors.requestTemperatures() to issue a global temperature 
  // request to all devices on the bus
  sensors.requestTemperatures();
  Serial.println("Requesting temperatures...DONE");

  float in = sensors.getTempC(temp1);
  Serial.print("In  C: ");
  Serial.println(in);

  float out = sensors.getTempC(temp2);
  Serial.print("Out  C: ");
  Serial.println(out);
  

  duration = pulseIn(ledPin, HIGH);

  digitalWrite(ledPin, LOW);
  
  while (in < (out + 1)) {
     digitalWrite(ledPin, HIGH);
     Serial.println("Heating Up...");
     sensors.requestTemperatures();
     in = sensors.getTempC(temp1);
     out = sensors.getTempC(temp2);
     Serial.print(" In Temp: ");
     Serial.println(in);
     delay(1000);
    }

    Serial.print("Was HIGH for:");
    Serial.println(duration);

     digitalWrite(ledPin, LOW);
     Serial.println("Heating OFF");
  
  delay (5000);
}

Don’t be too hard with me :wink:

Forget about pulseIn() and do the timing yourself.

When you turn on the heater save the value of millis() to a variable.
When you turn off the heater save the value of millis() to a variable.

Subtract one value from the other and you have the duration.

But i need to measure how long the resistor was ON during the heating process.

pulseIn() waits for the pin to change from one state to another. It expects the pin to be controlled by something outside of the Arduino. It also expects the pin to be an INPUT pin.

Since neither condition is true, pulseIn() is the wrong function to be using. UKHeliBob provided the right answer.

You guys are great. Made my day :slight_smile: