Guy on Simulink

Simulink & Model-Based Design

Implementing a PID Controller on an Arduino Board

This week my colleague Pravallika is back to continue her motor control story!

Pravallikota Vinnakota, guest blogger

In my previous post, we saw how to estimate continuous transfer functions with System Identification Toolbox. We estimated the following transfer function for a simple DC Motor using tfest:

Transfer function identified from a DC Motor

For this transfer function, we designed the following controller using pidtune:

Controller generated by pidtune

We will now implement the controller on the Arduino Uno and see how the DC motor fares with this controller. To deploy the controller on the hardware, we will use Simulink’s capability to generate an executable and run it on selected hardware.

Deploying controller to the Arduino board

You probably noticed that the controller shown above is in a continuous form. To use it on our target, the first thing to do is to discretize it using the c2d function:

Discretizing the controller generated by pidtune

Then we grab the PID block from the Simulink Library and configure it.

PID block configuration

To keep the PID controller’s output within the limits of the hardware, we go to the PID Advanced tab and enable output saturation along with anti-windup protection.

Advanced PID block configuration

To test the controller on the hardware, we created a Simulink model using blocks from the Arduino Support Package.

As you can see, we receive the desired motor position from the serial port and compare it to the measured position from the Analog Input. The position error goes through the PID block which generates a voltage to be sent to the motor. We also send the measured position through the serial port.

Controller model to be used on the Arduino board

Hardware response

We ran the model on the target, sent it some commands and logged the data transmitted through the serial port. Here is what it looks like:

Data logged from our controller

So, it does a pretty good job of tracking the reference signal. When we compare the response from the hardware to that of the simulation, we observe that they are very close! The System Identification Toolbox model is quite good.

Now it is your turn

How are you designing controllers when a system is difficult to model? Have you tried the Run on Target Hardware capability in Simulink to run your models on the supported boards? Let us know by leaving a comment here.

  • print


To leave a comment, please click here to sign in to your MathWorks Account or create a new one.