Arduino Tutorial #2 – Programming

This tutorial uses the circuit and the code from Tutorial #1.


This Arduino program is written in C – it is a programming language.  The code is going to be explained and then you will change it.


  • This line of code creates a variable.  int represents an integer (0, 1, 2, 3…)
  • ledLight is the name we are giving the variable – we can then refer to this variable in other parts of the code.
  • =13 sets the value of the variable to the number 13.  Note – this is the number of the digital pin we plugged our wire into!

By declaring our variable at the start of the program we are able to access it in the different methods.

Add another variable called ledLight2 and set the value to 12.  You will need to set up another LED light.


The setup method will run when the arduino starts up.

  • void setup() is a method.  Everything between the opening { and closing } will run when the method runs
  • // is used to indicate a comment – this doesn’t run, it is used to explain your code
  • pinMode(ledLight, OUTPUT); this statement sets your pin to output.  If you had a sensor (i.e. button) you would want it to be input.  Notice that OUTPUT has to be capital letters.

You will need to add another pinMode command to set your second light to OUTPUT as well.


The loop method runs after setup and it will keep “looping” over and over again as long as the arduino has power.

  • Like the setup() method there is an opening { and a closing }.  Every Arduino Program must have one (and only one) loop method
  • digitalWrite is a method that will send 5V to the variable listed when the keyword HIGH is used.  It sends 0V when LOW is used
  • delay is a method that will wait for the number of milliseconds passed.  1000 milliseconds is 1 second.

Try adding to digitalWrite commands to turn your ledLight2 on and off.  Can you:

  • get both lights on and off at the same time
  • have one light one when the other is off?

If you are having trouble, have a look at this sample program.

Arduino Tutorial #1


  • LED Light
  • Breadboard
  • Breadboard wires
  • 220 Ohm Resistor
  • Arduino UNO

Wiring Diagram


  • Connect the GND to the – rail (blue)
  • Connect the resistor to a terminal strip
  • Connect the cathode for the LED light (it is the short leg) to the same terminal strip as the resistor
  • Connect the anode for the LED light (it is the long leg) to another terminal strip
  • Connect the terminal strip for the anode to digital pin 13 (in the diagram it connects to the + rail and then the digital pin)


The following image shows the same circuit.  Schematic diagrams are often used because they can be eaily drawn by hand and it is a standard way to communicate information about a circuit.



Type the following into the Arduino software:


If you upload this code you should see the LED light go on and off.


  • If you get an error when uploading check that the spelling is correct (if a capital letter is required it has to be a capital letter).
  • Problem Uploading to Board  is a common error – click Tools and select the port that has your Arduino listed.
  • If the LED light on the arduino flashes but your LED light on the breadboard does not – make sure the wires are connected to the correct spot.
  • If the LED light on the arduino flashes but your LED light on the breadboard does not – make sure the anode (long leg) of the arduino is going to the digital pin 13.

Line Tracking with Arduino

I ordered a number of infrared line trackers for my class in September.  At 33 cents per sensor the price was awesome:

First things first I checked out the datasheet for them.  Since I’m travelling I don’t have any resistors but based on the voltages I should be fine for a short test.  Time to wire it up.

Wired up line tracker
Yellow is out on sensor to A0 on Arduino.
Red is VCC on sensor to 5V on Ardunio.
Black is Gnd

For the code the AnalogReadSerial example is all I need to test it out.  The sensor worked great – when a dark object (i.e. phone case) was 1 cm away it was receiving a value of 0.  When white paper was held 1 cm away a value of 1007 was received.


Next Stage:

One of the tasks that my students will be required to complete is to build a variety of line following robots – from the simple (i.e. turn left if on black, turn right if on white) to the complex (PID – proportional integral derivative).


PID tutorial

PID Line follower

Using IR Sensor

Line Follower

Infrared line tracker
Infrared line tracker