Arduino Workshop for the Curious/Lesson 1

From Electromagnetic Field

Contents

Structure of an Arduino Program

An Arduino program has 2 sections called setup and loop. They look like this:

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

}

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

Setup

The setup section contains code which is run once, when you turn the Arduino on. For example, we normally place code in here which tells the Arduino which pins are inputs and which are outputs. It can also be used to set up more complicated features of the Arduino, like serial communication with another Arduino (so that they can send data between one another) or perhaps the code to perform a handshake sequence in order set up a complicated sensor that the Arduino is connected to (maybe a GPS chip or an accelerometer).

Loop

When Arduinos are switched on, they start to execute code and will keep on executing code forever (or until the power is removed!) The loop section of an Arduino program contains code which is run again and again, so that it always has some code to run. This is where it gets it's name from: the Arduino loops round and round again executing your C code.

The main code of an Arduino program is placed into the loop section. For example, if an Arduino was used to flash an LED, you would place code here to turn the LED on and off. This is done by setting the voltage of the pin which the LED is connected to.

The Arduino has an LED built-in and connected to pin 13. If we set this pin to a high voltage, then the LED turns on. An Arduino program which flashes this built-in LED is shown below:


void setup() {                
  // initialize the digital pin as an output.
  pinMode(13, OUTPUT);     
}

void loop() {
  digitalWrite(13, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);               // wait for a second
  digitalWrite(13, LOW);    // turn the LED off by making the voltage LOW
  delay(1000);               // wait for a second
}

Programming the Arduino

To upload the program to your Arduino press the upload button Upload Button.png. If you are having problems, look at the official Arduino troubleshooting guide

Challenge Yourself.jpg

(1) Make the LED flash at 5Hz (5 times per second)

(2) How fast does the LED have to flash before you can't see the flashing?

(3) Make the LED flash in a dot-dash fashion (long-on, off, short-on, off, long-on, off, ...)

Variables

The code above will work fine, but the LED on the Arduino board is a little faint. If we wanted to use an brighter external LED, and we connected it to pin 5 of the Arduino, how many parts of the code would we have to change in order to flash that LED?

The answer is three. One change in the setup section and two changes in the loop section. With a bigger program, that might have been 15 or 20 changes to make! This is bad, and so in order to make this easier we can use something called a variable. Variables can hold data, which includes numbers, text, true/false, etc. If we store the LED pin number in a variable then we can use that variable all over our code, instead of the number. This means that if we change the LED again later on, we only have to change the code in one place!

Variables have a strict type, which means they can only hold one kind of thing. If a variable holds integer numbers then that's all it can hold. If it holds true/false values, that's all it can hold (these are called boolean variables.) You can't start trying to put non-integer numbers (like 3.14) into an integer variable for example. Below shows how to create an integer variable for the pin number and then how to use it throughout the rest of the code:

int led = 13;

void setup() {                
  // initialize the digital pin as an output.
  pinMode(led, OUTPUT);     
}

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
}

Challenge Yourself.jpg

(1) Use a variable to store the delay length, instead of hard-coding 1000ms

(2) Try modifying the delay variable inside of the loop. For example, here is the code to increment a variable called delay by one.

delay = delay + 1;
Personal tools