-->
Page 1 of 2

Led Flickering with analogeWrite and ds18B20 reading

PostPosted: Thu Feb 18, 2016 5:21 am
by MajorMadness
Hi there,
I found a strange behavior while working with some Led and Temperature readings. Basically I have a dimmed Led and reading out the Temperature:
Code: Select allfloat getTemp(OneWire ow) {
  byte present = 0;
  byte data[12];
  byte addr[8];
  ow.search(addr);

  ow.reset();
  ow.select(addr);
  ow.write(0x44, 1);
  present = ow.reset();
  ow.select(addr);
  ow.write(0xBE);
  for (byte i = 0; i < 9; i++) {
    data[i] = ow.read();
  }
  int16_t raw = (data[1] << 8) | data[0];

  return (float)raw / 16.0; //celsius;
}

Code: Select all#include <OneWire.h>
float curTemp = 0;
#define PIN_TEMP 0 // GPIO0
#define PIN_PWM 13 // N-Channel Mosfet CC Driver
OneWire  ds(PIN_TEMP);
void loop()
{
        analogWrite(PIN_PWM, 900);
  if (currentMillis - last_print > 1000) {
    last_print = currentMillis;
      curTemp = getTemp(ds);
      Serial.println(curTemp);
}
}

(Code is minimized to explain.)
So now, my Led is flickering every second. If I comment out "curTemp = getTemp(ds);" everything works fine. I use same code on Arduino as well without problem so I can't see why. Maybe it's just a small and stupid thing I'm not seeing, so maybe someone can help me.

Greetings

Re: Led Flickering with analogeWrite and ds18B20 reading

PostPosted: Thu Feb 18, 2016 8:23 am
by eduperez
The ESP-8266 does not have a hardware PWM, it is software-based, and the code reading the temperature is probably interfering with it.

Re: Led Flickering with analogeWrite and ds18B20 reading

PostPosted: Thu Feb 18, 2016 8:33 am
by MajorMadness
This is what I know and think too, but maybe there is a solution to avoid this.

Re: Led Flickering with analogeWrite and ds18B20 reading

PostPosted: Thu Feb 18, 2016 11:03 am
by xtal
if you are using parasitic DS18B20 then there is a 750ms+ delay reading
Try a non parasitic device...