Raspberry pi AD Converter using mcp3002

2 views (last 30 days)
Alex83
Alex83 on 29 Dec 2016
Answered: Alex83 on 31 Dec 2016
Hello! I am quite new to MATLAB/Simulink connecting to raspberry Pi 2.
My intention is to measure a voltage by a analogue digital converter. In order to do so, I am using a gertboard (the first model). I have already found the examples for usage of the mcp3008 SPI device in the help files of matlab and tried it on the gertboard internal mcp3002 device. It did not work: even if 3.3 V and GND are connected to AD pins of the gertboard it still shows 0 V. Is there any simple method for noobs to get the AD reading running ? Thanks in Advance, Alex

Answers (1)

Alex83
Alex83 on 31 Dec 2016
hello everybody, With a little patience I found a solution:
rpi = raspi('IP-ADRESS', 'pi', 'raspberry');
global A;
clear A;
n=20;
A = zeros(0,0);
global mcp3008;
global data;
global highbits;
global voltage;
for i = 1:n
clear mcp3008;
mcp3008 = spidev(rpi, 'CE0',0);
data = uint16(writeRead(mcp3008,[1, bin2dec('10000000'), 1]));
highbits = bitand(data(2), bin2dec('11'));
voltage = double(bitor(bitshift(highbits, 8), data(3)));
voltage = (3.3/1024)*voltage;
pause(0.1);
A =[A voltage];
end
My problem is now: how do I get this M-File in Simulink for a continuous voltage measurement. I have tried using interpreted Matlab function, but it always says that it didn't found my variables or the commands (for example writeRead). Does anybody know how you can run an M file form Simulink?
For the function block I of course removed "for i = 1:n" and "end" as well as the "rpi = ...." lines.
Thank you for your answers and helpful ideas in advance. Alex

Community Treasure Hunt

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

Start Hunting!