working with arduino PWM in matlab | applied electronics engineering


working with arduino PWM in matlab

By Applied Electronics - Monday, January 30, 2017 No Comments
This tutorial is for learning how to use the Arduino PWM(Pulse Width Modulation) with matlab. As you know we can directly write matlab program to execute commands that arduino microcontroller can understand, for example turn on/off a LED connected to arduino. This was shown in the earlier tutorial see Programming Arduino using Matlab for that.

But we are not limited to only blinking LED, we can do everything that you can do with Arduino programming using the Arduino IDE. One of them is the PWM programming. As you perhaps know, and for those who does know it, PWM is a technique of sending digital pulses with varying duty cycle(period over which the the signal is high). If one period is 1 second, and if the pulse is High/ON for 0.5 second and Low/Off for 0.5 second then the duty cycle is 0.5.

We explain here how this is done by matlab programming. At the end you will find Video demonstration of what has been mentioned here.

But before we do that we must know some more theoritical knowledge about the PWM. As said PWM is characterized by the duty cycle. If we send a digital pulse with 0.5 duty cycle it means that half of one period it is high and the other half is low. This makes a square wave.

We can change the duty cycle within the range 0 to 1. For example we can send out pulses with duty cycle of say 0.6. But how is this duty cycle determined? This is related to the microcontroller data bus and microprocessor bit processing capability. For example the Arduino UNO has 8 bit microprocessor which means that when it sends high it sends all 1's that is 11111111 to the digital pin. This all ones corresponds to 255 in decimal unit. So sending 0 or no pulse means sending 00000000 in binary or 0 in decimal and sending 1 or high pulse means sending 11111111 or 255 to the digital pin.

So to change the PWM width or the duty cycle which means the period over which the pulse remains high, we change the value that we send to the digital pin. That means we can send any value between 0 to 255 in decimal or equivalently in binary from 00000000 to 111111111. Thus if we send 40 to the digital pin we will get a duty cycle of 40/255 which is equal to 0.15. This means 15% of the total period the pulse will be high.

Matlab functions for PWM

There are two similar functions related to sending PWM in matlab for arduino. One is the writePWMDutyCycle() and the other is writePWMVoltage().

The writePWMDutyCycle() function is used in the manner described above. That is we specify the duty cycle for the pulse and send it on one of the Arduino PWM digital pins. The syntax for using writePWMDutyCycle() is,

writePWMDutyCycle(a, pin, dutycycle)

where a is the arduino object, pin is the PWM digital pin and dutycycle is the duty cycle

For example,
writePWMDutyCycle(a, 'D9', 0.8)

Another method of sending out PWM digital signal in arduino using matlab is writePWMVoltage() function. This has the following syntax:

writePWMVoltage(a, pin, voltage)

For example,

 writePWMVoltage(a, 'D9', 2)

This sends out PWM signal of 2V magnitude on pin 9.

In this tutorial we will not illustrate writePWMVoltage() function for PWM. For this visit the next tutorial on PWM -  Using PWM in arduino using Matlab's writePWMVoltage()

Example 1: Simple PWM signal

The program to send a simple PWM signal in arduino using the above two methods is then,

% create arduino object
a = arduino();

% send PWM signal using WritePWMDutyCycle() function of 0.8 duty cycle on pin 10

writePWMDutyCycle(a, 'D10', 0.8)

% send PWM signal using WritePWMVoltage() function of 2V on pin 10

writePWMVoltage(a, 'D10', 2)

For the demonstration of this example see the video below.

Example 2: Control the brightness of the LED using PWM signal

In this example we will control the brightness of the LED using PWM signal. First we will turn the LED slowing ON and then again OFF. The following is the matlab code for doing this,

dcyc_step = 1/20;

for k = 1:20
    writePWMDutyCycle(a, 'D10', k*dcyc_step);

for k = 1:20
    writePWMDutyCycle(a, 'D10', 1-k*dcyc_step);

Now see the following video which shows both these two examples.

If you liked this tutorial share it. And see also other arduino tutorials How to control LED using LDR using Arduino and Reading analog signal using Arduino Video demonstration.

No Comment to " working with arduino PWM in matlab "