You are on page 1of 3

TEE 451 - Control Systems

Winter 2021

Instructor: Michael McCourt Arduino Tutorial

1 Introduction to Arduino
We will be using Arduino boards this quarter for digital control. This involves using digital and analog
inputs and outputs to take sensor measurements and use actuators. This tutorial is an introduction to
the Arduino hardware and software. Most of the code used is provided as sample code that you will
modify for specific purposes. It is your responsibility to read and understand all sample code. We will be
using these Arduino sketches and functions all quarter. A guide on programming in Arduino is posted on
Canvas and can be found at the link below. Arduino programming is based on C. I recommend at least
understanding the structure of Arduino sketches with the “setup()” and “loop()” functions. You can read
more about Arduino programming from the “Arduino Programming Notebook” posted on Canvas.
More information about the Arduino platform and programming them can also be found at the official
website. https://www.arduino.cc/en/guide/introduction

2 Arduino Blink Sketch


1. If using your own computer, download the Arduino IDE for your specific computer and install.
https://www.arduino.cc/en/main/software

2. Open the Arduino IDE software.

3. In the “File” menu, click on “Examples”, and hover over the submenu “01. Basics”. Open the
“Blink” sample sketch.

4. Read through the code to understand each line. The code is well commented.

5. Connect the Arduino board you are using to the computer.

6. In the “Tools” menu, hover over the “Port” submenu. Click on the board you are using. Usually
you are using only one Arduino board at a time.

7. In the “Tools” menu, hover over the “Board” submenu. Click on the board you are using, for
example, the “Arduino/Genuino 2560 Mega”.

8. Also, in the “Tools” menu is an option to select which processor your board has. For example, the
board I use has the “ATmega 2560” processor.

9. In the “Sketch” menu, click on “Compile/verify” to make sure the code compiles without error.

10. Also in the “Sketch” menu, click “Upload”. After upload, record your observations about the LED.

page 1 of 3
Control Systems Winter 2021

11. Repeat the above process, but instead use the example code “BlinkWithoutDelay” under “02.
Digital”.

12. After seeing the built in LED flash, save this file under a new name, preferably in a new directory
for your own work.

13. Find the line of code where the variable “ledPin” is set. Change the “LED BUILTIN” to one of the
PWM outputs on the board, for example, pin 10.

14. Run the code again with an external LED connected from this pin to a GND on the board. Make
sure that the LED lights up.

3 Read Analog Inputs


1. Read this short guide to using a temperature sensor on the Arduino:
https://learn.adafruit.com/tmp36-temperature-sensor/using-a-temp-sensor

2. Load the example code “AnalogInOutSerial” under “03. Analog”.

3. Open the “serial monitor”, which is the magnifying class symbol in the upper right of the window.
In this new window, set the baud rate to the same as the code (9600 baud).

4. Connect the temperature sensor TMP-36 to the board using jumper wires. Look up the datasheet to
connect +5V, ground, and the middle pin to the analog input A0. Be sure to check the orientation
of the sensor for correct connections. If you switch Vcc and ground, you will destroy the sensor.

5. Compile and load the sketch to the board. If this step worked successfully, you will get sensor
readings in the serial monitor window.

6. Hold the temperature sensor between your fingers to warm it up. Verify that the temperature
reading in the serial window increases. Note, the temperature values may not be accurate as the
temperature sensor needs to be calibrated for accurate readings.

4 Analog Output and PWM Signals


You can read more about the “analogWrite” Function and PWM signals at the following links.
https://www.arduino.cc/reference/en/language/functions/analog-io/analogwrite/
https://www.arduino.cc/en/pmwiki.php?n=Tutorial/SecretsOfArduinoPWM

1. Load the example sketch “AnalogWriteMega” under “03. Analog”. Despite the name “Mega” this
will also work for an Arduino Uno.

2. Connect an LED to one of the pins used in this sketch.

3. Upload the sketch and observe the LED. Connect the LED to a different pin used in the sketch and
observe that as well. Compare your observations to the code to make sure you understand how this
code is working.

4. Save this file under a new name, preferably in a new directory for your own work.

5. Modify the sketch to only use a single analog output pin for this. Connect the LED to that pin and
test.

page 2 of 3
Control Systems Winter 2021

6. Modify the sketch again by commenting out the for loop. Then write code to test several different
values between 0 and 255 with the analogWrite function. Observe the LED for these different values
and make sure you understand how this function works.

page 3 of 3

You might also like