0% found this document useful (0 votes)
9 views10 pages

W1 Lab 2 - Arduino Programming

This lab introduces Arduino programming using the ATMEL microcontroller, focusing on digital and analog inputs, serial communication, and circuit building. Students will utilize the Arduino IDE to upload code and observe the behavior of circuits with components such as push buttons and potentiometers. A full lab report is required, detailing procedures, results, and simulations conducted in TinkerCAD.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views10 pages

W1 Lab 2 - Arduino Programming

This lab introduces Arduino programming using the ATMEL microcontroller, focusing on digital and analog inputs, serial communication, and circuit building. Students will utilize the Arduino IDE to upload code and observe the behavior of circuits with components such as push buttons and potentiometers. A full lab report is required, detailing procedures, results, and simulations conducted in TinkerCAD.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

ECPI University 2020

EET390L
Motor Drive Lab
Lab 2: Arduino Programming
I. Introduction:
This lab introduces the Arduino board using ATMEL microcontroller and demonstrates several of
the commands used on the Arduino to manipulate inputs, read digital and analog inputs and
perform tasks based on them, and send data through a serial connection to a PC so the user can
interact with the Arduino.
A FULL lab report is required. See Appendix for lab report requirements.

II. Objectives:
 Describe Arduino digital inputs versus analog inputs.
 Utilize the Arduino IDE and programming environment.
 Utilize communication with Arduino through serial communication.
 Connect Arduino to PC and communicate/go online with the microcontroller.
III. Theory:
 The Arduino microcontroller board is an example of “bare metal” programming using the C
programming language. There is no RTOS on the Arduino board so code is executed as
running an initial setup() function one time on startup and then continuous cycling through a
loop() function. The loop function runs through each command in line over and over for as
long as the board is powered on.
 The Arduino microcontroller has a USB serial connection to your computer loading code into
the board. The USB cable also provides power to run the board. The USB serial cable can
also be used to monitor what is happening in your program by sending serial data back to
your computer.
 The Arduino has several Analog inputs
 The Arduino has several Digital pins which can be assigned as input or output
 The Arduino has 5V output and ground pins that can be used to connect to external
components.
IV. Parts List:
 Arduino Uno or MEGA2560
 USB type A-to-USB Type B cable (USB printer cable)
 Push button switch
 10k ohm resistor
 10k ohm potentiometer

1
ECPI University 2020

 breadboard
 IDE to program Arduino board. This can be downloaded at the
[Link]
 [Link]

V. Procedures

Part I: Using TinkerCAD

1. Open a browser on your computer and go to the [Link]


2. Create an account using your ECPI school email address and login.
3. On the main menu (left middle side) click on the Circuits and then click on the Create new
Circuit.
4. On the search field (right side) search for the following components and drag them on to the
screen:
a. Arduino Uno
b. Pushbutton
c. Resistor (10K)
5. Build the circuit as shown in Figure 1.
6. Click on the code and in the drop-down menu select “Text”.
7. Copy the code shown below and replace it in the TinkerCAD editor.
8. Click on the simulation button.
9. Click on the Serial Monitor.
10. You should be able to see series of zeros and series of ones when pressing the push button.

Part II: Using Kit Components


Before you start programming Arduino projects, you need to setup the connection between your PC
and Arduino board and install the latest IDE

1. Arduino board. Connect the board to the computer using the USB cable.

2
ECPI University 2020

2. USB type A-to-USB Type B cable (USB printer cable)

3. The latest Arduino IDE can be downloaded at [Link]


Once the software is downloaded, the new IDE will show as bellow. The Arduino IDE
includes two sections: void setup() and void loop(). All lines of codes inside the void setup()
will execute once and all lines of codes inside the void loop() will run repeatedly.

4. On your PC:
Follow the steps below:
A) Connect your Arduino board with USB cable and USB port to your PC.
B) Check the COM port, which is allocated to the Arduino board by going to Device Manager
Ports.
C) If you don’t know which com port is allocated to the Arduino board, then you need to go to the
Device Manager and check the COM port number under Ports.
D) Select your Arduino board type: Tools>Boards>Arduino Uno

3
ECPI University 2020

5. Select your serial port, Tools>Serial Port>COM # (COM # found above).

4
ECPI University 2020

Circuit

Figure 1

6. Build: Using the components inside the Arduino kit, build the circuit on the breadboard
as shown in Figure 1. The resistor value is 10K.

7. Code: Copy the following code into an Arduino IDE and click on the verify the code for
syntax by clicking on the check mark below the file:

5
ECPI University 2020

8. Upload: Then the code needs to be uploaded to the Arduino board. In order to upload the
program into the board, click on the Upload button as follow:

int pushButton = 2;

// the setup routine runs once when you press reset:


void setup() {
// initialize serial communication at 9600 bits per second:
[Link](9600);
// make the pushbutton's pin an input:
pinMode(pushButton, INPUT);
}

// the loop routine runs over and over again forever:


void loop() {
// read the input pin:
int buttonState = digitalRead(pushButton);
// print out the state of the button:
[Link](buttonState);
delay(1); // delay in between reads for stability
}

In the program above, first the setup function is to begin serial communications, at 9600 bits of data per
second, between your Arduino and your computer with the line:
[Link](9600);

Next, initialize digital pin 2, the pin that will read the output from your button, as an input:
pinMode(2,INPUT);

6
ECPI University 2020

Now that your setup has been completed, move into the main loop of your code. When your button is pressed,
5 volts will freely flow through your circuit, and when it is not pressed, the input pin will be connected to
ground through the 10-kilohm resistor. This is a digital input, meaning that the switch can only be in either an
on state (seen by your Arduino as a "1", or HIGH) or an off state (seen by your Arduino as a "0", or LOW),
with nothing in between.

The first thing you need to do in the main loop of your program is to establish a variable to hold the
information coming in from your switch. Since the information coming in from the switch will be either a "1"
or a "0", you can use an int datatype. Call this variable sensorValue, and set it to equal whatever is being read
on digital pin 2. You can accomplish all this with just one line of code:

int sensorValue = digitalRead(2);

Once the Arduino has read the input, make it print this information back to the computer as a decimal value.
You can do this with the command [Link]() in our last line of code:

[Link](sensorValue);

Now, when you open your Serial Monitor in the Arduino environment, you will see a stream of "0"s if your
switch is open, or "1"s if your switch is closed.

Questions:
1. Using the components inside the starter kit, build above circuit and upload the code
through Arduino IDE. Go online. Press and depress the push button. Write down your
observations.
2. Using tinkerCAD, simulate the circuit in question 1 and verify the result.

PART III - Analog Read Serial


This example shows you how to read analog input from the physical world using a potentiometer.
A potentiometer is a simple mechanical device that provides a varying amount of resistance when its shaft is
turned. By passing voltage through a potentiometer and into an analog input on your Arduino, it is possible to
measure the amount of resistance produced by a potentiometer (or pot for short) as an analog value. In this
example you will monitor the state of your potentiometer after establishing serial communication between your
Arduino and your computer.

Build:
Connect the three wires from the potentiometer to your Arduino board. The first goes to ground from one of
the outer pins of the potentiometer. The second goes from 5 volts to the other outer pin of the potentiometer.
The third goes from analog input 0 to the middle pin of the potentiometer.

7
ECPI University 2020

Code

In the program below, the only thing that you do will in the setup function is to begin serial communications,
at 9600 bits of data per second, between your Arduino and your computer with the command:

[Link](9600);

8
ECPI University 2020

Next, in the main loop of your code, you need to establish a variable to store the resistance value (which will
be between 0 and 1023, perfect for an int datatype) coming in from your potentiometer:

int sensorValue = analogRead(A0);

Finally, you need to print this information to your serial window as a decimal (DEC) value. You can do this
with the command [Link]() in your last line of code:

[Link](sensorValue, DEC)

Now, when you open your Serial Monitor in the Arduino development environment (by clicking the button
directly to the right of the "Upload" button in the header of the program), you should see a steady stream of
numbers correlating to the position of the pot. As you turn your potentiometer, these numbers will respond
almost instantly.

/*
AnalogReadSerial
Reads an analog input on pin 0, prints the result to the serial monitor.
Attach the center pin of a potentiometer to pin A0, and the outside pins to +5V and ground.

This example code is in the public domain.


*/

// the setup routine runs once when you press reset:


void setup() {
// initialize serial communication at 9600 bits per second:
[Link](9600);
}

// the loop routine runs over and over again forever:


void loop() {
// read the input on analog pin 0:
int sensorValue = analogRead(A0);
// print out the value you read:
[Link](sensorValue);
delay(1); // delay in between reads for stability
}

9
ECPI University 2020

Questions:

3. Using the component inside the starter kit, build above circuit and upload the code. What
is the range of numbers observed on the serial terminal when the pot moves from
minimum to maximum? (to be able to see the serial terminal, click on the top right of the
IDE).

4. Rewrite the above code using the “map” command to change the range to 0-5 volts and
display this on the serial terminal. Hint: map(variable, initial range_min,
initial_range_max, final_range_min, final_range_max).

5. Using the TinkerCAD software, simulate the circuit in question 3 and 4 and verify the
result. You should get the same results using the real components.

APPENDIX: LAB report submission guidelines:

1- A full lab report with Cover Page, Abstract, Introduction, Procedure, Results,
Conclusion, and References must be submitted for the entire lab. You should include all
questions in the same lab report. You may group your Method, Procedures, and Results
together by question. Hint- write the abstract and introduction last. They should fall out
very easily once you have done the lab and written the conclusion.
2- A separate Arduino code needs to be created and uploaded for each question where an
Arduino is used. The actual [Link] developed for each question needs to be included
in your final submission. It must be sufficiently commented. The actual [Link]
developed for each question needs to be included in your final submission.
3- For each question, you need to simulate the LAB using tinkerCAD and the screenshot of
the tinkerCAD diagram needs to be put into a word document. Include the Arduino code
and the serial output if used. You need to simulate the LAB using tinkerCAD and the
snapshot of the tinkerCAD diagram needs to be included in your report for the main LAB
and the questions.
4- A short video of the physical setup showing how the real circuit works needs to be
submitted for each circuit. This video can be as short as 10 sec movies. The movie file
also needs to be included in your submission.

10

Common questions

Powered by AI

Using both a physical Arduino board and a simulation tool like TinkerCAD allows for a comprehensive approach to understanding and developing circuit prototypes. The physical board offers hands-on experience with hardware, ensuring that theoretical designs can be physically constructed and function properly with real components. Meanwhile, TinkerCAD provides a risk-free environment for experimenting with different circuit configurations and code without fear of damaging physical components. This dual approach enhances learning by allowing troubleshooting of software-driven errors and understanding practical connectivity issues, ultimately leading to deeper insights and more robust circuit designs .

The delay() function in Arduino is used to pause program execution for a specified duration in milliseconds, effectively managing timing between actions within the loop cycle. This helps in stabilizing sensor readings and mimicking real-world temporal behaviors. However, its use is double-edged: while beneficial for straightforward sequential tasks, it can block further code execution and interfere with time-critical processes requiring multitasking. Over-reliance on delay() can lead to inefficient projects where responsiveness is compromised, emphasizing the need for balanced programming approaches that might leverage non-blocking techniques like millis() for improved system responsiveness .

Using a potentiometer with an Arduino illustrates the conversion of a physical mechanical movement into numerical data by leveraging its variable resistance. As the potentiometer's shaft is turned, its resistance changes, altering the voltage between its terminals. When this variable voltage is input to an Arduino's analog pin, the built-in ADC (Analog-to-Digital Converter) interprets this voltage level as a value between 0 and 1023, effectively translating a physical position into a numerical representation that can be used for further processing and decision-making within the Arduino project .

Serial.print commands are crucial in debugging and verifying Arduino programs as they allow real-time monitoring of variables and states within the code. By strategically placing Serial.print statements, developers can visualize the flow of execution, track variable changes, and confirm expected behavior by printing output to the serial monitor. This capability is especially valuable during loop iterations and conditional checks where visual confirmation of logical operations ensures that the system operates correctly and efficiently, leading to quick identification and resolution of anomalies .

Digital inputs on an Arduino board can only recognize binary states, either HIGH (usually 5V) or LOW (0V), which corresponds to on/off or one/zero states. This makes them suitable for reading buttons or switches that have clear on/off states. In contrast, analog inputs can read a range of voltage levels, usually from 0V to 5V, and represent these levels as values between 0 and 1023. This allows them to interface with sensors and devices that provide a range of values, like potentiometers or other variable sensors, offering more nuanced data about conditions outside the binary framework .

Setting up serial communication between an Arduino and a computer involves initially establishing a physical connection through a USB cable. Next, within the Arduino IDE, you set up communication in the code by using the Serial.begin() function, specifying a baud rate (e.g., 9600bps). The device manager should be used to check the COM port allocation. Serial communication is vital because it allows the Arduino to send data back to the computer for logging, debugging, or analysis while also receiving instructions, making it possible to interact dynamically with the Arduino projects .

Commenting Arduino code is vital, especially in educational and collaborative settings, as it makes the code more understandable and easier to follow. Comments serve as annotations to explain the purpose of code segments, thereby assisting collaborators in grasping the logic or changes made by others. This practice accelerates the learning process, aids debugging, and ensures consistent development experiences for all contributors. Moreover, well-commented code can be more easily updated or repurposed for different projects, thus maximizing educational and developmental outcomes .

The absence of a real-time operating system (RTOS) on the Arduino board influences its programming paradigm by necessitating a 'bare metal' approach where code is executed sequentially with a simple loop structure. This means that upon startup, the setup() function is executed once, and then the loop() function continues to execute commands in sequence indefinitely. This structure implies that tasks need to be managed manually, requiring programmers to consider timing and resource management without the aid of an RTOS to handle multitasking or scheduled operations .

The 'map' function in an Arduino program is used to convert a value from one range to another. This is achieved by specifying the current range of the sensor values and the desired output range. The function 'map(value, fromLow, fromHigh, toLow, toHigh)' linearly interpolates the value linearly from its original range to the target range. This is particularly useful in mapping sensor outputs that may range from 0 to 1023 to more practical ranges such as voltage from 0 to 5 volts, facilitating easier interpretation and handling within the control logic of Arduino projects .

Creating videos of Arduino setups enhances understanding and assessment by providing a dynamic and context-rich glimpse into the project's functionality. Videos can clearly demonstrate how circuits are assembled and interacted with, offering visual proof of operational behavior that static images or text descriptions may not convey effectively. For assessment purposes, videos also offer instructors or peers insights into troubleshooting processes, the interaction between components, and real-time reactions to inputs or controls, facilitating comprehensive evaluation and reflection on project design and execution .

You might also like