How to connect Raspberry Pi to ADC MCP 3008 using new SPI Simulink blocks?
Show older comments
I want to interface a RaspberryPi with an ADC MCP3008 using Simulink, in particular using the new block "SPI Register Read" available in the latest Simulink version (without writing C code or Matlab code). In this block is required a Register Address where SPI has to read the value from the slave. I don't know how to find it because I'm not sure if it's a MCP3008 register or it's an address always used from RPi to communicate with all the slaves. Can anyone help me?
2 Comments
Bruder muss los
on 10 Aug 2022
Hi Pietro, as far as I understand it, the MCP3008 has no registers in that sense, so you can't use the "register read"-block for this particular chip.
Instead, as Sharan proposed, I used the "master transfer"-block and took a few more nights to figure out the rest of the required logic. Since the "master transfer"-block gives you three output Bytes separately, but in one signal path, you have to dearrange the Bytes (via "selector"-blocks) and add the significant Bytes to get the desired 10-Bit value from the SPI Bus.
I hope this arrangement does the trick for you, too.

Madhura Pabudanan
on 19 Jul 2023
I got the data from MCP 3008 from this method. I have an issue with the sample rate. I can not achive maximum sample rate from this method. How I overcome this problem ?
Accepted Answer
More Answers (3)
Bikash Sah
on 10 Jul 2018
Hai Sharan, can you please explain the use of a constant block having value [1, bin2dec('10000000'), 0] as input to SPI Master Transfer. You wrote about the first four numeric values to control bits; what about the other four? What does the value "1" and "0" corresponds to. Also, a better explanation to understand the following code would be a great help.
clear mcp3008
mcp3008 = spidev(rpi, 'CE0');
data = uint16(writeRead(mcp3008,[1, bin2dec('10000000'), 0]));
highbits = bitand(data(2), bin2dec('11'));
voltage = double(bitor(bitshift(highbits, 8), data(3)));
voltage = (3.3/1024) * voltage;
Johannes Börner
on 31 Jul 2018
0 votes
Hi, Im currently trying to read the data of an MCP3008 in the way described above. Though I can manage the readings from command line, I have no indication that the SPI Master Transfer is working in any way when running the model. Is there any Simulink example using this block? Did anyone recently manage to get readings from an MCP3008?
Kind regards
1 Comment
Kiyo Akabori
on 7 Nov 2019
For me, placing a Data Type Conversion block of uint8 between the constant block of [1, bin2dec('10000000'), 0] and SPI Master Transfer block did the trick. Without the conversion block, an array of type double was fed to the SPI master block, which ended up outputing values very close to zero (about 1e-300).
Abdo Be
on 18 Jun 2019
0 votes
Hi, After multiple trials to read data from MCP3008 using SPI read block, it finally works for the first analog input (CH0),
However, in order to read analog input from the other channels (CH1,..CH7), normally the SPI write block should be driven by a constant block, I want to know the source of this synthax "[1, bin2dec('10000000'), 0]" that Sharan Paramasivam Murugesan mentionned above, because when I tested this block it doesn't change the analog input.
7 Comments
MOKHLIS MOHCINE
on 31 Jul 2019
Hello Abdo Be, I did the same steps you said, but they didn't work. Can you please share with us the simulink file that you're used. It will be very helpfull. Thanks in advance.
Kiyo Akabori
on 7 Nov 2019
Same here. I'm not getting any reading.
gaopalelwe Mabeleng
on 5 Jul 2021
good day,I am trying to read voltage from different sensors and I am experiencing the same challenge,has anyone figured it out? your assistance will be hughly appreciated
Ariel Villalon Monsalve
on 9 Oct 2021
Hi, I'm struggling too with having some reading from a ADC Chip (MCP3204 in my case). I have followed more or less the steps given here, but so far no results. I leave here a screenshot of my Simulink model. Anyone has had any advance on this matter? It'd be so much appreciated any comments. Thanks all!

Michel Malengret
on 29 Dec 2021
Use the SPI read block specifying the ADC Channel address. You can return all the channels if you set
the number of bytes to return say 16 bytes. I am using thw MCP 3914 The read address of the first channel is 64 , i ser nb of return blocks to uint8 x 24 . I the cas of the 3008 its likely to be 16 if there are 8 channels.
Bruder muss los
on 10 Aug 2022
Edited: Bruder muss los
on 10 Aug 2022
Hey folks, as far as I understand it, the MCP3008 has no registers in that sense, so you can't use the "register read"-block for this particular chip.
Instead, as Sharan proposed, I used the "master transfer"-block and took a few more nights to figure out the rest of the required logic. Since the "master transfer"-block gives you three output Bytes separately, but in one signal path, you have to dearrange the Bytes (via "selector"-blocks) and add the significant Bytes to get the desired 10-Bit value from the SPI Bus.
I hope this arrangement does the trick for you, too.

Madhura Pabudanan
on 20 Jul 2023
I got the data from MCP 3008 from this method. I have an issue with the sample rate. I can not achive maximum sample rate from this method. How I overcome this problem ?
Categories
Find more on Simulink 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!