File Exchange

image thumbnail

Raspberry Pi PWM Driver Block SFunction

version (17.9 KB) by Joshua Hurst
Simple Soft PWM driver block example using WirngPi


Updated 22 Aug 2014

View Version History

View License

This is a simple example for motor speed control using the soft PWM function in the WiringPi library.
The range is set to 255 to emulate Arduino like implementation.
Other device driver block, and the hardware it was run on is available at our website:

A complete Arduino library and curriculum is available here:

coming soon:
- Quadrature Encoder
- MPU6050

and others!

This work is part of the Mechatronics curriculum at Rensselaer Polytechnic Institute.

Cite As

Joshua Hurst (2021). Raspberry Pi PWM Driver Block SFunction (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (9)

toshiyuki sakuragi

I finally PWM_Out_SFunction_Example, in Raspberry Pi3
We have to work with the R2016b.
RaspberryPi and other sample software will work as well.
The contents are
1. in the PWM_Out_SFunction_Example holders on the PC side of the /home/pi/wiringPi/wiringPi
To build the file.

2. in the /home/pi/wiringPi/wiringPi/devLib gertboard.h, gertboard.c
copy the /home/PI/wiringPi/wiringPi.

3. /home/pi/wiringPi/wiringPi on the SD card for the Raspberry Pi3 created on a PC as well as the
To build the file.

4. we try to again build work on the MatlabSimulink.

5. in this work, expanded to Raspberry Pi3 works.

Please use if you wish.

toshiyuki sakuragi

I'm in trouble because I can't carry out sample software of
Matlab [Simple Soft PWM SFunction ExampleFor Raspberry Pi: Motor Speed Control].
Where or, a corrective action, advice, please.

### Starting build procedure for model: PWM_Out_SFunction_Example
Code Generation
Elapsed: 9 sec
### Build procedure for model: 'PWM_Out_SFunction_Example' aborted due to an error.
The path 'C:\Users\sakuragi\Documents\MATLAB\Raspberry Pi PWM Driver Block SFunction\home\pi\wiringPi\wiringPi' does not exist


John Whittington

The errors you are having are as the error explains: The GPIO block is set to 'Model B' by default, most Pis are now 'Model B+'. Open the 'GPIO Write' block and change the 'Board' parameter to 'Model B+'.

There are bigger issues with this example though. The includes in 'Code Generation > Custom Code' need removing as they look within the local system. Additionally the wiringPi library needs adding to the linker flags using 'xmakefilesetup' - see my blog:

Walter Veiga

I'm having the same problem, did you manage to fix this?

Mark Laszlo

Hey guys,
I’m trying to use this PWM_Out_SFunction_Example for Raspberry Pi, but uunfortunately I can not. I’ve already tried on both R2013b and R2015a (using RPi Model B+), but it always gives me this problem: Error in S-function 'PWM_Out_SFunction_Example/Soft PWM Output SFunction': S-Function 'sfcn_PWM_Out' does not exist
And the command windows gives this error: Warning: The Board parameter in 'PWM_Out_SFunction_Example/GPIO Write' block does not match the board at ''.
I checked the wiringPi library on the Pi and it contains all the necessary files, so I have no idea what is the problem … 
Could you help me with this issue?

Carlos Moran

Paul Kassebaum

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

Inspired by: Device Drivers

Inspired: Raspberry Pi Simulink Driver Blocks ADC DAC PWM


Community Treasure Hunt

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

Start Hunting!