File Exchange

image thumbnail

Legacy 1-Wire Add-On Library for Arduino

version 1.2.1 (108 KB) by MathWorks MATLAB Hardware Team
Communicate with 1-Wire devices connected to Arduino hardware in MATLAB


Updated 22 May 2019

View Version History

View License

This add-on is supported for MATLAB R2016a - R2018b.

This add-on extends MATLAB Support Package for Arduino Hardware to read from and write to 1-Wire devices. With the add-on, you can reset the device, read or write a single byte or multiple bytes from or to the device and also check the data integrity using either CRC8 or CRC16 algorithm.

Sample usage:
% Create arduino object with the add-on library
a = arduino('COM38', 'Uno', 'Libraries', 'PaulStoffregen/OneWire');
% Create 1-Wire object
sensor = addon(a, 'PaulStoffregen/OneWire');
% Obtain sensor ROM address
addr = sensor.AvailableAddresses{1};
% Reset sensor
% Write to sensor
write(sensor, addr, 72, 1) % parasite power on
% Read 9 bytes from sensor
read(sensor, addr, 9)
% Check CRC of received data
checkCRC(sensor, [1 2 3 4], 56, ‘crc8’)

It includes documentation and an example that demonstrates the use with a DS18B20 temperature sensor and DS2431 1024-bit EEPROM.

Important: Before using this add-on library in MATLAB, you need to install the OneWire Arduino library. Here are the instructions:
1. Download the zip file from
2. Unpackage the zip into local directory and rename the folder to ‘OneWire’.
3. Move the ‘OneWire’ folder into the "libraries" folder inside your Arduino sketchbook folder:
On Windows, the default path is "My Documents\Arduino\libraries\"
On Mac, the default path is "~/Documents/Arduino/libraries/"
On Linux, the default path is "/home/<username>/Arduino/libraries"

Troubleshooting tips:
1. After installing this add-on library and the required Arduino library, type "listArduinoLibraries" in MATLAB to see if the add-on is properly installed.
2. Wire up the Arduino board and the add-on device properly before creating the object to avoid a connection error in MATLAB.
3. Refer to the documentation for details on usage and syntax. It can be found under Supplemental Software in the product documentation.

Feel free to contact the MATLAB Hardware Team if you have questions about this add-on library:

Cite As

MathWorks MATLAB Hardware Team (2021). Legacy 1-Wire Add-On Library for Arduino (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (20)

Dvir Peer

I tried it with Matlab2020a without any success. I didn't even see the library when asking for listArduinoLibraries...

Benjamin Alexander

Having trouble with MATLAB 2020a. Same issue as Eric Busvelle

Eric Busvelle

I tried it with Matlab2020b without any success. I didn't even see the library when asking for listArduinoLibraries...

Eyal Wurgaft

Anyone tried it with MATLAB 2020b?

Christopher Harness

Nevermind I created it!

Christopher Harness

Would anyone know how to loop this for X amount of increments? I would like to develop a code that can plot this data via line graph.

Jordie Verstappen

I would like to know how to read multiple ds18b20's in normal mode (non-parasite power mode). I seem to not be able to do this by changing: "write(sensor, addr, hex2dec('44'), true)"; to "write(sensor, addr, hex2dec('44'), false);"

jonathan khabbaz

will ths work with a dht22 sensor? thanks. works very well with a ds18b20!

John Victor

Hello,can anyone please tell me that what addon library should I install to connect an LCD display with arduino??And how to do it??please..

Federica Pasquali

I am not able to make more thank one OneWire device work at the same time. After adding the second device ( which is recognised, the data of the first one doesn't make resonable anymore). Can anyone help me?

Bruno Marin

I got it. But, I'm trying to create a matlab function in simulink using your code @Douglas Leaffer, a lot of problems here.

gihan ranasinghe

code works well and how to add more temp sensors to matlab - audrino, Thanks a lot!!

Bruno Marin

Did work it this code @DouglasLeaffer?? Do you have facebook? Than I can ask your help.

Douglas Leaffer

I'll share my code, below:

% Code modified by Doug Leaffer to use the MATLAB(R) Support Package
% for Arduino(R) Hardware to communicate with DS18B20 temperature sensor

% Hardware Requirements
% * Arduino board (UNO Rev.3)
% * DS18B20 digital temperature sensor
% * 4.7K Pull-up resistor (L to R: yellow-purple-red + gold or silver)

% Hardware Setup
% * Connect the center pin (blue lead) of DS18B20 to pin D10 on Arduino
% * Connect a 4.7kohm pull-up resistor between the center pin and 5V pin
% * Connect the other two pins of DS18B20 to GND pins on Arduino board

% Create arduino connection, include PaulStoffregen/OneWire add-on library
a = arduino('COM4','Uno','Libraries', 'PaulStoffregen/OneWire');
% DL note: type: listArduinoLibraries in Matlab command window ..
% 'PaulStoffregen/OneWire' library should be listed, if not ...
% add "OneWire" folder to current folder/path
% refer to:
% ... /57897-1-wire-add-on-library-for-arduino?s_cid=ME_prod_FX

% Create a 1-Wire object for all devices connected on digital pin 10.
sensor = addon(a, 'PaulStoffregen/OneWire', 'D10')

% Control DS18B20 Temperature Sensor
% Store the ROM address of the detected device, ...
% which is the DS18B20 digital temperature sensor
% (start with '28' family code)
% NOTE: 28 C1 B6 44 1 0 0 CRC OK
% Returned by Arduino for code run for DS18B20 Temp Probe
% using Arduino sketch_08_01_OneWire_List code from ArduinoNextSteps
% p.144 of "Programming Arduino, Next Steps", Simon Monk (2014)
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.');

% 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 5th 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)

% Clean Up
clear sensor
clear a

Bruno Marin

Hi! I can see the 'PaulStoffregen/OneWire' listed with the other installed libraires. But when I press Run, I have this error:
I'm using the correct port. Matlab 2016a

Error using arduino
Too many input arguments.

Error in OneWireExample (line 25)
a = arduino('COM3', 'Uno', 'Libraries', 'PaulStoffregen/OneWire').

Douglas Leaffer

In the Command Window try typing >> listArduinoLibraries You should see the 'PaulStoffregen/OneWire' listed with the other installed libraries. If not seen, make sure the folder "OneWire" (installed per instructions above) is in the current folder path.

Han Son Go

@Eyal Wurgaft
I have the same error. Did you resolve it?

Eyal Wurgaft

Hello. I have installed the library. I am running the "OneWireExample.m", and immediately after this line is executed:
a = arduino('COM3', 'Uno', 'Libraries', 'PaulStoffregen/OneWire')
I get the following error:
"Cannot program board Uno (COM3). Please make sure the board is supported and the port and board type are correct. "

Note that I changed the COM from the original COM9 to COM3 which is where my arduino is connected.
Any ideas??

Eshaan Ghosh

This is the error that I am getting. I opened it in serial monitor of Arduino and it was working. But in MATLAB I am getting this error

addr = sensor.AvailableAddresses{1};
Cell contents reference from a non-cell array object

Ricardo Quiroz

Hello everyone, I'm trying to use this 1-Wire library to get the temperature from a DS18B20 sensor, and after adding the library I'm getting this message:

>> listArduinoLibraries

ans =

>> a = arduino('COM3','Uno','libraries','PaulStoffregen/OneWire')
Updating server code on board Uno (COM3). Please wait.
The Arduino source 'OneWire/OneWire.h' for libraries 'PaulStoffregen/OneWire' cannot be found. Install
the 3P library source first and try again.

Could someone help me with this please?

MATLAB Release Compatibility
Created with R2016a
Compatible with R2016a to R2018b
Platform Compatibility
Windows macOS Linux

Inspired by: MATLAB Support Package for Arduino Hardware

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!