Hi there,
I don't have the hardware yet so I'm simulating it with Wokwi.
I want to connect a DS18B20 temperature sensor to a Nano and read and display a 12 bit temp value (3 decimal digits). Easy enough, or so I thought...
The sketch :
#include "OneWire.h"
#include "DallasTemperature.h"
#define ONE_WIRE_BUS 2
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
void setup() {
uint8_t sensorAddress[8];
// Begin serial communication:
Serial.begin(115200);
sensors.begin();
sensors.getAddress(sensorAddress, 0);
Serial.print("Sensor Address:");
for (byte i = 0; i < 8; i++) {
Serial.print(F(" 0x"));
if (sensorAddress[i] < 0x10) Serial.write('0');
Serial.print(sensorAddress[i], HEX);
}
Serial.println();
}
void loop() {
sensors.requestTemperatures();
float tempC = sensors.getTempCByIndex(0); // the index 0 refers to the first device
// Print the temperature in Celsius in the Serial Monitor:
Serial.print("Temperature: ");
Serial.print(tempC, 3);
Serial.write('°');
Serial.println('C');
// Wait 1 second:
delay(1000);
}
and the diagram.json :
{
"version": 1,
"author": "Anonymous maker",
"editor": "wokwi",
"parts": [
{ "type": "wokwi-arduino-nano", "id": "nano", "top": 0, "left": 0, "attrs": {} },
{
"type": "board-ds18b20",
"id": "temp1",
"top": 4.18,
"left": 257.23,
"rotate": 90,
"attrs": { "familyCode": "40", "deviceID": "111111111111", "temperature": "15.375" }
},
{
"type": "wokwi-resistor",
"id": "r1",
"top": 23.15,
"left": 172.8,
"attrs": { "value": "4700" }
}
],
"connections": [
[ "nano:5V.2", "r1:1", "red", [ "h0" ] ],
[ "nano:5V.2", "temp1:VCC", "red", [ "h0" ] ],
[ "temp1:DQ", "r1:2", "green", [ "v0" ] ],
[ "nano:23", "temp1:DQ", "green", [ "v0" ] ],
[ "nano:2", "temp1:DQ", "green", [ "v-14.4", "h124.3", "v38.4" ] ],
[ "nano:GND.2", "temp1:GND", "black", [ "v14.4", "h191.5" ] ]
],
"dependencies": {}
}
whose important part about the DS18B20 sensor is this :
"attrs": { "familyCode": "40", "deviceID": "111111111111", "temperature": "15.375" }
"familyCode": "40"
means DS18B20 as opposed to the default "familyCode": "10"
which designate the DS18S20.
As you can see running the simulation, the temperature resolution is 9 bit or half a °C.
My ultimate goal is to have 4 temp sensors accessed using their ROM address (this part actually already woks) but I need to read all 12 bit "first".
Is there anything wrong with my code ?