EEE 202 Lab 4 Arduino MANUAL Online
EEE 202 Lab 4 Arduino MANUAL Online
Lab 4: Arduino
Lab 4: Arduino
Arduino is Open-Source Code and Content and Support are available via the website:
https://www.arduino.cc/
Lab Overview:
In preparing for the final design project, you’ll need to get familiar with using a microcontroller and LCD
display for automation. Sparky Solar really wants to be a lead innovator in their field, which is why they’ve
come to ASU (tops in innovation). Automation is a cutting-edge trend that will distinguish their products and
services from their competitors. The use of an Arduino (or equivalent) board system will allow you to build
automation into the final design project.
Automation is a huge field of advancement today to yield higher safety and awareness of everyday objects.
This can exist in terms of digital or analog applications; where digital means that a value is either 0 or 1,
mimicking the theory of digital logic seen in modern microcontrollers. Analog sensors however have a range
of values that can indicate different situations for different values. In this lab, you will learn how to
incorporate digital and analog sensors and control an LCD display. An important outcome of this lab is to
familiarize yourself with the Arduino hardware and sensor uses, and see the potential of future automation of
everyday applications.
Inventory List:
The following is a list of available inventory for this project:
Part Details
1, 5, 10, 100, 220, 330, 1k, 2k, 5k, 10k,
Resistors
20k, 100k, 200k, 1M
Alligator clips Connectors
Assorted jumper wire Red, black, white, green, blue, orange
Potentiometer 10K
Slide Switch Breadboard-friendly SPDT Slide Switch
DC Supply LPS 161A DC Supply
Breadboard 830 Tie Points
Digital Multimeter HP or Agilent 34401A
UNO Controller Board Elegoo
USB Cable Controller board to USB
9V Battery with Snap-On Connector Clip 9V
LCD Display LCD1602 Module with Pin Header
Photoresistor Photocell (1528-2141-ND)
Lab Deliverables
1
1. As you work through the lab, complete the required work on the Data sheet.
2. Submit your data sheet online (Individual Submission).
Lab Instructions:
The final design project will require the use of an Arduino (or equivalent) type board and an LCD display as
shown:
In this lab you will learn how to build an Arduino circuit with analog and digital inputs and program the
controller to perform specific tasks.
If you’d like additional information to better understand how the Arduino works for this lab, you can find a set
of slides here and a video tutorial on the lab Canvas page.
2
Pre-Lab Work (complete before attending the Lab session):
Arduino cannot be easily simulated in LTSpice, but it can be virtually simulated using a tool called TinkerCAD.
All buttons, lights, switches, boards, LCD displays etc. can be simulated. Please beware that the TinkerCAD
compiling tool can have issues. If you are working in TinkerCAD and you get unexpected compilation errors,
copy your code to a new file and retry, as this may solve your problems. After you create a TinkerCAD account:
https://www.tinkercad.com there are lessons and tutorials available to get you started:
Tutorials
1. Start simulating
2. Editing components
3. Wiring Components
4. Adding Components
5. Introducing the Breadboard
6. Blink an LED with Digital Output (found under “More Tutorials”)
7. Multiple LEDs and Breadboards (found under “More Tutorials”)
3
If you have no prior Arduino experience, be sure to complete a few of the lessons/projects to get familiar with
the TinkerCAD tool and the Arduino system. If you attempt to complete this lab without the prior work and
with no experience, you may find the lab frustrating – help yourself by being prepared.
The Arduino software for creating code can be found here: https://www.arduino.cc/en/software . It’s free and
doesn’t take much space – install it on your computer prior to attending the lab session. The software will also
be available on the computers in the lab.
If you have any issues building the physical Arduino circuits, TinkerCAD is the simulation tool that you can use
to plan and troubleshoot your designs. (This will be very valuable for your Final Design Project)
4
Lab Procedure
Part 1: Blinky Test
For this part, connect your UNO board (or equivalent) to your computer with the Arduino IDE installed via the
USB cable - be sure to have no other power source connected to the Arduino at this time, or you may accidentally
fry your laptop!
Now, open the Arduino IDE software on your computer. Poke around to get to know the interface. This step is
to set your IDE to identify your UNO board. You will be building a basic circuit to blink an LED on and off for 1
second.
5
Verify: Compiles and approves your code. It will catch errors in syntax (like missing semi-colons or
parenthesis).
Upload: Sends your code to the UNO board. When you click it, you should see the lights on your board
blink rapidly.
Open: This button will let you open up and existing sketch.
Serial Monitor: This will open a window that displays any serial information your UNO board is
transmitting. It is very useful for debugging.
Sketch Name: This shows the name of the sketch you are currently working on.
Code Area: This is the area where you compose the code for your sketch.
Message Area: This is where the IDE tells you if there were any errors in your code.
6
Next, select Tools->Board-> Arduino Uno (or Arduino/Genuino Uno). This will select the type of board which
you are using.
Then Tools->Serial Port-> COM3 (Or higher, whichever shows up). (For Macs, Tools->Port->*usbserial*)
Windows:
Mac:
7
This is the pin diagram of the UNO board.
The digital pins (0 through 13) are either ON (HIGH/5V) or OFF (LOW/0V). Digital pins can be either inputs or
outputs.
The analog pins (A0 through A5) can read any voltage between 0V and 5V. The microcontroller converts the
analog voltage to a number between 0 (0V) and 1024 (5V). They can also act as additional digital inputs and
outputs.
The power pins (VIN, GND, 5V, 3.3V, RESET, IOREF, RFU) are supplies, grounds, and references.
8
This is the layout of the breadboard.
9
Using the hookup wires, a 330-ohm resistor, and an LED, construct the following circuit:
Remember LED polarity. The cathode is the side that has the flat edge on
the bulb, and that indicates the negative side of the light which should be
connected to Ground. The anode is the round side which should be
connected to the resistor.
10
In the editor, enter the following code: Comment – Arduino won’t even see this code.
/* Hello World! */
Defining pin 13 as “led” (int is an integer number)
int led = 13;
Everything in “setup” is done one time at the beginning.
void setup() {
pinMode(led, OUTPUT); Setting the function of the “led” pin to be an output.
}
Press the “Upload” button and observe the effect on the Arduino.
You can also create functions in the code (placed after the loop) that will perform tasks based on a given
input. For example, if I want the LED to flash on an off with a set duration, I could create a function called
“flash”:
void flash(int duration) {
digitalWrite(led, HIGH);
delay(duration);
digitalWrite(led, LOW);
delay(duration);
}
void loop() {
flash(1000);
}
The LED will flash high for 1000mS, then go low for 1000mS.
ON YOUR OWN: Modify the program to blink “S-O-S” in Morse Code. Make sure there is a clear long pause
between the end of the message and the start of the next one. Record your code on your Data sheet. Hint: if
you aren’t sure what S-O-S is in Morse code, look it up online.
Syntax matters! If you don’t use the correct syntax, the Arduino will not understand what you are trying to do.
11
Part 2: Digital Input with Switch
In this part, you will build a circuit that turns on a light when a SPDT (single pole-dual throw) slide switch is in
the correct position, to only act when an input has been activated. Build the following circuit:
int switchState = 0;
void setup() {
pinMode(ledPin, OUTPUT);
pinMode(switchPin, INPUT);
}
void loop(){
switchState = digitalRead(switchPin);
if (switchState == HIGH) {
digitalWrite(ledPin, HIGH);
}
else {
digitalWrite(ledPin, LOW);
}
}
12
Data Sheet Part 2
Upload the code to your UNO board and observe the effect of toggling the switch. Record your observations
on the Data sheet.
Using your existing SPDT switch and 1 LED circuit, run the following program to time how long the LED is on:
int switchPin = 2;
int ledPin = 13;
int switchState = 0;
unsigned long startTime = 0;
unsigned long timeON = 0;
void setup() {
pinMode(ledPin, OUTPUT);
pinMode(switchPin, INPUT);
Serial.begin(9600);
}
void loop(){
unsigned long now = millis(); //running counter in milliseconds
switchState = digitalRead(switchPin);
if (switchState == HIGH) {
digitalWrite(ledPin, HIGH);
timeON = now - startTime; //in milliseconds
}
else {
digitalWrite(ledPin, LOW);
startTime = now;
}
Upload the program to the Arduino board, then click the Serial Monitor Button:
13
Part 3: Writing to the LCD Display
In this part of the lab, you will write to an LCD Display using the LCD1602 module with pin header. The pinouts
of the LCD header are shown below:
14
In the sketch editor, enter the following code:
#include <LiquidCrystal.h>
const int rs = 2, en = 4, d4 = 9, d5 = 10, d6 = 11, d7 = 12; //set the pins
LiquidCrystal lcd(rs, en, d4, d5, d6, d7); //assign the display variables
void setup()
{
lcd.begin(16, 2);
delay(2000);
lcd.clear();
void loop()
{
lcd.setCursor(0, 0);
lcd.print("LCDs can print");
lcd.setCursor(0, 1);
lcd.print(" many things ");
delay(2000);
}
The code is written to add the functions for the LCD display from an Arduino library file (LiquidCrystal.h),
initialize the connections between the Arduino and the LCD display, and then allow the Arduino and LCD
display to communicate using the set functions programed on the header board. For this lab, you’ll use the
LCD functions:
begin - set’s the size of the LCD screen (16 characters per line, 2 lines)
print - sends the alphanumeric text to the display
clear - clears the display
setCursor - sets cursor position (character #, line #)
In the set-up section, the LCD is initialized to verify that it starts up (this will only run at start-up), and then in
the loop section, the LCD is used for the intended purpose (this will repeat until commanded to end).
15
Part 4: Analog Inputs with Light Sensors and LCD Display
In this part of the lab, you will learn how to incorporate an analog sensor and use a range of values to trigger a reaction.
Using the photoresistor and a 10K Ohm resistor, and your existing LCD circuit, build the following schematic:
16
Upload the following code to test your photoresistor circuit:
void setup()
{
Serial.begin(9600);
}
void loop()
{
int val = analogRead(0); //set analog pin 0 to sense photoresistor
Serial.print("Sensor Value: ");
Serial.println(val); //print value to Serial Monitor for testing
delay(100);
}
Open the Serial Monitor. Wave your hand over the photoresistor. Do not disconnect USB before closing the Serial
Monitor.
ON YOUR OWN: Write code to send the LCD message “Day Time” when the photoresistor is exposed to light
and “Nighttime” when the photoresistor is covered (combine lab parts 2, 3 & 4 together).
__________________________________________________________________________________________
Fill in your data sheet. Labs can be done as a team, but each member must complete and submit their own data
sheet.
Lab CLOS:
- Understand and apply the application of electrical circuits
- Design and build circuits using resistors
- Applies technical skills/knowledge to the development of a technology/product
- Integrates/synthesizes different kinds of knowledge
- Understanding of Arduino’s input, output pins and serial ports
- Arduino LCD display observations as per different inputs
17