Lecture 30 Arduino Basics

Task 1: (30 points, show me your code works for the credit)

Run the first Arduino program: 


Before we get started, please download the programming environment first, this takes time so do this first.

Download Arduino IDE from my server, or from the official website:
https://www.arduino.cc/en/Main/Software



Save it to your M Drive so you can use it next time.

Right click the zip file and select 'extract all' to extract the compressed files to the local drive.

Meet the Arduino Uno Board:



Open the Arduino programming environment (IDE):


Connect the board to any of your computer USB ports via the USB cable.

Select your board first:


Then select the current COM port being used for this board. The COM port may change if you use a different USB port on the computer.


Open the first C program in the example pool. This is a simple program to blink the on-board LED light with a certain frequency (you can change the frequency).


Scroll down the window, you can find the C code is right there with all the comments (in the gray color).



What does the code mean?

Void: The void keyword is used only in function declarations. It indicates that the function is expected to return no information to the function from which it was called.

setup (): This is a function that will be executed first to set up the control units in the CPU. It is used to make it ready to run the main functions below.

loop(): is the function that will be executed back and forth all the time as long as you power up the board. 

pinMode(): This is a function to setup a certain pin as 'INPUT' or 'OUTPUT'. We are controlling an LED now so it should be an 'OUTPUT' pin to deliver an 'ON' or 'OFF' voltage to the LED.

LED_BUILTIN: is an LED on the board that connected to a digital pin on the CPU chip.

digitalWrite(): is a function to write digital voltages (0V as LOW or 5V as HIGH) to a certain pin. Apparently, it is writing to the pin that connected to the built-in LED light on the board, whose name is 'LED_BUILTIN'.

delay(): is a function to pause the program execution on the CPU. 1000 in the function means 1000 mili seconds, which is 1 second.

The code will be executed very quickly (in the MEGA hertz level), so the 1 s delay is significant. The delay will slow down the program execution. It turns on the LED very quickly, and then pauses for 1 s, and then turn it off. After that, 1 loop is completed, and it will turn the LED back on again, and repeat the same cycle permanently until you power it off.

Now, click the 'download' button to download it to the board through the USB port.


After a few seconds, the LED on the board will blink at a frequency of once per 2 seconds, which is 0.5 hertz.

Task 2:
(30 points, show me your code works for the credit)

Go to File - Save as, save the current blinking code as '
lec30_1.ino' at your desktop.

Double click the 'lec30_1.ino' file, modify the code to make the LED blinks at the frequency of 2 hertz. Download the code to the board and observe the results.

Task 3:
(40 points, show me your code works for the credit)

Duplicate the 'lec30_1.ino' file using 'ctrl C and ctrl V' in the same directory. Rename the duplicate to 'lec30_2.ino'. Double click to open this file and delete all the codes inside this window.

Type the following code into the window instead: (including the comments):



Download and run the code on your board.

Open Matlab, create a new script file, and type the following code into it.

clear, clc
delete(instrfind(('Port'),('COM30')));  % This command will close the previous port you opened. Figure out your COM number on your computer by 'Device Manager':

s=serial('COM30','Baudrate',9600,'DataBits',8); % Open the serial port on your PC, define the transmittion rate to be 9600 bit per second, the length of each data is 8 bits.

fopen(s); % Open 's', which represents '
serial('COM3','Baudrate',9600,'DataBits',8);'
voltage=fscanf(s); % Use 'fscanf' function to acquire the data being stored in the memory of your PC.
y=str2double(voltage); % Convert the data into a 'double' type. The data is being sent as string characters, you need convert them into numbers for display.

Run the program, you will see the following result:


The data is be streamed very quickly, when you run the program in Matlab, Matlab will acquire ONE 8-bit data from the streamed digital signal. It is very likely the data is not ready or the you sampled the middle of an 8-bit data chain. So the first data can be invalid. In this case, Matlab just give up the first 8-bit data, and show you NaN as the frist data.

So the question is, how can we sample more data and store them in a vector?

A 'For Loop' will solve this issue. Think about it in this way: if you put the data aquisition part in a loop, Matlab will keep sampling the streamed data.


Complete the following code that includes a 'for loop' for the data acquisition, and run it again.
delete(instrfind(('Port'),('COM30'))); % Close previous port
s=serial('COM30','Baudrate',9600,'DataBits',8); % Configure the port
fopen(s); % Open the port
for i=1:100 % continuously sample 100 data points
   
voltage=fscanf(s); % Use 'fscanf' function to acquire the data being stored in the memory of your PC.
    y=str2double(voltage); % Convert the data into a 'double' type. The data is being sent as string characters, you need convert them into numbers for display.

end


However, this code is still incomplete. The data is continuously being streamed to the computer but only ONE single data is being handled and stored in 'y'. We will work on the code next time to receive multiple data points into Matlab.