Guy on Simulink

Simulink & Model-Based Design

Implementing a PID Controller on an Arduino Board 16

Posted by Guy Rouleau,

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.

16 CommentsOldest to Newest

bac replied on : 1 of 16

Sorry about a question outside the topic.
How did you make your photos frame like that? I really interested in it.

mog123 replied on : 2 of 16

Hey, this is great! thanks for this.

But you could have realised this in a better manner, by using only 1 output pin. Just check the sign of the outputting PID control signal and basing on that, control the PWM signal by negating it etc.

pushkin2023 replied on : 4 of 16

Hi. How are you representing the angle? 0-360 degrees or 0..+1..+2…+179..+180..-179..-178..-2..-1..0 degrees.

Hui Jin replied on : 5 of 16

Is this command ‘pidtune’ new introduced in 2013a?

Because in my version 2010a, doc pidtune doesn’t work.

Marco replied on : 8 of 16


I don’t understand how it was processed the PID output signal:

How can you transform the PID control law to PWM sequence? What is there inside “Convert to PWM” blocks?

Max replied on : 11 of 16

1. I don’t understand why also send the measured position through the “serial port 0”?
2. we receive the desired motor position from the “serial port 0”,and send the measured position through the same serial port?
3. receive the measured position by USB ?if i want to get the data from arduino serial port,which block should be used?serial receive block or serial transmit block?
thank you!

Sallem Marwen replied on : 12 of 16

I have a question about the “Convert to PWM” block , I can’t understand how did you do it

abdullah replied on : 14 of 16

Hi, This is a good study. I want to implement this on real but is there any scheme for connections and specifications of dc motor. I read previous blog but i couldn’t understand how did you measure the angle ? is there any sensor or encoder on dc motor if yes how can be create the system on real? like. fritzing project or etc. and can you upload the simulink files? Thank you.

Aravind replied on : 15 of 16

I have a question to the author. How a open loop control like the one in the picture can keep a desired angle of the motor? There is no system in the loop how is the control possible. Please explain

Pratik Kamble replied on : 16 of 16

Hello , i am using arduino for real time implementation for level control loop. When i use pid controller in Simulink its output gets vary ….thats why Arduino PWM pin recevies value higher than 0-255 range. I want to vary pid controller output between 0-255 for PWM. So what is the necessary arrangement should i do in matlab Simulink.. please suggest

Add A Comment

What is 3 + 10?

Preview: hide