File Exchange

image thumbnail

Raspberry Pi Simulink Driver Blocks ADC DAC PWM

version (36.9 KB) by John Whittington
Driver blocks allowing Simulink models to interface with PWM, MCP3008/ADS1115 ADC, MCP4725 DAC


Updated 18 Jun 2015

View License

Support for the Raspberry Pi beyond toggling GPIO. Joshua Hurst did some good work creating these blocks: but there were a number of errors/steps lacking to get them compiling and working on a Raspberry Pi.
Uncomment or copy into another Raspberry Pi model the blocks you wish to use.
# Usage
* You must open the S-Function driver block and click 'Build' to generate the
embedded coder files otherwise you will receive an error 'sfcn\_.... does
not exist'.
* If using the ADS1115 and MCP4725 you will require my fork of wiringPi: see the README for
installation instructions.
* If using the PWM block, you will need to enable SPI in Raspbian - see: or enable via SSH.
* The scopes work in real-time on the Pi to see the correct data is going
to/coming from blocks.
* The discrete timestep is set to 'Ts' throughout the model. This is
initialised as 1e-3s at model load - it seems to be the sweet spot for
performance/usage. Remember to consider quantisation that this can create
with signals driving the blocks.

# Notes

* wiringPi softPWM:
* Do not sure both ADC blocks at once without changing the wiringPi 'pin'
assignment - it won't work.
* See the post on my blog explaining how these work:

# Install wiringPi Fork

1. Login to the Pi via SSH.
2. `git clone`
3. `cd wiringPi-mcp4725`
4. `./build` - @j_whittington - 2015

Cite As

John Whittington (2021). Raspberry Pi Simulink Driver Blocks ADC DAC PWM (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (8)

Artur Kaminski

Tobias Elmøe

Thanks for this! I had some problems initially getting the remote build process to take place, as Simulink would not find the 'wiringPi' library, during compilation. I tried the proposed solution with xmakefilesetup, but I didn't get the correct toolchain, rather some default one that wasn't used. Instead, what solved it was to find the toolchain file "gnu_gcc_raspberrypi_toolchain.m" (located for me in "C:\MATLAB\SupportPackages\R2016a\toolbox\target\supportpackages\raspberrypi\registry") and add the link in row "cpplinker.Libraries = {'-lm','-lstdc++'};" so it would instead read "cpplinker.Libraries = {'-lm','-lstdc++','-wiringPi'};". After that I ran the ""gnu_gcc_raspberrypi_toolchain.m", renamed the old toolchain (in my case "gnu_gcc_raspberrypi_toolchain_gmake_win64_v1.0.mat", located in "C:\MATLAB\SupportPackages\R2016a\toolbox\target\supportpackages\raspberrypi\registry" ), and saved can "tc(1)" struct that came out of it as "gnu_gcc_raspberrypi_toolchain_gmake_win64_v1.0.mat" in the same location ("C:\MATLAB\SupportPackages\R2016a\toolbox\target\supportpackages\raspberrypi\registry"). And then I was able to build all my code!

Md. Mubarak Hossain

Dear respected sir, Hope you are in good health with success and happiness.

I'm trying to make a heart bit/pulse monitoring system using Raspberry pi. I'm using Heart Rate Pulse Sensor Pulsesensor Sensor which gives analog value as output. But you know raspberry pi is not able to read analog value.

Therefore I'm trying to convert analog value to digital as input for Rasperry pi.
I'm using MATLAB to code Raspberry pi.

Please could you help me about how to convert Analog data to digital data sothat it can be feed into Raspery pi?

Please sir, Help me. I'm waiting for response........

Valerio Rescigno

Valerio Rescigno

s p

heyho, i get an error after press play while building. Dou you have an idea why this happens?

Error executing command "touch -c /home/pi/rpi_driver_blocks_ert_rtw/*.*;make -f all -C /home/pi/rpi_driver_blocks_ert_rtw". Details:

STDERR: sfcn_ADS1115_Out_wrapper.c.o: In function `sfcn_ADS1115_Out_Outputs_wrapper':
sfcn_ADS1115_Out_wrapper.c:(.text+0x44): undefined reference to `analogRead'
sfcn_ADS1115_Out_wrapper.c.o: In function `sfcn_ADS1115_Out_Update_wrapper':
sfcn_ADS1115_Out_wrapper.c:(.text+0xa4): undefined reference to `ads1115Setup'
collect2: error: ld returned 1 exit status
make: *** [../rpi_driver_blocks.elf] Error 1

thanks for the shared file. i am currently developing active power filter controller using raspberry pi and i need to obtained at least 2 analoge signal using mcp3008. is it possible for me to use more then 1 channel for the mcp3008 and how am i going to declare or call the pin channel in the s-function.


MATLAB Release Compatibility
Created with R2014b
Compatible with any release
Platform Compatibility
Windows macOS Linux

Community Treasure Hunt

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

Start Hunting!