how i can read a signal from asd1115 with arduino and matlab?

13 views (last 30 days)
I'm try to write a program which can read a signal from ADS1115. I use a arduino as a hub. I'm starting with this code:
%check arduino
a=arduino();
%find ic2 adc and select it (do it after installing add onto arduino)
addrs = scanI2CBus(a);
%create a ic2 device. substitute ‘0x48’ if necessary
signal = i2cdev(a, '0x48')
Now i would to configure ADS1115to perform a lecture of register0(which is the Conversion Register The feature that i need is that while matlab show some figures, matlab will read from ADS1115 a voltage value. How i can perform this?

Answers (2)

T Rockett
T Rockett on 5 Sep 2017
Edited: T Rockett on 5 Sep 2017
TLDR version:
a = arduino('COM4','Uno'); % replace with relevant com port and model
dev = i2cdev(a,'0x48'); % creates i2c device object
writeRegister(dev,1,51075,'uint16');
pause(0.02)
data = readRegister(dev,0,'uint16');
This code will measure the signal from the A0 channel of the ADS1115, up to 1.024 Volts. To use a voltage above 1.024 Volts you can write a different number to the register. As far as I know, you need to call the writeRegister command every time before you read the data with readRegister.
How to make the chip work (with more detail)
First you need to tell the ADS1115 to start gathering data. You do this by writing a value to the configuration register (the number 1 in the writeRegister command corresponds to the config register). The number that you write to the register can be worked out by looking at the ADS1115 data sheet, in the section "Config Register Field Descriptions". Basically you need to work out a 16-bit binary sequence.
The values that I used as an example:
For example in the bits 11 to 9 I used "0 1 1", this corresponds to the gain amplifier setting of FSR = +/- 1.024 V from the data sheet. By gathering these 1's and 0's into a single 16-bit sequence (starting with bit 15 from my table) we get:
1100011110000011
We can then convert this sequence into a decimal number with:
bin2dec('1100011110000011')
This gives 51075. After writing this value to the configuration register, the chip will start gathering data. This will be stored in the conversion register (which has address 0). We can read the data from this register by using the readRegister command.
n.b. sorry for necroing this thread. There is literally no documentation on the internet on how to use this chip with Matlab / Arduino, and this is the first thing that comes up on google. I spent around 10 hours getting this to work, and I want other users to not have the same painful experience :)
  4 Comments
Mitch Bateman
Mitch Bateman on 29 Oct 2021
The information they provided is very helpful but it leaves out one very crutial part of the ADC. The ADS1115 reads and sends data in little endian (meaning it reads the bytes from smallest to greatest) and matlab sends and reads data in big endian (it reads the bytes from greatest to smallest). Thankfully matlab has a function to change it from big endian to little endian (swapbytes).
Here is my code that finally got my ADC to work properly and give me the right data.
%Initialize arduino
a = arduino('COM6','Uno','Libraries','I2C');
%Start connection with I2C Bus
dev = device(a,'I2CAddress','0x48');
%Compare A0 with GND for +- 2.048V with 128 SPS
adcCode = swapbytes(uint16(bin2dec('1100010110000011')));
%Write to the register
writeRegister(dev,1,adcCode,'uint16')
%Read value from register
data = swapbytes(uint16(readRegister(dev,0,'uint16')));
%Convert uint16 data to voltage
voltage = double(data)*2.048/32768;
As you can see, you have to swapp the bytes both when programming the ADC and reading data from the ADC. The 16 bit code can be changed to whatever you need it to be. Just look at the data sheet for the bits you need to change. It will be the same for all ADS1115. Just a recomondation though, there are some that you don't really need to change.
bit 15,8,4,2,1,0 don't need to be changed for basic use.
Only change bit 3 if you are doing a higher sps than the defalt 128.
The order is bit 15 on the left down to bit 0 on the right.
I hoped this helped, I've looked everywhere for help and couldn't find anything. I probably spend 40+ hours trying to figure this out but after I finally did, I though I should post it here since there is where all of the other forums on this question leads to.
James Findley de Regt
James Findley de Regt on 20 Jan 2022
A few additional notes, for any folks still worrying at this problem:
1) my ADS1115 is not doing the bitswap thing @Mitch Bateman reported. I've confirmed this on both the input and output end. Maybe there were different production runs or something?
2) the behaviour described by @Sarah Dickson seems to arise from the way the ADC is reporting its value. It seems to me that the ADC is reporting positive voltages in the 0 to 32768 space, and reporting negative voltages in the 65535 to 32768 space. If one constructs a signal which oscillates around 0 V, the values reported jump from small (say, under 10000) to very large (over 55000).
I've solved this with an if statement:
if data >= 32768
data = int32 (data) - 65535;
end
I use int32() because you need a signed variable type, to handle the negative numbers.

Sign in to comment.


daniela tovar
daniela tovar on 24 Feb 2020
Hi! Has anyone tried this programming? I am trying to program my ADS1115 module with this example but when I read the ADC (0) and convert it to Volts it is not the same as I measure with the multimeter.

Categories

Find more on MATLAB Support Package for Arduino Hardware in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!