Arduino programming
Terminology
Terminology
• Sketch
– A program you write to run on an Arduino
board
• Pin
– An input or output connected to something
• Eg: output to an LED, input from a knob
• Digital
– Value is either HIGH or LOW
• Aka on/off, one/zero
• Eg: switch state
• Analog
– Values ranges, usually from 0-255
• Eg: LED brightness, motor speed, etc.,
Digital I/0
• pinMode(pin, mode)
– Sets pin to either INPUT or OUTPUT
• digitalRead(pin)
– Reads HIGH or LOW from a pin
• digitalWrite(pin, value)
– Writes HIGH or LOW to a pin
• Electronic stuff
– Output pins can provide 40 mA of
current
– Writing HIGH to an input pin installs a
20KΩ pullup
Arduino Timing
• delay(ms)
– Pauses for a few milliseconds
• delayMicroseconds(us)
– Pauses for a few microseconds
• More commands:
arduino.cc/en/Reference/HomePage
Digital? Analog?
• Digital has two values: on and off
• Analog has many (infinite) values
• Computers don’t really do analog,
they quantize
Bits and Bytes
Variables
Putting It Together
• Complete the sketch
(program) below.
• What output will be
generated by this
program?
• What if the
schematic were
changed?
Structure
– Variables
• setup()
• loop()
– User Defined functions
setup()
• The setup() function is called when a
sketch starts.
• Use it to
– initialize variables,
– pin modes, \
– start using libraries, etc.
• The setup function will only run once,
after each powerup or reset of the
Arduino board.
loop()
• After creating a setup() function,
which initializes and sets the initial
values,
• the loop() function loops
consecutively
• Use it to actively control the Arduino
board.
Control Structures
• if
• if...else
• for
• switch case
• while
• do... while
• break
• continue
• return
• goto
Further Syntax
• ; (semicolon)
• {} (curly braces)
• // (single line comment)
• /* */ (multi-line comment)
• #define
• #include
Operators
• Arithmetic Operators
= (assignment operator), + (addition), -
(subtraction), * (multiplication), /
(division), % (modulo)
• Comparison Operators
== (equal to), != (not equal to), < (less than), >
(greater than), <= (less than or equal to), >=
(greater than or equal to)
• Boolean Operators
– && (and), || (or), ! (not)
• Pointer Access Operators
• Bitwise Operators
• Compound Operators
Operators
• Pointer Access Operators
* dereference operator, & reference operator
• Bitwise Operators
& (bitwise and), | (bitwise or), ^ (bitwise xor), ~
(bitwise not), << (bitshift left), >> (bitshift right)
• Compound Operators
++ (increment), -- (decrement), += (compound
addition)
-= (compound subtraction), *= (compound
multiplication)
/= (compound division), %= (compound
modulo), &= (compound bitwise and), |=
(compound bitwise or)
Variables
• Constants
• Data Types
• Conversion
• Variable Scope & Qualifiers
• Utilities
Variables: Constants
• HIGH | LOW
• INPUT | OUTPUT | INPUT_PULLUP
• LED_BUILTIN
• true | false
• integer constants
• floating point constants
Variables: Constants: High/Low
• In reference to a pin , whether a pin
is set to an INPUT or OUTPUT.
– When a pin is configured as an
• INPUT with pinMode(), and read with
digitalRead(), the Arduino will report
HIGH/Low if:
– 5V boards
» greater than 3.0V is present at the pin;
(High)
» less than 1.5V is present at the pin
(Low)
– 3.3 V boards
» greater than 2.0V is present at the pin;
High
» less than 1.0V (Approx) Low
Variables: Constants: High/Low
• In reference to a pin , whether a pin
is set to an INPUT or OUTPUT.
– When a pin is configured as an
• OUTPUT with pinMode(), and set to
HIGH with digitalWrite(), the pin is at:
– 5 volts (5V boards);, 3.3 volts (3.3V boards)
• OUTPUT with pinMode(), and set to Low
with digitalWrite(), the pin is at:
– the pin is at 0 volts (both 5V and 3.3V
boards).
– In this state it can sink current,
– e.g. light an LED that is connected through a
series resistor to +5 volts (or +3.3 volts).
Functions
• Segmenting code into functions allows a
programmer to create modular pieces of
code that perform a defined task
• Standardizing code fragments into
functions has several advantages
– Help the programmer stay organized.
– only has to be thought out and debugged
once.
– reduces chances for errors in modification,
if the code needs to be changed.
– sections of code are reused many times.
And by making it more modular, and as a
nice side effect, using functions also often
makes the code more readable.
Anatomy of a C function
Sample Program
Builtin Functions
• Digital I/O
• Analog I/O
• Due & Zero only
• Advanced I/O
• Time
• Math
• Trigonometry
• Characters
• Random Numbers
• Bits and Bytes
• External Interrupts
• Interrupts
• Communication
• USB (32u4 based boards and Due/Zero only)