ad space

Push Button controlling LED - Programming Arduino using Matlab 2

 In this programming Arduino using Matlab tutorial, you will learn how to turn on and off a LED using a push button. When the push button is pressed, the LED will turn on and when the push is not pressed the LED will remain off. This all is done via Matlab code, code is executed from within Matlab and command is sent to Arduino. The push button and LED are connected to Arduino UNO. When the Arduino receive the compiled code from Matlab it will execute the program.

In order to follow this Arduino Matlab project, you need to install Matlab support package for Arduino hardware. If you don't have this already see the tutorial Programming Arduino using Matlab/Simulink setup. This is our second part of the tutorial series Programming Arduino using Matlab. To see the first part of the tutorial see led blinking tutorial.

1. Hardware setup

The following pictures shows the hardware connection and schematic diagram between the push button, led and the Arduino UNO. One end of the push button is connected to digital pin 10 which is pulled high by external 10KOhm resistor. The other end of the push button is connected to ground. Thus in normal situation the pin 10 of Arduino is pulled high and when the push button is pressed, it is grounded, that is the digital pin will become low. Similarly, the led positive terminal is connected to the digital pin 8 and the negative terminal grounded via 220Ohm resistor.

On a breadboard the hardware connection of LED, resistors, push button and Arduino UNO looks like the following.

Push Button controlling LED - Programming Arduino using Matlab 2

The Arduino push button led wiring diagram is as shown below.

arduino push button led circuit diagram

2. Create a new script file

You may want to create a new script file because you can later use it. Also it is beneficial because you can turn it into a function file. Here we have create a new script file called led_pushbutton saved with extension .m.

3. Create Program code

We will create a Matlab program code that reads the state of the push button state and accordingly turns on or off the LED.  If the state of push button is high then the LED is turn off. If the state of push button is low then the LED is turned on. This is so because, in default state the push button is in high state in which case or default case we want the LED to be off. When the push button is pressed, it is grounded, becomes low and in this case we want to turn on the LED.

Below is the program code:

clear;
clc;

pushbtn = 'D10';
led = 'D8';

ard = arduino();

configurePin(ard, pushbtn, 'DigitalInput');

disp('press Ctr-C to exit');    

while 1
    btnstate = readDigitalPin(ard,pushbtn);
    writeDigitalPin(ard,led,~btnstate);
end

In the above code, we used clear and clc Matlab command to remove any existing variables in the Matlab workspace and clear the matlab prompt command window screen. Otherwise you might get error that there already exist an object for the Arduino board. Then we declared pushbtn and led as alias name for digital pin 10 and 8 respectively. This helps us to identify which pin is being referred later in the program and also this makes easy to later modify pin if we wanted to use another pin. using ard = arduino() we created an arduino object called ard. Once ard object has been created, we have used configurePin() function to make pushbtn as a digital input pin. The disp() function was used to display the user the message that to exit out of the program that they should use crtl+c keys. Then using the endless while loop we read in the pushbtn state using the readDigitalPin() function and store that value in btnstate variable. Then using the writeDigitalPin() function we send out the negated value of the btnstate because in our case, the push button is in normally high state and we want to turn on the LED when the push button is in low state.

Video Demonstration

Watch the following video to see how this works in real time.



Post a Comment

Previous Post Next Post