Arduino Overview - borq79/cs.edu GitHub Wiki
Arduino Projects
Welcome to the Cary Area Public Library Arduino Project! This is where all information will be posted regarding the projects. There will be several aspects to the project and this site contains all the information necessary to participate in the course(s).
The goal of this course, offered by the Cary Area Public Library, is to provide an introduction to the concept and application of microcontrollers, specifically the Arduino class of microcontrollers. It should be noted that the Cary Area Public Library is only responsible for providing the classroom and Arduino kits. The instruction and course material is provided by Ryan Brock, a Cary resident. Ryan is a volunteer and not an employee of the Library. Any views or opinions, etc. of Ryan Brock's do not necessary reflect the views and opinions of the Library.
Parts List
What is an Arduino
Arudino is an Open Source prototyping microcontroller. The Arduino Uno (that we will use in this course) is based on the ATmega328P. The Arduino is an 8-bit, 16 MHz, microcontroller that has 14 digital IO pins (6 can be PWM outputs), 6 analog inputs, USB, and a reset button. The Arduino is programmed using the Arduino Programming Language (based on Wiring).
While the Arduino may have limited memory and operate at a relatively slow clock rate, it is great for applications that involve interfacing with external accessories such as motors, sensors, LEDs, networks, etc. There are also many add on boards (shields) that you can add that increase the functionality of the base Arduino.
Visit the Arduino Playground to get started on any number of projects. There are projects of all varieties and levels of difficulty.
Microcontrollers vs. Microprocessor
The Arduino is a microcontroller and not a microprocessor, but what does that mean? A microprocessor is a CPU that doesn't contain external RAM, storage, etc. A microcontoller, on the other hand, does have on-chip RAM, and some sort of storage such as ROM or Flash. Microprocessors tend to be for more general applications and are meant to be part of a larger architecture such as a PC or a Raspberry Pi that allow easily loading and executing multiple programs. Microcontrollers are typically designed for a single purpose and tend to be in closed systems where the program is flashed or burned to a ROM. The difference used to be more apparent, but the two concepts are blurring together more and more. As it stands, the Arduino is a microcontroller and the Raspberry Pi is a computer that houses an Arm-based microprocessor and it typically runs a Linux-kernel variant Operating System. Many projects can be done with either an Arduino or a Raspberry Pi, but some applications tend to be more suited to one or the other.
An Arduino, which isn't running any operating system, can only run a single program (unless you load an OS - which is itself a program). This may not sound very useful, but if you have a simple time sensitive application (driving a stepper motor or reading sensors at a specific interval) the Arduino is a great choice. If you have the need to load several programs and care less about them being 'real-time' your application may be better suited towards the Raspberry Pi (note that the OS has more to do with the 'real-time' aspect, but typically the Raspberry Pi is not loaded with a 'real-time' operating system).
Obviously cost is a factor to consider as well. The Arduino tends to be cheaper than the Raspberry Pi, but it really depends upon the form-factor and configuration. There are several variants of each product.
Getting to Know your Arduino Uno
The Arduino Uno runs on 5V, but accepts input from 6V to 20V, but 7V to 12V is recommended (I typically use a 9V battery for mobile applications).
Different Arduino Models
The Application Loop
There are two parts to the program (called a sketch) that you load onto the Arduino:
setup()
loop()
The setup() function is run once at the start of the program execution (e.g. when the Arduino is powered on or reset). It is used for global/static variable initialization, pin configuration/initialization, etc.
The loop() is called after setup() and loops over and over again until the Arduino is reset or powered off. If you simply have a program that has a blank loop(), the program counter of the Arduino will just keep looping forever doing absolutely nothing - except maybe heat up the small area around it. The loop() function is where most of the program logic resides.
Memory Management
The Arduino Uno has:
- 32KB of Flash space (0.5 KB is allocated by the bootloader)
- 2KB for SRAM
- 1KB EEPROM
The 1KB EEPROM (Electrically Erasable Programmable Read-Only Memory) is a non-volatile storage location that will survive between power cycles. It is typically not used for most applications.
The 2KB of SRAM (Static Random Access Memory) is memory that is available during program execution. It is where (typically - see below) variables (integers, strings, etc) are stored during execution. Programs need to be written with this in mind as memory hungry applications will behave erratically.
The program space, or the space used to store the actual program object code (instructions) is 32KB of Flash. This space can also be used for variables/data using the PROGMEM keyword.
Note that each time you compile or verify your program you are given the space usage at the bottom of the Arduino IDE:
Build options changed, rebuilding all
Sketch uses 20,768 bytes (64%) of program storage space. Maximum is 32,256 bytes.
Global variables use 1,040 bytes (50%) of dynamic memory, leaving 1,008 bytes for local variables. Maximum is 2,048 bytes.
Data Types and Variables
Operators
Arrays
Global and Local Variable Space
When writing a program you have the option to store the variable in global or local scope (where the variable can be seen). You typically want to limit a variable's scope only to where it is needed to avoid accidental corruption or updates to a variable. Limiting the scope and defining variables only once they are needed tends to greatly improve the readability of your program.
Control Structures
Most programs must make one or more decisions or run an operation more than once. Decisions and program flow alterations are done using one of several control structures.
Conditionals are used to make decisions and read somewhat like a sentence: if( didSomethingHappen() ) { doSomething(); } else { doSomethingElse(); }
You can add multiple conditional branches together to make something a little more complex, or you can organize them using a switch statement. The following two structures have the same result:
if (i == 1) {
Serial.println("i is equal to 1");
} else if (i == 2) {
Serial.println("i is equal to 2");
} else {
Serial.println("i is neither 1 or 2");
}
switch(i) {
case 1:
Serial.println("i is equal to 1");
break;
case 2:
Serial.println("i is equal to 2");
break;
default:
Serial.println("i is neither 1 or 2");
break;
}
If you need to iterate over an array or perform an operation more than once you typically do so with a loop. There are three standard types of loops: for, while, do-while.
For loops are useful when you have a bounded loop that you need to run from a starting point to a known ending point. While loops run a body of code, indefinitely, until some condition is false. Do-While loops are similar to while loops except they are run at least once as the condition is evaluated at the end of the loop rather than the start of the loop (while and for evaluate before the conditional code body is executed)
These three loops will print numbers 1 to 100:
for(int i = 1; i <= 100; i++) {
Serial.println(i);
}
int i = 1;
while (i <= 100) {
Serial.println(i);
i++;
}
int i = 1;
do {
Serial.println(i);
i++;
} while (i <= 100);
See the Arduino help for more information on the structure and usage of various control structures, etc.
Hello World
Delay
I/O
The Arduino has 6 analog input pins. They are connected to a 10-bit A/D converter to allow for 1024 (0-1023) values of resolution. There are 14 digital pins that can be either input or output. 6 of the digital pins can function as PWM output.
Drive an LED
See different examples on how to drive an LED
The DHT11 Temperature and Humidity Sensor
The W5100 Ethernet Shield
Connecting to Twitter
Putting it All Together
[https://github.com/borq79/capl-tweet-temp/blob/master/temp-to-twitter/temp-to-twitter.ino](Send Temperature to Twitter)