|
| 1 | +--- |
| 2 | +title: 'Controlling PWM Output with a Potentiometer via MATLAB®' |
| 3 | +difficulty: intermediate |
| 4 | +compatible-products: [Arduino Uno Rev 3, Potentiometer] |
| 5 | +description: 'Learn how to dim an LED connected to the UNO R3 with MATLAB®.' |
| 6 | +tags: |
| 7 | + - MATLAB |
| 8 | +author: 'Mohammed Hussain Jahangiri' |
| 9 | +source: 'community' |
| 10 | +hardware: |
| 11 | + - hardware/boards/arduino-uno-rev3 |
| 12 | +software: |
| 13 | + - MATLAB |
| 14 | +--- |
| 15 | + |
| 16 | +## Introduction |
| 17 | + |
| 18 | +In this tutorial, we will use MATLAB® to turn on an external LED, and then to adjust the output using MATLAB® Support Package for Arduino® Hardware. |
| 19 | + |
| 20 | + |
| 21 | +### Goals |
| 22 | + |
| 23 | +The goals of this project are: |
| 24 | + |
| 25 | +- Control the UNO R3 LED with MATLAB. |
| 26 | +- Control the UNO R3 PWM output with MATLAB. |
| 27 | +- Create a dimmable light using a potentiometer. |
| 28 | + |
| 29 | +### Hardware & Software Needed |
| 30 | + |
| 31 | +- [UNO R3](https://store.arduino.cc/products/arduino-uno-rev3) |
| 32 | +- [USB 2.0 Cable Type A/B](https://store.arduino.cc/products/usb-2-0-cable-type-a-b) |
| 33 | +- Potentiometer trough-hole |
| 34 | +- Valid MATLAB® licence |
| 35 | +- [MATLAB® Support Package for Arduino® Hardware](https://www.mathworks.com/matlabcentral/fileexchange/47522-matlab-support-package-for-arduino-hardware) |
| 36 | + |
| 37 | + |
| 38 | + |
| 39 | +***A valid MATLAB® licence is needed. Your workplace or education institution may have a subscription. Alternatively, a one-year trial subscription to MATLAB® is included as part of the [Arduino Engineering Kit R2](https://store.arduino.cc/products/arduino-engineering-kit-rev2).*** |
| 40 | + |
| 41 | + |
| 42 | +### Why MATLAB? |
| 43 | +MATLAB® is an educational and industrial programming platform used to analyse data, perform simulations and carry out model based designs. Through an interactive communication with an Arduino board, you can expand MATLAB®'s capabilities, while also gaining access to a wide range of math, engineering and plotting functions. Check out the capabilities of MATLAB® over on the [MathWorks website](https://www.mathworks.com/solutions.html#capabilities). In this tutorial, we will establish the connection with MATLAB® via a USB and Wi-Fi connection. The [MATLAB® Support Package for Arduino Hardware](https://www.mathworks.com/matlabcentral/fileexchange/47522-matlab-support-package-for-arduino-hardware) must be installed (which may require a MathWorks® account), in order to facilitate the communication between the MATLAB® software on your computer and your Arduino board. |
| 44 | + |
| 45 | +## Connecting the Board to MATLAB |
| 46 | +**1.** Connect your board to the computer via the USB 2.0 Cable Type A/B. |
| 47 | + |
| 48 | + |
| 49 | + |
| 50 | +**2.** Open MATLAB and run the command `arduinosetup()` in the Command Window. |
| 51 | + |
| 52 | + |
| 53 | + |
| 54 | +**3.** A Graphical User Interface (GUI) appears, that will help you set up the MATLAB®-Arduino connection. To establish the connection over the USB cable, make sure that the USB radio box is selected and then click on Next. |
| 55 | + |
| 56 | + |
| 57 | + |
| 58 | + |
| 59 | +**4.** Choose the "UNO" from the dropdown menu, as well as the relevant COM port, and click on the blue `Program` button to upload the Arduino Server to the board. When you have done so, click on next. |
| 60 | + |
| 61 | + |
| 62 | + |
| 63 | + |
| 64 | +**5.** In this step you can review the connection type, Port, board and loaded libraries. You can also click on Test connection to evaluate the Arduino-MATLAB® connection. Next, you should see a Green check-mark signalling the successful connection, as shown in the image below. |
| 65 | + |
| 66 | + |
| 67 | + |
| 68 | + |
| 69 | +**6.** The UNO R3 is now configured to interact with MATLAB®. Now to create the object in MATLAB®, we run the command `a = arduino()`. The properties of the object, including the COM port, are displayed as shown in the example below. |
| 70 | + |
| 71 | + |
| 72 | + |
| 73 | +**7.** Enter the command `writeDigitalPin(a, 'D13', 1);`. This command is similar to the `digitalWrite(D13, HIGH)` by the Arduino programming language. Which means that digital pin 13 **D13** is connected to the built-in LED, `a` is the Arduino object we created, and 1 represents a HIGH or ON state. Try turning the LED ON and OFF several times by changing 1 to 0 and vice-versa. |
| 74 | + |
| 75 | + |
| 76 | +***Unlike in the Arduino IDE, here you cannot address the built-in LED using the `LED_BUILTIN` macro.*** |
| 77 | + |
| 78 | + |
| 79 | +**8.** Now, you can use this command to continuously blink an LED as part of a `while` loop. Try entering the script below. |
| 80 | + |
| 81 | +``` |
| 82 | +while (1) |
| 83 | + writeDigitalPin(a, 'D13', 0); |
| 84 | + pause(0.5); |
| 85 | + writeDigitalPin(a, 'D13', 1); |
| 86 | + pause(0.5); |
| 87 | +end |
| 88 | +``` |
| 89 | + |
| 90 | + |
| 91 | +## Circuit |
| 92 | + |
| 93 | +On one breadboard there is a white jumper wire sending 5 volts to the potentiometer. The orange cable is connected to GND(Ground) and the gray wire is our input to A0. A0 will read the variable voltage coming in from pin13 after being adjusted through the potentiometer. |
| 94 | +On the right breadboard we have a simple LED circuit comprised of a red output wire from pin11, a resistor and the LED finally connected to GND via the green wire. |
| 95 | + |
| 96 | + |
| 97 | + |
| 98 | +***Make sure to connect the longer leg of the LED (anode) to the resistor and the shorter (cathode) to GND.*** |
| 99 | + |
| 100 | +## Programming the Board |
| 101 | + |
| 102 | +**1.** Set your output to 1 in the previously mentioned way using `writeDigitalPin(a, 'D13', 1);`. As mentioned earlier, 1 means ON or HIGH and sets the digital pin 13 **D13** to 5V allowing the current flow towards the LED. Check to see if the analog pin 0 **A0** is actually reading an adjustable voltage using `readVoltage(a, 'A0');` in a loop and then use `display(Volts)` to print the value of `Volts` on the "Command Window". |
| 103 | + |
| 104 | +``` |
| 105 | +a = arduino(); |
| 106 | +writeDigitalPin(a, 'D13', 1) |
| 107 | +while(1) |
| 108 | + Volts = readVoltage(a, 'A0'); |
| 109 | + display(Volts); |
| 110 | + pause(0.5) |
| 111 | +end |
| 112 | +``` |
| 113 | + |
| 114 | +The amount assigned to `Volts` should be changing as you rotate the potentiometer's knob. |
| 115 | + |
| 116 | + |
| 117 | + |
| 118 | +**2.** Set the value of `Volts` as the voltage to be set on digital pin 11 **D11** using the command: `writePWMVoltage()`. This command is adjustable between 0 and 5 volts. It will convert the value to a PWM Signal on the digital pin. |
| 119 | + |
| 120 | +``` |
| 121 | +a = arduino(); |
| 122 | +writeDigitalPin(a, 'D13', 1) |
| 123 | +while(1) |
| 124 | + Volts = readVoltage(a, 'A0'); |
| 125 | + writePWMVoltage(a, 'D11', Volts) |
| 126 | + display(Volts); |
| 127 | + pause(0.5) |
| 128 | +end |
| 129 | +``` |
| 130 | + |
| 131 | +### Troubleshoot |
| 132 | + |
| 133 | + |
| 134 | +- If the code gets frozen or seems to not be working try to clear previous Arduino objects linked to the device. You can do this by running the command `clear <object>`, for example `clear a` will clear the Arduino object. |
| 135 | +- If you get the message `Unrecognized function or variable 'arduinosetup'` ensure that you have installed the [MATLAB® Support Package for Arduino Hardware](https://www.mathworks.com/matlabcentral/fileexchange/47522-matlab-support-package-for-arduino-hardware). |
| 136 | + |
| 137 | + |
| 138 | +## Conclusion |
| 139 | + |
| 140 | +You can now control your output using variable inputs to control your devices and make use of the powerful features for scientific computing and developing engineering applications! Different input modulators similar to the potentiometer processed by the various toolboxes offered my MATLAB can lead to infinite types of output beyond that of the brightness of an LED. |
| 141 | + |
| 142 | +## Further Ideas |
| 143 | + |
| 144 | +- You can also use the command line arguments to upload the MATLAB® server to the Arduino® UNO via the `arduino()` command. See more information on using this function in the [MathWorks® documentation](https://www.mathworks.com/help/supportpkg/arduinoio/ref/arduino.html). |
| 145 | +- Try writing a MATLAB® code that slowly increases and then decreases the blink speed instead of adjusting the brightness. |
| 146 | +- Use different sensors and parts instead of the potentiometer and LED, for example a light sensor. |
0 commit comments