Tutorial: Introduction to Modeling with Arduino Boards

Learn how to create a model and generate code that controls an LED light on an Arduino Uno board.

Files for This Tutorial

button_led_2sec.scm

The model for this tutorial is available through the Demo Browser. From the ribbon, select View > Demo Browser > Arduino > button_led_2sec.scm.

Overview of the Hardware and Circuit

These components are required to complete the tutorial:

Hardware

  • Arduino Uno board
  • Breadboard + wiring for connecting components
  • LED light
  • 10K-ohm resistor
  • 100-ohm resistor

Circuit

Figure 1. Circuit illustration

Figure 2. Physical components connected to the breadboard

Create the Activate Model

The behavior of the hardware button and LED light is defined in the following Activate model:

The model contains the following components:
  • Arduino Configuration block: This block is set in simulation mode to open the serial communication port with the Arduino board.
  • Button block: This block represents the hardware button connected to pin 11 on the Arduino board.
  • LED block: This block represents the hardware to which the LED light is connected to pin 10 of the Arduino board.
  • LED Button super block: This block contains the dynamics of the system, including the super blocks led off and led on. When the button is pressed, the LED light switches on for two seconds, then switches off automatically.

The model uses a state machine construction method in Activate, which enables the model to include two different states: one where the light is on (defined by the super block led on) and another where the light is off (defined by the super block led off). The led on and led off super blocks are located inside of the LED Button super block.

See the Activate Extended Definitions for more information on the state machine construction method.

Figure 3. System constructed with two states (expanded LED Button super block)

Figure 4. Contents of the yellow led off super block

Initially the led off super block is activated through its activation input port. This process changes the state number to 2 and sets the led signal to zero. Subsequent activations come through the activation signal A2. No output activation is generated until the button signal becomes positive. Once the output activation is signaled on, it enters the led on super block.

Figure 5. Contents of the red led on super block

The on signal changes the value of the state to 1, and thus deactivates the state represented by the led off super block and activates the led on super block state. It also sets the signal led to 1, which turns on the LED light and resets the counter.

The subsequent and successive activation of A1 signals then increments, through tick input, the counter until the desired value is reached, which in this case is 2 seconds.

Steps for Simulation

  1. In the simulation setup of the model, set the real-time scale to 1. This value indicates that 1 unit of Activate simulation time corresponds to 1 second.
  2. Connect the Arduino board to a USB port of the computer system.
  3. Double-click the ArduinoConfiguration block. The connected serial communication ports are listed. Use the port connected to the Arduino board.
  4. With the Arduino IDE, open the file: <activate_installation>/hwx/databases/activate/system/Arduino/_bin/arduino.ino.
  5. In the Arduino IDE, install the required libraries if they are not already installed.
  6. In the Arduino IDE, compile the file arduino.ino and upload the sketch to the Arduino board.
  7. Return to Activate, then click the Run button.
    The simulation runs for 30 seconds.
  8. On the breadboard, press the hardware button and observe how the light turns on and off.

Steps for Code Generation

  1. Set the status of the ArduinoConfiguration block to off (This step avoids errors if the Arduino board is not connected.).
  2. Create a super block to contain the top level diagram.
    1. Select all blocks that are at the top level of the diagram.
    2. Click the Super Block tool and create a super block with the selected blocks.
  3. Select the super block you created and execute the function vssGenerateInoFile.
  4. Open the created .ino file with the Arduino IDE.
  5. From the Tools menu, select the card and the ports.
  6. Compile and upload the sketch to the card.
    Once compiled and downloaded to the Arduino board, the code can run standalone without connection to the Activate model.