Intro To Arduino Tutorial No Hardware or Experience Required 1
Intro To Arduino Tutorial No Hardware or Experience Required 1
Spring 2020
Objectives:
- Get familiar with common functions and the programming syntax of the Arduino
language
- Work with different microcontroller accessories to build a foundation for future tinkering
Note: There’s a wide variety of Arduino starter kits out there that can be purchased that contain
all the components you would need for this tutorial and more. Here are two options available on
Amazon:
- Starter kit 1
- Starter kit 2
Inside TinkerCAD, you can write and upload code to your virtual circuit. However, TinkerCAD
creates basic code for you to start with based on the components you’re using. In the case of
this tutorial, that’s not really conducive for learning how to code in Arduino so we’re going to
write the code in the Arduino IDE and then copy it into TinkerCAD.
2. To add components to the canvas, click on them in the components menu on the right.
Start by placing an Arduino Uno and a breadboard into the canvas.
a. Once you place a component you can select and move it by clicking and
dragging.
b. Use your mouse scroll wheel to zoom in and out. Hold down the scroll wheel to
pan.
We’re going to start by making our power/control and ground connections between the Arduino
and the breadboard.
3. Make a connection between the 5V pin on the Arduino and the + power column on the
breadboard.
a. TinkerCAD: click on the pin on the arduino (the pin will turn red and display the
label “5V” when you hover over it with your mouse) and then click on one of the
holes in the + power column on your breadboard. This will make a wire. You can
change the color of the wires in TinkerCAD after you place them. In this tutorial
power wires are going to be red for easy identification.
b. Hardware: plug a jumper wire into the 5V pin on the arduino and into the + power
column on the breadboard.
4. Make a connection between one of the ground (GND) pins on the Arduino Uno and the -
power column on the breadboard.
a. TinkerCAD: brown wires will be used for gnd connections for ease of
identification.
b. Hardware: use another jumper wire to make this connection.
Next, we’re going to add a 330Ω resistor and an LED to the circuit. The resistor is used to limit
the current flowing to the LED so that it doesn’t burn out. The higher the resistance of the
resistor, the less current that will flow to the LED and the dimmer it will appear.
6. Connect the resistor to the breadboard between the + power column and one of the
holes in the a-e columns of the breadboard. Since all the holes in the + power column
are connected, this is making the electrical connection between the 5V pin on our
Arduino and the resistor.
a. TinkerCAD: Use wires to make the connections. It helps to place the resistor to
the side of the breadboard, draw the connections, and then move the resistor
onto the breadboard so that you can see where you’re making connections. The
wires will move with the resistor when you move it.
b. Hardware: plug one leg of the resistor into the + power column and one leg into
the middle of the breadboard.
LEDs are polarized, meaning they only work in one direction, while resistors are unpolarized. In
a circuit, current is going to flow in the direction of the LED/diode symbol arrow (i.e., in the
anode (+) and out the cathode (-)). On an LED, what’s the anode and what’s the cathode is
denoted in a coupled ways:
● The legs are two different lengths - the longer in the anode
● One of the sides of the “light bulb” portion of the LED is flat. It’s hard to see but you can
feel it. This is the cathode side.
● To remember which way to put an LED in the circuit, you can think of the symbol like a
funnel.
In TinkerCAD when you hover over one of the legs, it tells you whether it’s the anode or the
cathode. You can rotate components by selecting them and clicking the rotate button in the
upper left.
7. Connect the anode of the LED to the same row of the breadboard as the resistor and
connect the cathode to any other row. This will make a connection between the resistor
and the LED.
8. Complete the circuit by connecting the cathode of the LED to the - power column.
9. Test your circuit. If it’s wired correctly the LED will turn on.
a. TinkerCAD: Click “Start Simulation” in the upper right of TinkerCAD. This will plug
in the USB cable and power the circuit.
b. Hardware: plug in the USB cable to the arduino and your computer. The Arduino
power light will turn off if the Arduino is receiving power from your computer.
1. Move the connection to the 5V pin on the arduino to digital pin 13 (D13).
1. Open up the Arduino IDE and start a new sketch (File - New).
In the Arduino IDE and language (the language you code in is also called Arduino), programs
are referred to as sketches. In the sketch there are two main components/functions:
● Void setup(): This is where we lay the foundation of the code by telling the IDE what
we’re going to use each of the pins we’re using for. This only runs once when the code is
uploaded to the physical (or virtual) Arduino.
● Void loop(): this is where the body of the code goes. This is where we’re going to tell the
pins what we want them to do so that we can control the accessories, like the LED, that
are attached to the pins. This part of the code will run over and over again until you
either remove power from the Arduino or change the code.
2. Select the Arduino Uno as the board you will be using by going into the “Tools” menu at
the top of the IDE. This informs the IDE so it knows what pin is which.
3. If you are using hardware, plug in your Arduino (the power light on the board should be
on) and check your Port through the “Tools” menu. If you don’t have the proper com port
selected, your computer won’t be able to upload code onto the Arduino.
The Arduino language is similar to C and has a lot of built in functions that we’ll be working with.
Builtin function definitions are provided below. The function names are in blue. Note the
capitalization.
Functions:
pinMode(pin, type)
- Pin: this is the arduino pin you are referencing
- Type: INPUT, OUTPUT, INPUT_PULLUP
- INPUT_PULLUP: pin is an input but will be pulled high by default
- Example: pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(pin, level)
- Pin: pin you want to control
- Level: HIGH or LOW
- Pay attention to the device you are controlling. If it is active low it will be on when
the pin is set LOW
- Example: digitalWrite(13,LOW);
digitalRead(pin)
- Pin: the pin whose value you want to determine
- Often used in if statements to see if a pin meets certain conditions
- Example: digitalRead(2);
analogWrite(pin, value)
- Pin: pin you want to control
- Value: between 0 and 255. The value varies the duty cycle of the PWM signal being
written to a pin
- Example: analogWrite(A0, 27);
analogRead(pin)
- Pin: the pin whose value you want to determine
- Example: analogRead(A2);
delay(x)
- x: millisecond value that determines the duration of the delay. The next command won’t
be executed until the delay is complete.
- Example: delay(200);
Code tips:
- Make sure you’re including semi-colons. Every code line you add needs to end with a
semi-colon.
- Watch your capitalization
- Make sure you’re referencing the correct pin
4. Setup digital pin 13 as an OUTPUT pin in the setup function using the pinMode()
function. Try to do this without checking the answer in the Figure below.
In order to make the LED blink we need to turn it on and off through pin 13. Since the code in
the loop() functions repeats over and over again and runs really quickly, we need a delay
between turning the LED on and turning the LED off and turning it back on again so that we can
actually see it blinking.
5. Write the code in the loop function to make the LED blink using the digitalWrite() and
delay() functions. Use a delay of 500 milliseconds to start.
6. Hit verify (checkmark in the upper lefthand corner) to save and compile your code. If you
have any mistakes like a missing semicolon, you’ll get an error print out in the bottom
message section of the IDE.
8. Confirm that your hardware is acting as you would expect. The LED should turn on and
off repeatedly.
9. Play with the delays and check out the effect on your simulation or hardware. What’s the
smallest delay you can use and still tell that the LED is turning on and off?
Instead of using the default pin names, we can use variables to make pin usage more intuitive.
This is helpful when you start to utilize a lot of pins and are trying to remember which is doing
what in your code.
10. Add a variable called LED to represent digital pin 13. This code goes above the setup()
function and the syntax will be “int LED=13;” In TinkerCAD you’ll need to stop the
simulation before you can modify the code.
11. Replace the instances of “13” with “LED” in your code. You can do this in either the
TinkerCAD code window or the Arduino IDE since this is your own code that you’re
working with.
12. Upload and check that your hardware is reacting to the code as you’d expect.
1. Connect the bottom left pin of the push button to digital pin 8 on the arduino (red wire in
TinkerCAD). Connect the bottom right pin of the push button to the - power column on
the breadboard (brown wire in TinkerCAD).
Write the Code:
Since we’re now using digital pin 8 to control the button, we need to define a variable for the
button and declare its pin type in the setup() function. You can modify the code you wrote for the
Blinking LED circuit or start a new sketch.
1. Add a variable called “Button” that’s set to digital pin 8. You will also still need a variable
called “LED” that’s set to digital pin 13.
2. Set the LED pin to be an OUTPUT and the Button pin to be of type “INPUT_PULLUP”.
Push buttons are active low devices so when they are given a LOW signal they are on.
By setting the pin type to INPUT_PULLUP, the pin is HIGH by default so that the button
is off by default.
Control structures allow the code to branch or repeat based on conditions. Examples of control
structures are if/elseif/else statement, do/while loops, and for loops.
For if/elseif/else statements we use conditions to determine what the code should do. If the
condition is met then the code under it is executed.
if(condition)
{
Then do this code
}
elseif(condition)
{
Then do this code
}
else
{
Do this code
}
Conditions are often equalities or inequalities and can utilize digitalRead or analogRead to
access the value of a pin.
- Example: if(digitalRead(8)==LOW)
- Example: elseif(x<12)
- Example: if(analogRead(A2)>12)
For a do/while statement, we tell the code to run over and over again until a condition is met.
do
{
This code
}while(condition)
A for loop is a control structure that allows for iteration, such as incemenenting to look through a
data set or increasing speed one notch at a time.
The initialization should include the data type (e.g., int i=0; i<5; i++)
3. In the loop() function, use an if/else statement so that if the button is pushed
(digitalRead(Button)==LOW) then the LED is turned on, else then the LED is turned off.
Use digitalWrite to turn the LED on and off.
4. Upload and test your code.
a. TinkerCAD: click on the push button to press it. Holding down your left mouse
button on the push button will hold it down.
Circuit 4: Servo Motor
Next, we’re going to move to working with servos. This circuit won’t require a breadboard so you
can either start a new circuit in TinkerCAD or disconnect your Arduino from the breadboard.
Select and then hit the delete key on your keyboard to delete a wire or component in
TinkerCAD.
Servos are rotary actuators with position control. Through code you can set their angle between
0° and 180°. Note that they are not capable of rotating continuously.
3. Create a servo object in your code. We’ll be able to then access the properties of this
object to set and read angles. The syntax is “Servo myservo;” and it goes below the
inclusion of the servo library. “myservo” is a chosen variable name so you can set it to
whatever you would like.
4. In the setup, instead of using pinMode, we’re going to attach the servo to a PWM pin. In
the setup function, use the following syntax: “myservo.attach(11);”
5. In the loop function, start by setting the position of the servo to 0. The syntax to do so is
“myservo.write(0);” This will also bring the servo back to the 0° position at the start of
every execution of the loop function.
6. Make the servo increment angles from 0° to 180° in 5 degree increments by using a for
loop.
a. For loop initialization: int i=0;
b. For loop condition: i<=180;
c. For loop increment: i+=5
d. For loop body: myservo.write(i); delay(500);
i. A delay is used so that you can see the increments in angle.
9. In the setup function add “Serial.begin(9600);” This will allow you to use the serial port.
9600 is the baud rate, which is the rate at which info is transferred to a communications
channel. Having a baud rate of 9600 means that the maximum rate of info transfer is
9600 bits per second.
10. In the for loop set “servo_position” to myservo.read(). This should go below the delay.
11. Use Serial.print to print the value of servo_position. Refer to the examples in the
Function definitions above.
a. “Serial.print(servo_position);” will print the positions one after another in the serial
monitor like words on a page. If you want each position printed on a newline, you
need to include a second print command: Serial.print(“\n”);
1. Start a new TinkerCAD circuit. Place an Arduino Uno and a TMP36 sensor into the
canvas. Connect the legs of the sensor to pins on the Arduino. Power will go to 5V,
ground to ground, and Vout will go to an analog pin - A0.
5. Use the following formula to convert the value in the variable “reading” to a temperature
in celsius. You can break the forumla up into multiple lines with intermediate varialbes
like “voltage”
reading *4.68
tempC = (( 1024 ) − 0.5) * 100
6. Print the temperature to the serial monitor and then include a 0.5 second delay to create
a pause between sensor readings.
7. Upload the code and look at the values on the Serial monitor.
DHT11 Sensor: Hardware
The DHT11 sensor comes in two common configurations: a 4 pin version like the one shown
below and a 3 pin version that’s mounted on a small PCB. The PCB mounted version includes a
10kΩ pull up resistor. When using the 4 pin version, this resistor just has to be included in the
circuit.
2. Connect the 10kΩ resistor between the Vcc and Signal pin rows on the breadboard.
3. Connect the VCC sensor pin to the 5V pin on the Arduino. Connect the ground sensor
pin to ground on the Arduino. Connect the signal sensor pin to digital pin 7 on the
Arduino.
4. Start a new sketch in Arduino. If you don’t already have the dht.h library installed, go to
Library Manager through Tools - manage libraries in the top menu. Search dht and install
the DHT sensor library by Adafruit.
5. Open the DHT tester example sketch after installing the Adafruit DHT library.
6. Change the pin and sensor type in the example sketch so that digital pin 7 is declared as
the signal pin of the sensor and the DHT11 sensor is selected.
7. Upload the code and check the output on the serial monitor. An easy way to check if the
sensor is reacting is to breathe on the sensor. The humidity and temperature should rise
when you do this.