Skip to content

Instantly share code, notes, and snippets.

@CelliesProjects
Created August 8, 2017 07:57
Show Gist options
  • Save CelliesProjects/16bbef7f3eb7a9f49034f38aae3b4813 to your computer and use it in GitHub Desktop.
Save CelliesProjects/16bbef7f3eb7a9f49034f38aae3b4813 to your computer and use it in GitHub Desktop.
ESP32 DS18B20 no delay
#include "MHDS18B20.h"
OneWire ds(5); // on pin D2 (a 4.7K resistor is necessary)
int numberOfSensors;
byte currentAddr[8];
struct sensorStruct {
byte addr[8];
} sensor[3];
byte data[12];
byte type_s;
float celsius, fahrenheit;
unsigned long sensorReadTime;
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
while ( ds.search(currentAddr) ) {
numberOfSensors++;
Serial.write( "Sensor "); Serial.print( numberOfSensors ); Serial.print( ":" );
for ( byte i = 0; i < 8; i++) {
Serial.write(' ');
Serial.print(currentAddr[i], HEX);
sensor[numberOfSensors].addr[i] = currentAddr[i];
}
Serial.println();
}
Serial.print(numberOfSensors); Serial.println( " sensors found." );
for ( byte thisSensor = 0; thisSensor < numberOfSensors; thisSensor++) {
ds.reset();
ds.select( sensor[thisSensor].addr );
ds.write( 0x44, 1); // start conversion, with parasite power on at the end
}
if ( numberOfSensors > 0 ) {
sensorReadTime = millis() + 750;
}
for (byte nos = 1; nos <= numberOfSensors; nos++) {
for ( byte i = 0; i < 8; i++) {
Serial.write(' ');
Serial.print( sensor[nos].addr[i], HEX );
}
Serial.println();
}
}
void loop() {
// put your main code here, to run repeatedly:
if ( millis() >= sensorReadTime ) {
readTemps();
}
}
void readTemps() {
for ( byte thisSensor = 1; thisSensor <= numberOfSensors; thisSensor++) {
ds.reset();
ds.select( sensor[thisSensor].addr );
ds.write(0xBE); // Read Scratchpad
//Serial.print( thisSensor ); Serial.print(" Data = ");
//Serial.print(present, HEX);
Serial.print(" ");
for ( byte i = 0; i < 9; i++) { // we need 9 bytes
data[i] = ds.read( );
//Serial.print(data[i], HEX);
//Serial.print(" ");
}
//Serial.print(" CRC=");
//Serial.print(OneWire::crc8(data, 8), HEX);
//Serial.println();
int16_t raw = (data[1] << 8) | data[0];
if (type_s) {
raw = raw << 3; // 9 bit resolution default
if (data[7] == 0x10) {
// "count remain" gives full 12 bit resolution
raw = (raw & 0xFFF0) + 12 - data[6];
}
} else {
byte cfg = (data[4] & 0x60);
// at lower res, the low bits are undefined, so let's zero them
if (cfg == 0x00) raw = raw & ~7; // 9 bit resolution, 93.75 ms
else if (cfg == 0x20) raw = raw & ~3; // 10 bit res, 187.5 ms
else if (cfg == 0x40) raw = raw & ~1; // 11 bit res, 375 ms
//// default is 12 bit resolution, 750 ms conversion time
}
celsius = (float)raw / 16.0;
fahrenheit = celsius * 1.8 + 32.0;
Serial.print(" Temperature = ");
Serial.print(celsius);
Serial.print(" Celsius");
Serial.println();
ds.reset();
ds.select( sensor[thisSensor].addr );
ds.write( 0x44, 1); // start conversion, with parasite power on at the end
}
sensorReadTime = millis() + 750;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment