Integrate Legacy C code to Waijung 2 using s-function builder

View table of content.


Enable user to create a Simulink block that can execute a custom C code.


Problem Statement

Create a Simulink block that would act as a conditional statement block to toggle an LED.

Steps to Create the Library File

  • Browse the current folder to the location which you want to create the library file and open Simulink.
  • Click 'Blank Model' and save it as: toggle_led_condition_lib.slx
  • Double click on the workspace and insert a 'S-Function Builder' block.
  • Double click on the block and enter the S-Function name.
  • Configure input ports
  • Configure output ports
  • Add dependencies
  • Add the custom C code
  • Click the 'build' button and it will create a few files in the 'Current Folder'.
  • Congratulations! you have successfully created your first custom block library.

Using the library block created

  • Right click on the block and copy the library block to clipboard.
  • Move the 'Current Folder' to a new location where you want to create the model file
  • Open Simulink and click 'Blank Model'.
  • Save the model file as 'toggle_led_using_pot.slx'.
  • Create the model file shown below using Waijung2 blockset and upload the code to an ESP32.


Connect the potentiometer to the Analog Pin 33 and connect an LED to Digital Pin 2. Potentiometer should be connected in a way where the maximum input voltage is 3.3V and the minimum is 0V;


LED will toggle On and Off depending on the potentiometer reading.

Demo files


Previous : Scheduling mechanism in Waijung 2
Next : Create embedded web application with Aimagin Connect

Also available in: PDF HTML TXT