Basic Arduino Notes
Basic Arduino Notes
Microcontrollers
A microcontroller is a little computer, that has a processor, 1-2 kilobytes of RAM for holding data, and
a few kilobytes of EPROM aka flash memory for holding your programs.
I/O pins can be either digital or analog.
Voltage Regulators
Regulate voltage to a constant value.
Ex. (7V-12V) - - > regulates to - - > (5V)
Microcontroller
The CPU controls everything that goes on within the device. It fetches instructions stored in the flash
memory and executes them. This could also mean fetching data from and to the RAM. The EEPROM
memory is similar to flash memory, but it is nonvolatile which means that it remains in the device even
after it is turned off. EEPROM is used whenever you dont want to lose data after being turned off.
Memory Types
RAM = working memory
Flash = computer programs aka (sketches)
EEPROM (Non-Volatile)
05/21/14
====================================================
/*
Blink
Turns on an LED on and off
*/
digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
delay(1000);
=====================================================================
This section at the very top of the code is a multiline comment started by (/*) and ended by (*/). This
code section explains the code that will be written. It is not really needed. A single line comment is
started with a double slash (//).
This section initializes any variables that are required before the Setups Section
( int led = 13, initializes a variable called led and assigns it a value of 13 )
Setup Section
// the setup routine runs once when you press reset:
void setup() {
// initialize the digital pin as an output.
pinMode(led, OUTPUT);
}
The Setup section initializes weather pins will be input or output pins. The setup portion of the code will
only run once at the beginning of the program or anytime the reset pin is pressed.
( pinMode(X,A), sets the mode of the pin X as either an INPUT or OUTPUT)
(A = INPUT) if the pin will be a input pin, or (A = OUTPUT) if it will be an output. Must be capitalized.
Loop Section
// the loop routine runs over and over again forever:
void loop() {
The loop section runs after the setup section has run and will run forever or until the power source has
run out.
( digitalWrite(X,B), turns an output pin X on [HIGH] or off [LOW] )
(B = HIGH) if we want pin X to be on (5V) or (B = LOW) if we want pin X to be off (0V). Must be
capitalized.
( delay(X), will pause the microcontroller program by X milliseconds)
05/22/14
C Language Basics
digitalWrite(13, HIGH);
the digital write function must be spelled like shown and is used to turn on and off the pin. This example
above turns on pin #13. Everything should end with a semicolon.
05/23/14
void setup()
{
}
The code above defines a function called setup. Setup and loop must be defined in every
sketch/program that you write. The void means that these function do not return a value. After the
keyword void comes the name of the function and parentheses to contain any arguments.no semicolon
after setup() because the function is being created and not called. Anything within the curly braces is
known as a lock of code
pinMode(13, OUTPUT);
pinMode sets a particular pin to be either an input or an output. In this case, pin 13 is set as an output
pin. To set as an input, change OUTPUT to INPUT. Must be capitalized. pinMode is usually put in the
setup portion of the code and not in the loop section since there is no need to
int variable1;
the command above creates an integer but does not assign a value for it. This is useful when you dont
know the value of a variable.
variable1 = 2;
the command above assigns a value to a previously created variable.
Serial.begin(9600);
The line above initializes the serial communication between Arduino and software on computer. The
number inside parenthesis is the baud rate and is the speed at which the computer communicates with
the Arduino.
Serial.println(variable);
The line above prints the value of the variable.
If (a<b)
{
Put code here
}
The if statement above takes a condition that goes in the parenthesis. And the code to execute if the
condition is true goes in the curly braces.
5/27/14
Operator
<
>
<=
>=
==
!=
Meaning
Less than
Greater than
Less than or equal to
More than or equal to
Equal to
Not equal to
Example
9 < 10
10 > 10
9 < = 10
10 >= 9
9 == 9
9 != 9
Result
True
False
True
True
True
False
In this example FOR loop shown above, the counter is i with an initial value of zero. If i is below
20, the code will execute. After the code is executed but before it is executed again, i increases
value by one.
X ++;
The expression above is exactly the same as [ X = X + 1;]
void loop()
{
static int count = 0;
code
count = count + 1;
}
The code above is just to demonstrate static int. static int makes sure that the int declaration is only
done at the beginning of the loop. If the static were not placed, count would never go above 1 because
it would be re-declared to zero at the beginning of the loop. However, since there is a static in front of
the integer declaration, once it is initialized with a value of zero, it will not be reinitialized.
Floats
If the numbers required will require decimal points, integers cannot be used easily. Floats are used in
these situations. An example function with a float is shown below.
float centToFaren(float C)
{
float f = C * 9.0 / 5.0 + 32.0;
return f;
}
The above code will be a floating variable. The numbers are written with a .0 like 8.0 to treat them as
floats rather than ints.
// comments should be added constantly in order to keep you code easy to read and understand.
?
?
20
2
3
2
?
An(6)
An(7)
Array2(0)
Array2(1)
Array2(2)
Array2(3)
Bn(0)
Be careful when calling arrays. When a array is created, the next available memory spaces are saved for
use by that array. If you try to call Array2(4), you will get a value, however who knows what will be
returned since that space is used by another array. Make sure that you do not go outside the size of the
array
6/11/14
Serial.avaliable()
The function above returns the number of bytes of data in the buffer waiting to be read.
6/12/14
Pull Up Resistors
Pull up resistors are used to prevent a pin from floating when it is not being used. See pg. 90.
Debouncing
Debouncing is placing a small delay after a button is pressed before the reading is actually taken. This is
because the value after a button is pressed bounces for a small amount of time which could create
problems when taking readings with a microcontroller. See pg. 94.
random(1, 10)
the function above creates a random number between 1 and 10. See pg. 105 for information on random
numbers. The random numbers are no really random since this is against the inner workings of the
microcontroller. If you want really random numbers, youre going to need to seed values from the real
world.
6/16/14
Interrupts
If the Arduino receives a signal in a specified way, the Arduino processor will suspend what its
doing and run a function attached to that interrupt. See pg 113
6/19/14
int interruptPin = 2;
int ledPin = 13;
int period = 500;
void setup()
{
pinMode(ledPin, OUTPUT);
pinMode(interruptPin, INPUT);
digitalWrite(interruptPin, HIGH); // pullup
attachInterrupt(0, goFast, FALLING);
}
void loop()
{
digitalWrite(ledPin, HIGH);
delay(period);
digitalWrite(ledPin, LOW);
delay(period);
}
void goFast()
{
period = 100;
}
=============================================================
The key to interrupts is the attachInterrupt function that is placed in the setup part of the code. The first
of the inputs is the pin that will be used. Since the Arduino uno only has only two interrupt pins, the first
input to the attach function will be 0 or 1 (pin 2 or 3). The second input is the name of the function that
will be executed when the interrupt is started. The last input is the condition that will cause the
interrupt to start running the code. (CHANGE, FALLING, or RISING). The signal is five volts max, so a
falling signal is from 5V to 0V. a rising signal is 0V to 5V, and a change is any of the two.
6/20/14
PROGMEM
To save data in flash memory, the progmem library has to be included using the following
statement.
#include <avr/pgmspace.h>
EEPROM
EEPROM is designed to store data for years. (electrically erasable read only memory). Despite
the name it is not really read-only. You can write to it. It a little more work to store memory here since
you have to store one byte at a time. (pg 117)