Chapter 2


In this chapter, we will explore microcontrollers, and the ATmega (used by the Arduino) in particular. This chapter is not meant to be an in-depth instruction manual for new Arduino users. Instead it will give you a brief introduction to the platform and point you to resources that cover different subjects in detail, and can be used for later reference. We will return to the Arduino in chapter 4, where we cover the connection-layer between the microcontroller and the webSocket server.

What is a microcontroller?

A microcontroller (MCU) is basically a simple computer.
Like your desktop, MCUs have components like CPUs, memory, input / output peripherals, and are programmable. MCUs generally are integrated systems where the entire computer is build into a single chip.

The big difference between your desktop and a microcontroller is that your desktop can run any number of programs and has software support for different hardware components. Microcontrollers generally only run one program. In general, this program is specifically written to control known hardware components.


Microcontrollers are used in almost all electrical devices from cars, medical devices, microwaves, toys or even computer parts like mice and keyboards.

Microcontrollers remove the difficulties of programming system logic at a hardware level. Instead of programming hardware using switches and gates, microcontrollers allow the hardware logic to be controlled from software. In most cases Microcontroller programs are written in system-level programming languages like Assembler or C.

Imagine you want to build a device that detects the amount of light in a room and adjusts a lamp accordingly. In hardware terms this would be a rather difficult system to build. Using a microcontroller however makes this much simpler and requires minimal knowledge about electronics.

Another nice thing about microcontrollers is that they can be extremely fast and reliable, since they are simple by design.

If you want to learn more about microcontrollers, you should read the following HowStuffWorks article on the subject.


In this course we will use a popular microcontroller board called the Arduino Uno. Arduinos come in many different flavors (Uno, BT, Due, Fio…). The UNO uses an ATmega microcontroller (the ATmega328 to be precise).
You can find the documentation of the ATmega328 at the Atmel website.

On the Arduino website you find the following product description:

Arduino is a tool for making computers that can sense and control more of the physical world than your desktop computer. It's an open-source physical computing platform based on a simple microcontroller board, and a development environment for writing software for the board. Arduino can be used to develop interactive objects, taking inputs from a variety of switches or sensors, and controlling a variety of lights, motors, and other physical outputs. Arduino projects can be stand-alone, or they can be communicate with software running on your computer (e.g. Flash, Processing, MaxMSP.) The boards can be assembled by hand or purchased preassembled; the open-source IDE can be downloaded for free. The Arduino programming language is an implementation of Wiring, a similar physical computing platform, which is based on the Processing multimedia programming environment.

💡 Exercise

Read the Arduino product description.
(The Arduino website contains a vast array of information for both new and advanced users. This reader will regularly refer to different pages on this website.)

Arduino the Documentary

To learn more about the history of the Arduino platform I strongly recommend the fantastic documentary “Arduino the Documentary” from 2010.

Getting started

Lets start programming microcontrollers.

  1. Download and install the Arduino IDE.
    This is the tool you will use to write programs for your microcontroller. The programming language and environment, used by the Arduino IDE are based on Processing. You can find a full description of the development environment and its features at:
  2. Open the "Blink" example program in your Arduino IDE.
    File > Examples > 01.Basics > Blink
  3. Connect your Arduino to your Computers USB port.
    In the Blink sketch; press the Upload button.

If everything works, the reference LED that is build into the Arduino will start blinking.
Congratulations! You have successfully programmed a microcontroller.


Lets take a closer look at the core concepts of Arduino programming.

  Turns on an LED on for one second, then off for one second, repeatedly.
  This example code is in the public domain.
// Pin 13 has an LED connected on most Arduino boards.
// give it a name:
int led = 13;

// the setup routine runs once when you press reset:
void setup() {                
  // initialize the digital pin as an output.
  pinMode(led, OUTPUT);     

// the loop routine runs over and over again forever:
void loop() {
  digitalWrite(led, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);               // wait for a second
  digitalWrite(led, LOW);    // turn the LED off by making the voltage LOW
  delay(1000);               // wait for a second

If we look at the Blink sketch there are a couple of things worth pointing out.

Introduction to the Arduino board

The basics on how to use your Arduino are actually really simple.

When you look at the board you notice that there are a bunch of holes (called pins) on the left and right side. These pins have different labels like: GND, 5V, 7, ~3, A0…
Some pins like GND and 5V have one fixed function, and can not be programmed.
The following article covers the technical details of the Arduino UNO.

Pins that can be programmed, are the pins marked with A0 – A5 and 0 – 13.
Your program can tell a pin do different things. The Arduino website has a lot of resources about digital pins. You should start by reading the following article about digital pins.

Pins can be programmed, to either read or write, a electrical current.

In addition to reading and writing there are to modes a pin can operate in.

NOTE: The analog pins on your Arduino can’t be used to write an analog signal. To use analogWrite you need to use a digital pin that supports pulse with modulation (PWM). These pins are marked with an ~in front of the pin number (e.g. ~11). Read more about PWM at

Note that pins 0 & 1 are marked with TX & RX. TX & RX are reserved for serial communication. Although they can be overwritten you are not advised to do so since any serial communication in return will overwrite your code to these pins.

Pin programming

If you want to power a simple LED lamp, you connect the LEDs long pin (anode) to pin 13. And the LEDs shorter pin (cathode) to the ground pin (GND).

You first need to set the mode for the pin. Do this in the setup function of your code.

void setup() {

    // pinMode(pin, value);
    pinMode(13, OUTPUT);

To turn on the lamp you need to turn on pin 13. Do this in the loop function of your program.

void loop(){
    // digitalWrite(pin, value);
    digitalWrite(13, HIGH);

Let’s Rock and Roll

To dive even deeper into the Arduino hardware and software, you should read the foundations section on the Arduino website.

I also strongly advice you to take a look at the “Arduino Video Tutorial” series created by Arduino co-founder Massimo Banzi.

💡 Exercises

You should build and program the examples in 1.Basics on the Arduino learning site.
Find ways to expand on these examples to challenge your knowledge.


Wow, that was a lot of information and there is so much more to learn about. For now we will move away from the microcontroller and focus our attention on the next challenge.

As you remember from chapter 1, we are trying to connect a lamp to the Internet. To refresh your memory these are the components needed to get this done.

  1. The Microcontroller (to interface with the hardware)
  2. The SocketServer (to expose the controller to the internet)
  3. The Client (to talk to the server)

Up next - webSockets.