MATLAB Examples

Communicate with 1-Wire® Devices on Arduino® Hardware

This example shows how to use the MATLAB® Support Package for Arduino® Hardware to communicate with DS18B20 temperature sensor


Hardware Requirements

  • Arduino board
  • DS18B20 digital temperature sensor
  • 4.7K Pull-up resistor

Hardware Setup

  • Connect the center pin of DS18B20 to pin D10 on Arduino board.
  • Connect the pull-up resistor between the center pin and 5V pin on Arduino board.
  • Connect the other two pins of DS18B20 to GND pin on Arduino board.

Create OneWire Device Object

Create arduino connection with PaulStoffregen/OneWire add-on library included.

a = arduino('COM9', 'Uno', 'Libraries', 'PaulStoffregen/OneWire')
a = 

  arduino with properties:

                    Port: 'COM9'
                   Board: 'Uno'
           AvailablePins: {'D2-D13', 'A0-A5'}
               Libraries: {'PaulStoffregen/OneWire'}

Create a 1-Wire object for all devices connected on digital pin 10.

sensor = addon(a, 'PaulStoffregen/OneWire', 'D10')
sensor = 

  OneWire with properties:

               Pins: 'D10'
 AvailableAddresses: '28EAFE4B070000F2'

Control DS18B20 Temperature Sensor

Store the ROM address of the detected device, which is the DS18B20 digital temperature sensor(start with '28' family code).

addr = sensor.AvailableAddresses{1};

Reset the device, which is required before any operation. Then, start conversion with command '44' and also turn on parasite power mode.

write(sensor, addr, hex2dec('44'), true);

% Make sure temperature conversion is done. This is necessary if all commands run continuosly in a script.

Read the device's scratchpad memory which is consisted of eight data bytes and another byte of CRC, computed from the data bytes.

write(sensor, addr, hex2dec('BE')); % read command - 'BE'
data = read(sensor, addr, 9);
crc = data(9);
sprintf('Data = %x %x %x %x %x %x %x %x  CRC = %x\n', ...
    data(1), data(2), data(3), data(4), data(5), data(6), data(7), data(8), crc)
if ~checkCRC(sensor, data(1:8), crc, 'crc8')
    error('Invalid data read.');
ans =

Data = 48 1 4b 46 7f ff 8 10  CRC = ad

Combine LSB and MSB of the temperature reading into one value using the first and second byte of the scratchpad data.

raw = bitshift(data(2),8)+data(1);

Get the R0 and R1 bits in the config register, which is the fifth byte in scratchpad data. R0 and R1 together determines the resolution configuration.

cfg = bitshift(bitand(data(5), hex2dec('60')), -5);
switch cfg
    case bin2dec('00')  % 9-bit resolution, 93.75 ms conversion time
        raw = bitand(raw, hex2dec('fff8'));
    case bin2dec('01')  % 10-bit resolution, 187.5 ms conversion time
        raw = bitand(raw, hex2dec('fffC'));
    case bin2dec('10')  % 11-bit resolution, 375 ms conversion time
        raw = bitand(raw, hex2dec('fffE'));
    case bin2dec('11')  % 12-bit resolution, 750 ms conversion time
        error('Invalid resolution configuration');
% Convert temperature reading from unsigned 16-bit value to signed 16-bit.
raw = typecast(uint16(raw), 'int16');

Convert to the actual floating point value since the last bit of LSB represents $2^{-4}$.

celsius = double(raw) / 16.0;
fahrenheit = celsius * 1.8 + 32.0;
sprintf('Temperature = %.4f Celsius, %.4f Fahrenheit', celsius, fahrenheit)
ans =

Temperature = 20.5000 Celsius, 68.9000 Fahrenheit

Clean Up

clear sensor
clear a