Main Content

Measure Servo Motor Shaft Angle Using a Device Driver Block in Connected I/O

This topic explains how to enable Connected I/O on a Servo Read device driver block to read the angle of a standard servo motor shaft connected to the Arduino® hardware during normal mode simulation.

Step 1: Connect the Servo Motor to the Hardware

  1. Connect the micro-USB end of the USB cable to the hardware and the regular USB end of the USB cable to the computer. Wait until the PWR LED on the hardware starts blinking.

  2. Connect the power wire of the servo motor (usually red) to the 5-V pin of the hardware.

  3. Connect the ground wire of the servo motor (usually black) to the ground pin of the hardware.

  4. Connect the signal wire of the servo motor (usually orange) to digital pin 4 of the hardware.

  5. Using the Hardware Setup screen, configure the Arduino hardware network.

For more information on the pins on the hardware, see Pin Mapping for Arduino Timer Dependent Blocks.

Step 2: Create a Servo Read System Object

Click Create a Device Driver Block to create a device driver System object™. Optionally, you can use the sample Standard Servo Read block, arduino_StandardServoRead.m, which is available in your Arduino support package installation folder. To open the sample file, execute this command in the MATLAB® Command Window.


Step 3: Create a Servo Read Add-On

This section shows how to read the angle of a standard servo motor shaft from the hardware to the Simulink® block and create all the necessary files in your custom library using MATLAB and C++.

Step 4: Specify the Behavior of the Servo Read Device Driver Block

These methods define initialization, output, and termination behavior of the device driver block. Use setupImpl to initialize the hardware peripheral. Use stepImpl to read from or write to the hardware peripheral. Use releaseImpl to release hardware resources used.

  1. In the MATLAB editor, open the arduino_StandardServoRead.m class file.

  2. At the top of the code, define the classes the system object inherits from.

    & codertarget.arduinobase.ioclient.SimulinkAddonLib
  3. Add the ioServoRead property along with the other properties listed in the file.

       properties (Nontunable,Hidden)
  4. Update the setupImpl method with this code.

    function setupImpl(obj)
                   obj.SimulinkIOEn = codertarget.arduinobase.internal.isSimulinkIoEnabled;
                   if obj.SimulinkIOEn
                     obj.registerAddon('ServoRead/ServoRead'); % Register the addon
                     obj.ioServoRead = arduinoioaddons.ServoRead.ServoRead(obj); % Create an object of the m-class
                     obj.getArduinoIOClient(); % Create IO server with the ServoRead addon
  5. Update the stepImpl method to call the methods defined in the class file.

    function stepImpl(obj,u) 
             if obj.SimulinkIOEn
                      angle = int32(;
  6. Update the releaseImpl method to unregister the addon and delete the IO client object.

    function releaseImpl(obj) 
           if obj.SimulinkIOEn

Step 5: Create a Simulink Model with Servo Read System Object

To bring the ServoRead System object into Simulink, follow the steps:

  1. Open a new Simulink model.

  2. From the Simulink Support for Arduino Hardware library, add the Standard Servo Read block to the model.

  3. Double-click the block to specify the Pin Number parameter as 4.

  4. From the Sinks library, add the Display block to the model.

  5. Connect the blocks as shown.

Step 6: Read the Shaft Angle in Connected I/O

  1. In the Simulation tab of the Simulink Model Toolbar, change Simulation mode to Normal.

  2. To open the Configuration Parameters dialog box, in the Modeling tab, click Model Settings.

  3. On the Hardware Implementation pane, set Hardware board to match your hardware, for example, 'Arduino Due'.

  4. On the Hardware tab of the Simulink model, in the Mode section, select Connected IO and then click Run with IO. This action will build and run the Simulink model containing your System object on the host computer.

While the model runs on the computer, the Servo Read block reads the position of the shaft once per second and displays the output in the Display block. Modify the model design and monitor the effect of the modified design before deploying the model on the hardware.

See Also