Phil-duino

A collection of Arduino tutorials put together to help a colleague dive into embedded systems!

View the Project on GitHub ChrisAlphabet/Phil-duino

Hello World!

Every great journey begins with a single step, and this great journey will start the same way. We will start with the classic program, “Hello World!”. This serves two purposes. It validates that we have setup our development system properly, and more importantly, we get a euphoric sense of achievement when it works.

But first, I will get the boring stuff out of the way.

Introduction to Arduino

What is an Arduino?

It is a microcontroller board.

What is a microcontroller?

A little blob of silicon with some memory and programmable peripherals.

Why use an Arduino?

It is easier than not using one.

Phew, I am glad that is out of the way. Lets take a look at the specific Arduino we will be using.

The Arduino Nano

This tutorial series will be based around the Arduino Nano for the incredibly technical reason of I had some lying around. It is based around the ATmega328, which gives us lots of fun stuff to play with! I could list all the features, or you could read the datasheet. You reading the datasheet is less work for me, so you should read the datasheet. If you don’t want to do that, don’t worry. We will discover all the features along the way to building the line following robot!

So lets get the development environment setup.

Development Environment Setup

There are alot of ways that you can set yourself up to develop for the Arduino, but as I am your teacher you will be doing it my way. If you even think of using the Arduino IDE, you are dead to me. We will be using PlatformIO, because it is an open source ecosystem that supports hundreds of boards the tits. Go to their website and follow the instructions on how to install it for Atom.

Creating Your First Project

  1. Launch Atom and you should see the PlatformIO Home page. If not, navigate to PlatformIO->Platformio Home.
  2. Select New Project
  3. Use settings:
  4. Name: HelloWorld
  5. Board: Arduino Nano ATmega328
  6. Framework: Arduino
  7. Uncheck location and browse to the location you will be storing these tutorials
  8. Click Finish

The first time you create a project with a new board you need an internet connection. This is because PlatformIO will go and download the toolchain required for the selected board. And that is the beauty of PlatformIO.

You will now have two folders, lib and src. We will use the lib folder later on in the series, but for now we just need src. There are also some other files created that we will explore a bit later. But quickly, .gitignore tells git (version control system, more on this later) to not track changes to particular files, .travis.yml tells your continuous integration server what to do and platformio.ini is the PlatformIO Project Configuration File.

Write Some Code!

This is the moment you have been waiting for. I can feel your fingers trembling with anticipation as they hover over the keyboard. So here we go! In the src folder, you will find a file called main.cpp. If this was an episode of MTV Cribs and you were in the bedroom, this is where they would say “this is where the magic happens”. So, lets take a look inside.

#include <Arduino.h>

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

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

The keen observer will notice this is slightly different to what we would see if we created a new project in the Arduino IDE. The Arduino IDE is designed for idiots non-technical people and tries to hide some of the implementation details.

A typical embedded main.cpp will look like this:

#include <Some_Standard_Library.h>

void main() {

    some_setup_function();

    while(1) {
    	some_code_that_runs_forever();
    }
}

So, the implementation detail that the Arduino IDE is trying to hide is that you are using C++, you are using a standard library, your setup function runs once in main and your loop function runs repeatedly in an infinite loop. You are now forever enlightened. As we are using an Arduino Nano, I will use the Arduino setup and loop convention. But you could use a typical main if you are feeling wild and crazy. You do you, Phil.

Lets examine each part of main.cpp.

Library Include

#include <Arduino.h>

This is a directive to the preprocessor to go and search for a system header file called Arduino.h. But you don’t care about that. What you care about is that it allows you to use code written somewhere else in your program. Specifically, this allows us to use all the code that the kind people at the Arduino organization have written for using their platform. Any function that we use that we don’t declare ourselves has been defined in Arduino.h

On Windows, you can read it at C:\Users\insert_your_username\.platformio\packages\framework-arduinoavr\cores\arduino\Arduino.h

Setup

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

This is a function! Functions are bits of code that we can reuse in our program. This is very important. Writing functions will become so natural to you soon that you won’t be able to imagine a world without them. But first, we have to master some syntax. A function has the form:

returnType functionName(listOfParameters) {
    function body
}

The return type is the type of data that the function returns. In this case, the return type is void, which simply means don’t return anything.
The function name is the name that you give to the function, normally something meaningful to the human reading it. For example, if you have a function that adds numbers together, a good name for the function would be add. This function is called setup. You use it to set things up.
The setup function doesn’t have any input parameters, so it has been left empty.
Finally we come to the function body. The function body is the statements that will be executed when the function is called. In this case, our function body contains one statement, which is the comment // put your setup code here, to run once:.

A brief side note on comments. In C++ their are two types of comments, single line and multi-line. A single line comment starts with two backslashes. A multi-line comment starts with a backslash+asterisk and ends with an asterisk+backslash. The compiler doesn’t care about comments. They are for you and your fellow humans.

// this is a single line comment

/* 
	This
	is
	a 
	multi-line
	comment
*/

If you ever think “I don’t need to write a comment, I’ll remember what this does”, punch yourself in the face. You are a human, and humans are idiots. Write comments everywhere. Write more comments than code. Comment comment and comment some more.

Setup is a function that is called once before loop.

Loop

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

This is also a function. You now know all about them! Anything placed in loop will run repeatedly. Simple!

So, now we have the framework for our very first program in place and ready to go!

Hello World for Embedded Systems

The traditional Hello World for Embedded Systems is to flash an LED. The Arduino Nano has an LED built-in, connected to pin 13. Now that is pretty bloody handy.

Setup the pin

The first thing we have to do is setup the pin. Pins can be setup to be read from or written to, ie inputs or outputs. We want to setup pin 13 as an output so that we can write to it. Take a guess which function we need to add code to. If you guessed setup, well done!

void setup() {
    pinMode(13, OUTPUT);
}

We use the function pinMode(), declared in Arduino.h to set the mode of a pin. The above call to pinMode() sets pin 13 to an output. The first parameter tells the function which pin and the second parameter tells the function which IO type. To make this more readable to a human, we can use a #define.

#define LED 13

void setup() {
    pinMode(LED, OUTPUT);
}

This is a directive to the preprocessor which says that anywhere you see LED put 13.

Turn on the LED

The next thing we need to do is turn on the LED! We are getting close now! We will be turning the LED completely on or completely off (we will cover PWM in a later tutorial). To do this, we use the function digitalWrite().

void loop() {
    digitalWrite(LED, HIGH);
}

The above call to digitalWrite() turns the LED on by setting the first parameter (LED pin) to the second parameter (state).

#include <Arduino.h>

#define LED 13

void setup() {
    pinMode(LED, OUTPUT);
}

void loop() {
    digitalWrite(LED, HIGH);
}

Now lets upload the code to the Arduino Nano!

Upload To Board

Plug one end of the USB cable into the Arduino Nano and the other end into the PC. Compile the code either by clicking the tick in the toolbar or ctl+alt+B. This will turn the C++ into ones and zeros that the Arduino Nano can understand. If you want more information on this, go and get your own Elec Eng degree. Finally, click the arrow in the toolbar or ctl+alt+U to upload the binary to the board.

Bask in your own magnificence as you have accomplished what you set out to do.

Homework Exercises

  1. Modify the program to turn the LED off
  2. Use a delay function to flash the LED at 2 Hz
  3. Use a delay function to flash the led at 5 Hz