0% found this document useful (0 votes)
3 views44 pages

Arduino Programming and Interfacing

The document provides an overview of Arduino programming, covering topics such as variable declaration, functions, control structures, and interfacing with various components. It includes examples of code snippets for common tasks like blinking an LED, using digital and analog inputs, and debugging with serial print commands. Additionally, it outlines how to install libraries for specific components and offers guidance on building algorithms and control structures.

Uploaded by

asadmechanical31
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views44 pages

Arduino Programming and Interfacing

The document provides an overview of Arduino programming, covering topics such as variable declaration, functions, control structures, and interfacing with various components. It includes examples of code snippets for common tasks like blinking an LED, using digital and analog inputs, and debugging with serial print commands. Additionally, it outlines how to install libraries for specific components and offers guidance on building algorithms and control structures.

Uploaded by

asadmechanical31
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Arduino Programming and Interfacing

Walter Ditch
Programming

Information on following slides is based on Arduino


website content, including: -
• Arduino Foundations -
[Link]
• Sketches
• [Link]
• Arduino Tutorials -
[Link]
Comments
• Multiple Line Comments
• /*
* Blink
*
* The basic Arduino example. Turns on an LED on for one second,
* then off for one second, and so on... We use pin 13 because,
* depending on your Arduino board, it has either a built-in LED
* or a built-in resistor so that you need only an LED.
*
* [Link]
*/
• Single Line Comment
• int ledPin = 13; // LED connected to digital pin 13
Variables

• [Link]
• A variable is a place for storing a piece of data. It has
a name, a type, and a value. For example, the line
from the previous Blink
int ledPin = 13;
• declares a variable with the name ledPin, the type int
(integer), and an initial value of 13.
Declaring and Using Variables

int pin; // Declares an integer variable


int pin = 13; // Declares and initialises
pin = 2; // Changes the value
pinMode(13, OUTPUT) // Command uses a number
pinMode(pin, OUTPUT) // Command uses a variable
Variable ‘Scope’
int pin = 13; // Global variable (visible everywhere)

void setup()
{
int my2ndPin = 3; // Local variable (to setup function)
pinMode(pin, OUTPUT);
pinMode(my2ndPin, OUTPUT);

void loop()
{
digitalWrite(pin, HIGH); // Works – uses a global variable
digitalWrite(my2ndPin, HIGH); // Won’t work!
}
Some Other Variable Types
float pi = 3.14; // a ‘floating point’ or non-integer variable
const float pi = 3.14; // a constant can’t be changed later
boolean running = false; // a ‘Boolean’ or true / false variable
running = true; // change a ‘Boolean’ value
running = !running; // invert the Boolean value
Example using Variable Types
int LEDpin = 5; // LED on pin 5
int switchPin = 13; // Momentary switch on 13, other side connected to ground
// Switch will go ‘low’ when pressed
boolean running = true; // Assume LED is on if running = true

void setup()
{
pinMode(LEDpin, OUTPUT);
pinMode(switchPin, INPUT);
digitalWrite(switchPin, HIGH); // Turn on the internal pullup resistor
}

void loop()
{
if (digitalRead(switchPin) == LOW) // Use two equal signs ‘==‘ when testing a value
{ // Switch is pressed so goes ‘low’ - pullup keeps pin high normally
digitalWrite(LEDpin, running); // LED On
}
else
{
digitalWrite(LEDpin, !running); // Send inverted value to LEDpin – LED off
}
}
Functions

• A function (otherwise known as a procedure or sub-


routine) is a named piece of code that can be used
from elsewhere in a sketch. For example, here's the
definition of the setup() function from the Blink
example:
• void setup()
{
pinMode(ledPin, OUTPUT); // sets the digital pin as output
}
Setup() and Loop()

• There are two special functions that are a part of


every Arduino sketch: setup() and loop().
The setup() is called once, when the sketch starts. It's
a good place to do setup tasks like setting pin modes
or initializing libraries. The loop()function is called
over and over and is the heart of most sketches. You
need to include both functions in your sketch, even if
you don't need them for anything.
pinMode() and digitalWrite()

• The pinMode() function configures a pin as either an


input or an output.
• The digitalWrite() functions outputs a value on a pin.
For example, the line:
• digitalWrite(ledPin, HIGH);

• sets the ledPin (pin 13) to HIGH, or 5 volts. Writing a


LOW to pin connects it to ground, or 0 volts
Delay()

• The delay() causes the Arduino to wait for the


specified number of milliseconds before continuing
on to the next line. There are 1000 milliseconds in a
second, so the line:
• delay(1000);

• creates a delay of one second.


Arduino Debugging 1 of 3

The Serial Print command, combined with the serial


monitor feature may be used for simple debugging.

You first need to initialise the serial interface in the


setup function

void setup() {
[Link](9600); // open the serial port at 9600 bps:
}
Arduino Debugging 2 of 3

Insert a serial print command in your program –


typically followed by a delay to slow it down, if it
happens in a loop

void loop() {
//Send the variable to the the Serial port.
[Link](myVariable1);
Delay(1000);
}
Arduino Debugging 3 of 3

Print output can be formatted

void loop() {
[Link](myVariable1);
[Link](“ ”); // Add some spaces
[Link](myVariable2); // This version starts a new line
Delay(1000);
}
Grove Shield Pin-outs
Case Study – Simple Alarm

• We’ll use a shield and two inputs – A0 and A1, and


these will be configured as digital inputs (rather than
analogue)
• A single alarm output will be activated if either input
is activated (pin 3).
• The Button sample sketch will be used as a starting
point …
Button Sample Sketch (part of)
Alarm – Part 1
Alarm – Part 2

Notice how the switches are ‘logically’ combined


‘||’ = OR, ‘&&’ = AND – see [Link]
Algorithms and Control Structures

A definition from [Link]: -


• “A step by step procedure designed to perform an
operation, and which (like a map or flowchart) will lead
to the sought result if followed correctly. Algorithms have
a definite beginning and a definite end, and a finite
number of steps. An algorithm produces the same output
information given the same input information, and
several short algorithms can be combined to perform
complex tasks such as writing a computer program. A
cookbook recipe, a diagnosis, a problem solving routine,
are some common examples of simple algorithms.”
Algorithm Building Blocks

• Sequence – doing tasks in a specific order


• Iteration – repeating a task a certain number of
times, or until a test condition occurs.
• Decisions – making choices based on true / false
tests.
• Menus – selecting one option from a range of
choices
• Input/Processing/Output – standard commands for
reading inputs, performing calculations and sending
results to outputs
Sequence

• Simply code the tasks in the correct order.

• (But be aware that a sequence placed inside setup()


is done once only at the start of the program, while a
sequence placed inside loop() will be repeated
continuously.)
Iteration (1 of 2)

• The ‘for’ loop structure can repeat a task a fixed


number of times, typically using a count variable
which can be either incremented (count up) or
decremented (count down).
• E.g.
for (int thisPin = 2; thisPin < 8; thisPin++)
{
pinMode(thisPin, OUTPUT);
}
• [Link]
n
Iteration (2 of 2)

• The ‘Do – While’ structure can repeat a section of


code as long as a condition is true.
• Eg.
do
{ // statement block }
while (test condition);

(the code will always run at least once)


• [Link]
Decisions (1 of 2)

• The ‘IF’ structure allows code to be conditionally


executed.
• E.g.
if (someVariable > 50) {
// do something here
}

• [Link]
Decisions (2 of 2)

• The and ‘IF … ELSE’ structure allows a first section of


code to be conditionally executed if a condition is
true, or a second section if the condition is false.
• E.g.
if (pinFiveInput < 500)
{
// action A
}
else
{
// action B
}

• [Link]
Menus
• The ‘switch case’ structure allows a single choice from a list of options,
which is simpler than nesting if … else structures.
• E.g.
switch (var) {
case 1:
//do something when var equals 1
break;
case 2:
//do something when var equals 2
break;
default:
// if nothing else matches, do the default
// default is optional
break;
}
• [Link]
Example Sketches

From the Arduino IDE, see: -

• File > Examples > Control

for a range of examples related to control structures


Interfacing Basics, including…

• Digital Input
• Analogue Input
• Light / Temperature Sensor
• Digital Output – LED
• Transistor Output
• Relay Output
• Inductive Loads
• Ultrasonic distance sensor
• LCD Display
Digital Input
Analogue Input
Light / Temperature Sensor
Digital Output - LED
Transistor Output
Relay Output
Inductive Loads
Servo
(Controls rotary position 0-180◦)
Keypad Input
LCD Display
Ultrasonic Distance Sensor
How to Use these Components?

• Look for an example sketch (File > Examples) and


adapt it for your application.
What if a Component requires a Library to be
installed?
• First download the library (usually to the Downloads folder)
• The Arduino has options to automatically install libraries
(Sketch > Include Library > Add Zip Library).
• Manual installation is also relatively easy (and more reliable).
First Unzip the previously downloaded file then expand the
folder tree to find the application files. Rename this folder to
be the same as the [Link] and xxx.h files. By default, libraries
are installed in a folder: Documents\Arduino\libraries. First
copy and paste the previously renamed folder below into the
Arduino libraries folder, then close and reopen the Arduino
IDE.
• Either way, the new library should be visible under File >
Examples. It will typically have its own set of examples.
Library Example – Grove I2C LCD display
• The product page is here, from where you can
download the Github library.
• Unzip the folder and look for the [Link] and
xxx.h files.
• In this case, rename the parent folder as rgb_lcd,
then copy and paste it below
Documents\Arduino\libraries

You might also like