User Tools

Site Tools


Sidebar

Introduction to microcontrollers, Arduino and Galileo

Objectives

  1. The basics of what a microcontroller is/does
  2. What is Arduino? What is Galileo?
  3. How to make a LED blink

1. What is a microcontroller?

A microcontroller (sometimes abbreviated µC, uC or MCU) is a small computer on a single integrated circuit containing a processor core, memory, and programmable input/output peripherals. Program memory in the form of NOR flash or OTP ROM is also often included on chip, as well as a typically small amount of RAM. Microcontrollers are designed for embedded applications, in contrast to the microprocessors used in personal computers or other general purpose applications. Because of a large amount of peripherals integration and a low production cost, a µC operates on low frequencies, usually tens or hundreds of MHz. However, microcontrollers are suitable for a wide range of applications, being used both in industrial environments and in consumers goods. Unlike a microprocessor, a µC is used in situations where a single main functionality is required, this functionality being implemented in the program that it runs.

The most common structure of a µC is:

  • central processing unit(µP core), with an 8, 16, 32 or 64 bit architecture
  • volatile (RAM) or non-volatile (FLASH or EEPROM) memory for the data and program
  • digital I/O ports
  • serial interfaces (RS232, SPI, I2C, CAN, RS485)
  • timers, PWM generators or watchdog
  • ADCs (Analog to Digital Converters)
  • support for programming and debugging

Some microcontrollers are based on a Harvard architecture, which means there is a physical separation of the storage and signal pathways for instructions and data. Therefore, the instructions are stored in a different memory than the data, and they can have a different length than the registers and memory. We take as an example the AVR Family from Atmel. Here, the instructions are 16-bit length, while the internal registers are 8-bit length.

2. AVR Family from Atmel

The AVR is a modified Harvard architecture 8-bit RISC (Reduced Instruction Set Computing) single chip microcontroller which was developed by Atmel in 1996.

Flash, EEPROM and SRAM memories are integrated in the same core, removing the need for external memory. The program consists of 16-bit length instructions, which are stored in the Flash memory. The size of the program memory is indicated by the component's name. As an example, ATmega128 has a 128kb of Flash memory. The addressing space consists of 32 general 8bit registers, I/O registers and the SRAM memory.

AVR uCs have a two-level execution pipeline, allowing the next instruction to be brought from memory(Fetch) while the current one is in execution(Exec). Most instructions are executed in a single cycle, resulting in a 1MIPS/MHz throughput.

A main advantage in favor of Atmel architecture is the optimization in executing a compiled C code on the AVR microcontrollers.

3. ATmega328

Fig. 1: ATMega328 chip

ATmega328 is a microcontroller with a 8bit architecture. Therefore, the registers and the data bus have 8 bits each. However, when you write a program in C, you can use 32 bits variables in both integer and floating point. The compiler is the one who will translate the instructions that are using 32 bits variables into assembly code (8 bits).

A short summary of the ATmega328 microcontroller:

  • Operating Voltage - 1.8 - 5.5V
  • Clock Speed - 16MHz
  • Self-Programmable Flash Program Memory - 32KBytes
  • EEPROM Memory - 1KByte
  • Internal SRAM - 2KBytes
  • Write/Erase Cycles - 10.000 Flash/100.000 EEPROM

Some of the internal peripherals of ATmega328:

  • Two 8-bit Timer/Counters with Separate Prescaler and Compare Mode
  • One 16-bit Timer/Counter with Separate Prescaler, Compare Mode and Capture Mode
  • 6 PWM Channels
  • Programmable Serial USART
  • Master/Slave SPI Serial Interface
  • Programmable Watchdog Timer with separate on-chip Oscillator
  • On-chip Analog Comparator
  • Interrupt and Wake-up on Pin Change

These internal peripherals can be accessed from outside via port pins. These pins are also used as I/O pins for external peripherals.

4. Arduino

Arduino is a popular open-source single-board microcontroller, descendant of the open-source Wiring platform, designed to make the process of using electronics in multidisciplinary projects more accessible. The hardware consists of a simple open hardware design for the Arduino board with an ATmel AVR processor and on-board I/O support. The software consists of a standard programming language compiler and the bootloader that runs on the board.

We will use in this workshop an Arduino Uno board, as shown in Figures 3a and 3b.

Arduino Uno FrontFig. 2: Arduino Uno

The Arduino Uno is a microcontroller board based on the ATmega328. It has 14 digital input/output pins (of which 6 can be used as PWM outputs), 6 analog inputs, a 16 MHz crystal oscillator, a USB connection, a power jack, an ICSP header, and a reset button. It contains everything needed to support the microcontroller; simply connect it to a computer with a USB cable or power it with a AC-to-DC adapter or battery to get started.

A short summary of the Arduino Uno board:

  • Microcontroller - ATmega328
  • Operating Voltage - 5V
  • Input Voltage(recommended) - 7-12V
  • Input Voltage(limits) - 6-20V
  • Digital I/O Pins - 14(of which 6 provide PWM output)
  • Analog Input Pins - 6
  • DC Current per I/O Pin - 40mA
  • DC Current for 3.3V Pin - 50mA
  • Flash Memory - 32 KB(ATmega328) of which 0.5 KB used by bootloader
  • SRAM - 2 KB(ATmega328)
  • EEPROM - 1 KB(ATmega328)
  • Clock Speed - 16 MHz

Power

The Arduino Uno can be powered via the USB connection or with an external power supply. The power source is selected automatically.

External (non-USB) power can come either from an AC-to-DC adapter (wall-wart) or battery. The adapter can be connected by plugging a 2.1mm center-positive plug into the board's power jack. Leads from a battery can be inserted in the Gnd and Vin pin headers of the POWER connector.

The board can operate on an external supply of 6 to 20 volts. If supplied with less than 7V, however, the 5V pin may supply less than five volts and the board may be unstable. If using more than 12V, the voltage regulator may overheat and damage the board. The recommended range is 7 to 12 volts.

The power pins are as follows:

  • VIN. The input voltage to the Arduino board when it's using an external power source (as opposed to 5 volts from the USB connection or other regulated power source). You can supply voltage through this pin, or, if supplying voltage via the power jack, access it through this pin.
  • 5V.This pin outputs a regulated 5V from the regulator on the board. The board can be supplied with power either from the DC power jack (7 - 12V), the USB connector (5V), or the VIN pin of the board (7-12V). Supplying voltage via the 5V or 3.3V pins bypasses the regulator, and can damage your board. We don't advise it.
  • 3V3. A 3.3 volt supply generated by the on-board regulator. Maximum current draw is 50 mA.
  • GND. Ground pins.

Input and Output

Each of the 14 digital pins on the Uno can be used as an input or output, and the available API provides functions to access them: pinMode(), digitalWrite(), and digitalRead(). They operate at 5 volts. Each pin can provide or receive a maximum of 40 mA and has an internal pull-up resistor1) (disconnected by default) of 20-50 kOhms. In addition, some pins have specialized functions:

  • Serial: 0 (RX) and 1 (TX). Used to receive (RX) and transmit (TX) TTL serial data. These pins are connected to the corresponding pins of the ATmega8U2 USB-to-TTL Serial chip.
  • External Interrupts: 2 and 3. These pins can be configured to trigger an interrupt on a low value, a rising or falling edge, or a change in value.
  • PWM: 3, 5, 6, 9, 10, and 11. Provide 8-bit PWM output with the analogWrite() function.
  • SPI: 10 (SS), 11 (MOSI), 12 (MISO), 13 (SCK). These pins support SPI communication using the SPI library.
  • LED: 13. There is a built-in LED connected to digital pin 13. When the pin is HIGH value, the LED is on, when the pin is LOW, it's off.

The Uno has 6 analog inputs, labeled A0 through A5, each of which provide 10 bits of resolution (i.e. 1024 different values). By default they measure from ground to 5 volts, though is it possible to change the upper end of their range using the AREF pin and the analogReference() function. Additionally, some pins have specialized functionality:

  • TWI: A4 or SDA pin and A5 or SCL pin. Support TWI communication using the Wire library.

There are a couple of other pins on the board:

  • AREF: Reference voltage for the analog inputs. Used with analogReference().
  • Reset: Bring this line LOW to reset the microcontroller. Typically used to add a reset button to shields which block the one on the board.

Communication

The Arduino Uno has a number of facilities for communicating with a computer, another Arduino, or other microcontrollers. The ATmega328 provides UART TTL (5V) serial communication, which is available on digital pins 0 (RX) and 1 (TX). An ATmega16U2 on the board channels this serial communication over USB and appears as a virtual com port to software on the computer. The '16U2 firmware uses the standard USB COM drivers, and no external driver is needed. However, on Windows, a .inf file is required. The Arduino software includes a serial monitor which allows simple textual data to be sent to and from the Arduino board. :!: The RX and TX LEDs on the board will flash when data is being transmitted via the USB-to-serial chip and USB connection to the computer (but not for serial communication on pins 0 and 1).

The SoftwareSerial library allows for serial communication on any of the Uno's digital pins.

The ATmega328 also supports I2C (TWI) and SPI communication. The Arduino software includes a Wire library to simplify use of the I2C bus. For SPI communication, use the SPI library.

Galileo

What is Galileo?

Galileo is an Arduino-compatible board designed by Intel, and powered by an Intel Quark system-on-chip. The benefit of compatibility is that the pinout is the same and all Arduino sketches work as designed on the Galileo board (so it is both hardware and software compatible). In addition to Arduino-compatible pins, Galileo offers the following extra features:

  • 400MHz processor
  • mini PCI-Express slot
  • 100 MB Ethernet port
  • USB Client
  • USB Host

All the extra features of Galileo are put at the Arduino programmer's disposal through Arduino libraries. The sketch itself isn't running directly on the processor, instead this is mediated by a Linux system.

Getting started with Galileo

It's time to plug your Galielo in and power it up. The most common way to do this is to plug one end of the USB cable into the Arduino and the other end into a computer. The power cable is separate, and you will find it in your Galileo package. Make sure your cable is an A-microB cable. One end should be thin. The other end should be rectangular.

USB cable leadsFig. 3: USB Cable

Plug the thin end into the Galileo and the larger end into your computer. You should get a small green light on the right side of the board and a few blinking orange lights, on the left side, as shown here.

The Galileo board can be programmed with the Arduino software2), modified specifically to include the Galileo libraries. Download the apropriate file for your OS, on the Intel site

Extract the package onto your computer. To open the workspace double click arduino.exe.

Arduino software folderFig. 4: Arduino software folder

Programming

The Galileo board uses a bootloader that allows you to upload new code to it without the use of an external hardware programmer. You can use this directly from the Arduino IDE with the “Upload” command.

Open the workspace by navigating to the Arduino software folder and double clicking arduino.exe.

Arduino workspaceFig. 5: Arduino workspace

Now you need to select the correct board. Go to the Tools menu and from the Board dropdown menu select Galileo Board.

Fig. 6: Select the Galileo board

Next, you need to configure the Serial Port (also known as the COM Port). Look in Device Manager (Control Panel → System → Device Manager), in the Ports (COM & LPT) section, to see which port it is. On a PC it will probably be something like COM3 or COM4. On a Mac it will be something like tty.usbserial-xxxxx. Select that port from Tools menu, Serial Port dropdown.

Fig. 7: Select the Galileo port

And now it's time to start coding!

Example Code - Output - LED blinking

/*
 * Blink
 *
 * The basic Arduino example.  Turns on an LED on for one second,
 * then off for one second, and so on...  We use pin 13 because,
 * depending on your Arduino board, it has either a built-in LED
 * or a built-in resistor so that you need only an LED.
 *
 * http://www.arduino.cc/en/Tutorial/Blink
 */
 
 
int ledPin = 13;                // LED connected to digital pin 13
 
void setup()                    // run once, when the sketch starts
{
  pinMode(ledPin, OUTPUT);      // sets the digital pin as output
}
 
 
void loop()                     // run over and over again
{
  digitalWrite(ledPin, HIGH);   // sets the LED on
  delay(1000);                  // waits for a second
  digitalWrite(ledPin, LOW);    // sets the LED off
  delay(1000);                  // waits for a second
}

Lets take a closer look at the functions used.

int ledPin = 13 : declares a variable named ledPin of type int (integer) and assigns the value 13 to it. This variable will be used to describe the pin on the board where the LED in connected.

void 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 power-up or reset of the Arduino board.

pinMode(pin, mode) : configures the specified pin to behave either as an input or an output. See the description of digital pins for details. pin: the number of the pin whose mode you wish to set; mode: either INPUT or OUTPUT

void loop() : after creating a setup() function, which initializes and sets the initial values, the loop() function does precisely what its name suggests, and loops consecutively, allowing your program to change and respond. Use it to actively control the Arduino board.

digitalWrite(pin, value) : write a HIGH or a LOW value to a digital pin. If the pin has been configured as an OUTPUT with pinMode(), its voltage will be set to the corresponding value: 5V (or 3.3V on 3.3V boards) for HIGH, 0V (ground) for LOW. If the pin is configured as an INPUT, writing a HIGH value with digitalWrite() will enable an internal 20K pullup resistor (see this tutorial on digital pins). Writing LOW will disable the pullup. The pullup resistor is enough to light an LED dimly, so if LEDs appear to work, but very dimly, this is a likely cause. The remedy is to set the pin to an output with the pinMode() function. pin: the pin number; value: HIGH or LOW.

NOTE: Digital pin 13 is harder to use as a digital input than the other digital pins because it has an LED and resistor attached to it that's soldered to the board on most boards. If you enable its internal 20k pull-up resistor, it will hang at around 1.7 V instead of the expected 5V because the onboard LED and series resistor pull the voltage level down, meaning it always returns LOW. If you must use pin 13 as a digital input, use an external pull down resistor.

delay(ms) : pauses the program for the amount of time (in miliseconds) specified as parameter. (There are 1000 milliseconds in a second). ms: the number of milliseconds to pause (unsigned long).

Connecting LEDs on other pins.

Connect the red LED on digital pin number 6, like in the picture below:

Fig. 8: Galileo schematic for LED blink

Exercise 1: Rewrite your code to make this LED blink.

Blinking the LED without using delay()

Sometimes you need to do two things at once. For example you might want to blink an LED (or some other time-sensitive function) while reading a button press or other input. In this case, you can't use delay(), or you'd stop everything else the program while the LED blinked. The program might miss the button press if it happens during the delay(). This sketch demonstrates how to blink the LED without using delay(). It keeps track of the last time the Arduino turned the LED on or off. Then, each time through loop(), it checks if a long enough interval has passed. If it has, it toggles the LED on or off.

The code below uses the millis() function, a command that returns the number of milliseconds since the Arduino board started running its current program, to blink an LED.

/* Blink without Delay
 
 Turns on and off a light emitting diode(LED) connected to a digital  
 pin, without using the delay() function.  This means that other code
 can run at the same time without being interrupted by the LED code.
 
 The circuit:
 * LED attached from pin 13 to ground.
 * Note: on most Arduinos, there is already an LED on the board
 that's attached to pin 13, so no hardware is needed for this example.
 
 
 created 2005
 by David A. Mellis
 modified 8 Feb 2010
 by Paul Stoffregen
 
 This example code is in the public domain.
 
 
 http://www.arduino.cc/en/Tutorial/BlinkWithoutDelay
 */
 
// constants won't change. Used here to
// set pin numbers:
const int ledPin =  13;      // the number of the LED pin
 
// Variables will change:
int ledState = LOW;             // ledState used to set the LED
long previousMillis = 0;        // will store last time LED was updated
 
// the follow variables is a long because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long interval = 1000;           // interval at which to blink (milliseconds)
 
void setup() {
  // set the digital pin as output:
  pinMode(ledPin, OUTPUT);      
}
 
void loop()
{
  // here is where you'd put code that needs to be running all the time.
 
  // check to see if it's time to blink the LED; that is, if the
  // difference between the current time and last time you blinked
  // the LED is bigger than the interval at which you want to
  // blink the LED.
  unsigned long currentMillis = millis();
 
  if(currentMillis - previousMillis > interval) {
    // save the last time you blinked the LED
    previousMillis = currentMillis;  
 
    // if the LED is off turn it on and vice-versa:
    if (ledState == LOW)
      ledState = HIGH;
    else
      ledState = LOW;
 
    // set the LED with the ledState of the variable:
    digitalWrite(ledPin, ledState);
  }
}

For more info go to:

galileo/1.txt · Last modified: 2014/08/14 17:29 by andrei [dot] voinescu [at] upb [dot] ro