Accelerating the pace of engineering and science

# Applied Project-Based Learning: Building Applications for Low-Cost Hardware

By Davide Ferraro, MathWorks

Project-based learning is a comprehensive teaching and learning approach that engages students in the investigation of authentic engineering problems1. To make this approach effective, the teacher must identify a problem that is accessible but challenging enough to stimulate the learning process. Unfortunately, many hardware applications are too complex, distracting students from the real task with low-level hardware issues.

MATLAB® and Simulink® address this challenge by making it easy for students to implement their projects on low-cost hardware platforms. This article describes three project-based learning activities that demonstrate the range of approaches and skills that can be taught with MATLAB and Simulink.

## Two Approaches to the Arduino Blink Challenge

A problem may be solved at different levels of abstraction and using different implementation strategies. Let's consider a popular example of a basic application involving hardware: the Arduino Blink Challenge. In this popular example Arduino®, an open-source, single-board microcontroller, controls the blinking logic of several LEDs. Through a potentiometer and a button, the user controls the blinking mode (on, off, simultaneous, or sequential), frequency, and speed.

The problem can be solved using two very different styles: coding and modeling. The coder solves the problem by developing an algorithm, setting up decision logic, and writing code; the modeler solves it by creating a system diagram highlighting connections and information flows. MATLAB and Simulink support both approaches.

## Solving the Blink Challenge Using Code

With MATLAB Support Package for Arduino, students can establish a tethered connection with the board to acquire information from sensors (a button and a potentiometer), process this information through an algorithm, and send it back to the actuators (LEDs). With this package, the user’s algorithm runs on the computer in MATLAB and Simulink, and the Arduino processes requests to read or write from its I/O pins.

Communication with the board is reduced to the following straightforward commands:

1. Establish a connection with the board:
```a = arduino('COM7');
```
2. Define the role of each pin:
```a.pinMode(3, 'INPUT');
a.pinMode(5, 'OUTPUT');
```
3. Read sensor input and write actuator output:
```ain = a.analogRead(aPin);
a.digitalWrite(led(i),state);
```

This simplified communication approach enables the student to concentrate on algorithm implementation. The compact and flexible MATLAB language supports many different implementations based on classic programming constructs, such as loops and conditional statements, as well as more specialized algorithms for different projects, such as data analysis and signal processing.

## Solving the Blink Challenge Using a Model

Writing code for an embedded system can be extremely complex: Students must use the proper language and take care of data types and interfaces. With Simulink they can design the system in a graphical environment (Figure 1) and then automatically translate this representation into an executable to be deployed on the board.

They can download a set of libraries, examples, and documentation to help them deploy their models on different hardware platforms, including Arduino (UNO and MEGA 2560), LEGO® MINDSTORMS® NXT, BeagleBoard, and PandaBoard.

A possible Simulink implementation for solving the Blink Challenge consists of three main subsystems:

• Inputs from Arduino. Students can add analog and digital input blocks for acquiring information from the potentiometer and the button.
• Modes (Figure 2). Thanks to the large library of blocks, students can design a scheduler that controls the operation modes.
• LEDs. Students can use PWM Arduino blocks to control the outputs and switch the different LEDs on and off.
Figure 2. Modes subsystem. Conditional subsystems can react to user input in a timely manner.

The students run the complete model on the board automatically by selecting Tools > Run On Target Hardware > Prepare To Run. This option lets users select the target hardware, host COM port, and other simple configuration settings. To start the executable application generation and deployment process that will transfer the model to the board, they select Tools > Run On Target Hardware > Run. The board is then programmed with their algorithm and can run autonomously, without any connection to the PC.

This one-click approach to executable generation lets the student concentrate on system modeling. Applications from different fields, such as controls, image processing, computer science, and signal processing, can be easily experienced interacting with the hardware platform. The student is immediately able to design, simulate, and test on the hardware without facing all the low-level issues related to the interaction with the board.

## Introducing Robotics and Model-Based Design with LEGO MINDSTORMS NXT

A robotic application requires mechanical design, signal processing, task scheduling, and control tuning. As a result, and thanks to its multidisciplinary nature, a robot is a perfect platform for project-based learning.

LEGO MINDSTORMS NXT provides all the tools the students need to develop simple robots. A set usually includes a control brick based on an ARM processor, as well as servomotors, sensors, and construction elements. Using this set, students can build the robot by themselves and then design and test it on the hardware.

A project like this provides a perfect opportunity to introduce the four major components of Model-Based Design: modeling, simulation, code generation, and testing (Figure 3).

Figure 3. Simplified workflow using Model-Based Design.

Students begin by creating a system model in Simulink. To deploy the model on the robot they can use the additional libraries described in the section on solving the Arduino Blink Challenge. They can simulate the model to understand the robot’s behavior, and tune parameters and controllers to enable it to perform the required tasks.

Using Simulink support for target hardware they can automatically convert the model into an executable application for deployment on the LEGO MINDSTORMS NXT control brick.

They can simulate and test the algorithm and control logic on the computer and then compile it for the hardware. By enabling External Mode, students can establish a Bluetooth connection with the robot so as to change parameters during execution.

A simple application based on the LEGO MINDSTORMS NXT Robot in the Tribot configuration will illustrate this workflow. In this example, the goal is to move the robot forward or backward a certain distance. Before working with the actual hardware, the students create a model of the robot servomotors and design a PID controller using Simulink PID autotuning features (Figure 4). They use this model to design the controller before assembling the actual robot.

Figure 4. Tribot model for simulation and control design. The PID block can be automatically tuned to meet system response time requirements.

When the controller is ready, they replace the simulation subsystem with an Input-Output model based on the LEGO MINDSTORMS NXT Motor and Encoders blocks. This model can be used to generate an executable to be deployed on the robot. Students can test different working conditions for the system by enabling External Mode and changing the position of the Tribot during execution.

## Making Objects See: Introducing Computer Vision with BeagleBoard

Working with hardware offers opportunities for students to develop specific skills. In digital image processing courses, for example, the students learn how an image is acquired, interpreted, and processed through algorithms. They can practice on real applications working with image acquisition and processing instrumentation.

In this sample computer vision project, the students create an object detection algorithm in Simulink and deploy it on BeagleBoard, a popular single-board PC with an ARM® processor and a Linux® operating system.

The image is acquired from a standard webcam connected to the board. It is then processed through a color space conversion algorithm based on blocks in the Computer Vision System Toolbox™ library. The algorithm extracts the Chroma component, performs a logical comparison with a threshold to define the item shape, applies a median filter to preprocess the data, and then executes a blob analysis to identify the object shape. The image is then sent back to a video output to display the bounding box around the identified object (Figure 5).

Figure 5. Red object detection algorithm.

As we have seen with Arduino and LEGO MINDSTORMS NXT, this algorithm can be automatically compiled into an executable application and deployed on BeagleBoard. Using External Mode students can tune the threshold manually during simulation in order to adapt the process to different light conditions.

## Extending Project-Based Learning

Through C and HDL code generation, MATLAB and Simulink can target a broad array of hardware platforms. For many platforms, including Arduino, BeagleBoard, PandaBoard, and TI's C6000™, students can include target-specific functionality, such as processor-dependent algorithms, timer support, and pin-level I/O.

The examples described here are only the starting point for a lesson, a laboratory, or a longer project. Students may develop control applications on Arduino for innovative products, participate in LEGO robot competitions such as ET Robocon, or develop vision systems for futuristic applications such as drones and self-driving cars. With MATLAB and Simulink they can design applications to be deployed on the hardware with ease, enjoying all the fun of making something realistic and innovative.

1 “Motivating Project-Based Learning: Sustaining the Doing, Supporting the Learning.” Educational Psychologist Volume 26, Issue 3-4, 1991.

Published 2013 - 92094v00