Getting Started
Introduction
Microcontrollers are an important topic for the Electronics Engineer. So many modern devices have a microcontroller at their core, even apparently “dumb” products such as toasters and electric fans. Specifying the right microcontroller for a specific task is a useful skill, so in our Electronic and Electrical Engineering degree program you will be exposed to a number of different microcontrollers.
As an introduction to the subject, we shall learn how to program a popular 8-bit microcontroller in the “C” language. The specific microcontroller we shall be using is the Arduino Nano, a low-cost board with a range of useful features. Before we can program it, however, we shall have to install the Arduino Integrated Development Environment (IDE) onto your computer. If you are using one of the PCs in room B107, the software is already installed. The software is free, so you can install it on your own computer if you wish. The IDE has been produced in several versions, which can be run on Linux, Windows, and Apple computers.
A vast amount of information about the Arduino project can be found on their website, arduino.cc, including downloads for the IDE. Many of you already have some experience using Arduino harware and software, and so may have the IDE already installed.
Our friends in the Swansea Hackspace have an extensive web site, which also offers guidance on getting started with Arduino projects. Their website can be found at swansea.hackspace.org.uk. If you click on “Activities” followed by “Learning” you will be offered a range of tutorials, including one on the Arduino. Click on “Introduction to Arduino” and you will find a detailed description of the device. You may find it useful to read through some of these pages before starting on the installation procedure detailed below.
Installing the Arduino IDE 2 on Your Computer
Windows
Modern Windows 10 and 11 machines are capable of finding the necessary driver for the CH340 interface chip automatically. A good strategy would be to start the Windows 10 machine, and when it is ready plug in the Arduino Nano board using the supplied lead. After a few minutes, type Device Manager into the search box, and run the application offered to you. You should see a list of devices, including “Ports (COM and LPT)”. Click on this item and look at the lines following. One of these should read “USB-SERIAL CH340 (COM5)” or similar. Make a note of the COM port number and close the Device Manager.
Next, unplug the Arduino Nano board. Go to arduino.cc (Figure 20.1) and select the tab “Software&rdquo. This will bring up a new page of options (Figure 20.2). Move past”Arduino Web Editor” and look at the section titled “Downloads” and the box labelled Arduino IDE 2.1.1. Look on the right-hand side and click on “Windows Installer for Windows 10 and newer”, which is the first item in the list. When the installation file has finished downloading return to the previous page using “back” on your browser. In the section marked “Arduino IDE 2.1.1” you will see the words “For more details, please refer to the Arduino IDE 2.0 documentation.” . Click on the link labelled Arduino IDE 2.0 documentation to open the Arduino IDE 2 documentation page (highlighted in Figure 20.3) and then select Downloading and installing the Arduino IDE 2 from the table of contents to open the Downloading and installing the Arduino IDE 2 page (Figure 20.4).
Follow the detailed instructions for Windows to start the installer. There will follow quite a lengthy installation procedure, during which you will be offered additional drivers for other members of the Arduino family, including the boards offered by the company Adafruit. We recommend installing these additional drivers. When the installation is complete, plug in the Arduino Nano board and click on the “Arduino IDE” icon that has appeared on your desktop.Linux
Fortunately, all Linux systems have the necessary drivers in place already, so it is only necessary to install the Arduino IDE application.
Go to arduino.cc and select the tab “Software”. This will bring up the Arduiono IDE 2 page (Figure 20.2). Move past the instructions for “Arduino Web Editor” and look at the section titled “Downloads”. In the section marked “Arduino IDE 2.1.1” you will see the words “For more details, please refer to the Arduino IDE 2.0 documentation.” . Click on the link labelled Arduino IDE 2.0 documentation to open the Arduino IDE 2 documentation page (highlighted in Figure 20.3`) and then select Downloading and installing the Arduino IDE 2 from the table of contents to open the Downloading and installing the Arduino IDE 2 page (Figure 20.4). Follow the detailed instructions for Linux to start the installer.
There will follow quite a lengthy installation procedure, involving unpacking the tar file, and running the script “install.sh”, either from the command line or using “Run in terminal” from “Files”. During the installation you will be offered additional drivers for other members of the Arduino family, including the boards offered by the company Adafruit. We recommend installing these additional drivers. When the installation is complete, plug in the Arduino Nano board and click on the “Arduino IDE” icon that has appeared on your desktop.
Apple (MacOS)
Once again, go to arduino.cc and select the tab “Software”. This will bring up the Arduiono IDE 2 page (Figure 20.2). Move past the instructions for “Arduino Web Editor” and look at the section titled “Downloads”. In the section marked “Arduino IDE 2.1.1” you will see the words “For more details, please refer to the Arduino IDE 2.0 documentation.” . Click on the link labelled Arduino IDE 2.0 documentation to open the Arduino IDE 2 documentation page (highlighted in Figure 20.3`) and then select Downloading and installing the Arduino IDE 2 from the table of contents to open the Downloading and installing the Arduino IDE 2 page (Figure 20.4). Follow the detailed instructions for MacOS to start the installer.
Initial set up
When you are satisfied that the Arduino IDE has been successfully installed, run the application. Assuming that the installation procedure described above has gone to plan, and after the splash screen, the Arduino IDE main screen (Figure 20.5) appears.
Plug in the Arduino Nano board (Figure 20.6) using the supplied lead. Next look at the dropdown control labelled Select Board. Identify the connection which depends on your operating system but will be identified as a USB connection on MacOS and Linux, or may be labelled COM 5 (or some other number) on Windows (see Figure 20.7).
Once you have selected the correct port, you need to identify the board you are using. Search for nano as shown in Figure 20.8.
Once you have set up your board, the board idenitfier changes to Arduino Nano in the menu bar as shown in Figure 20.9. You are now ready to compile your first program.
The Structure of an Arduino Program
Arduino provides a useful program stucture that is called a sketch. The structure of a sketch is illustrated in Figure 20.10.
After the program starts, the setup subprocess (subroutine) is called once, and then the loop subprocess is called over and over again until the processor is restarted or power removed. Note there is no end state for the sketch program.
In the setup subprocess, the microcontroller will be set up for the application it is to execute. In the labs to follow, this will be to ensure that the IO is correctly configured.
In the loop process, we will set up the activities that will read inputs from the IO and provide suitable outputs. We will provide flow-charts for the examples that follow and you are expectd to provide flow diagrams for any modified programs in your lab diary.
You may wish to note that I used PowerPoint flowchart shape library to draw the flowcharts in this document.
On the blink
The first program we shall run rejoices in the unlikely name of Blink. Click on File
and select Examples
from the drop-down menu. A further menu appears, with all the example programs. Select 01.Basics
, followed by Blink
(Figure 20.11).
Select Blink
. The program appears in a new IDE window (Figure 20.12`). The program at this stage is just text. It needs to be compiled in order to create an executable file.
Flowchart for Blink
The flowcharts for the blink program are shown in Figure 20.13 and Figure 20.14. These are the contents of the Arduino sketch suprocesses setup and loop shown in Figure 20.10.
In setup (Figure 20.13), the pin that is connected to the built-in on-board LED (LED_BUILTIN
), is setup as an output1.
In loop (Figure 20.14), the built-in LED is turned on as indicated by the I/O block. There is then a one second (1000 ms) delay indicated by the first subprocess. The built-in LED is then turned off, and there is another 1 second delay. Because the whole of the loop sub-process ends at this point, the return to the top of the sub-process shown in Figure 20.10 means that the whole process is repeated. The effect is that the LED will “blink” at the rate of 1 second on and 1 second off.
Code for Blink
The code for blink is reproduced below in Listing 20.1. It is clear how the program represented by the flow-charts in Figure 3.12, Figure 20.13 and Figure 20.14 are turned into subroutine calls in the ‘C’-program. Note that much of the code is comments
Compiling Blink
Identify the icon for compiling the program, this is a tick in a blue circle which is called Verify on the Arduino IDE. Click this once, and after a short period of time the message “Done compiling” should appear near the bottom of the window. Hopefully, as we are compiling an example program, there will be no errors! If there are errors, information on the type of error and where it has occurred in the program can be found in the dialogue box near the bottom of the window.
Uploading Blink
Next, we need to transfer the executable file to the Arduino Nano board. This is done by identifying the icon Upload, which is a right arrow in a blue circle, and clicking it once. Annoyingly, the IDE insists on re-compiling the program. If all has gone to plan, the message “Done Uploading” appears. Then after a few seconds the executable file starts running on the Nano board. One of the LEDs begins flashing at a regular rate, one second on, one second off.
Modifying Blink
So far, so good.
Now, let us try changing the program. Look at the text screen, the “source code”. You will find two instances of the instruction delay(1000)
.
Move the cursor and change the argument of the two instructions to 100
. Now, with your new-found knowledge, re-compile the program and upload it to the Nano.
When the message “Done Uploading” appears, look at the LED on the Nano. It should be flashing at ten times the previous rate.
Congratulations! You have successfully edited the program.
You should save the edited program by clicking on File, selecting “Save As” and when the dialogue appears, give the program a new name, for example “myblink”.
How would you change the flow chart in Figure 20.14 to document the change you made in the code? Put a copy of your modified flow-chart and a code listing in your lab diary. You will find a copy of the PowerPoint that we used to create the flow charts in this chapter on-line by following this link: eg-151-textbook/website/labs/get_started/pictures/flow-charts.pptx. You can use the diagrams in that PowerPoint file as a template for your own flow-charts.
You are now ready to attempt the first laboratory exercise: Experiment 1: Binary Counter.
The built-in LED on the Arduino Nano board is connected to digital pin 13 which is GPIO pin 5 on port B. This port is not connected to any of the pins that come out of the Ardino Nano board (see Figure 3.12) and are available for connection to external I/O devices.↩︎
Copyright © 2021-2024 Swansea University. All rights reserved.