Arduino Board Uno Rev3-Atmega 328: First Steps

From Embedded Lab Vienna for IoT & Security
Revision as of 16:16, 10 August 2020 by CCinay (talk | contribs) (Created page with "<div style="max-width: 970px"> == Summary == The Arduino UNO R3 is the current version of the Arduino UNO. This is the standard Arduino. It is completely sufficient for most...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Summary

The Arduino UNO R3 is the current version of the Arduino UNO. This is the standard Arduino. It is completely sufficient for most projects and is very affordable.

  • Arduino_oard_Uno_Rev3-Atmega_328

Arduino UNO Überblick

Main component is a microcontroller from ATMEL, the ATMEGA 328P. It contains the Arduino bootloader software. It allows to execute Arduino programs. The Arduino UNO has 14 digital I/O pins, six of which can be used as PWM channels (allow e.g. dimming of LEDs). Six analog input pins are also available. The board handles communication via UART, SPI and I2C (TWI).

For data exchange and programming it is connected to the computer via USB. Since the microcontroller cannot communicate directly via USB, there is a second microcontroller (ATmega8U2) on the Arduino UNO which does the "translation".

Structure of an Arduino UNO

In the following figure the most important components of an Arduino UNO are listed with short descriptions.

  • Arduino_oard_Uno_Rev3-Atmega_328-Overview

The pin strips on both sides of the Arduino can be used to connect the Arduino to external electronic components or a circuit board using jumper cables:

 ➥ The analog pins A0 to A5 are suitable as sensor inputs for measuring voltage values between 0V and 5V; a built-in analog-to-digital converter
   maps the measured voltage values to a numerical range from 0 (no voltage) to 1023 (maximum voltage, i.e. 5V).

 ➥ The digital pins 0 to 13 can also be defined as sensor inputs: An applied voltage of >2.5V is interpreted as HIGH (numerical value 1),
   a lower voltage as LOW (numerical value 0).

 ➥ The digital pins 0 to 13 can also be defined as digital voltage outputs: They output a voltage of about 5V in HIGH mode and 0V in LOW mode.
   However, the current is limited to 40mA; if necessary, the voltage of the pins is automatically reduced to reach this limit.

 ➥ A special feature is the digital pin 13: There the output current is limited to only 20mA, so that an LED can be connected there directly
   (without series resistor) (directly next to pin 13 is a GND pin, so that not even a plug-in board is necessary for this).
   Newer versions of the Arduino UNO even have an SMD LED permanently installed between pin 13 and GND.

 ➥ The pins (3, 5, 6, 9, 10, 11) marked with the tilde character ~ can, when set as output pins, also switch back and forth between 0V and 5V very
   quickly by means of a so-called pulse width modulation (PWM).
   You can specify values between 0 and 255, where 0 stands for "always off" and 255 for "always on".

 ➥ The other connectors on the board (AREF and ICSP headers) are not important for normal use.

Notes: For example, PWM can be used to drive a motor or a light bulb at a value of 128 with only "half power", because it is only supplied with voltage for half of the time and the other half of the time is idle. An LED can also be "dimmed" in this way: The LED is fast enough to blink at the same frequency, but our eye is not. As we can only perceive 25 frames per second, an LED that is only on half of the time appears darker to us than a permanently bright LED.

Typical Arduino applications

 ➥ Electronics

 ➥ Robotics

 ➥ Controls

 ➥ Sensor data acquisition

 ➥ Programming close to the hardware

Software

The Arduino IDE is a cross-platform Java application that acts as a program code editor and compiler and is also able to send firmware serially to the board. The development environment is based on Processing, an IDE designed to program artists, who normally do not have much contact with the art world programming environments. The programming language is based on Wiring, a C similar language which has a similar scope for a more restricted board design, whose IDE is also based on processing. Make sure you have an updated version of Arduino IDE installed.

Struktur

The basic structure of the Arduino programming language is relatively simple and is divided into at least two parts. These two required parts or functions enclose blocks of statements.

 void setup() {
  // put your setup code here, to run once:

}

void loop() {
  // put your main code here, to run repeatedly:

} 

 ➥ Here setup() is the preparation and loop() is the execution. Both functions are necessary to execute the program.

 ➥ The setup function should follow the variable definition, which must be listed before. Setup must be run through as the first function in a program.
   It is executed only once and is used to set PinMode or to initiate serial communication.

 ➥ The setup() function is called once when the program starts. Use this function to set PinModes or start serial communication. The setup() function must
   appear in every program, even if it does not contain any instructions.

 ➥ After the setup() function follows the loop() function. It contains program code that is executed continuously in an infinite loop - read inputs, trigger
   outputs, etc. This function is the core of all Arduino programs and does the main work.

setup()

The setup() function is called once when the program starts. Use this function to set PinModes or start serial communication. The setup() function must appear in every program, even if it does not contain any instructions.

void setup()
{
 pinMode(pin, OUTPUT); // 'pin' define as output
}

loop()

After running through the setup() function, the loop() function does exactly what its name suggests and runs in an endless loop. This allows the program to interact with the Arduino board via changes, reactions and controls.

void loop()
{
 digitalWrite(pin, HIGH); // switches 'pin' on
 delay(1000); // Pause for one second
 digitalWrite(pin, LOW); // switches 'pin' off
 delay(1000); // Pause for one second
}

Functions

A function is a block of program code that has a name and a set of instructions that are executed when the function is called. The functions void setup() and void loop() have already been explained. There are more built-in functions that will be discussed later.

Type FunctionName(parameter)
{
 instructions;
}

The following integer type function delayVal() is used to insert a delay value into a program by reading a potentiometer. First a local variable 'v' is created. Next, 'v' is equated with the position of the potentiometer, which can have a value between 0 and 1023. The value is then divided by 4 to get a scale from 0 to 255 and at the end the result of the function is returned to the main program.

int delayVal()
{
 int v; // create temporary variable 'v
 v = analogRead(pot); // read potentiometer Value
 v /= 4; // Convert from 0-1023 to 0-255
 return v; // return the calculated value
}

{} curly brackets

Curly brackets (also called 'curly brackets') define the start and end of function and statement blocks as in the 'void loop()' function and also in the 'for' and 'if' statement.

type function()
{
 instructions;
}
Note: An opening curved bracket '{' must always be followed by a closing curved bracket '}'. Here it is often said that the number of brackets must be balanced. Unbalanced parentheses often lead to cryptic, inscrutable errors in the compiler, which are sometimes hard to find, especially in large programs. The Arduino programming environment helps to check that the curved brackets are balanced. To do this, simply select a brace or click just behind it, and the logically connected counterpart is highlighted.

;semicolon

A semicolon must be used at the end of a statement and is used to separate the elements of a program. A semicolon is also used to separate the elements of a for loop.

int x = 13; // declares variable 'x' as integer with value 13
Remark: Forgetting a semicolon at the end of a line leads to an error in the compiler. The error description can be very clear and directly point to the missing semicolon, but this does not have to be the case. If an inscrutable or apparently illogical error is reported, the first thing to do is to add missing semicolons near the reported error.

/*... */ block comments

Block comments or multiline comments are text areas that are ignored by the program. They are used for longer descriptions or comments and help other authors to understand program parts. They start with /* and end with */ and can cover several lines.

/* This is an inserted block comment
 please do not forget the closing comment -
 These must be balanced
*/

// Single line comments

Simple single-line comments are defined with a // at the beginning of the line and end with the end of the line. They are ignored by the program and do not consume memory.

// this is a single line comment

Variablen

A variable is the name of a numeric value with a name and memory location for later use in a program. A variable must be declared and optionally provided with a value. The following example declares a variable 'inputVariable' and assigns the value of analog pin 2 to it:

int inputVariable = 0;       // declares a variable and
                                 // sets its value to 0
inputVariable = analogRead(2);  // sets the value of the variable equal
                                // with the value of analog pin 2 

'inputVariable' is the variable itself. The first line explains that its data type is 'int', which is the short expression for integer. The second line gives the variable the value of the analog pin 2, making the value of the pin available everywhere in the code.

Note: Variables should always have descriptive names as clearly as possible to make the code easier to read.

Declaration of variables:

All variables must be declared before use. Declaring a variable means defining its type such as int, long, float, etc., giving it a name and optionally an initial value.This only needs to be done once in the program. Afterwards the value can be changed at any time by calculations or various assignments.

The following example declares 'inputVariable' as 'int', i.e. integer data type and sets the initial value to 0. This is called a 'simple assignment'.

int inputVariable = 0;

A variable can be declared at many places in the program. The location of the declaration determines which program parts have access to the variable.

Data types

 ➥ byte: Byte stores an 8-bit numeric, integer value without decimal point. The value can be between 0 and 255.

byte someVariable = 180;  // declares 'someVariable
                         // as a 'byte' data type 

 ➥ int: Integers are the most common data type for storing integer values without decimal point. Its value is 16 bits and ranges from -32,767 to 32,768.

int someVariable = 1500;  // declares 'someVariable
                         // as an 'integer' data type

 ➥ long: Data type for long integers with extended size, without decimal point, stored in a 32-bit value in a spectrum from -2,147,483,648 to 2,147,483,647

long someVariable = 90000;  // declares 'someVariable
                           // as a 'long' data type 

 ➥ float: A data type for floating point values or numbers with decimal places. Floating point numbers have a better resolution than integers and are expressed as 32-bit values with a spectrum from -3.4028235E+38 to 3.4028235E+38.

float someVariable = 3.14;  // deklariert 'someVariable'
                           // als einen 'float' Datentyp 
Note: Floating point numbers are not precise and may lead to strange results when compared. Also, floating-point calculations are much slower than with integer data types. Calculations with floating-point values should be avoided if possible.

 ➥ arrays: