Using the DS2484 breakout with Arduino involves wiring up the breakout to your Arduino-compatible microcontroller with a DS18B20 sensor, installing the Adafruit_DS248x library, and running the provided example code.
Wiring
Wire as shown for a 5V board like an Uno. If you are using a 3V board, like an Adafruit Feather, wire the board's 3V pin to the breakout VIN.
Here is an Adafruit Metro wired up to the breakout with a DS18B20 sensor using the STEMMA QT connector:
- Breakout terminal block GND to DS18B20 ground (blue wire)
- Breakout terminal block 1W to DS18B20 1-wire output (yellow wire)
- Breakout terminal block V+ to DS18B20 VIN (red wire)
-
Board 5V to breakout STEMMA VIN (red wire)
-
Board GND to breakout STEMMA GND (black wire)
-
Board SCL to breakout STEMMA SCL (yellow wire)
- Board SDA to breakout STEMMA SDA (blue wire)
Here is an Adafruit Metro wired up using a solderless breadboard:
- Breakout GND to DS18B20 ground (blue wire)
- Breakout 1WIRE to DS18B20 1-wire output (yellow wire)
- Board 5V to DS18B20 VIN (red wire)
-
Board 5V to breakout VIN (red wire)
-
Board GND to breakout GND (black wire)
-
Board SCL to breakout SCL (yellow wire)
- Board SDA to breakout SDA (blue wire)
Library Installation
You can install the Adafruit_DS248x library for Arduino using the Library Manager in the Arduino IDE.
Click the Manage Libraries ... menu item, search for Adafruit_DS248x, and select the Adafruit DS248x library:
If asked about dependencies, click "Install all".
If the "Dependencies" window does not come up, then you already have the dependencies installed.
If the dependencies are already installed, you must make sure you update them through the Arduino Library Manager before loading the example!
#include "Adafruit_DS248x.h" #define DS18B20_FAMILY_CODE 0x28 #define DS18B20_CMD_CONVERT_T 0x44 #define DS18B20_CMD_MATCH_ROM 0x55 #define DS18B20_CMD_READ_SCRATCHPAD 0xBE Adafruit_DS248x ds248x; void setup() { Serial.begin(115200); while (!Serial) delay(10); Serial.println("Adafruit DS248x test sketch!"); if (!ds248x.begin(&Wire, DS248X_ADDRESS)) { Serial.println(F("DS248x initialization failed.")); while (1); } Serial.println("DS248x OK!"); // Speed up I2C, as searching for ROMs, specifically, is slow! Wire.setClock(400000); while (! ds248x.OneWireReset()) { Serial.println("Failed to do a 1W reset"); if (ds248x.shortDetected()) { Serial.println("\tShort detected"); } if (!ds248x.presencePulseDetected()) { Serial.println("\tNo presense pulse"); } delay(1000); } Serial.println("One Wire bus reset OK"); } void loop() { uint8_t rom[8]; if (!ds248x.OneWireSearch(rom)) { Serial.println("No more devices found\n\n"); return; } Serial.print("Found device ROM: "); for (int i = 0; i < 8; i++) { if (rom[i] < 16) { Serial.print("0"); } Serial.print(rom[i], HEX); Serial.print(" "); } Serial.println(); // Check if the device is a DS18B20 (Family code 0x28) if (rom[0] == DS18B20_FAMILY_CODE) { // Read and print temperature float temperature = readTemperature(rom); Serial.print("\tTemperature: "); Serial.print(temperature); Serial.println(" °C"); } } float readTemperature(uint8_t *rom) { // Select the DS18B20 device ds248x.OneWireReset(); ds248x.OneWireWriteByte(DS18B20_CMD_MATCH_ROM); // Match ROM command for (int i = 0; i < 8; i++) { ds248x.OneWireWriteByte(rom[i]); } // Start temperature conversion ds248x.OneWireWriteByte(DS18B20_CMD_CONVERT_T); // Convert T command delay(750); // Wait for conversion (750ms for maximum precision) // Read scratchpad ds248x.OneWireReset(); ds248x.OneWireWriteByte(DS18B20_CMD_MATCH_ROM); // Match ROM command for (int i = 0; i < 8; i++) { ds248x.OneWireWriteByte(rom[i]); } ds248x.OneWireWriteByte(DS18B20_CMD_READ_SCRATCHPAD); // Read Scratchpad command uint8_t data[9]; for (int i = 0; i < 9; i++) { ds248x.OneWireReadByte(&data[i]); } // Calculate temperature int16_t raw = (data[1] << 8) | data[0]; float celsius = (float)raw / 16.0; return celsius; }
Upload the sketch to your board and open up the Serial Monitor (Tools -> Serial Monitor) at 115200 baud. You'll see the DS2484 recognized over I2C. In the loop, a search is performed for an attached DS18B20 temperature sensor. When the sensor is found, its temperature data is printed to the Serial Monitor.
Text editor powered by tinymce.