Wednesday, March 4, 2020

SETTING UP THE ARDUINO ENVIRONMENT


Tools needed:
1.    PC running Windows Operating System (OS)
2.    Internet access
3.    Arduino UNO board
4.    USB Cable

Setting up Arduino IDE:

In this lab session, besides being able to program a PC, which is a rather large piece of hardware we will also learn to program a miniature version of a PC, built on a single IC chip, the size of a nail! The one we are using is called Arduino UNO. For ease of access the chip comes built on a board as a usable kit. But just as with Windows based PC, we need to set up an IDE that allows us to write code for it. The IDE is set up on a Windows based PC but the finalized software will be run on the minuature PC on the board once it gets downloaded to it. All this is achieved through the Arduino IDE.

Before we set up the Arduino IDE, let’s get familiar with the hardware we’ll be using.
The Arduino Board:

A picture explaining layout of important sections is given below. Try to match each feature to the actual one on the board.



A brief description of various components on the kit is given. You do not need to memorize the information. Important and relevant information will be explained well when it will be needed:

Input/Output pins:Each of the 14 digital pins on the Arduino UNO can be used as input or output pin, using pinMode(), digitalWrite(), and digitalRead() functions. They operate at 5 V. Each pin can provide or receive a maximum of 40 mA and has an internal pull-up resistor (disconnected by default) of 20-50 kΩ.

Serial communication:pins 0 (RX) and 1 (TX) are used for serial communication to receive (RX) and transmit (TX) TTL serial data.


External Interrupts: pins 2 and 3. These pins can be configured to trigger an interrupt on a low value, a rising or falling edge, or a change in value

Pulsewidth Modulation: pins 3, 5, 6, 9, 10, and 11, provide 8-bit pulse width modulation output with the analogWrite() function.

LED: The power LED shows the board is powered up when it glows. There are few LEDs on the board that blink when code is uploaded to the board. Then there is an LED connected to digital pin 13. When the pin is HIGH value (5V), the LED is on, when the pin is LOW (0V), it is off.

Analog pins:The Arduino UNO has 6 analog inputs, labeled A0 through A5. By default they measure from 0 - 5 V.

Reset:Bringing this line LOW (0V) resets the microcontroller.

USB Cable:
The USB cable (that you will get with the kit) connects the USB type B socket with the USB type A socket on you PC. It also provides power to the board. We do not need external power supply when the kit is connected with the PC through this cable. In standalone mode, however, the kit must be powered by a source providing 9 to 12 V @ 2A.

The various steps needed to set up Arduino IDE are:

1.    Get Arduino IDE:
Arduino IDE can be downloaded from the Internet (web) from https://www.arduino.cc/en/Main/Software, Scroll down a page until you see:


Click "Windows Installer" and download the Arduino IDE (version 1.8.3). It is a 89.6 MB sized file named "arduino-1.8.3-windows.exe".

Note:
In case the link is not accessible or not working, Arduino IDE installers are also available in the shared Google Docs folder. Ask your instructor to help you get the file on to your PC.

2.    Install it:
Run the downloaded file. It required at least 417.1 MB space on your computers hard disk's drive C to install it. Continue the installation with default options and click the appropriate button to move forward with the installation.


Click on Install on the windows that pop up asking for various software installs.
Click Close once done.

The whole installation process typically takes less than 10 minutes (after the file has been downloaded and run).

Arduino IDE can be run by clicking the Arduino icon on the Windows desktop.

3.    Add iostream library:
We have written some supporting code to allow Arduino Kit to take input from Windows keyboard and display output to Windows screen. The supporting code is enclosed in two files, viz., iostream.h and iostream.cpp, that we have packed in a compressed file named iostream.zip and available on SLATE in Resources -> Arduino.

Get the iostream.zip file from SLATE. Open it in Windows. In another window, go to folder "C:\Program Files (x86)\Arduino\libraries\" and create a folder titled iostream. Copy all files from the zip archive and paste them in the folder "C:\Program Files (x86)\Arduino\libraries\iostream" folder on your PC.

4.    Connecting Arduino Kit to PC:
After successfully installing Arduino IDE, you need to connect the Arduino UNO Kit (sometimes breifly referred to as Arduino Kit, Arduino Mainboard, Arduino Board or simply Arduino) using the USB cable to your PC.



Click the Arduino icon on Desktop and run the Arduino IDE now. A first run may look like this:


Note that at the bottom of the window a Serial Communication (COM) Port Number is specified, e.g., COM14 in the above screenshot, this must be the port number at which your Arduino connects to your PC. If this is not correctly specified, you will not be able to communicate with the kit. So select this COM port for the Arduino.

Important Tip:
Note the COM port # may be different for your system than what is shown here. You should use the COM port # that is for your system, even if it is different than what is shown in this manual.



Click Tools --> Board
See that Arduino/Genuino UNO (or something similar) is selected.

Click Tools -->Port and select to put a checkmark by the correct COM port number (identified by the label Arduino/Genuino Uno next to it).


Click Tools --> Board Info
A small window should open showing some basic info about the connected board


If it does display something similar to the figure above your board is properly configured and connected and you are ready to proceed. Note that numbers will be different to that shown above. You may skip Step 5 below and proceed to Task # 1.

If it does not bring up the Board Info display, your board is not properly connected and/or configured. Redo the steps to make sure you didn’t miss a step. If it still doesn’t work, proceed to Step 5 below.



5.    Ardunio Kit Drivers:
The PC usually detects connection of a USB device by itself and would attempt to install software needed to interact with it, often referred to as the "driver", automatically. Let Windows do it. In case it fails, perform the following steps:

Click Windows Start button --> Right Click on Computer --> Click on Properties --> Click Device Manager --> Click on other devices (if not already showing Unknown device) --> Right Click on Unknonw device --> Right Click Update Driver Software ... --> Click Browse my computer for driver software --> Browse to the folder "C:\Program Files (x86)\Arduino\drivers" --> Click Next and complete update


Get help, in case you cannot get Arduino IDE to run.



Task # 1: Writing code in Arduino IDE, compiling, downloading and running it:

Run Arduino IDE.Create a file named alab01_1.cpp. Write the following code verbatim into the space to write.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include<iostream.h>
using namespace std;

void setup()
{
  pinMode(13, OUTPUT);    //set the pin13 as output
}

void loop()
{
  digitalWrite(13, HIGH); //set the LED on
  delay(1000);            //wait for 1000 ms
  digitalWrite(13, LOW);  //set the LED off
  delay(1000);            //wait for 1000 ms
}
Code: alab01_1.cpp

1.    Compiling code in Arduino IDE:
Note that a program in Arduino environment is called a “sketch” and the compilation process is called “verification”, Sketches have to be compiled and uploaded to the board before the code can be successfully run.

After writing code in the alab01_1.cpp save the file and click Sketch-> Verify/Compile  or press Ctrl + R keys or clicking the checkbox symbol on the IDE to compile.

If you have not copied the iostream folder as mentioned earlier, you will get an error like:

...\alab01_a1.cpp\alab01_a1.cpp.ino:1:21: fatal error: iostream.h: No such file or directory

Else it will compile successfully.

2.    Uploading & Running code on Arduino Kit:

You can now upload the code on the Arduino UNO microprocessor (a mini computer) on the Arduino Kit by clicking Sketch->Upload, pressing Ctrl + U keys or clicking the arrow symbol on the IDE. Verify that the board is properly connected (an LED shines on it) and properly configured as explained earlier.

The application (not the code file) file will be uploaded to the Arduino UNO and starts executing (running) right away. You will see an amber LED light blinking continuously.

Note:
If the Arduino UNO losses power it stops working. However, as soon as it is powered up again, it starts running the previously uploaded code stored in its memory!

To prove this point, remove the Arduino USB cable from your computer. All lights will go blank. Reinsert the cable and the flashing light will resume.
Also carefully note the two functions in the code, setup() and loop() (these are different from DevC++ code where there was only one function main()). The setup() function is executed once at the start and the loop() function repeatedly executes until the board is reset, powered off (by disconnecting from PC for example) or a condition to stop the execution occurs.

Task # 2:
Run the code alab01_1.cpp on the Arduino and observe the change you see on the board. Record your observation:


After we run the Code we see that first ‘L’ led starts to blink fastly at this point flashing of code starts at  then ‘XT’ and ‘RX’ led also blinks in a sequnce after this code is succesfully flashed and L led continues to turn onn for 1 second then turn off for 1 second 









Task # 3:
Intuitively change the code in alab01_1.cpp, save it as file alab01_1a.cpp to make the LED turn off for 2 seconds and turn on for 1 sec.

Only rewrite the code statements you have changed in the original code. Mention the line numbers for ease of reference:

We change  line 14
digitalWrite(13, LOW);  //set the LED off
delay(1000);            //wait for 1000 ms
to
digitalWrite(13, LOW);  //set the LED off
delay(2000);            //wait for 2000 ms












No comments:

Post a Comment

Functions & BASIC FILE I/O

A.    Files : For storage of data for later use, files are used. Files are stored on the Hard Disk Drive (HDD). There are two types of f...