In my current project I'm using a DS18B20, which seems to be the only temperature sensor to show up when googling for waterproof temperature sensor for an arduino.
The sensor itself is fine, however, I'm outputting the sensor's data to a SPI screen, and whenever the sensor is called to read data I believe the SPI communication is interrupted because of 1-Wire, and you can see a white flicker on the screen. The background LED is flashing more brightly for one frame. It's almost unnoticeable, but it bothers me.
I'm not using delays, and I'm not redrawing unnecessary elements on the screen again. Only dynamic elements are refreshed when they need to, static elements are only drawn once on the screen. The sensor is called every 5 seconds with a comparison:
if(currentTime - lastTimeQueried >= delay){
lastTimeQueried = currentTime;
temperature = new temperature;
return temperature;
} else {
return temperature;
}
I also tried a function where I'm constantly calling the sensor and only drawing on the screen if there's a change in temperature, but then the screen is flickering all the time. Thus I conclude that the flickering is caused by the actual calling of the sensor, and not from redrawing anything on the screen.
So I'm wondering if there's an alternative to the DS18B20 that communicates via SPI, and that I can use in an aquarium, so I don't have to use both protocols in my project.
Edit:
As requested some more of my code and wiring:
The screen is wired to the ICSP headers on my Arduino Mega 2560 and power is supplied via 5V on a powersupply that is connected to my breadboard.
The DS18B20 is wired to pin 29 on the Arduino and is also supplied with power via 5V on the same powersupply.
//TFT SPI
#define TFT_DC 9
#define TFT_CS 10
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);
//SD Card Reader SPI
//#define SD_CS 4
//Touchscreen I2C
UTouch myTouch(5, 8, 6, 7, 2); //(T_CLK, T_CS, T_DIN, T_DOUT, T_IRQ)
#define ONE_WIRE_BUS 29
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
Here are some more variables that I have defined to help with using the sensor:
//Temperature measurement
unsigned long lastTempMeasure; //Holds last unix timestamp temperature was measured at
char tempMeasureDelay = 5; //Call temperature every 5 seconds
float sensorTemp; //Holds measured temperature
In my loop I then use this function:
tft.print(temperatureCall());
and then:
float temperatureCall(){
//get unix time for right now
unsigned long currentUnix = now();
//if 5 seconds passed since last poll, ask for new temperature, else return old measurement
if(currentUnix - lastTempMeasure >= tempMeasureDelay){
lastTempMeasure = currentUnix; //save last polling time
sensors.requestTemperatures();
sensorTemp = sensors.getTempCByIndex(0);
return sensorTemp;
} else {
return sensorTemp;
}
}
As I said, the flicker only occurs when these two are used every 5 seconds:
sensors.requestTemperatures();
sensorTemp = sensors.getTempCByIndex(0);
Edit 2:
Pic of wiring:
-
Can you share your wiring including LCD display over SPI and DS18B20 and some more of your code?Sener– Sener2018年09月07日 10:47:54 +00:00Commented Sep 7, 2018 at 10:47
-
@Sener I have updated my postStreamline– Streamline2018年09月07日 11:58:59 +00:00Commented Sep 7, 2018 at 11:58
-
3It sounds more like a power issue rather than a communication issue. How are you powering the backlight for the display?Majenko– Majenko2018年09月07日 12:10:11 +00:00Commented Sep 7, 2018 at 12:10
-
1What I have also thought of that it might indeed be a powering issue since the sensor is using a pullup resistor on the data line which is connected to 5V. @Majenko the backlight LED is powered by the powersupply via 5V. I will update my post with a picture of the wiringStreamline– Streamline2018年09月07日 12:23:11 +00:00Commented Sep 7, 2018 at 12:23
-
1Any time. A last note, you may try to place a 10uF Electrolytic on your power rail between GND and 5V. This can buffer (buy) some time when power is affected during sensor reading. Good luck with your project.Sener– Sener2018年09月07日 13:06:02 +00:00Commented Sep 7, 2018 at 13:06
1 Answer 1
There is a delay in the DallasTemperature requestTemperatures()
function - it blocks until the temperature conversion is complete unless you have previously called setWaitForConversion(FALSE)
. If you did that, you would still have to wait for the conversion but you can do it an a non-blocking manner, and I don't see that happening in your code.
-
Thank you. Is this something I need to call once in the setup? Or every time I am polling temperature from the sensor?Streamline– Streamline2019年03月01日 10:20:23 +00:00Commented Mar 1, 2019 at 10:20
-
Looking at this DS18b20 library, the flag does not appear to get changed except by an external call, so setting it once should be sufficient. [Sorry for my typo in the original reply].JRobert– JRobert2019年03月03日 17:16:34 +00:00Commented Mar 3, 2019 at 17:16
Explore related questions
See similar questions with these tags.