File Exchange

image thumbnail

1-Wire Add-On Library for Arduino

Communicate with 1-Wire devices connected to Arduino hardware in MATLAB.

57 Downloads

Updated

View License

This add-on library 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
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.

MATLAB R2016a or higher and MATLAB Support Package for Arduino Hardware are required.

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 https://github.com/PaulStoffregen/OneWire/archive/v2.3.2.zip
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 "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:
http://www.mathworks.com/matlabcentral/profile/contact/4922363-mathworks-matlab-hardware-team

Comments and Ratings (10)

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.

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.

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: https://www.mathworks.com/matlabcentral/fileexchange
% ... /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.
reset(sensor);
write(sensor, addr, hex2dec('44'), true);
% Make sure temperature conversion is done.
% This is necessary if all commands run continuosly in a script.
pause(1);
% Read the device's scratchpad memory ...
% which is consisted of eight data bytes and another byte of CRC, ...
% computed from the data bytes.
reset(sensor);
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.');
end

% 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
otherwise
error('Invalid resolution configuration');
end

% 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').

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

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 =

'Adafruit/MotorShieldV2'
'ExampleAddon/HelloWorld'
'I2C'
'PaulStoffregen/OneWire'
'SPI'
'Servo'
>> 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
MATLAB 9.0 (R2016a)
Acknowledgements

Inspired by: MATLAB Support Package for Arduino Hardware

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video

+arduinoioaddons/+PaulStoffregen/