Nothing Special   »   [go: up one dir, main page]

Arduino Lab - COVID - 5301

Download as pdf or txt
Download as pdf or txt
You are on page 1of 9

Arduino Programming Laboratory Manual

Objective
To utilize the Arduino IDE Software platform to write simple control logic commands. This
control code will be used to make effective use of a variety of inputs and outputs connected to an
Arduino Uno microcontroller. This lab will also expose students to more complicated control and
communication codes, and online resources available for the Arduino software.

Background
In conjunction with different models of microcontrollers, the Arduino company provides the
open-source Arduino Integrated Development Environment (IDE). This programming
environment is written in Java and has been designed to introduce programming to an audience
that is less familiar with coding. Programs written in this environment are called ‘sketches’, and
the language used is based on the Processing language with support for C and C++ programming
languages.

A basic Arduino sketch consists of at least two functions: a ‘setup’ function and a ‘loop’
function. The setup function performs any actions that are initially required to run the rest of the
program, such as initializing any peripheral components and setting the communication
frequency between the Arduino board and PC. The loop function acts as the programs driver; it
runs in a continuous loop, and specifies the order of operations that the microcontroller will
perform.

Arduino control boards as well as many Arduino-compatible peripherals contain hardware for
serial communication. This form of communication transmits data one bit at a time over the
serial connection. This allows the components of the system (or a PC) to communicate larger and
more complex sequences of data much more easily, without physical input or output pin
limitations.

Because of Arduino’s open-source nature, third party companies, as well as end users are free to
take the software and tailor it to their individual needs. This means that for almost any
application, sketches are available for download online, and companies making peripherals (such
as a motor shield or sensors) for use with Arduino boards often have example sketches and
sketch libraries available for specific use for their products. These resources can be used to create
a custom sketch that can use many different peripherals at once, by combining aspects of many
different example sketches.

Apparatus and Equipment Overview


The equipment that will be used in this lab includes:
● The www.tinkercad.com online platform (circuits section)

Pre-Lab Preparation
Before arriving to the lab, students should review the lab manual and familiarize themselves with
the lab setup and procedures. Students may use their own computer for this lab if they wish,
provided that they have downloaded and installed the Arduino IDE, as well as the required
libraries outlined in downloads section. Reviewing the Arduino IDE syntax, as well as trying to
write some of the programs beforehand is also encouraged. A useful list of commands used in
the Arduino IDE can be found here: https://www.arduino.cc/en/Reference/HomePage.

Downloads
If you plan on using a physical card later, you will need some downloads:
The Arduino IDE can be downloaded from here: https://www.arduino.cc/en/Main/Software
The libraries used in this lab are:
● AFMotor.h (download from
https://learn.adafruit.com/adafruit-motor-shield/library-install)
● NewPing.h (download from https://bitbucket.org/teckel12/arduino-new-ping/downloads/)
To install Arduino libraries, go to Sketch>Add .ZIP Library... and browse to and select the folder
that contains the library or libraries that need to be added. For a more integrated (and permanent)
option, move the folder containing the library to the Arduino libraries folder in the
system files. For most windows users the default pathway will be Program Files
(x86)>Arduino>libraries, but this can change with different operating systems, different versions
of windows, or if the Arduino IDE was installed to a different location on the computer. For both
methods the Arduino IDE should be restarted for the libraries to become useable. Adafruit.com
has a useful tutorial on installing libraries that can be found here:
https://learn.adafruit.com/adafruit-all-about-arduino-libraries-install-use
Review Questions
What is the role of a microcontroller?

______________________________________________________________________________

______________________________________________________________________________

Which microcontroller are we using in this lab?

______________________________________________________________________________

______________________________________________________________________________

How can the ultrasonic sensor measure distance?

______________________________________________________________________________

______________________________________________________________________________

How will the motors be controlled to rotate in both directions?

______________________________________________________________________________

______________________________________________________________________________

Which programming language does Arduino use?

______________________________________________________________________________

______________________________________________________________________________
Part A – LED Blink Program
This program will go over the basic method of programming and simulating an Arduino
microcontroller. It utilizes a sample sketch included in Tinkercad, and is primarily used for
verifying that the microcontroller and the connection are both functioning properly.
1. Go to Tinkercad.com, sign in or create an account.
2. Select Circuits from the menu on the left and then create a new project.
3. Drag an Arduino Uno from the components list on the right to the middle of the blank
workspace.
4. Open the Code window with the ‘</Code’ button at the top right. Select the text option
from the dropdown at the top and then confirm your choice.
5. There will already be some code which is the simple Blink program as below.

6. Now start the simulation by clicking the ‘Start Simulation’ button at the top right beside
the Code button.
7. Once it has started, the code will be simulated on the Arduino. In this case, the LED
labelled ‘L’ on the Arduino should flash slowly. Show the TA your work.

Part B – Sensor Reading


This program will automatically poll an ultrasonic sensor, causing the sensor to emit an
ultrasonic sound pulse. The program will read the elapsed time between sending the pulse and
receiving an echo, convert the time to distance using the speed of sound, and print the distance to
the computer screen via the serial port connection.
1. Drag an ultrasonic sensor (HC-SR04) to the workspace and connect it to the Arduino
board by using the wiring diagram below.

2. Copy the following code and run the simulation.


const int trigPin = 12;
const int echoPin = 11;

long time;
int distance;

void setup() {

pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);

Serial.begin(9600);
}
void loop() {

// We initialize the trigger to LOW


digitalWrite(trigPin, LOW);
delayMicroseconds(2);

// The trigger receives a 5V during 10 microsecondes


// which generates an ultrasonic wave.
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);

// With echoPin and the pulseIn function,


// we read the time value necessary for the sound wave
// to do go and come back.
time = pulseIn(echoPin, HIGH);

// We calculate the distance knowing the speed of sound is


// around 340 m/s in the air
// speed of sound considered at 347 m/s therefore 0,0347 m/ms
distance= time*0.0347/2;
// note that the speed of sound depends on atmospheric conditions
// for example: the temperature and humidity.

// Displays the distance on the serial monitor


Serial.print("Distance: ");
Serial.println(distance);
}

3. You can click on the sensor while the simulation is running and move the green circle to
see the distance values change in the serial monitor (can be found at the bottom of the
code window). Show the TA your work.

Part C – DC Motor Control


This program will test and control two DC motors using a pre-set sequence of commands.
1. Remove the sensor from the workspace. Drag the L293D H-bridge motor driver on the
breadboard and drag a gearmotor beside it.
2. Drag wires to make the connections in the following picture. The motor leads are
connected to the output pins of the driver and the Arduino controls it via the input pins.
3. The following code will turn the motor clockwise for 2 seconds, then anticlockwise for
another 2 seconds and repeat. Copy it into the editor and run the simulation. Open the
serial monitor to see the print statements. Show your work to the TA.

void setup()
{
Serial.begin(9600);
pinMode(10, OUTPUT);
pinMode(9, OUTPUT);
pinMode(8, OUTPUT);
pinMode(7, OUTPUT);
}

void loop()
{
// clockwise
Serial.println("tick");
digitalWrite(10, HIGH);
digitalWrite(9, LOW);
digitalWrite(8, HIGH);
digitalWrite(7, LOW);
delay(2000);
// anti clockwise
Serial.println("tock");
digitalWrite(10, LOW);
digitalWrite(9, HIGH);
digitalWrite(8, LOW);
digitalWrite(7, HIGH);
delay(2000);
}

4. Swap the connections for the output pins of the driver. What happens?
5. Now add commands to control a second DC motor in conjunction with the first motor.
Connect a gearmotor to the other side of the motor driver chip using the output 3 and 4
pins.
6. Add command lines to the sketch to control the second motor. Start by initializing the
new motor pins being used. Write two more pinMode functions to initialize the pins as
OUTPUT.
7. Copy the commands for the first motor to add the second (see below).
8. Run the simulation. Verify that both motors now spin in unison. Show the TA your work.
a. Note that you can vary the speed of the motors by varying the voltage provided to
the enable pin on the chip with an analogWrite function.

Additional resources
● Arduino is open source which means there is lots of information out there. To start you
can browse https://www.arduino.cc/en/Tutorial/HomePage to find tutorials and extra
information.
● Adafruit also has lots of tutorials on many different types of Arduinos and sensors,
https://learn.adafruit.com/.

You might also like