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: