In this project we are going to introduce variables, as well as create a circuit that allows you to control the speed at which your LED blinks!
Let’s start by picking up the necessary equipment.
What you need
Four long wires. (One red, one black, two of any colour)
Three short wires.
One 220-ohm resistor. (red-red-brown-gold)
One potentiometer.
One LED
The potentiometer is new and will look similar, but not exactly like the picture below.
A potentiometer is a three terminal resistor that take a voltage input and divides the voltage based on where the dial is at. It will output the result as a number between 0 and 1023. If there is no voltage it outputs the number 0. If there are all 5V then it outputs the number 1023. If you have the dial halfway then it will restrict the voltage and output 512.
What this means is that you now have a way to provide your program with a number that changes from 0 to 1023 depending on where you have the knob set.
Attached is a schematic of the circuit you need to build.
Make sure that the side of the potentiometer with two prongs is facing the left side of the board, where the side with one prong is facing the right side of the board.
The potentiometer will be connected to both side of your breadboard.
This introduces a new port. The potentiometer is hooked up to analog port A0. Analog ports take a real world reading of the voltage making it to the port. The reading can then be used in different parts of your program!
To experiment first code the following:
Before we break the code down I would like you to upload the program to your Arduino and open up your Serial Monitor. Play around with the potentiometer and see if you can figure out the code on your own.
This Button ^^^
Now let us look at the code looking at each line.
This command is telling the Arduino that it wants to take in a value from analog pin A0. Whatever voltage is coming in to A0 can be read.
This line is your first variable! Variables store values so you can use them later on in your program.
Because Arduino runs off a C based language you must declare what type of variable you want. If you are storing any regular number, the declaration int will suffice. We will discuss the different variable types in the future.
Next we named the variable “voltage”. This is how we will access the value in the future.
Finally we need to give the variable a value. The value is the numerical representation of voltage read from port A0.
That explains half of the code.
This line inside void setup() tells the Arduino that it will be using the Serial Monitor. Do not worry about the number 9600 for now. It is not important.
These two commands tell the Arduino to print out the numerical representation of our voltage variable to the serial monitor. We stored a number in to the variable “voltage” and then we printed it out to the screen. The delay command stops the Arduino from printing thousands of commands each second.
To recap:
The Arduino will read a value between 0 and 1023 based on where the potentiometer is at.
It stores the number in to a variable called “voltage”.
It then prints that number out to the serial monitor!
Now let’s change our code to the following:
Exercise
After you run and upload the code to your Arduino please show me so I give you credit for this project.
Next you must write a comment in on each line that explains what that line does. Some of this information can be found in this project and some are found in project 1 (if you don’t remember). Once you have written in your comments please submit this code.
Scoring
Circuit performs correctly 55%
Comments are correct (9 comments in total) 45% (5% each comment)
Total: 100%