Lecture slides, class recording, and project specs for this module are available below.
You can find additional resources for this module (and others) in the Course Wiki and linked below.
Notice how this pinout diagram looks slightly different from the one in the lecture slides.
See how some of the pins are called IO# here instead of D#? That's another common prefix MCU documentation uses to refer to input/output pins. There are also labels for the internal naming of each pin (like PCINT18 or ADC3); if you read the ATMega328P datasheet (it's only 300 pages!), it will use these more specific pin naming schemes instead of a simple D or A prefix.
What other differences do you spot between the two diagrams?
Different development boards and microcontrollers will present their pinouts in different ways, so it's helpful to keep an open mind as you read their documentation and try to spot the differences between them. Not every piece of documentation is created the same (and sometimes it just sucks).
The Arduino Reference is your friend. Bookmark it; keep it open in a pinned tab; reference it as you write code. In the real world, you'll have reference materials to look at, and OPS is the same way (i.e., we're not going to make you memorize every Arduino function and code operator, and there will be no tests on this).
On the reference, you'll find explanations of every command, what arguments it takes (like pin numbers, INPUT/OUTPUT, HIGH/LOW, etc.), and example code for how to use that command in your Arduino sketch.
Reminder: The code projects / files we create in the Arduino IDE are called sketches and have a .ino file extension.
You'll need (at least) the following materials to complete this project with the minimum specs:
1x Arduino Nano (with USB cable)
1x Breadboard
1x 10kΩ Potentiometer
1x 470Ω Resistor (color bands: yellow, purple, brown)
1x LED of any color.
The LEDs in your kit are rated for an operating current of 20mA and must always be connected in series with a current limiting resistor.
Make sure you aren't using the clear IR LED from your kit; it's one of the two LEDs with longer legs (the black one is actually a photodiode). Infrared (IR) light isn't visible to us silly humans so you won't be able to tell when it's on!
4-6x jumper wires (depending on how you build your circuit on the breadboard)
470Ω Resistor
Make sure you connect your LED with the correct polarity or it won't light up! Remember: Diodes only allow current to flow in one direction, like a one-way valve.
Potentiometer
There are two parts in the schematic below.
On the left side, you can see the potentiometer symbol with the two outer terminals connected to GND and 5V and the middle terminal connected to an analog input pin (A4). Make sure the middle terminal is not connected to 5V or GND!
This creates a voltage divider between 5V and GND, with Vout connected to the analog pin so we can read the position of the potentiometer (with analogRead([pot pin]);).
On the right side, you can see the LED circuit consisting of an LED and a resistor in series. We call this a current limiting resistor, and it has two jobs to protect our circuit: it prevents us from drawing too much current from the Arduino's digital output (pin D7), and prevents us from putting too much current through the LED. Allowing too much current to flow through a component is a common way beginners (and experts!) accidentally kill their components.
Let's do some "napkin math" on our current limiting resistor to see what exactly is going on in the LED portion of our circuit:
When we turn the LED on (with digitalWrite([led pin], HIGH);) we'll have 5V at the LED pin (and 0V on that pin if we set the pin to LOW, turning the LED off).
We know that the full 5 volts will drop across the LED and resistor, because the other end of the two components is connected to the ground (GND) pin, and GND = 0V.
So we can use Ohm's Law to find the current going through both the resistor and the LED (because they're in series): 5V / 470Ω ≈ 0.0106A (or 10.6mA).
From our pinout diagram above, we know we shouldn't draw more than 20mA from any one pin, and we're drawing less than half that, so our Arduino is safe.
From the LED specifications in our parts list, we know these LEDs are designed to operate at about 20mA; our LED will still light up, but not at it's full brightness—and it definitely doesn't have enough current going through it to burn it out.
Using the coding structure we talked about in class (#defines, then setup() then loop()), program your LED to blink (turn off, delay, then turn on, delay, and repeat) for a certain amount of time based on the value you read from the potentiometer.
In other words, the way the LED blinks should change depending on how far you turn the potentiometer knob. Maybe make the speed it blinks at change, or how long it stays on for each blink, or how long it stays off between blinks, or any combination; the choice is yours!
You also need to output some value to the Serial monitor on your computer.
This might be the potentiometer value, your LED delay time, or maybe something else—as long as the output changes as you turn the potentiometer knob.
Below is the pseudocode for your project (to give you a general idea of what your Arduino sketch will look like):
Some useful functions to remember:
Serial.print(“Some statement to print to the serial monitor on your laptop”);
e.g.,
int x = 0;
x = analogRead([pin]);
Remember: There are 1000 milliseconds in 1 second
For a simple guide on how to connect and upload code to your Arduino, you can review the demo we did in the lecture video, or read step by step instructions from arduino.cc here.
When you first plug in your Arduino, some of the lights on the board should turn on and blink a few times.
This is caused by the startup sequence running on the MCU. At least one of them will stay on all the time, it's the one with "ON" or "POW" next to it, and means exactly what the label says; power is connected to the Arduino. (And the RX and TX LEDs blink when the serial interface is being used, we'll learn more about that later on).
The LED with a simple L next to it is attached to pin D13 and you can turn it on and off in your code (e.g., with digitalWrite(LED_BUILTIN, HIGH); or digitalWrite(13, HIGH);). This is a great tool for debugging if you want to test something without sending outputs to the Serial monitor or connecting an external LED and resistor. But remember, because this LED is always connected to pin 13, if you use pin 13 as an input, you won't be able to use that LED in your program anymore.
We know this is a simple project; the goal here is to get used to using your Arduinos, the breadboards, and writing code in the Arduino IDE. As always with OPS, we want you to explore. Play with the code, try building a circuit with two or three LEDs and blinking them with different patterns, or open some of the example sketches (under File -> Examples -> Built-In Examples) and see if you can build those circuits.