Learning the C Language with Arduino

June 4, 2009

Wiring the Breadboard

In this example we’re using a single LED on the breadboard wired to the Arduino. As shown in the example, attach a ground wire (black) to the Gnd location on the bottom of the Arduino. Connect the wire to a ground rail on the breadboard. Using additional black wires jump to the bottom ground rail on the breadboard to complete a circuit adjacent to the LED position on the breadboard.

Place the LED in the breadboard and connect the black ground wire between the ground rail and the cathode lead (-) on the LED. Place a 150 to 220 ohm resistor in the breadboard. Attach another wire from one end of the resistor to the anode lead (+) of the LED. Finally, attach a wire between the remaining end of the resistor and Digital Pin 3 on the Arduino to compelete the circuit. This will allow us to write a program that controls flashing the LED.

Breadboard Wiring LED Circuit

Breadboard Wiring LED Circuit

Programming the Arduino

Create a new program named: LEDBlink

Type in the following code, typing the comments (shown in green) are optional. You could copy and paste into the program editor but typing your own code helps you learn the programming language too.

LEDBlink Arduino Program

/*--- Blink an LED  ---*/
//Associate LEDs with an Arduino Digital pin.
int ledPin = 3;  //We're using Digital Pin 3 on the Arduino.</code>

void setup()
   pinMode(ledPin, OUTPUT);   //Set up Arduino pin for output only.

void loop()
   //The HIGH and LOW values set voltage to 5 volts when HIGH and 0 volts LOW.
   digitalWrite(ledPin, HIGH); //Setting a digital pin HIGH turns on the LED.
   delay(1000);  //Get the microcontroller to wait for one second.
   digitalWrite(ledPin, LOW);  //Setting the pin to LOW turns the LED off.
   delay(1000);  //Wait another second with the LED turned off.

C Language Programming

As described in the related article “Introduction to Programming the Arduino“, the programming language used for the Arduino is based primarily on the C language.

Language Reference

The Arduino’s language features used in this program example are described in the Language Reference section of the Arduino web site.


In the following program statement we are doing three things as we declare a user defined variable. First we are identifying the data type as being an integer number int”. Second, the variable name ledPin was made up for this program. Third we are assigned a value to this variable. In this case we assigned the value 3 to the variable ledPin.

int ledPin = 13; //We’re using Digital Pin 3 on the Arduino.

Syntax, Punctuation Usage

; semicolons

{} curly braces


One of the differences between programming languages like C and Visual Basic is the use of semicolons to end a statement.

When not to use semicolons.

  • They are ignored if used in comments.
  • They are not used in a function definition like setup()
  • They are not used on the curly braces { }

When to use semicolons.

Semicolons are used to end program statements. In the LEDBlink program they are used to end the variable declaration, ledPin, and to end program statements like pinMode, digitalWrite, and delay functions.

Curly Braces

In the LEDBlink program, the braces are used to completely surround the collection of statements used to make up the setup() function. They are also used separately to surround the loop() function’s program statements.

Standard Arduino Functions

This program only uses three different function calls.

  • pinMode()
  • digitalWrite()
  • delay()


pinMode(ledPin, OUTPUT); //Set up Arduino pin for output only.

In our example project, we’re using one of the Arduino’s digital pins to make an LED turn on and off. These pins are able to operate in an input mode as if to read a switch to see if it is on or off. The pins can operate in an output mode for our LED. As shown in the line of code above we are setting the pin to operate in the output mode.


digitalWrite(ledPin, HIGH); //Setting a digital pin HIGH turns on the LED.

The digitalWrite function does the actual work for us by turning the LED on or off. By specifying a High value it causes the output voltage to go high, in this case we’re using 5 volts to light the LED. Causing digitalWrite to go HIGH turns the pin on with 5 volts. When we want to turn the LED off we use digitalWrite and LOW to cause the pin to go to 0 volts.


delay(1000); //Get the microcontroller to wait for one second.

The article “Microcontrollers as Time Keepers” provides some additional information about the time intervals used within the Arduino’s capabilities. We will be using millseconds (one thousandth of a second) with this program. The delay() function causes the program to wait the specified number of milliseconds, in the following code statement it waits 1000 milliseconds for a total of one second.

Getting it Right the First Time

Hopefully you typed everything correctly getting your punctuation right with the right spelling, upper case and lower case letters as required. Selecting the Sketch -> Verify/Compile will soon let you know if something is wrong. If so, what do you do to correct the problems? Next, we’ll take a look at some possible error conditions and the error messages you might see when writing your own programs.

Check out the article “Correcting Arduino Compiler Errors“.

(c) 2009 – Vince Thompson


Microcontrollers as Time Keepers

June 4, 2009

Microcontrollers allow us to do things that would be difficult for us to do on our own. They can help with things like take a picture the instant lightning flashes in the sky or to photograph a balloon popping as soon as you hear it burst.

The Importance of Timing

Timing is an important part of working with microcontrollers. First, lets consider some units of time people are most familiar with. We measure time in terms of seconds, minutes, hours, days, months, and years. We use clocks and calendars to keep track of time.

Scientists think about the time to reach great distances in terms of the speed of light and measure distances in space in terms of light years.

What is the speed of light? Constant c = 299,792,458 meters per second in free space (vacuum) or 186,282.387 miles per second.

Circumference of earth at equator is 40,075,020 meters. At that rate the speed of light would circle the earth approx 7.48 times per second. To circle the earth only once it will take a fraction of a second to be more precise 1/7.48 th of a second or approximately 0.13 of a second.

From Very Large Time Units to Very Small

Microcontrollers work on the other end of a time spectrum – in very small fractions of a second. Although they are good at simply switching things on and off, microcontrollers are great as stop watches keeping track of very small time spans.

Time Keeping

An electronic watch uses a crystal to help it keep time. This is an oscillator that vibrates at the rate of 32,768 times a second or 32.768 Kilohertz (kHz).

The Arduino Duemilanove uses a time keeping oscillator too. This one operates at 16 million times a second or 16 Mega Hertz (MHz). That’s over 488 times faster than a watch crystal. This clock speed helps us measure some very small time intervals.

Units of Time at Fractions of a Second

Millisecond = 0.001 second 1 thousandth of a second.
Microsecond = 1.0 x 10-6
or 0.000001 second
1 millionth of a second
Nanosecond = 1.0 x 10-9
or 0.000000001 second
1 billionth of a second
Picosecond = 1.0 x 10-12
or 0.000000000001 second
1 trillionth of a second

Some Events Occurring at a Fraction of a Second.

  • Car engine running at 1,000 RPM (revolutions per minute) is turning at a rate of 16.6 revolutions per second. This would be one revolution in 60 milliseconds (60 thousandths of a second).
  • People on average are only able to see changes occur about thirty times a second. That is the reason movie film and videos often update at 30 frames per second. This would be one frame every 33.3 milliseconds.
  • Household alternating current (AC) electricity in the United States switches polarity 60 times a second and 50 times a second in Europe. This would switch polarity one time every 16 milliseconds in the US and 20 milliseconds in Europe.
  • High speed video cameras running at 325,000 frames per second can be played back in slow motion allowing us to see what happens with things like bullets hitting an apple or allowing us to see what a rain drop looks like when hitting a pool of water. This would be one frame for each 0.325 millisecond or 325 microseconds (325 millionths of a second).
  • The speed of light travels about one foot in one nanosecond or one meter in approximately 3.3 nanoseconds (billionths of a second).

Arduino Time Functions

The Arduino programming language provides functions that handle timing features.

  • millis() returns the number of milliseconds (thousands of a second) since the Arduino began running or since the last number overflow.
  • micros() returns the number of microseconds (millionths of a second) since the Arduino’s program began running or the last number overflow.
  • delay(ms) causes the program to delay the specified number of milliseconds.
  • delayMicroseconds(us) causes the program to delay the specified number of microseconds.
(c) 2009 – Vince Thompson

Introduction to Programming the Arduino

June 4, 2009

The programming language used for the Arduino is based primarily on the C language. Some features of the C++ language may be used in more advanced programming features but are not used in this introduction.

Each program must include two parts for it to be valid, the setup and loop functions. Any statements included in a setup function are only run once by the program. Any statements in the loop function are executed over and over.

/*--- Minimum Arduino programming functions, setup and loop  ---*/

void setup()

void loop()

The first line in the above program is a comment. The comment is not really required but are helpful in explaining what a program is supposed to do.

/*— Minimum Arduino programming statements —*/
Comments can have two forms. The previous comment starts with the /* characters and continues until the ending characters */ are found.

/* This style of
comment can
cross multiple lines

The other comment form is with the double slash characters

// This is a comment too but is only on a single line in the program.

Comments with the // characters can begin anywhere on a line and are often used following a programming statement.

Verify and Compile

Using the Sketch → Verify/Compile command to check your work.

Arduino Sketch - Verify/Compile

Arduino Sketch -> Verify/Compile

If everything compiles successfully the Arduino’ programming environment shows the message “Done compiling”. When the sketch compiles without errors it also shows the message stating the Binary sketch size.

Sketch Successfully Compiled

Sketch Successfully Compiled

This simple program will compile properly but doesn’t do anything useful. It just shows the bare minimum needed to create a program.

(c) 2009 – Vince Thompson

Instructable: DIY Servo Extensions.

December 15, 2008

Servos are great when building a robot and you want to be able to rotate back and forth like you’re looking for something. You may have sensors that scout around for another robot and putting it on top of a servo provides that mobility. Servos are also very useful when building a robotic arm and many other things.

One problem you’ll run into is wires that aren’t  long enough to reach from your controller to the servos. That is when you need servo extensions.

Arduino servo extensions

Arduino servo extensions

This Instructable page for DIY Servo Extensions will lead you through the process of making your own servo extensions.

(c) 2009 – Vince Thompson

Online Electronics Textbooks

December 4, 2008

Lessons In Electronic Circuits is a free series of textbooks on electricity and electronics.

There is a series of six volumes focusing on:

  • DC circuits
  • AC circuits
  • Semiconductors
  • Digital
  • Reference
  • Experiments

Getting DIY Robotics Labs Started.

December 4, 2008

I’ve been a member of robotics clubs for several years and an active mentor with FIRST robotics teams in the Kansas City area. I’ve noticed while helping FIRST students that there is the lack of robotics at the personal level.  The FIRST robot kit of parts are too expensive for mom and dad to buy the same parts to play with at home. Also, many homeschool students don’t have access to the same level of robotics parts and resources for their studies.

Getting started with robotics doesn’t have to be overly expensive. This blog is dedicated to getting people started experimenting with robotics at home.

(c) 2009 – Vince Thompson