Arduino Simulink Tutorial 2 – Reading Switch

How to read switch using arduino simulink using digital input block

In this Arduino Simulink Tutorial 2 – Reading Switch, you will learn how to read switch connected to arduino via simulink. To do this we use the digital input block from the Simulink Support for Arduino Hardware library.

In the Arduino Simulink Tutorial 1 – LED blinking, we showed how to blink a LED using arduino and simulink. This tutorial also contains details on how to download and install the Simulink Support Package for Arduino Hardware that you will need to work with arduino via simulink.

Hardware Setup for Reading Switch

Our switch reading circuit will be connected as follows. We will connect a switch to pin 8 in such a way that one side of the pin is connected to ground using 10KOhm resistor and the other side is connected to 5V supply. Also in order to see that the Arduino microcontroller is indeed reading the switch we will connect a LED to the Arduino to indicate the switch state. The LED will be connected to the pin 7 on one side and to the ground using 220Ohm resistor on the other side.

The circuit diagram is shown below and is basically the same as that in the tutorial digitalRead and digitalWrite – Arduino Proteus Tutorial

digitalRead and digitalWrite – Arduino Proteus Tutorial

 

Simulink Model for Reading Switch

To program the Arduino via simulink, open a new simulink model and save it with some name like arduino_simulink_2 in this example. Then in the new simulink model add Digital Input and Digital Output blocks from the Simulink Support Package for Arduino Hardware library and connect them.

This is as shown.

Arduino Simulink Tutorial 2 – Reading Switch Simulink Model

You will find the Digital Input and Digital Output blocks from the Simulink Support Package for Arduino Hardware > Common library as shown under.

Digital Input and Digital Output blocks from the Simulink Support Package for Arduino Hardware

The digital input block will be used for reading switch and the digital output block is for the LED.

Now we setup the digital input and output block default pin value. Double click on the digital input block and change the pin number to 8 and also change the sample time to 0.1. Similarly double click on the digital output block and change the pin number to 7.

arduino digital input block configuration arduino digital output block configuration

Simulink Configuration

Now we have to set up the configuration so that simulink uses the correct arduino board which is UNO and also set up the run time and simulink simulation configured to run with external device.

So first we setup the which board to use. For this go to the Tools > Run on target Hardware > Options as shown under.

simulink arduino board Configuration

There in the window which shows up, select Arduino UNO as your hardware and select ATMEL as your Device Vendor.

select Arduino UNO as your hardware and select ATMEL as your Device Vendor

Click apply and OK to exit.

Next we configure run time which typing in inf into the run time box. Also we change the simulation deployment to External. See the following figure.

run time and external deployment configuration arduino simulink

Now the simulink model is ready for simulation. Connect the Arduino USB to your computer. Click on Deploy to Hardware button or just the Run button to start the simulation. This will upload the program file via USB to your Arduino board and the Arduino pins are configured to read the switch and write the switch state to the LED. And this happens continuously.

Now we show how it looks like through the following video.

The video demonstrates that as the switch is pushed on the arduino reads the switch and then turns on the LED, all via simulink.

If you like this Arduino Simulink Tutorial 2 – Reading Switch, share it. Also subscribe to our blog and leave some comments if you have any.

See also our other arduino tutorials-

Leave a Reply

Your email address will not be published. Required fields are marked *