Arduino Lab Answers
AnalogReadSerial
Evaluation of operation:
The sketch reads from a potentiometer connected to pin A0. When you turn the dial the
resistance changes which changes the voltage and the Arduino turns that into a number
between 0 and 1023. That number then gets printed to the Serial Monitor so you can see it go
up and down as you turn the knob.
Important notes:
The potentiometer acts like a volume knob, turning it adjusts the voltage that the Arduino reads.
The Serial Monitor needs to be set to 9600 baud to display the values properly otherwise
nothing shows up.
Blink
Evaluation of operation:
This sketch makes the LED on pin 13 turn on and off repeatedly. It turns on for 1 second then
off for 1 second and just keeps doing that over and over.
Task result description:
I changed the first delay from 1000 to 2500 so the LED stays on for 2.5 seconds and changed
the second delay from 1000 to 500 so it stays off for 0.5 seconds. When I uploaded it the LED
was on for way longer than it was off which is what was supposed to happen.
DigitalReadSerial
Evaluation of operation:
This sketch reads whether a button on pin 2 is being pressed or not and prints it to the Serial
Monitor. It prints a 1 when the button is pressed and a 0 when its not. It updates really fast so
the monitor scrolls heaps quickly.
Task result description:
I added delay(500); at the end of the loop so it only prints twice a second instead of hundreds of
times. This made the Serial Monitor a lot easier to read because it wasnt scrolling so fast
anymore.
Fade
Evaluation of operation:
The sketch makes an LED on pin 9 slowly get brighter then dimmer then brighter again. It uses
PWM to do this by stepping the brightness up from 0 to 255 then back down with a small delay
between each step so it looks like a smooth fade.
Task result description:
To speed it up I made the delay smaller and increased the step size so it jumped through the
brightness levels faster. To slow it down I made the delay bigger and reduced the step size so
each change was smaller and took longer. The bigger the step the faster but choppier it looks
and the smaller the delay the quicker it fades.
ReadAnalogVoltage
Evaluation of operation:
This sketch reads the potentiometer on A0 and converts the number into a voltage between 0
and 5 volts then prints it to the Serial Monitor. As you turn the dial the voltage shown changes
between 0.00V and 5.00V.
Task result description:
To change it to use digital pin 3 I added pinMode(3, INPUT); in setup() so the Arduino knows its
an input. Then in the loop I changed analogRead(A0) to digitalRead(3). I also removed the *
(5.0 / 1023.0) part because digital pins only give you a 0 or 1 so theres no point converting it to
a voltage. After uploading it just printed 0 or 1 to the Serial Monitor depending on the signal.
Summary Questions A
What is the purpose of the setup() routine?
setup() runs once at the start when the Arduino turns on or gets reset. You use it to set things
up before the main code runs like telling the Arduino which pins are inputs or outputs and
starting the Serial Monitor.
What is the purpose of the loop() routine?
loop() runs over and over again after setup() is done. Its where you put the main code because
most Arduino projects need to keep checking things and responding to them constantly like
reading a sensor or blinking an LED.
What pin is the LED connected to on the Arduino Uno?
The built in LED is connected to digital pin 13. On the board its labelled with an L next to it.
Explain the difference between an analog pin and a digital pin.
A digital pin can only be HIGH which is 5V or LOW which is 0V so its either on or off. An analog
pin can read any voltage between 0V and 5V and converts it to a number from 0 to 1023. So
analog pins are better for things like potentiometers that have lots of different values and digital
pins are better for things like buttons that are just on or off.
How does Pulse Width Modulation (PWM) work?
PWM works by switching a pin on and off really fast so it looks like its in between on and off. If
its on half the time the LED looks half as bright. The amount of time its on compared to off is
called the duty cycle. It switches so fast you cant see it flickering so it just looks like a dimmer
light. On Arduino you use analogWrite() with a value from 0 to 255 to control it.
Outline a variable.
A variable is like a labelled box in memory that stores a value you can use in your code. You
give it a name and a data type and you can change what value its holding throughout the
program. For example int count = 0; makes a variable called count that starts at 0 and you can
add to it or read it whenever you need.
What is an 'int' datatype?
An int is a data type that stores whole numbers with no decimals. On an Arduino Uno it can
store numbers from -32768 to 32767. Its probably the most common data type used in Arduino
sketches and works well for things like sensor values, counters and pin numbers.
A Light Emitting Diode (LED) only allows current to flow in one direction. Identify the
names of the two terminals and label whether the terminal is positive or negative. How
can we recognise the positive or negative terminal?
The positive terminal is called the anode and the negative terminal is called the cathode. The
easiest way to tell them apart is that the longer leg is the anode which is positive and the shorter
leg is the cathode which is negative. You can also look at the flat edge on the side of the plastic
part of the LED, the leg closest to the flat edge is the cathode.
Explain how a resistor works.
A resistor limits how much current can flow through a circuit. It has a set resistance measured in
ohms and the higher the resistance the less current gets through. In Arduino circuits you usually
put one in series with an LED to stop too much current flowing through it and burning it out.
How does a potentiometer work?
A potentiometer is a variable resistor that you can adjust with a knob. It has three legs, the two
outer ones connect to 5V and GND and the middle one called the wiper connects to an analog
pin. When you turn the knob the wiper moves along a resistive strip and changes how much
voltage comes out of the middle leg. This gives a voltage anywhere from 0V to 5V and the
Arduino reads it as a number from 0 to 1023.