Arduino 101

Arduino 101

Concepts

Arduino is a design and implementation of a microcontroller board based on the Atmel ATmega328. The 20 I/O pin base model is the Arduino Uno. This replaces the DueMilleNove (Italian for 2009). There are several variants to the base model. The 70 I/O pin models are exemplified by the Arduino Mega 2560, based on the ATmega2560, and its variants.

 

Open source hardware and software

Circuit board schematics are available in Eagle. Thus if no one makes a board with Arduino and 12 relays, we can use published schematics to make our own.

Stages of board production:

  1. Circuit schematic.
  2. Breadboard circuit and components.
  3. Test prototype board.
  4. Route traces for printed circuit board (PCB).
  5. Fabricate PCB.
  6. Stuff PCB and solder in components.
  7. Test.
  8. Rework.
  9. Production manufacturing.

 

Terminology

Arduino Standard
Sketch Software program
Verify Compile
Shield Daughter board
.ino .cpp

 

Arduino Program

C is well suited to microprocessors, but C++ has some features not designed for them. Arduino is pretty much standard C/C++. There are some additions to make life easier for you. Arduino provides libraries to perform common tasks, but these are outside the scope of this 101 class. The downside is that there are some C++ concepts that do not work under Arduino, and these are not well documented. Don’t try to get too fancy. Sticking to C is usually safe.

Microprocessors

Applications on PCs and mainframes Microcontrollers
Runs on an Operating System (OS) No OS
Often multi-programming and multi-tasking. Only one thing at once.
Graphical User Interface (GUI) Often no GUI.
General purpose. Specialized.
Perceived as a computer. Perceived as pager, GPS, meter, music player, part of an appliance, etc.
Expensive Cheap
Test with source code debugger. Difficult to debug
Mostly deterministic. Behavior causing a glitch can arise from unforeseen timing issues.
Fast with large memory. Limited storage and speed.
File system and networked. No file system or network.

Microcontroller debugging techniques

Emulate on a PC.

Present it with all possible inputs and verify outputs.

LEDS for diagnostics. (I have written a routine to use Morse code on an LED).

Serial line for diagnostics.

Data-logger.

Oscilloscope.

In-circuit emulator: Replace the main integrated circuit (IC) with a socket that goes to a larger computer programmed to respond appropriately.

Logic Analyzer: Wire appropriate leads to a machine that can record all machine states before and after a glitch.

Sample code

Download the Arduino IDE.

Select File | Examples | 01.Basics | Blink

Compile the program, and load it onto the Arduino. It will blink the LED on for 1 second, and off for a second. Experiment with different timings.

 

There are many other example programs that illustrate Arduino capabilities. If the code that we deal with is unclear, it can be useful to go back to the example that deals with a new feature.

 

// Comments are anything on the line following slash-slash.

/* or any text between an initial slash-star

and the closing star-slash.

This type can extend over multiple lines */

 

/* The behavior /* of nested

comments is undefined */

and is best avoided. */

 

/*

// However, it is safe to use the C++ style slash-slash

between the original C style comment delimiters.

*/

 

The scope of a routine is limited by { … }

A standard C program starts with a routine called main()

Instead, Arduino has two routines called setup() and loop()

 

Case is significant in C.

Anything outside the scope of a routine is global.

 

Arduino keywords that are not standard in C++ are setup, pinMode, INPUT, OUTPUT, digitalWrite, LOW, HIGH, loop, digitalRead.

Pin Layout

The basic Arduino has 20 I/O pins. The Mega has 70 I/O pins. There are also 6 power pins and a few others. Pins have special purposes.

 

On the 20 pin layout, pins are designated D0-D13 and Analog In 0-5. Any of these 20 pins may be used as digital input or output. When the Analog In pins are used digitally, they are referred to as D14-19. Analog Input can only come from the 6 pins so designated. Analog output can only be done on pins D3, D5, D6, D9, D10 and D11.

Serial input is on D0 and serial output on D1.

Interrupts can be attached to D2 or D3.

Any digital write to D13 will turn on or off an on-board LED.

SPI communications are done on D10, D11, D12 and D13.

Pins A4 and A5 support TWI communication using the Wire library.

 

The 70-pin layout has D0-D53 and A0-A15.

Analog input is on A0-A15.

Analog output can be done on any of pins D0-D13.

Serial line pairs are (D0,D1), (D14,D15), (D16,D17) and (D18,D19)

Interrupts are on D2, D3, D18, D19, D20 and D21.

Any digital write to D13 will turn on or off an on-board LED.

SPI communications are done on D50, D51, D52 and D53.

Pins D20 and D21 support TWI communication using the Wire library.

 

Most shields designed for the 20-pin layout work with the 70-pin layout, except those that depend on the Wire library or SPI.

Analog read and write

Analog reads and writes can only happen on the designated pins. Analog input is 10 bits and produces a value from 0 to 1023. By default, 1023 means 5 volts. The input signal can be any value between 0 and 5 volts. This contrasts to a digital input, where any signal less than 0.7 volt is considered LOW and any signal greater than about 2.5V is HIGH, with intermediate values undefined.

 

The meaning of 1023 from analogRead() can be changed by putting a voltage on the AREF pin. AREF is located where you would expect to find D15. The pin that you would expect to be D14 is one of the Arduino’s three ground pins. The value for AREF can be less than 5V, but not more.

 

Analog write is different type of signal. It is basically a digital signal in that it can only be at 0 or 5V. However, it is pulse width modulated (PWM), meaning that it is quickly turned on an off so that it acts like an analog voltage. It is always turned on and off at the same frequency: 490 Hz, which means a 2.04 ms pulse width. The pulse has a duty cycle. If you do analogWrite(pin, 127), the pulse will be on for 1.02 ms and off for 1.02 ms. analogWrite(pin, 0) has the pulse always off and analofWrite(pin, 255) has the pulse always on.

 

Note that analogRead() produces values from 0 to 1023 and analogWrite() uses values of 0 to 255. If you do an analogWrite(pin, 64) and then an analogRead of that signal, you will see zero 75% of the time and 1023 25% of the time.

Security and reliability

Reliability means that a program will always do what it is supposed to do. Security is a guarantee that the program will never do what it is not supposed to do. Systems such as Windows, Linux or Mac OS are not secure. They are simply too big and there will always be unanticipated exploits. Complexity is a core risk for all software systems.

 

It is possible to mathematically prove that a program is a correct implementation of its specifications. This is only practical for small programs. Safety critical programs must be kept simple. A safety critical program must have no Internet connection.

Interrupt programming

Arduino provides the convenient delay(milli_seconds) function, which will wait until the time has elapsed. Avoid using delay(), since it prevents anything else from using the processor. Better practice is to get current time with the millis() function and do other stuff until time has elapsed.

 

Interrupts let you break into the main code and do important stuff. An interrupt is hardware driven. When it occurs, all the machine’s registers are pushed into a buffer while the interrupt gets attention. Interrupt processing should be short. When an interrupt occurs during interrupt processing, things get complicated.

 

Critical items (such as power dropping too low) should be on interrupts. Arduino provides the attachInterrupt() function.

 

C provides the seldom-used type modifier volatile, which is the opposite extreme from constvolatile tells the compiler that this variable can change externally and it cannot optimize it away.

Advanced Programming

Turning on the verbose option in Arduino IDE will reveal the temp directory housing the intermediate files. Arduino generates a main program with a .cpp or .c extension. It generates object files and the executable files that are uploaded to the board. You can use the Atmel Studio 6 professional development environment with these files.

Open Source Autonomy